CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Read letters from text
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
strsh



Joined: 16 Jul 2013
Posts: 37

View user's profile Send private message

PostPosted: Mon Aug 12, 2019 8:38 am     Reply with quote

Greeting.
The code is fine, but the rest is wrong.
The whole story is to read a text file in an SD card and break it down into letters. I thought the code I wrote worked just fine but I have a problem with the command "fat_read_data (16, text)" because the whole text inserts me into the "text" variable and if the text is too long it fills my memory in the controller.
I'm trying to read letter by letter from the SD card and assign it to a variable and process it in the controller. That way I would avoid the problem of the file length in the SD card.
I try but I fail.
Example: A SD card has a file named "cnc.txt" and a text of 1000 characters (letters) written in it. The controller should read the first character and assign it to the variable. After processing that variable to take the second character and assign it to the variable, then to take the third character and assign it to the variable and so on. I would compare the variables with the reference text, and depending on what is written, the controller would perform some process. After that I would delete all the variables but remember where I left off and then re-read the next character from the SD card.
I hope it's clear what I want.
According to the 18f4550 controller it says "Wide Operating Voltage Range (2.0V to 5.5V)". Am I wrong or not?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 12, 2019 10:12 am     Reply with quote

Look at this graph in the Electrical Characteristics section of the data sheet:
Quote:

FIGURE 28-2: PIC18LF2455/2550/4455/4550 VOLTAGE-FREQUENCY GRAPH
(INDUSTRIAL LOW VOLTAGE)

That wide voltage range is for the "LF" chip. You probably don't have that.
You probably have the "F" chip.


Quote:
A SD card has a file named "cnc.txt" and a text of 1000 characters

Post the first several lines from that file. Or post the first 200 characters.
strsh



Joined: 16 Jul 2013
Posts: 37

View user's profile Send private message

PostPosted: Mon Aug 12, 2019 10:38 am     Reply with quote

You're right about the voltage. I see also the LF chip must be powered with a minimum of 4.2V for 48MHz?
I thought about that solution and I will try it tomorrow, but I would still try it another way (letter by letter).
Thank you.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Aug 12, 2019 10:40 am     Reply with quote

If the SD text fle is smaller than say 1800 bytes, I'd read the whole file into a simple one dimensioned array THEN have main() 'process' the contents of the array.
That PIC has 2K of RAM, so 1800 would allow 200 bytes for other variables.

Perhaps this is an option ??

Also when working with 99% of peripherals, it's best to use an 'L' rated PIC , as they are good for 3 volt operation. There are several PICs, the 18F46K22, that acually will run at 3 or 5 no problem. Be sure to check processor clock speed AT the operating VDD though !! Some will NOT run at full speed !

Jay
strsh



Joined: 16 Jul 2013
Posts: 37

View user's profile Send private message

PostPosted: Mon Aug 12, 2019 11:23 am     Reply with quote

This is temporary. I ordered an SD card reader via Aliexpress so I will power PIC with 5V.
I think about it. I'll try.
Thank you.
PrinceNai



Joined: 31 Oct 2016
Posts: 478
Location: Montenegro

View user's profile Send private message

PostPosted: Mon Aug 12, 2019 9:17 pm     Reply with quote

Quote:

Example: A SD card has a file named "cnc.txt" and a text of 1000 characters (letters) written in it. The controller should read the first character and assign it to the variable. After processing that variable to take the second character and assign it to the variable, then to take the third character and assign it to the variable and so on. I would compare the variables with the reference text, and depending on what is written, the controller would perform some process. After that I would delete all the variables but remember where I left off and then re-read the next character from the SD card.


I must say I don't really understand what you want to do. I don't understand what you mean by assigning a variable to each letter. The way I see it, you want to find some fixed text, let's say "FIND" when you read the card char by char and the position or address of the last character of that text. You don't need a buffer for that. You can most likely do that on the fly. From the top of my head: create a switch statement. read the character into a variable. Compare with 'F'. If it is 'F', move to the next state that expects 'I', and then 'N' and so on. If 'I' doesn't come after 'F', reset and wait for another 'F'. Increment counter of characters read.

