buenas, aca les respondo o publico el programa que hice para leer, escribir y guardar datos en eeprom externa, por si a alguien le sirve. y si alguien tiene alguna correccion que la haga saver, o depronto una manera mas corta de hacer el programa. me quedo algo largo!
#include <18F452.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XT //Crystal osc <= 4mhz for PCM/PCH , 3mhz to 10 mhz for PCD
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //Power Up Timer
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOCPD //No EE protection
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=4000000)
#include <stdlib.h>
#include <2408.c>
#include<external_eeprom.c>
#define LCD_DATA_PORT getenv("SFR:PORTD")
#include <LCD.C>
#define use_portb_kbd TRUE
#include <KBD.C>
char suelta,recibe;
char lee,lee2;
short dato_s;
int shortmemo[5],intmemo[5],longmemo[5],int32memo[5],floatmemo[20];
int contador,contador2,bitpos,datos;
int intdato[5],longdato[5],int32dato[10],floatdato[20];
long posmemo,datoe2,lee3;
long long datoe3,datoe5,lee4;
float datoe4,datoe7,lee5;
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
set_tris_a(0b00111111);
set_tris_b(0b11111111);
set_tris_c(0b11100111);
set_tris_d(0b11111111);
set_tris_e(0b00000000);
init_ext_eeprom();
lcd_init();
kbd_init();
port_b_pullups(TRUE);
in: lcd_gotoxy(1,1);
lcd_putc('\f');
lee=0;
lcd_putc("1 Escribir Dato");
lcd_gotoxy(1,2);
lcd_putc("2 leer Dato");
while(true)
{
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe!=0)
{
if(recibe=='1')
primo: {
do
{
suelta=kbd_getc();
}
while(suelta!='\0');
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("1)Short2)Int3)Long");
lcd_gotoxy(1,2);
lcd_putc("4)int32 5)Float");
do
{
recibe=kbd_getc();
}while(recibe==0);
if( (recibe>='1') && (recibe<='5') )
{
if(recibe=='1')
primo2: {
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc(" Ingrese Dato");
do
{
recibe=kbd_getc();
}while(recibe==0);
if( (recibe>='0') && (recibe<='1') )
{
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(LCD_PUTC"Dato: %c",recibe);
if(recibe=='1')
{
dato_s=True;
}
else
{
dato_s=False;
}
lcd_gotoxy(1,2);
lcd_putc("y presione *");
primo3:
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe=='*')
{
primo4: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc("EEPROM ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
shortmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(shortmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
primo5: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Posicion");
lcd_gotoxy(1,2);
lcd_putc(" del bit: ");
do
{
recibe=kbd_getc();
}while(recibe==0);
if( (recibe>='0') && (recibe<='7') )
{
printf(LCD_PUTC"%c",recibe);
delay_ms(1000);
bitpos=recibe-0x30;;
write_int1_ext_eeprom(posmemo,bitpos,dato_s);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Dato short Escrito");
delay_ms(2000);
goto in;
}
else
{
goto primo5;
}
}
else
{
goto primo4;
}
}
else
{
goto primo3;
}
}
else
{
goto primo2;
}
}
if(recibe=='2')
{
primo6: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Escribir Dato");
contador2=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe!=0)
{
intdato[contador2]=recibe;
printf(LCD_PUTC"%c",recibe);
contador2++;
}
}
while(contador2<=2);
datoe2=atol(intdato);
if( (datoe2>=0) && (datoe2<=255) )
{
lcd_gotoxy(1,2);
lcd_putc("presione *");
primo7:
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe=='*')
{
primo8: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc("EEPROM ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
intmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(intmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
datos=atoi(intdato);
write_ext_eeprom(posmemo,datos);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("CORRECTO");
delay_ms(2000);
goto in;
}
else
{
goto primo8;
}
}
else
{
goto primo7;
}
}
else
{
goto primo6;
}
}
if(recibe=='3')
{
primo9: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Dato:");
contador2=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe!=0)
{
longdato[contador2]=recibe;
printf(LCD_PUTC"%c",recibe);
contador2++;
}
}
while(contador2<=4);
datoe3=atoi32(longdato);
if( (datoe3>=0) && (datoe3<=65535) )
{
lcd_gotoxy(1,2);
lcd_putc("Presione *");
primo10:
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe=='*')
{
primo11: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc("EEPROM: ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
longmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(longmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
datoe2=atol(longdato);
write_int16_ext_eeprom(posmemo,datoe2);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("CORRECTO");
delay_ms(1000);
goto in;
}
else
{
goto primo11;
}
}
else
{
goto primo10;
}
}
else
{
goto primo9;
}
}
if(recibe=='4')
{
primo12: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Dato:");
lcd_gotoxy(1,2);
contador2=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe!=0)
{
if(recibe=='*')
{
contador2=9;
}
int32dato[contador2]=recibe;
printf(LCD_PUTC"%c",recibe);
contador2++;
}
}
while(contador2<=9);
datoe3=atoi32(int32dato);
if( (datoe3>=0) && (datoe3<=4294967295) )
{
lcd_gotoxy(1,2);
lcd_putc("Presione *");
primo13:
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe=='*')
{
primo14: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc("EEPROM: ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
int32memo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(int32memo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
datoe5=atoi32(int32dato);
write_int32_ext_eeprom(posmemo,datoe5);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("CORRECTO");
delay_ms(1000);
goto in;
}
else
{
goto primo14;
}
}
else
{
goto primo13;
}
}
else
{
goto primo12;
}
}
if(recibe=='5')
{
primo15: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Dato:");
lcd_gotoxy(1,2);
contador2=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe!=0)
{
if(recibe=='*')
{
contador2=15;
}
if(recibe=='#')
{
recibe='.';
}
floatdato[contador2]=recibe;
printf(LCD_PUTC"%c",recibe);
contador2++;
}
}
while(contador2<=15);
datoe4=atof(floatdato);
if((datoe4>=-1.5)&&(datoe4<=3.4))
{
lcd_putc('\f');
lcd_gotoxy(1,2);
lcd_putc("Presione *");
primo16:
do
{
recibe=kbd_getc();
}while(recibe==0);
if(recibe=='*')
{
primo17: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc("EEPROM: ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
floatmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(floatmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
datoe7=atof(floatdato);
write_float_ext_eeprom(posmemo,datoe7);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("CORRECTO");
delay_ms(1000);
goto in;
}
else
{
goto primo17;
}
}
else
{
goto primo16;
}
}
else
{
goto primo15;
}
}
}
else
{
goto primo;
}
}
if(recibe=='2')
{
do
{
suelta=kbd_getc();
}
while(suelta!='\0');
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("1)Short2)Int3)Long");
lcd_gotoxy(1,2);
lcd_putc("4)int32 5)Float");
delay_ms(1000);
primo18:
do
{
recibe=kbd_getc();
}while(recibe==0);
if( (recibe>='1') && (recibe<='5') )
{
if(recibe=='1')
{
primo19: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc(": ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
shortmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(shortmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(2000);
primo20: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Posicion");
lcd_gotoxy(1,2);
lcd_putc("BIT: ");
do
{
recibe=kbd_getc();
}while(recibe==0);
if( (recibe>='0') && (recibe<='7') )
{
printf(LCD_PUTC"%c",recibe);
delay_ms(1000);
bitpos=recibe-0x30;;
lcd_putc('\f');
lcd_gotoxy(1,1);
lee=read_int1_ext_eeprom(posmemo,bitpos);
printf(LCD_PUTC"Dato=%u",lee);
delay_ms(2000);
goto in;
}
else
{
goto primo20;
}
}
else
{
goto primo19;
}
}
if(recibe=='2')
{
primo21: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc(": ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
intmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(intmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
lcd_putc('\f');
lcd_gotoxy(1,1);
lee2=read_ext_eeprom(posmemo);
printf(LCD_PUTC"Dato=%u",lee2);
delay_ms(2000);
goto in;
}
else
{
goto primo21;
}
}
if(recibe=='3')
{
primo22: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc(": ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
longmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(longmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Dato");
lcd_gotoxy(1,2);
lee3=read_int16_ext_eeprom(posmemo);
printf(LCD_PUTC"=%Lu",lee3);
delay_ms(2000);
goto in;
}
else
{
goto primo22;
}
}
if(recibe=='4')
{
primo23: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc(": ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
int32memo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(int32memo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Dato");
lcd_gotoxy(1,2);
lee4=read_int32_ext_eeprom(posmemo);
printf(LCD_PUTC"=%Lu",lee4);
delay_ms(2000);
goto in;
}
else
{
goto primo23;
}
}
if(recibe=='5')
{
primo24: lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Ingrese Direccion");
lcd_gotoxy(1,2);
lcd_putc(": ");
contador=0;
do
{
do
{
recibe=kbd_getc();
}while(recibe==0);
printf(LCD_PUTC"%c",recibe);
floatmemo[contador]=recibe;
contador++;
}
while(contador<=3);
posmemo=atol(floatmemo);
if( (posmemo>=0) && (posmemo<=1024) )
{
delay_ms(1000);
lcd_putc('\f');
lcd_gotoxy(1,1);
lcd_putc("Dato=");
lcd_gotoxy(1,2);
lee5=read_float_ext_eeprom(posmemo);
printf(LCD_PUTC"%1.5f",lee5);
delay_ms(2000);
goto in;
}
else
{
goto primo24;
}
}
}
else
{
goto primo18;
}
}
}
}
}