![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
jelodavid
Joined: 03 Apr 2005 Posts: 22 Location: Laguna Philippines
|
need eeprom sample program |
Posted: Fri Apr 15, 2005 6:16 am |
|
|
Hello all!,
Pls. help me on how to save and retrieve contents of eeprom. A very short example for a newbie would be very helpful. I just want a very simple program, for example a counter saved to internal eeprom then it can be retreive later and displayed to lcd.
I'm using PIC16F876 and pcwh 3.212 |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 15, 2005 7:13 am |
|
|
Plenty of examples have been posted. Try using the search feature and see what you can come up with. If you have problems, then post your code and we can help. No one is probably going to write the program for you. |
|
![](templates/subSilver/images/spacer.gif) |
jelodavid
Joined: 03 Apr 2005 Posts: 22 Location: Laguna Philippines
|
|
Posted: Fri Apr 15, 2005 7:17 am |
|
|
Here's my sample program, but it doesn't work
#include <16F876.h>
#device adc=8
#use delay(clock=16000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
#include <PICDEM2PLUS_LCD.h>
#include <string.h>
#include <internal_eeprom.c>
#byte porta = 5
#byte portb = 6
#byte portc = 7
#define save PIN_B7
#define recall PIN_B6
#define select PIN_B5
#define wrt_comp PIN_B1
#define memory 10
long count=0;
byte display=0;
#int_RB
RB_isr()
{
}
#int_TIMER0
TIMER0_isr()
{
}
#int_EEPROM
EEPROM_isr()
{
output_high(wrt_comp);
delay_ms(200);
output_low(wrt_comp);
}
// I/O bit configs and hardware initialization
void on_init()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_RB);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
set_tris_a(0b11111); //set portA as input
set_tris_b(0b11110001);
set_tris_c(0b11110000);
porta = 0;
portb = 0;
portc = 0;
delay_ms(20);
lcd_init();
lcd_putc("\fWelcome to Power B/E\nAngelo D. Catamco");
delay_ms(500);
delay_ms(500);
display=read_eeprom(memory);
lcd_putc("\fCounter: ");
lcd_gotoxy(10,1);
lcd_putc(display);
}
void on_idle()
{
count++;
printf(lcd_putc,"\fCounter: %lu ",count);
delay_ms(100);
}
void main()
{
on_init(); // initialized hardware
for(;;) // forever loop
{
if (input(save))
{
write_eeprom(memory,count);
delay_ms(50);
delay_ms(100);
}
else if (input(recall))
{
/ display=read_eeprom(memory);
lcd_putc("\fCounter: ");
lcd_gotoxy(10,1);
lcd_putc(display);
delay_ms(100);
}
else if (input(select))
on_idle();
}
} |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 15, 2005 1:19 pm |
|
|
Read through the code to see what I corrected:
Code: |
// changed the device to an 877 since the PICDEM 2 Plus board has the LCD on portd
#include <16F877.h>
#device adc=8
#use delay(clock=16000000)
#fuses NOWDT,XT, NOPUT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG
// my lcd.c file for the PICDEM 2 Plus board. It's in the Code Archive
#include <mylcd.c>
// don't know what this file is
//#include <PICDEM2PLUS_LCD.h>
#include <string.h>
// don't know what this file is
//#include <internal_eeprom.c>
#byte porta = 5
#byte portb = 6
#byte portc = 7
#define save PIN_B7
#define recall PIN_B6
#define select PIN_B5
#define wrt_comp PIN_B1
#define memory 10
// you defined display as 8 bits so count should be as well
// besides, the function write_eeprom only takes an int8
int8 count=0;
int8 display=0;
// Not using these so I took them out. The compiler might optimize them out
// and then you'll have a problem since the interrupt is enabled
/*
#int_RB
void RB_isr(void)
{
}
#int_TIMER0
void TIMER0_isr(void)
{
}
*/
#int_EEPROM
void EEPROM_isr(void)
{
output_high(wrt_comp);
delay_ms(200);
output_low(wrt_comp);
}
// I/O bit configs and hardware initialization
void on_init(void)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
// took the following out since you are not using them
// enable_interrupts(INT_RB);
// enable_interrupts(INT_TIMER0);
// added this since you intend on using it
enable_interrupts(INT_EEPROM);
enable_interrupts(GLOBAL);
set_tris_a(0b11111); //set portA as input
set_tris_b(0b11110001);
set_tris_c(0b11110000);
// not sure how you have your hardware but the ports need pullups. I
// just used the internal ones
port_b_pullups(true);
porta = 0;
portb = 0;
portc = 0;
delay_ms(20);
lcd_init();
lcd_putc("\fWelcome to Power B/E\nAngelo D. Catamco");
delay_ms(500);
delay_ms(500);
display=read_eeprom(memory);
lcd_putc("\fCounter: ");
lcd_gotoxy(10,1);
// you can't just print the value, you have to convert it first
// lcd_putc(display);
printf(lcd_putc,"%u ",display);
}
void on_idle(void)
{
count++;
printf(lcd_putc,"\fCounter: %u ",count);
delay_ms(100);
}
void main(void)
{
on_init(); // initialized hardware
for(;;) // forever loop
{
// If your pullups are active high, then you pull them low so test for a
// low condition
if (!input(save))
{
write_eeprom(memory,count);
delay_ms(50);
delay_ms(100);
}
// If your pullups are active high, then you pull them low so test for a
// low condition
else if (!input(recall))
{
display=read_eeprom(memory);
lcd_putc("\fCounter: ");
lcd_gotoxy(10,1);
// you can't just print the value, you have to convert it first
// lcd_putc(display);
printf(lcd_putc,"%u ",display);
delay_ms(100);
}
// If your pullups are active high, then you pull them low so test for a
// low condition
else if (!input(select))
on_idle();
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
jelodavid
Joined: 03 Apr 2005 Posts: 22 Location: Laguna Philippines
|
|
Posted: Fri Apr 15, 2005 1:28 pm |
|
|
thanks for very big help!. Is there any way to save a 16 bit or 32 bit wide to internal eeprom? ![Very Happy](images/smiles/icon_biggrin.gif) |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Apr 15, 2005 1:52 pm |
|
|
Code: |
int16 data1;
int32 data2;
address = 0;
write_eeprom(address,make8(data1,0));
address++
write_eeprom(address,make8(data1,1));
address++
write_eeprom(address,make8(data2,0));
address++
write_eeprom(address,make8(data2,1));
address++
write_eeprom(address,make8(data2,2));
address++
write_eeprom(address,make8(data2,3));
// To read
data1 = make16(read_eeprom(address+1),read_eeprom(address);
data2 = make16(read_eeprom(address+3),
read_eeprom(address+2);
read_eeprom(address+1);
read_eeprom(address);
|
Note that you can also use a union to make it easier. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|