example:
Code:


int16 PositionCounter = 0;
int8 FIND_MY_TEXT = 0;      // init state machine
// you come here every time you read a character from the card

tmp = CHARACTER_FROM_THE_CARD;

switch FIND_MY_TEXT {
   case 0:{
      if(tmp = 'F'){
         FIND_MY_TEXT = 1;            // got'F', start looking for 'I'
      }
      else{
         FIND_MY_TEXT = 0;
      }
      PositionCounter++;            // count processed characters, no matter what came in
      break;
   }
   case 1:{
      if(tmp = 'I'){
         FIND_MY_TEXT = 2;            // got 'I' after 'F', look for 'N'
      }
      else{
         FIND_MY_TEXT = 0;            // 'I' didn't come after 'N', reset. And so on for each character
      }
      PositionCounter++;
      break;
   }   
   case 2:{
      if(tmp = 'N'){
         FIND_MY_TEXT = 3;
      }
      else{
         FIND_MY_TEXT = 0;
      }
      PositionCounter++;
      break;
   }   
   case 3:{
      if(tmp = 'D'){
         FIND_MY_TEXT = 0;         // reset state machine.
         SequenceFound = 1;      // Stop executing the state machine and process the data as you wish. Then proceed from that point. Either with PositionCounter 0 or from there on.      
                                 // You have found your text and position of the last character. Beware that PositionCounter starts with 0, so you have 3 here if the first character was 'F'
      }
      else{
         FIND_MY_TEXT = 0;
                        PositionCounter++;
      }         
      break;
   }
      default:{
         FIND_MY_TEXT = 0;
                        PositionCounter++;
         break;
      }    
}


strsh



Joined: 16 Jul 2013
Posts: 37

View user's profile Send private message

PostPosted: Tue Aug 13, 2019 6:53 am     Reply with quote

Your code is fine and similar to my request but I have a problem with "walking" through the text file that is in the SD card. I was able to read the text from top to bottom and have information where I got in the text, but the problem is if I want to return to a previous position in the text. I'm sending new code and if anyone has an idea how to solve the problem write it.
The command "file_pointer" shows me where I am, but it is not possible to move it to another place with its change.
I apologize if I "walk" with questions.
Thanks for your patience.
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>
#device PASS_STRINGS = IN_RAM
#fuses MCLR HSPLL PLL2 CPUDIV1
#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>
//file_size ; file_pointer
char file[]="cnc.txt";
int1 ok = 0;
int8 x, y, data[16];

void read_SD_card(){
   while(fat_read_data(16, data) == 0){
      for(x = 0; x < 16; x++){
    y = data[x];
    lcd_putc(y);
      }
      lcd_gotoxy(1,2);
      delay_ms(2000);
      lcd_putc("\f");
   }
     lcd_putc("END!");while(1);
}

void main(void){
   lcd_init();
   ok |= sdcard_init();
   ok |= fat_init();
   ok |= fat_open_file(file);
   if(ok == 0){
      read_SD_card();
   }
}
PrinceNai



Joined: 31 Oct 2016
Posts: 478
Location: Montenegro

View user's profile Send private message

PostPosted: Tue Aug 13, 2019 7:37 am     Reply with quote

How long does it take to read the whole file? Maybe if you read it whole every time, but flush all the data up to the point you found your text?

From my previous example:

at the start PositionCounter = 0

read data, process data, increment counter. Somewhere you find your text. Remember that position. If you want to check further, read all the data again, but start processing AFTER you reach the point you found the first text.
strsh



Joined: 16 Jul 2013
Posts: 37

View user's profile Send private message

PostPosted: Tue Aug 13, 2019 2:45 pm     Reply with quote

That works too. But isn't it easier to write the correct reading location than to read the entire text each time?
If we can't solve the problem of typing in the correct location for reading the text, then I will use your idea.
I apologize for writing confusingly, but my native language is not English. I use google translator.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group