PA3040
Advanced Member level 3
Dear All
Can I use PIC 16F877a I2C functions as it is for PIC18F452
I create a project using Hi TECH C 18F lite to read RTC DS1307
But it is not updating the time and LCD is displaying only 00:00:00
Please fine the I2C code that I used with PIC16F877a and it is working successfully
please advice
Can I use PIC 16F877a I2C functions as it is for PIC18F452
I create a project using Hi TECH C 18F lite to read RTC DS1307
But it is not updating the time and LCD is displaying only 00:00:00
Please fine the I2C code that I used with PIC16F877a and it is working successfully
Code:
void I2CWait(){
while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */
}
void I2CInit(void){
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled */
SSPCON1 = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPADD = 0x28; /* 100Khz @ 4Mhz Fosc */
}
void I2CStart(){
I2CWait();
SEN = 1;
while(SEN);
}
void I2CStop(){
PEN = 1; /* Stop condition enabled */
while(PEN); /* Wait for stop condition to finish */
/* PEN automatically cleared by hardware */
}
void I2CRestart(){
RSEN = 1; /* Repeated start enabled */
while(RSEN); /* wait for condition to finish */
}
void I2CAck(){
ACKDT = 0; /* Acknowledge data bit, 0 = ACK */
ACKEN = 1; /* Ack data enabled */
while(ACKEN); /* wait for ack data to send on bus */
}
void I2CNak(){
ACKDT = 1; /* Not acknowledge data bit, 1 = NAK */
ACKEN = 1; /* Not Ack data enabled */
while(ACKEN); /* wait for Not ack data to send on bus */
}
void I2CAck_status(){
ACKSTAT = 1;
while(ACKSTAT); /* wait for Not ack data to send on bus */
}
void I2CSend(unsigned char dat){
SSPBUF = dat; /* Move data to SSPBUF */
while(BF); /* wait till complete data is sent from buffer */
I2CWait(); /* wait for any pending transfer */
}
unsigned char I2CRead(void){
unsigned char temp;
I2CWait(); /* Reception works if transfer is initiated in read mode */
RCEN = 1; /* Enable data reception */
while(!BF); /* wait for buffer full */
temp = SSPBUF; /* Read serial buffer and store in temp register */
/* wait to check any pending transfer */
}
please advice