|
|
View previous topic :: View next topic |
Author |
Message |
whmeade10
Joined: 27 Jan 2004 Posts: 12
|
Placing EEPROM values in a 18F2420 |
Posted: Tue Dec 11, 2007 12:59 pm |
|
|
I have looked up how to pre-program some data into a 18f2420 eeprom and I can't seem to get it working. It is probably a very simple error, on my part of course. My code is below. What am I doing wrong? The first two numbers are correct, but the 3rd, 4th, and 5th numbers are wrong.
Code: |
#include <18F2420.h>
#use delay(clock=20000000,RESTART_WDT)
#fuses HS,PROTECT,WDT,NOLVP,PUT,BROWNOUT // set fuses
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, stream=RS485)// setup 232 serial communication
#rom int 0xf00000={0,20,75,2000,6000,32766}
// ********** DEFINE VARIABLES ***********************************************************************
int16 counts_empty, counts_partial, counts_full, percent_partial, percent_full;
// ********** START OF MAIN PROGRAM ******************************************************************
void main()
{
percent_partial = read_eeprom(1);
percent_full = read_eeprom(2);
counts_empty = read_eeprom(3);
counts_partial = read_eeprom(4);
counts_full = read_eeprom(5);
///////////////////////////////////////////////////////////////////////////////////////
fprintf(RS485 ,"PROGRAM STARTUP\n\r");
fprintf(RS485 ,"Percent Partial = %lu %% \n\r", percent_partial);
fprintf(RS485 ,"Percent Full = %lu %% \n\r", percent_full);
fprintf(RS485 ,"Counts Empty = %lu \n\r", counts_empty);
fprintf(RS485 ,"Counts Partial = %lu \n\r", counts_partial);
fprintf(RS485 ,"Counts Full = %lu \n\r\n\r", counts_full);
///////////////////////////////////////////////////////////////////////////////////////
do
{
} while (true); // never ending loop
}
|
This is what I am getting out the serial port. Used the code input to get the formatting correct.
Code: |
PROGRAM STARTUP
Percent Partial = 20 %
Percent Full = 75 %
Counts Empty = 208
Counts Partial = 112
Counts Full = 254
|
Thanks for taking a look at my question..[/code] |
|
|
frequentguest Guest
|
|
Posted: Tue Dec 11, 2007 1:13 pm |
|
|
The EEPROM locations are only 8 bits in size. Your 3rd, 4th, and 5th values are greater than 255 and therefore require 2 memory locations. |
|
|
Ttelmah Guest
|
|
Posted: Tue Dec 11, 2007 1:18 pm |
|
|
The key to your problem, is that you are telling the #ROM directive to write int8 values, with the keyword 'int'(remember an int in CCS, is an int8), but are then writing int16 values to it (using numbers over 255). 2000 decimal, is 0x7D0. D0 in decimal, is '208'...
The internal read function, reads bytes.
Best Wishes |
|
|
whmeade10
Joined: 27 Jan 2004 Posts: 12
|
|
Posted: Tue Dec 11, 2007 2:24 pm |
|
|
I would like to thank everyone that responded to my question. It got me thinking that all I need to do is modify the write_float_eeprom routines. All I do is check to see if the program is being run for the first time, and if so write defaults to the EEPROM.
Here is my revised code:
Code: |
#include <18F2420.h>
#use delay(clock=20000000,RESTART_WDT)
#fuses HS,PROTECT,WDT,NOLVP,PUT,BROWNOUT // set fuses
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, stream=RS485)// setup 232 serial communication
#rom int 0xF00000={0}
// ********** DEFINE VARIABLES ***********************************************************************
int eeprom_status;
int16 counts_empty, counts_partial, counts_full, percent_partial, percent_full;
// ********** WRITE FLOAT VARIABLES TO EEPROM *******************************************************
void write_int16_eeprom(long int n, int16 data)
{
int i; // define variable
for (i = 0; i < 2; i++)
write_eeprom(i + n, *((int8*)&data + i) ) ;
}
// ********** READ FLOAT VARIABLES TO EEPROM ********************************************************
int16 read_int16_eeprom( long int n)
{
int i; // define variable
int16 data; // define variable
for (i = 0; i < 2; i++)
*((int8*)&data+ i) = read_eeprom(i + n);
return(data);
}
// ********** START OF MAIN PROGRAM ******************************************************************
void main()
{
read_eeprom(0);
if (eeprom_status == 0)
{
write_eeprom(0,1);
write_int16_eeprom(1,20);
write_int16_eeprom(3,75);
write_int16_eeprom(5,2000);
write_int16_eeprom(7,6000);
write_int16_eeprom(9,32766);
}
percent_partial = read_int16_eeprom(1);
percent_full = read_int16_eeprom(3);
counts_empty = read_int16_eeprom(5);
counts_partial = read_int16_eeprom(7);
counts_full = read_int16_eeprom(9);
///////////////////////////////////////////////////////////////////////////////////////
fprintf(RS485 ,"PROGRAM STARTUP\n\r");
fprintf(RS485 ,"Percent Partial = %lu %% \n\r", percent_partial);
fprintf(RS485 ,"Percent Full = %lu %% \n\r", percent_full);
fprintf(RS485 ,"Counts Empty = %lu \n\r", counts_empty);
fprintf(RS485 ,"Counts Partial = %lu \n\r", counts_partial);
fprintf(RS485 ,"Counts Full = %lu \n\r\n\r", counts_full);
///////////////////////////////////////////////////////////////////////////////////////
do
{
} while (true); // never ending loop
|
This is the output:
Code: |
PROGRAM STARTUP
Percent Partial = 20 %
Percent Full = 75 %
Counts Empty = 2000
Counts Partial = 6000
Counts Full = 32766
|
|
|
|
|
|
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
|