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 support@ccsinfo.com

Writing eeprom isn't working for me.

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







Writing eeprom isn't working for me.
PostPosted: Tue Nov 15, 2005 10:16 pm     Reply with quote

I am trying to write a data table to eeprom on the 16F873A. I would like the device to perform and A to D (8-bit) on A0, and pull data out of the resultant eeprom address. The data is then output to the ports as a 3 bit decimal number.

This code was working and displaying the A-D conversion.

Code:
#include <16F873A.h>
#device adc=8
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)

void main()
{
   int value, hunten, hund, tens, ones;
   setup_adc_ports(AN0_AN1_VSS_VREF); //VREF = A3
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   set_tris_b(0x00);
   set_tris_c(0x00);
   set_adc_channel(0);


while ( TRUE )
   {
   value = read_adc();
   hund = value / 100;
   value = value % 100;
   tens = value / 10;
   ones = value % 10;
   hunten = 16*hund + tens;
   output_b(hunten);
   output_c(ones);
   delay_ms(2000);
   }

}


I then tried to create the eeprom lookup table and i get only 0's on the output. I know there must be easier ways but i thaught this might work.




Code:

#include <16F873A.h>
#device adc=8
#fuses NOWDT,HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=4000000)

void main()
{
   int data, value, hunten, hund, tens, ones;
   setup_adc_ports(AN0_AN1_VSS_VREF); //VREF = A3
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   set_tris_b(0x00);
   set_tris_c(0x00);
   set_adc_channel(0);

//***** WRITE DATA TABLE TO EEPROM******//

   write_eeprom(   15   ,   112   );
   write_eeprom(   16   ,   104   );
   write_eeprom(   17   ,   97   );
   write_eeprom(   18   ,   90   );
   write_eeprom(   19   ,   85   );
   write_eeprom(   20   ,   80   );
   write_eeprom(   21   ,   75   );
   write_eeprom(   22   ,   71   );
   write_eeprom(   23   ,   68   );
   write_eeprom(   24   ,   64   );
   write_eeprom(   25   ,   61   );
   write_eeprom(   26   ,   58   );
   write_eeprom(   27   ,   56   );
   write_eeprom(   28   ,   54   );
   write_eeprom(   29   ,   51   );
   write_eeprom(   30   ,   49   );
   write_eeprom(   31   ,   47   );
   write_eeprom(   32   ,   46   );
   write_eeprom(   33   ,   44   );
   write_eeprom(   34   ,   42   );
   write_eeprom(   35   ,   41   );
   write_eeprom(   36   ,   40   );
   write_eeprom(   37   ,   38   );
   write_eeprom(   38   ,   37   );
   write_eeprom(   39   ,   36   );
   write_eeprom(   40   ,   35   );
   write_eeprom(   41   ,   34   );
   write_eeprom(   42   ,   33   );
   write_eeprom(   43   ,   32   );
   write_eeprom(   44   ,   31   );
   write_eeprom(   45   ,   30   );
   write_eeprom(   46   ,   30   );
   write_eeprom(   47   ,   29   );
   write_eeprom(   48   ,   28   );
   write_eeprom(   49   ,   27   );
   write_eeprom(   50   ,   27   );
   write_eeprom(   51   ,   26   );
   write_eeprom(   52   ,   26   );
   write_eeprom(   53   ,   25   );
   write_eeprom(   54   ,   24   );
   write_eeprom(   55   ,   24   );
   write_eeprom(   56   ,   23   );
   write_eeprom(   57   ,   23   );
   write_eeprom(   58   ,   22   );
   write_eeprom(   59   ,   22   );
   write_eeprom(   60   ,   22   );
   write_eeprom(   61   ,   21   );
   write_eeprom(   62   ,   21   );
   write_eeprom(   63   ,   20   );
   write_eeprom(   64   ,   20   );
   write_eeprom(   65   ,   20   );
   write_eeprom(   66   ,   19   );
   write_eeprom(   67   ,   19   );
   write_eeprom(   68   ,   19   );
   write_eeprom(   69   ,   18   );
   write_eeprom(   70   ,   18   );
   write_eeprom(   71   ,   18   );
   write_eeprom(   72   ,   17   );
   write_eeprom(   73   ,   17   );
   write_eeprom(   74   ,   17   );
   write_eeprom(   75   ,   16   );
   write_eeprom(   76   ,   16   );
   write_eeprom(   77   ,   16   );
   write_eeprom(   78   ,   16   );
   write_eeprom(   79   ,   15   );
   write_eeprom(   80   ,   15   );
   write_eeprom(   81   ,   15   );
   write_eeprom(   82   ,   15   );
   write_eeprom(   83   ,   15   );
   write_eeprom(   84   ,   14   );
   write_eeprom(   85   ,   14   );
   write_eeprom(   86   ,   14   );
   write_eeprom(   87   ,   14   );
   write_eeprom(   88   ,   14   );
   write_eeprom(   89   ,   13   );
   write_eeprom(   90   ,   13   );
   write_eeprom(   91   ,   13   );
   write_eeprom(   92   ,   13   );
   write_eeprom(   93   ,   13   );
   write_eeprom(   94   ,   12   );
   write_eeprom(   95   ,   12   );
   write_eeprom(   96   ,   12   );
   write_eeprom(   97   ,   12   );
   write_eeprom(   98   ,   12   );
   write_eeprom(   99   ,   12   );
   write_eeprom(   100   ,   12   );
   write_eeprom(   101   ,   11   );
   write_eeprom(   102   ,   11   );
   write_eeprom(   103   ,   11   );
   write_eeprom(   104   ,   11   );
   write_eeprom(   105   ,   11   );
   write_eeprom(   106   ,   11   );
   write_eeprom(   107   ,   11   );
   write_eeprom(   108   ,   11   );
   write_eeprom(   109   ,   10   );
   write_eeprom(   110   ,   10   );
   write_eeprom(   111   ,   10   );
   write_eeprom(   112   ,   10   );
   write_eeprom(   113   ,   10   );
   write_eeprom(   114   ,   10   );
   write_eeprom(   115   ,   10   );
   write_eeprom(   116   ,   10   );
   write_eeprom(   117   ,   10   );
   write_eeprom(   118   ,   9   );
   write_eeprom(   119   ,   9   );
   write_eeprom(   120   ,   9   );
   write_eeprom(   121   ,   9   );
   write_eeprom(   122   ,   9   );
   write_eeprom(   123   ,   9   );
   write_eeprom(   124   ,   9   );
   write_eeprom(   125   ,   9   );
   write_eeprom(   126   ,   9   );
   write_eeprom(   127   ,   9   );

while ( TRUE )
   {
   data = read_adc();
   value = read_eeprom(data);
   hund = value / 100;
   value = value % 100;
   tens = value / 10;
   ones = value % 10;
   hunten = 16*hund + tens;
   output_b(hunten);
   output_c(ones);
   delay_ms(2000);
   }

}


