Dakshata
Newbie
I am trying to communicate between pic32 to RTC using i2c communication , but i have only received lowest nibble value and highest nibble value not proper received .
it means suppose i have to write
writeRTC readRTC
0xAD Received 0x2D
0xBF 0x3F
0xCB 0x4B
but i have write
0x23 Received same 0x23
0x55 Received same 0x55
and also i have to write in SRAM address then same data received . so can you please check below i2c read and write function.
void writeRTC(uint8_t highByte, uint8_t lowByte, uint8_t data)
{
I2C1CONbits.SEN = 1; // Initiate Start condition
__delay_us(50);
// Send device address and memory address
I2C1TRN = 0b11011110 ; //(RTC_I2C_ADDRESS_WRITE << 1); //| 0b11011110; // D0 + Write
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = highByte;
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = lowByte;
while (I2C1STATbits.TRSTAT) {}
// Send data
I2C1TRN = data;
while (I2C1STATbits.TRSTAT) {}
// Ensure that the write operation is completed
while (I2C1STATbits.ACKSTAT) {}
I2C1CONbits.PEN = 1; // Initiate Stop condition
while (I2C1CONbits.PEN) {}
// Add a small delay to allow the EEPROM's internal write cycle to complete
__delay_ms(1); // Adjust the delay as needed
}
// Function to read data from the MCP7940N RTC
uint8_t readRTC(uint8_t highByte, uint8_t lowByte) {
I2C1CONbits.SEN = 1; // Initiate Start condition
unsigned char x;
__delay_ms(1);
//while (I2C1CONbits.SEN);// {}
I2C1TRN = 0b11011110; //(RTC_I2C_ADDRESS_READ << 1); //| 0b11011110; // D1 + Write (Device address with R/W bit set for write)
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = highByte;
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = lowByte;
while (I2C1STATbits.TRSTAT) {}
I2C1CONbits.RSEN = 1; // Initiate repeated Start condition
while (I2C1CONbits.RSEN){}
I2C1TRN =(0b11011111 ) ; //(RTC_I2C_ADDRESS_READ << 1) | 0b11011111; // A0 + Read (Device address with R/W bit set for read)
while (I2C1STATbits.TRSTAT) {}
I2C1CONbits.RCEN = 1; // Enable reception
__delay_us(50);
// while (!(I2C1STATbits.RBF)) {}
// int i =0;
int data = I2C1RCV; // Read received data
I2C1CONbits.ACKDT = 1; // Set ACK bit to NACK (no acknowledge) for the last byte
I2C1CONbits.ACKEN = 1; // Send NACK
while (I2C1CONbits.ACKEN) {}
I2C1CONbits.PEN = 1; // Initiate Stop condition
while (I2C1CONbits.PEN) {}
return data;
}
its changes will required please let me know . as soon as possible
it means suppose i have to write
writeRTC readRTC
0xAD Received 0x2D
0xBF 0x3F
0xCB 0x4B
but i have write
0x23 Received same 0x23
0x55 Received same 0x55
and also i have to write in SRAM address then same data received . so can you please check below i2c read and write function.
void writeRTC(uint8_t highByte, uint8_t lowByte, uint8_t data)
{
I2C1CONbits.SEN = 1; // Initiate Start condition
__delay_us(50);
// Send device address and memory address
I2C1TRN = 0b11011110 ; //(RTC_I2C_ADDRESS_WRITE << 1); //| 0b11011110; // D0 + Write
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = highByte;
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = lowByte;
while (I2C1STATbits.TRSTAT) {}
// Send data
I2C1TRN = data;
while (I2C1STATbits.TRSTAT) {}
// Ensure that the write operation is completed
while (I2C1STATbits.ACKSTAT) {}
I2C1CONbits.PEN = 1; // Initiate Stop condition
while (I2C1CONbits.PEN) {}
// Add a small delay to allow the EEPROM's internal write cycle to complete
__delay_ms(1); // Adjust the delay as needed
}
// Function to read data from the MCP7940N RTC
uint8_t readRTC(uint8_t highByte, uint8_t lowByte) {
I2C1CONbits.SEN = 1; // Initiate Start condition
unsigned char x;
__delay_ms(1);
//while (I2C1CONbits.SEN);// {}
I2C1TRN = 0b11011110; //(RTC_I2C_ADDRESS_READ << 1); //| 0b11011110; // D1 + Write (Device address with R/W bit set for write)
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = highByte;
while (I2C1STATbits.TRSTAT) {}
I2C1TRN = lowByte;
while (I2C1STATbits.TRSTAT) {}
I2C1CONbits.RSEN = 1; // Initiate repeated Start condition
while (I2C1CONbits.RSEN){}
I2C1TRN =(0b11011111 ) ; //(RTC_I2C_ADDRESS_READ << 1) | 0b11011111; // A0 + Read (Device address with R/W bit set for read)
while (I2C1STATbits.TRSTAT) {}
I2C1CONbits.RCEN = 1; // Enable reception
__delay_us(50);
// while (!(I2C1STATbits.RBF)) {}
// int i =0;
int data = I2C1RCV; // Read received data
I2C1CONbits.ACKDT = 1; // Set ACK bit to NACK (no acknowledge) for the last byte
I2C1CONbits.ACKEN = 1; // Send NACK
while (I2C1CONbits.ACKEN) {}
I2C1CONbits.PEN = 1; // Initiate Stop condition
while (I2C1CONbits.PEN) {}
return data;
}
its changes will required please let me know . as soon as possible