[SDCC] Accessing EEPROM on PIC18 - simple example
Monday, May 22, 2006 8:56:22 PM
// read and write eeprom of PIC18F2550 (and 18F2455, 18F4455, 18F4550)
// EEPROM size is 256 bytes
// (c) Raphael Wimmer. Licensed under GNU GPL v2 or higher
#include <pic18fregs.h>
#include <stdio.h>
#include <usart.h>
#pragma stack 0x300 0xff // set 64 byte stack at 0x300, needed by sdcc
void ee_write_byte(unsigned char address, unsigned char *_data){
EEDATA = *_data;
EEADR = address;
// start write sequence as described in datasheet, page 91
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1; // enable writes to data EEPROM
INTCONbits.GIE = 0; // disable interrupts
EECON2 = 0x55;
EECON2 = 0x0AA;
EECON1bits.WR = 1; // start writing
while(EECON1bits.WR){
_asm nop _endasm;}
if(EECON1bits.WRERR){
printf("ERROR: writing to EEPROM failed!\n");
}
EECON1bits.WREN = 0;
INTCONbits.GIE = 1; // enable interrupts
}
void ee_read_byte(unsigned char address, unsigned char *_data){
EEADR = address;
EECON1bits.CFGS = 0;
EECON1bits.EEPGD = 0;
EECON1bits.RD = 1;
*_data = EEDATA;
}
void initUsart()
{
usart_open( // Use USART library to initialise the hardware
USART_TX_INT_OFF
& USART_RX_INT_OFF
& USART_BRGH_HIGH
& USART_ASYNCH_MODE
& USART_EIGHT_BIT,
10 // '10' = 115200 Baud with 20 MHz oscillator and BRGH=1
);
stdout = STREAM_USART;
}
// very simple example. use on an erased eeprom
void main(){
char save_me = 'x';
char from_eeprom;
initUsart();
printf("EEPROM-Demo\n");
ee_read_byte(0x00, &from_eeprom);
printf("Char read from 0x00: %c\n", from_eeprom);
ee_write_byte(0x00, &save_me);
printf("Char written to 0x00: %c\n", save_me);
ee_read_byte(0x00, &from_eeprom);
printf("Char read from 0x00: %c\n\n", from_eeprom);
}








Anonymous # Wednesday, July 12, 2006 5:55:58 PM
Raphael Wimmerraphman # Thursday, July 20, 2006 8:55:43 PM
I'm not quite sure if your PIC is supported by sdcc. The (not current) snapshot of sdcc I use doesn't include header files for the 18f4620 or other PICs from its family.
Usually you have to provide both architecture and model of the microcontroller to sdcc.
If your PIC is supported the following command should work:
sdcc -mpic16 -p18f4620 main.c
The -m architectures are named very confusingly: pic14 means 16Fxxxx PICs and pic16 means 18Fxxxx PICs. There is no -mpic18
I'll post some more (easier to use) example code on this blog the next days. However, I still doubt that your PIC model is supported by sdcc. If it is not, one could write appropriate header and linker files - but this involves lots of reading in the datasheet and a better understanding of the PIC architecture and gplink/sdcc than I (and perhaps you) have.
Raphael
Anonymous # Friday, December 14, 2007 9:02:14 PM
Raphael Wimmerraphman # Tuesday, December 18, 2007 7:04:35 AM
I'm not sure if I completely understand your question.
In your example you can access _VARIABLE because it is memory-mapped. However, on the 18F2550,18F2455,18F4550,18F4455 the EEPROM is not mapped to main memory. See datasheet:
"The data EEPROM is a nonvolatile memory array,
separate from the data RAM and program memory, that
is used for long-term storage of program data. It is not
directly mapped in either the register file or program
memory space, but is indirectly addressed through the
Special Function Registers (SFRs)."
If this didn't answer your question, please ask again.
Anonymous # Tuesday, June 3, 2008 6:29:44 PM
Raphael Wimmerraphman # Tuesday, June 3, 2008 7:44:55 PM
I do not completely understand your question. The EEPROM code does not depend on the USART code. I just initialized USART for printing out status messages over the serial line. Of course you could send data to be stored in the EEPROM via the serial connection. However, 256 Bytes are not that much.
Anonymous # Tuesday, June 3, 2008 8:33:35 PM
Anonymous # Monday, June 23, 2008 10:46:19 AM
Anonymous # Monday, June 23, 2008 7:40:53 PM
Raphael Wimmerraphman # Tuesday, June 24, 2008 7:19:31 AM
Anonymous # Tuesday, November 4, 2008 10:31:54 AM
Anonymous # Tuesday, November 11, 2008 12:40:39 PM
Anonymous # Tuesday, November 11, 2008 10:44:12 PM
Raphael Wimmerraphman # Thursday, November 13, 2008 9:48:14 AM
Eljun Pacomiosurefowei # Wednesday, January 7, 2009 6:32:50 PM
i am doing a project on PIC18F4550..
i would like to ask assistance on programming this PIC's USB module.
i hope i get a reply on you from this..
you can also contact me through yahoo messenger or email at urefowei@yahoo.com
or by gmail at, urefowei@gmail.com
we really think you are of big help to us!!
thank you!
Eljun Pacomiosurefowei # Wednesday, January 7, 2009 6:34:48 PM
our project is about transferring data from a USB storage device to another USB storage device.. thanks
pls contact me please..
Raphael Wimmerraphman # Thursday, January 8, 2009 11:52:16 AM
unfortunately I do not have that much experience with USB to be helpful. Additionally I'm extremely busy at the moment.
You might want to have a look at the PIC USB Framework
If you put code from your project online, please let me know.
Eljun Pacomiosurefowei # Thursday, January 8, 2009 1:09:55 PM
thank you anyway.. but if you do know good resources about programming PIC18f4550,please feel free to let us know.. actually, we are just novice on these.. LOL.. Thanks!
Anonymous # Friday, June 26, 2009 2:44:38 PM
Anonymous # Wednesday, November 11, 2009 12:45:19 PM
Anonymous # Saturday, January 23, 2010 7:19:39 PM
Anonymous # Sunday, April 11, 2010 12:46:21 PM
Anonymous # Wednesday, May 26, 2010 2:04:54 PM
Anonymous # Thursday, August 5, 2010 6:17:12 AM
Anonymous # Sunday, October 17, 2010 10:13:20 AM
Anonymous # Wednesday, February 9, 2011 7:56:47 PM
Raphael Wimmerraphman # Wednesday, February 9, 2011 8:13:38 PM
that's hard to say from this description. Does your programmer pull down _MCLR when programming, as it should?
Do you have a schematic of your programmer?
Anonymous # Wednesday, February 9, 2011 9:43:25 PM
Raphael Wimmerraphman # Wednesday, February 9, 2011 10:04:23 PM
Anonymous # Wednesday, July 20, 2011 6:30:24 PM
Anonymous # Thursday, August 11, 2011 8:50:11 PM
Anonymous # Friday, December 2, 2011 6:35:09 AM