Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC32MM0256GPM064 interface with MCP7940N RTC using I2C communication

Dakshata

Newbie
Joined
Oct 16, 2023
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
25
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
 
I don't recognize valid MCP7940 access.
It expects one I2C address byte 0xDE (as written) and one register address byte, not yet existing in your code.
 
Hi,

and for "reading" a byte you still should "transmit" an "0xFF" byte.

* "1" for "releasing the data bus SDA line (not driven by master, pulled up by resistor, pulled down by slave)
* and sending out 8+1 SCK clock cycles.

I recommend to read the I2C specification, or at least basic I2C operation documentation.

Klaus
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top