Estoy intentando almacenar algunos datos en la EEPROM 24AA01 utilizando el PIC18F13K22. Como estoy esperando los componentes, pensé que seguiré adelante y simularé en Proteus.
Esteesmicódigo(basadoencompiladorXC8).
#include<xc.h>#include"config.h"
#include "uart.h"
void i2c_writebyte(char x);
char i2c_readbyte();
void main(void) {
OSCCON = 0x43; //2Mhz
TRISBbits.RB4 = 1;
TRISBbits.RB6 = 1;
TRISCbits.RC5 = 0;
LATCbits.LATC5 = 0; //Low to Enable Writing
SSPADD = 4; //100kHz
SSPSTAT = 0x80;
SSPCON1 = 0x28;
char a = 'y', c;
uart_init();
i2c_writebyte(a);
c = i2c_readbyte();
print(c, 1);
LATCbits.LATC5 = 1; //High to disable Writing
while(1);
return;
}
void i2c_writebyte(char x){
try:
SSPCON2bits.SEN = 1; //Start bit
while(SSPCON2bits.SEN != 0);
SSPBUF = 0xA0;
while(SSPSTATbits.BF != 0);
while((SSPCON2 & 0x1F) || (SSPSTAT & 0x04));
if (SSPCON2bits.ACKSTAT !=0){
print("1", 1);
return;
}
SSPBUF = 0x0; //Address
while(SSPSTATbits.BF != 0);
if (SSPCON2bits.ACKSTAT !=0){
print("2", 1);
return;
}
SSPBUF = x; //Byte
while(SSPSTATbits.BF != 0);
if (SSPCON2bits.ACKSTAT !=0){
print("3", 1);
return;
}
SSPCON2bits.PEN = 1; //Stop bit
while(SSPCON2bits.PEN != 0);
}
char i2c_readbyte(){
char x;
try:
SSPCON2bits.SEN = 1; //Start bit
while(SSPCON2bits.SEN != 0);
SSPBUF = 0xA0;
while(SSPSTATbits.BF != 0);
if (SSPCON2bits.ACKSTAT !=0){
print("4", 1);
return;
}
SSPBUF = 0x0; //Address
while(SSPSTATbits.BF != 0);
if (SSPCON2bits.ACKSTAT !=0){
print("5", 1);
return;
}
SSPCON2bits.SEN = 1; //Start bit
while(SSPCON2bits.SEN != 0);
SSPBUF = 0xA1;
while(SSPSTATbits.BF != 0);
if (SSPCON2bits.ACKSTAT !=0){
print("6", 1);
return;
}
SSPCON2bits.ACKDT = 1;
SSPCON2bits.RCEN = 1;
while(SSPSTATbits.BF == 0);
x = SSPBUF;
SSPCON2bits.ACKEN = 1;
SSPCON2bits.PEN = 1; //Stop bit
while(SSPCON2bits.PEN != 0);
return x;
}
Utilizo el UART para decirme en qué momento no se recibió el ACK. El osciloscopio también confirma que cuando compruebo la forma de onda.
¿PorquélaEEPROMnoproporcionaunACK?¿Esunerrordesimulaciónoestoyhaciendoalgomal?
Cambiarlosdominiosa10ky4.7ktampocotuvoefecto.UséundepuradorI2Cparaversilosdatoscorrectosibanperovialgocomoesto.¿Puedealguienayudarmeainterpretaresto?