// EEPROM_1.C SourceBoost C, PIC16F886, ICD2
//
// Reads preprogrammed data at EEPROM location 0x00 and displays.
//
// Writes values to all 256 bytes of high enfurance data EEPROM.
//
// Reads it back and displays on serial LCD.
//
// copyright, Peter H Anderson, July 19, '09
#include <system.h>
#include <stdio.h>
#include <icd2.h>
#include <string.h>
#pragma DATA 0x2007, 0x28e4
#pragma DATA 0x2008, 0x3eff
#pragma CLOCK_FREQ 4000000
#define byte unsigned char
#define FALSE 0
#define TRUE !0
byte read_eeprom(byte adr);
void write_eeprom(byte adr, byte dat);
void asynch_setup(void);
void print_str(char *s);
void main()
{
byte n, d;
char s[25];
osccon = 0x61;
anselh = 0x00;
asynch_setup();
delay_ms(25);
strcpy(s, "?f"); // clear LCD
print_str(s);
d = read_eeprom(0x00); // read and display location 0x00
sprintf(s, "%u", d);
print_str(s);
delay_s(3);
for (n=0; n<0xff; n++)
{
d = 0xff - n;
write_eeprom(n, d);
}
for (n=0; n<0xff; n++)
{
d = read_eeprom(n);
strcpy(s, "?x00?y1?l "); // col 00, row 1, clear line, some spaces
print_str(s);
sprintf(s, "%u", d);
print_str(s);
delay_ms(250);
}
while(1)
{
}
}
byte read_eeprom(byte adr)
{
eeadr = adr;
eecon1.EEPGD = 0;
eecon1.RD = 1;
return(eedata);
}
void write_eeprom(byte adr, byte dat)
{
byte gie_status;
eeadr = adr;
eedata = dat;
eecon1.EEPGD = 0;
eecon1.WREN = 1;
gie_status = intcon.GIE; // save GIE status
while(intcon.GIE)
{
intcon.GIE = 0; // turn off ints
}
eecon2 = 0x55; // password
eecon2 = 0xaa;
eecon1.WR = 1;
delay_ms(50);
intcon.GIE = gie_status;
eecon1.WREN = 0;
}
void asynch_setup(void)
{ // for UART, see Manual pages beginning at page 151
trisc.7 = 1;
trisc.6 = 1; // TX
rcsta.SPEN = 1; // enable serial port
txsta.TXEN = 1; // enable transmitter
txsta.SYNC = 0; // asynchronous
txsta.BRGH = 1;
spbrg = 25; // 9600 baud at 4.0 MHz
}
void print_str(char *s)
{
while(*s != '\0')
{
txreg = *s;
while(txsta.TRMT == 0)
{
}
++s;
}
}
#pragma DATA 0x2100, 0x55