View previous topic :: View next topic |
Author |
Message |
strsh
Joined: 16 Jul 2013 Posts: 37
|
Read letters from text |
Posted: Fri Aug 09, 2019 4:43 am |
|
|
Greeting.
I am interested in how I can read a letter in a specific place in the text.
for example:
text is "SD card is initialized"
i want to assign the variable letter by letter and know where it is.
I thank in advance. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Aug 09, 2019 6:37 am |
|
|
What code have you written so far? It is always best to provide some source code so we can have a starting point to see if you are even thinking about the problem in the right way.
But from the most general position, a string is an array of characters so Code: | char text[] = "string"; | can be thought of like Code: | char text[] = {'s', 't', 'r', 'i', 'n', 'g'}; | This means that each letter is indexed.
From the above example, the letter 's' is
If you want to use functions like searching for a character or sorting, then you should look at the functions in the header file string.h
There are also differences between string literals or char arrays, and you should read about that too. |
|
|
strsh
Joined: 16 Jul 2013 Posts: 37
|
|
Posted: Fri Aug 09, 2019 6:52 am |
|
|
This is my code:
Code: |
#define SDCARD_SPI_HW
#define SDCARD_PIN_SELECT PIN_E0
#define LCD_RS_PIN PIN_D0
#define LCD_ENABLE_PIN PIN_D1
#define LCD_RW_PIN PIN_D6
#define LCD_DATA4 PIN_D2
#define LCD_DATA5 PIN_D3
#define LCD_DATA6 PIN_D4
#define LCD_DATA7 PIN_D5
#include <18F4550.h>
#fuses MCLR HSPLL PLL2 CPUDIV1 INTRC_IO
#use delay(internal=8MHz)
#use delay(clock = 48MHz)
#use rs232 (baud=9600, xmit=PIN_E1, rcv=PIN_E2)
#use fast_io(D)
#include <lcd.c>
#include <FAT_Lib.c>
#include <string.h>
char file[]="CNC.txt";
int1 ok = 0;
int8* text[8];
void main(void) {
lcd_init();
ok |= sdcard_init();
ok |= fat_init();
if (ok != 0) {lcd_putc("\fNo SD!");while(1);}
delay_ms(1);
ok |= fat_open_file(file);
if(ok == 0) while(fat_read_data(16, text) == 0);
else {lcd_putc("\fNo file!");while(1);}
lcd_putc("\fData:");
lcd_gotoxy(1,2);
printf(lcd_putc,"%s",text);
}
|
I need to take letters from the text and know where the letter is located. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Fri Aug 09, 2019 7:50 am |
|
|
Before we talk about strings, there are significant issues with your
settings.
You have the fuse HSPLL, which says to use an external crystal with the PLL.
Yet you then have the clock setting trying to use the internal oscillator.
You have the clock speed set to 48MHz, which is not a speed available
from the internal oscillator on this chip.
So your serial will not be working.
Do you have a crystal?.
What frequency if so?.
Understand the PIC18F4550, cannot run it's USB without a crystal.
If you are using the internal oscillator, you need to simply use:
#use delay(internal=8MHz)
Which is the fastest speed available from the internal oscillator on this chip.
Look at the data sheet. The 'clock diagram'. Realise there is no connection
from the internal oscillator to the PLL. This is because the PLL on this
chip is primarily designed to drive the USB, and the internal oscillator
is not accurate enough to do this. So the manufacturer does not give this
connection....
Then there is a second (huge) issue. The 18F4550, is a 5v PIC. It will
_require_ level translation hardware to talk to an SD card. Read
the 'sticky' at the top of the forum about connecting to an SD card. |
|
|
strsh
Joined: 16 Jul 2013 Posts: 37
|
|
Posted: Fri Aug 09, 2019 8:53 am |
|
|
My mistake. I put the code wrong. Crystal is an external 8MHz. I don't use USB but I read the SD card. It contains the file "cnc.txt" and the file contains text eg "123456789 etc". I supply the PIC with 3.3V and I have no problem reading the entire text from the SD card, but I want to decompose the text and know where it is in the text. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Fri Aug 09, 2019 9:06 am |
|
|
Seriously change PIC's.
The F4550, is rated to run at 4.2v _minimum_.
You are asking for failures particularly when the temperature rises
trying to run the PIC outside it's specification this far. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Aug 09, 2019 9:18 am |
|
|
About voltage, you should be able to just get a LF version of your processor and drop it right in |
|
|
strsh
Joined: 16 Jul 2013 Posts: 37
|
|
Posted: Fri Aug 09, 2019 9:38 am |
|
|
I know that the power supply is less than its minimum, but with that voltage I can read the SD card and read the text inside it into a variable called text. When I do the final version I will pay attention to it. Thanks for the suggestions.
My question is: How to parse variable text? Letter by letter.
dluu13
strchr () and strcspn () are for comparing (finding) letters in words and I do not need this because I do not know what I will have in the text. If you have an example with these commands, then write it to me. I would be grateful. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Fri Aug 09, 2019 10:10 am |
|
|
Seriously, get a proper PIC, quick !
Just because, today, you can read the SD card is NO guarantee the PIC will actually operate and function 100%, an hour from now let alone tomorrow !! Add some minor hardware, add more code and the PIC will crash (good news) or worse randomly work OK then 'errors' occur (bad news).
Also if you need USB remember that 1/3rd of the PIC's program space will probably be filled with the USB driver code. Unless your main() program is very small, you can easily run out of code space ! BTDT...It only costs me $1 more to buy and use a TTL<>USB module. They've always worked for me, no USB driver needed, so you save a LOT of code space. AS well nearly ANY 1 or 2 pins can be used for 'USB' interfacing.
Jay |
|
|
strsh
Joined: 16 Jul 2013 Posts: 37
|
|
Posted: Fri Aug 09, 2019 10:46 am |
|
|
I have this module (purchased via aliexpress) and have worked with it. If I include a USB connection, I'll use it. Thanks for the suggestion. But I still have a problem decomposing the text variable. Any suggestions? |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Aug 09, 2019 10:56 am |
|
|
But you must be looking for something in the text. Do you have a list of keywords you are looking for? You should read string.h header file yourself and try and find the functions you can put together to help you parse your text.
Besides, the two links I sent you have clear examples about how the functions work. You can follow the site directory back and it will show a listing of all the functions in string.h, with a similarly detailed example for each one. |
|
|
strsh
Joined: 16 Jul 2013 Posts: 37
|
|
Posted: Fri Aug 09, 2019 11:29 am |
|
|
I know what I'm looking for in the text, but it's important that I find it in a certain order and I'm sure nothing is skipped. I think the safest reading of the text is letter by letter (as we read it) and I can conclude what the word is.
Obviously I don't see what you see. I probably don't have enough knowledge. I know this forum does not like to require finished code, but I have no knowledge to see what I need to see. Help. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Aug 09, 2019 11:44 am |
|
|
Even if you provide an example of a text file, and then explain what the text in the file means in your application, and then how you want to process it, then we can help you much better. At this point, I don't know what you are looking for...
But if you really want to read it character by character, then you can do something like:
Code: | char text[] = "teststring";
uint8_t len = strlen(text);
for (int i = 0; i < len; ++i)
{
printf("The current letter is: %c\r\n", text[i]);
} |
This example prints each character in your text on one line. |
|
|
strsh
Joined: 16 Jul 2013 Posts: 37
|
|
Posted: Sat Aug 10, 2019 9:25 am |
|
|
Thanks for the example. I'll try it on Monday and let you know if everything's okay.
Greeting. |
|
|
|