problem saving 16 bit numbers to eeprom Memory

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
David Halliday
Posts: 9
Joined: Tue Jan 10, 2006 4:36 pm
Location: Hampshire
Contact:

problem saving 16 bit numbers to eeprom Memory

Post by David Halliday »

Hi me again !! Still struggling with eeprom memory :shock:

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

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Post by Benj »

Hello David

The problem is this piece of code

Code: Select all

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 
should look like this

Code: Select all

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 
Also this piece of code

Code: Select all

// WRITE value to eeprom memory 
char write_eeprom(unsigned char addr, short data) 
should probably be like this to save a byte or two of memory

Code: Select all

// WRITE value to eeprom memory 
char write_eeprom(unsigned char addr, unsigned char data) 

keshavamurthy k.c
Posts: 18
Joined: Fri Feb 26, 2010 5:05 am
Contact:

EEPROM COMPILING ERROR

Post by keshavamurthy k.c »

HI
i am using EEPROM in my program ,read and write data from EEPROM display in lcd .
if i compile flowcode i am getting following error

E:\lcd-lookuptable\lcdlookuptable.c(176): Illegal redefinition of symbol: MX_EEADR
E:\lcd-lookuptable\lcdlookuptable.c(187): Illegal redefinition of symbol: MX_EEDATA
please suggest how to solve this problem

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: problem saving 16 bit numbers to eeprom Memory

Post by Benj »

Hello

In the C code you have added you may find the lines:

#define MX_EEADR
#define MX_EEDATA

You will be able to see exactly where the problem is by looking at lines 176 and 187 of the output C code file produced by Flowcode.

keshavamurthy k.c
Posts: 18
Joined: Fri Feb 26, 2010 5:05 am
Contact:

Re: problem saving 16 bit numbers to eeprom Memory

Post by keshavamurthy k.c »

hi
What you suggested i check it out in c code file nothing goes wrong in code file defined everything correct.
i am using pic 16f946 microcontroller in my project .
i am unable read and write data from EEPROM and display into lcd and also does not compile flowcode file into hex file because of following error .
any other configuration need it for EEPROM . Is it problem with compiler configuration or any other configuration.

http://www.matrixmultimedia.com
Launching the compiler...
C:\Program Files\Matrix Multimedia\Flowcode V4\BoostC\boostc.pic16.flowcode.exe -v -t PIC16F946 "Storing and retrieving INT variables.c"
BoostC Optimizing C Compiler Version 6.95 (for PIC16 architecture)
http://www.sourceboost.com
Copyright(C) 2004-2009 Pavel Baranov
Copyright(C) 2004-2009 David Hobday

Licensed to FlowCode User under Single user Pro License for 1 node(s)
Limitations: PIC12,PIC16 max code size:Unlimited, max RAM banks:Unlimited


Storing and retrieving INT variables.c
Starting preprocessor: C:\PROGRA~1\MATRIX~1\FLOWCO~1\BoostC\pp.exe "E:\EEPROM STORING\Storing and retrieving INT variables.c" -i C:\PROGRA~1\MATRIX~1\FLOWCO~1\BoostC\include -d _PIC16F946 -la -c2 -o "E:\EEPROM STORING\Storing and retrieving INT variables.pp" -v -d _BOOSTC -d _PIC16

E:\EEPROM STORING\Storing and retrieving INT variables.c(179): Illegal redefinition of symbol: MX_EEADR
E:\EEPROM STORING\Storing and retrieving INT variables.c(190): Illegal redefinition of symbol: MX_EEDATA

2 errors detected
Error: preprocessing error
failure


Return code = 1

Flowcode was unable to compile the flowchart's C code due to the following errors:


If your flowchart contains C code, please review this carefully. If your flowchart contains no C-code or you have thoroughly reviewed the code, contact Technical Support.

FINISHED

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: problem saving 16 bit numbers to eeprom Memory

Post by Benj »

Hello,

Sorry the problem is on lines 179 and 190 of the file E:\EEPROM STORING\Storing and retrieving INT variables.c

Here you must have lines that look something like this.

#define MX_EEADR
#define MX_EEDATA

Comment out these lines in the C file and the code should then compile correctly.

Otherwise please paste lines 178 through 191 onto the forum and I will have a look for you.

Textpad is a good shareware text editor that will show you line numbers if you dont already have something that will do this.

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: problem saving 16 bit numbers to eeprom Memory

Post by medelec35 »

Benj wrote: Textpad is a good shareware text editor that will show you line numbers if you dont already have something that will do this.
Also Notepad++ Which can be used to replace original windows notepad is freeware and also has line numbering.
Martin

keshavamurthy k.c
Posts: 18
Joined: Fri Feb 26, 2010 5:05 am
Contact:

Re: problem saving 16 bit numbers to eeprom Memory

Post by keshavamurthy k.c »

thanks for your quick reply
i attached .c file as well as flow code file .
Attachments
Storing and retrieving INT variables.c
generated c code file
(14.49 KiB) Downloaded 450 times
Storing and retrieving INT variables.fcf
flow code file for eeprom reading and writing
(6 KiB) Downloaded 580 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: problem saving 16 bit numbers to eeprom Memory

Post by Benj »

Hello,

Sorry I cannot answer Flowcode v4 questions outside of the v4 topic. I thought you were programming directly in C using v3 of Flowcode.

If you require access to the Flowcode 4 forum topic then please follow the "Online Resources" link located in the Flowcode V4 help menu.
You will require a registered copy of Flowcode 4 to gain access to the forum topic.

Post Reply