Proteus: RC3 (clock) and RC4 (data) ports are used two wire I2C bus connection. Pullup resistors R1, R2 are 4.7kohm and are connected to VCC. Be careful that R1 and R2 will be set to DIGITAL in proteus (edit, properties) and power line will be VCC not separate +5V, otherwise it is not working and took my couple of days. It may work with debugger but may not work without debugger if you keep resistors in analog.
You can use oscilloscope and I2C debugger for better understanding of I2C bus operation or diagnostics.
MCC: Add MSSP peripheral for I2C bus. PIC will be the master on the bus and you can select bus clock as you want, such as 1kHz. 1MHz internal oscillator is used as in previous examples. default selections in interrupt and pin modules. RC3, RC4 will be selected in pin manager.
XC8: include delays.h and myxlcd.h for LCD operation. stdio.h is required for sprintf() function. ENABLE Global and Peripheral interrupts.
I2C_MasterWrite( 0, 1, 0b1001101, &I2C_status) will select register-0 at address 0x9A=0b1001101 for reading.
I2C_MasterRead( &readValue, 1, 0b1001101, &I2C_status) will read 1 byte at the same address. dont worry about adding 0 for write and 1 for read addresses, these functions will handle it for you.
Convert readValue to decimal and print on LCD.
TC74 has single one byte register for temperature.
common errors:
If address of TC74 is not the same as in the code you can read zero value.
write addresses in binary 0b1001101 not in hex 0x9A otherwise will not work.
#include "mcc_generated_files/mcc.h"
#include "delays.h"
#include "myxlcd.h"
#include <stdio.h>
/*
Main application
*/
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
unsigned char s1[10], s2[10];
uint8_t readValue[1];
I2C_MESSAGE_STATUS I2C_status;
OpenXLCD(FOUR_BIT & LINES_5X7);
WriteCmdXLCD(DON&CURSOR_OFF&BLINK_OFF);
while (1)
{
// Add your application code
I2C_MasterWrite( 0, 1, 0b1001101, &I2C_status);
while (I2C_MESSAGE_PENDING == I2C_status);
I2C_MasterRead( &readValue, 1, 0b1001101, &I2C_status);
while (I2C_MESSAGE_PENDING == I2C_status );
sprintf(s1, "%02d", readValue[0]);
while(BusyXLCD());
SetDDRamAddr(0x01); // clear LCD
putrsXLCD("Thermometer");
SetDDRamAddr(0x40); // goto second line
while(BusyXLCD());
putrsXLCD("Temp=");putrsXLCD(s1);
__delay_ms(1000);
}
}
download the example