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

Write Integer to EEPROM

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Write Integer to EEPROM
PostPosted: Wed Jul 26, 2017 2:19 am     Reply with quote

Hi,
I need to write an integer 1234 to eeprom.
Suppose it should display 1234, but it will show 210.
I need help to solve this problem.
Here is my code.



Code:


#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <math.h>
#include "24256.c"


void main()
   {
      char c;
      unsigned char d;
      unsigned char key;
      int32 data=1234;
     

   usb_init_cs();
   


   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                                       

                 while(true)
                 
                     {

     
                        d = usb_cdc_getc();
     
                               if(d=='C')   // push C
                                   {
                                      while (key!=32) // push SPACE BAR to stop
                                         {
     
                                          if(usb_cdc_kbhit())
                                            {key=usb_cdc_getc();}
         
                                              init_ext_eeprom();
                                                 {
                                                    write_ext_eeprom(0, data);
                                               
                                                    read_ext_eeprom(0);
                                                   
                                                    printf(usb_cdc_putc,"\r\nmSEC: %lu\r\n",(int16)read_ext_eeprom(0) );
                                                 }                                   
     
                                         } key=0; // to initialize 'key' not as SPACE BAR
                                   }


                     }
               }     
         }           
}                   
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 2:43 am     Reply with quote

write_eeprom, and read_eeprom, read _one byte_, to/from the EEPROM. That is what it says in the manual....

The functions to read/write larger types, are in the library 'internal_eeprom.c'.

write_int32_eeprom, and read_int32_eeprom
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 2:44 am     Reply with quote

Look in the CCS drivers directory for the external_eeprom.c file.
It has functions to read and write int32 variables to eeprom.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 2:48 am     Reply with quote

Apologies. Heading said 'EEPROM', not 'external EEPROM', so I assumed the internal EEPROM.....
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 3:29 am     Reply with quote

If I change to int32, it will compile with error --> Undefined identifier -- write_int32_ext_eeprom.

What should I do?

Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <math.h>
#include "24256.c"


void main()
   {
      char c;
      unsigned char d;
      unsigned char key;
      int32 data=1234;
     

   usb_init_cs();
   


   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                                       

                 while(true)
                 
                     {

     
                        d = usb_cdc_getc();
     
                               if(d=='C')   // push C
                                   {
                                      while (key!=32) // push SPACE BAR to stop
                                         {
     
                                          if(usb_cdc_kbhit())
                                            {key=usb_cdc_getc();}
         
                                              init_ext_eeprom();
                                                 {
                                                    write_int32_ext_eeprom(0, data);
                                               
                                                    read_int32_ext_eeprom(0);
                                                   
                                                    printf(usb_cdc_putc,"\r\nmSEC: %lu\r\n",read_int32_ext_eeprom(0) );
                                                 }                                   
     
                                         } key=0; // to initialize 'key' not as SPACE BAR
                                   }


                     }
               }     
         }           
}               
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 3:52 am     Reply with quote

Art, when you add a driver file, you #include it, just like you've done with
all the other drivers near the start of your program.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 3:58 pm     Reply with quote

Hi PCM,
I've just modified the code. I found out if i put #include <external_eeprom.c> , it will compile with many errors , however if write the code as below it can successfully compile. However i have not test it. Is it correct ?

Code:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <math.h>
#include "24256.c"


int8 i;
int16 data;

void write_int16_ext_eeprom(EEPROM_ADDRESS address, int16 data)
{
   for(i = 0; i < 2; ++i)
   {
     write_ext_eeprom(address + i, *((int8 *)(&data) + i));     
   }
}


int16 read_int16_ext_eeprom(EEPROM_ADDRESS address)
{
   for(i = 0; i < 2; ++i)
   {
      *((int8 *)(&data) + i) = read_ext_eeprom(address + i);
   }
   return(data);
}


void main()
   {
      char c;
      unsigned char d;
      unsigned char key;
      int16 data=1234;
     

   usb_init_cs();
   


   while (TRUE)
         {
         usb_task();


            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                                       

                 while(true)
                 
                     {

     
                        d = usb_cdc_getc();
     
                               if(d=='C')   // push C
                                   {
                                      while (key!=32) // push SPACE BAR to stop
                                         {
     
                                          if(usb_cdc_kbhit())
                                            {key=usb_cdc_getc();}
         
                                              init_ext_eeprom();
                                                 {
                                                    write_int16_ext_eeprom(0, data);
                                               
                                                    read_int16_ext_eeprom(0);
                                                   
                                                    printf(usb_cdc_putc,"\r\nmSEC: %lu\r\n",read_int16_ext_eeprom(0) );
                                                 }                                   
     
                                         } key=0; // to initialize 'key' not as SPACE BAR
                                   }


                     }
               }     
         }           
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 5:01 pm     Reply with quote

You have to put it below the 24256.c eeprom driver, as shown in bold
below. You will get some USB driver warnings, but those don't matter.
Quote:

#include <18F4550.h>
#fuses HSPLL,NOWDT,PROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)

#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <math.h>
#include "24256.c"
#include <external_eeprom.c>

If you still get errors, then post the errors and your CCS compiler version.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 6:51 pm     Reply with quote

Hai PCM

If i put #include<external_eeprom.c> before #include "24256.c". It will show errors when compile . If i put after it, it can compile without error. I have test the code and it works. It shows output 1234.

Thanks PCM
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Wed Jul 26, 2017 11:13 pm     Reply with quote

It's worth saying 'why'.

The 'driver' here is a set of code. It provides functions to read/write multiple bytes to EEPROM, _using the standard functions to read/write one byte to the EEPROM_.
The compiler reads things sequentially.
So the function to read/write one byte, needs to be prototyped or defined before the multi byte 'driver' can use them.

This is why the includes need to be before you use them. Very Happy

First a comment.
You only need to init the EEPROM once at the start of the code.

Then a general question.
Why are you including math.h?.
Math.h is not needed for the normal arithmetic (+-*/ etc..). It is only needed for 'high level' maths (sin, code, exp etc.). Generally this maths is stuff that you should think very carefully before using in a microprocessor.
At the moment you are not using it, but 'beware' if you do.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Thu Jul 27, 2017 1:40 am     Reply with quote

Dear ttelmah

Thanks for the explanation.

Actually, the original code is quite long and i forgot to delete the Math.h function.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Jul 27, 2017 4:14 am     Reply with quote

The comment about math.h still applies though... Very Happy

You should think _very_ hard if there is a way not to use it. 99.9% of code can be done using a bit more ingenuity, and often saving a lot of size/time, without this. The few things that do 'need' high level maths are often better coded using perhaps a look up table, or slightly less accurate alternative functions.

In possibly several thousand projects, I've only used math.h on a PIC in perhaps less than 1% of them.
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Thu Jul 27, 2017 4:31 am     Reply with quote

Ttelmah wrote:
several thousand projects


Shocked Shocked Shocked Shocked

Superman.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Thu Jul 27, 2017 4:44 am     Reply with quote

No, just been at it a _long_ time.... (There isn't a standard emoticon for something like a 'father time' figure).

When MicroChip posted a 'potted history' of the PIC, I had to point out to them that one of their dates was wrong, since I had been using a PIC before they said it had been released....
Also some things can be tiny. Just a chip to generate a particular timing etc..
If you are involved in perhaps ten big projects, and then a few dozen little things like this a year, and do it non stop for perhaps 40 years, it 'adds up'....
Many of the big things use larger processors, but less and less now, with the capabilities of the 16bit PIC's.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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