problem saving 16 bit numbers to eeprom Memory
Posted: Fri Feb 09, 2007 2:58 pm
Hi me again !! Still struggling with eeprom memory
I'm trying to save a recipe of numbers to eeprom memory. 5 time values ie ,1.5Sec's and a powerlevel say 600 watts.
The time values, save and recalls as you would expect. But the powerlevel figure only will save a maximum number of 255 before it loops back round and give an incorrect figure.
I realise this is a 16-bits numbers to eeprom memory. I understand that you must save the lower eight bits seperate from the upper eight bits.
But when I've tested the save and recall on my test board. The information returned for power is still only eight bits. Can anybody see where I'm going wrong. As the only other method of saving is to divide the number by 255 and re-add the value when reloaded. Which will take up much more eeprom memory space.
Here is a copy of the code I'm using to save information to memory
many thanks Dave H

I'm trying to save a recipe of numbers to eeprom memory. 5 time values ie ,1.5Sec's and a powerlevel say 600 watts.
The time values, save and recalls as you would expect. But the powerlevel figure only will save a maximum number of 255 before it loops back round and give an incorrect figure.
I realise this is a 16-bits numbers to eeprom memory. I understand that you must save the lower eight bits seperate from the upper eight bits.
But when I've tested the save and recall on my test board. The information returned for power is still only eight bits. Can anybody see where I'm going wrong. As the only other method of saving is to divide the number by 255 and re-add the value when reloaded. Which will take up much more eeprom memory space.
Here is a copy of the code I'm using to save information to memory
Code: Select all
This function will hold 10 recipes with 5 data values which have been setup within the
main function. The files are placed into an array for easier storage and easier unstorage.
#include<system.h>
// READ value to eeprom memory
unsigned char read_eeprom(unsigned short addr)
{
eeadr = addr; // low byte
eeadrh = 0; // high byte
clear_bit(eecon1,EEPGD); // select eeprom memory
set_bit(eecon1,RD); // set read bit
return(eedata);
}
// WRITE value to eeprom memory
char write_eeprom(unsigned char addr, short data)
{
eeadrh = 0; // high byte
eeadr = addr; // low byte
eedath = 0; // high byte
eedata = data; // low byte
clear_bit(eecon1,EEPGD); // select eeprom data memory
set_bit(eecon1,WREN); // enable write
eecon2 = 0x55;
eecon2 = 0xaa;
set_bit(eecon1,WR); // write command
while(eecon1&2); // wait untill writing is ready
clear_bit(eecon1,WREN); // disable write
}
unsigned char stored_values[5];
unsigned int power;
char Send_values_to_memory(unsigned char start_address)
{
int i=0;
for ( i=0; i < 4; i++ )
{
write_eeprom(start_address,stored_values[i]);// write values to eeprom memory
start_address++;
}
// Save 16 bit number to, two 8 bit EEPROM addresses
write_eeprom(start_address,power); // load power level to memory first 8 bits
start_address++; // increment holding address
write_eeprom(start_address,(power>>8)); // Load power level to memory second 8 bits
}
/* This function will hold 10 recipes with 5 data values which have been setup within the
main function. The files are placed into a array for easier storage and easier unstorage*/
char write_to_memory (unsigned char pre_weld,unsigned char pre_warm_time,unsigned char weld_time,unsigned char dwell_time,
int powerlevel,char save)
{
stored_values[0]=pre_weld;
stored_values[1]=pre_warm_time;
stored_values[2]=weld_time;
stored_values[3]=dwell_time;
power =powerlevel;
switch(save)
{
case 1 : Send_values_to_memory(0x00);
break;
case 2 : Send_values_to_memory(0x08);
break;
case 3 : Send_values_to_memory(0x0E);
break;
case 4 : Send_values_to_memory(0x14);
break;
case 5 : Send_values_to_memory(0x19);
break;
case 6 : Send_values_to_memory(0x1E);
break;
case 7 : Send_values_to_memory(0x23);
break;
case 8 : Send_values_to_memory(0x28);
break;
case 9 : Send_values_to_memory(0x2D);
break;
case 10 : Send_values_to_memory(0x32);
break;
}
}
unsigned char reloaded_recipe[4];
int reload_values_from_memory(unsigned char start_add)
{
int i;
for ( i=0 ; i < 4 ; i++ )
{
reloaded_recipe[i]= read_eeprom(start_add);
start_add++;
}
power=read_eeprom(start_add);// load first 8 bits
start_add++; // Increment address location
power= read_eeprom(start_add)<<8; // load second 8 bits back into power to reform 16bit int
}
char read_from_memory(char program_number)
{
switch(program_number)
{
case 1 : reload_values_from_memory(0x00);
break;
case 2 : reload_values_from_memory(0x08);
break;
case 3 : reload_values_from_memory(0x0E);
break;
case 4 : reload_values_from_memory(0x14);
break;
case 5 : reload_values_from_memory(0x19);
break;
case 6 : reload_values_from_memory(0x1E);
break;
case 7 : reload_values_from_memory(0x23);
break;
case 8 : reload_values_from_memory(0x28);
break;
case 9 : reload_values_from_memory(0x2d);
break;
case 10 : reload_values_from_memory(0x32);
break;
}
}
many thanks Dave H