The compiler tells me out of ROM but the 16F873A has 128 bytes (8-bit) of eeprom. I then tried deleting until it worked. It would compile after everything lower than 80 is deleted and the says 51% full. makes me think it wants a hex address but i have read that decimal should be fine. If anyone has a solution or a way around this to get me reading off some sort of preprogrammed table with every A-D conversion the help would be great.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 15, 2005 11:19 pm     Reply with quote

Quote:
The compiler tells me out of ROM but the 16F873A has 128 bytes (8-bit) of eeprom.

ROM does not mean eeprom. It means the main program memory.
I compiled your program with PCM vs. 3.239 and it only used 190 words
of ROM and 14 bytes of RAM. I don't see how you can be running
out of ROM.

There are a few problems with your program. You're reading an 8-bit
value from the A/D, which can range from 0 to 255. Then you're
using that value as an index into eeprom which only exists at addresses
from 0 to 127. Also, you haven't initialized addresses 0 to 14 in the
eeprom.

You have enabled Vref on pin RA3. Are you applying a reference
voltage to that pin ?

If you want to have a lookup table, it would be a lot easier if you
put it in program memory instead of data eeprom. Then you can
initialize the table at compile-time, instead of at run-time as you are
doing. Example:

Code:
#include <16F873A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


int8 const lookup_table[] =
{
   0,1,2,3,4,5,6,7,8,9
  10,11,12,13,14,15,16,17,18,19,
  20,21,22,23,24,25,26,27,28,29,
  30,31,32,33,34,35,36,37,38,39,
  40,41,42,43,44,45,46,47,48,49,
  50,51,52,53,54,55,56,57,58,59,
  60,61,62,63,64,65,66,67,68,69,
  70,71,72,73,74,75,76,77,78,79,
  80,81,82,83,84,85,86,87,88,89,
  90,91,92,93,94,95,96,97,98,99,
  100,101,102,103,104,105,106,107,108,109,
  110,111,112,113,114,115,116,117,118,119,
  120,121,122,123,124,125,126,127
 };
 
//======================================
void main()
{
int8 i;

// Display the lookup table.
for(i = 0; i < sizeof(lookup_table); i++)
    printf("%u \n\r", lookup_table[i]);
   
while(1);
}
kender



Joined: 09 Aug 2004
Posts: 768
Location: Silicon Valley

View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger

PostPosted: Tue Nov 15, 2005 11:48 pm     Reply with quote

Wow, it's the first time I see the lookup table in the EEPROM. It could be a neat trick for creating dynamic lookup table. I know I should probably read the datasheet for this (and I will), but how does the read time compare for the on-chip EEPROM, ROM and RAM?

Sincerely,
~K
Guest








PostPosted: Wed Nov 16, 2005 1:04 am     Reply with quote

Thanks I'll give the data table a go how you've suggested
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