'I2C examplecode'에 해당되는 글 1건

  1. 2019.01.14 I2C Master and I2C Slave example code

Development Tool: MPLAB X v5.1(MCC v3.66)
Compiler: XC8 v2.0
Device: PIC16F1827

I2C Master: SCL(RB4 - 10pin), SDA(RB1 - 7pin)
I2C Slave:    SCL(RB5 - 11pin), SDA(RB2 - 8pin)


PIC16F1827_I2C.X.zip


void main(void) { // initialize the device SYSTEM_Initialize(); // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits // Use the following macros to: // Enable the Global Interrupts INTERRUPT_GlobalInterruptEnable(); // Enable the Peripheral Interrupts INTERRUPT_PeripheralInterruptEnable(); // Disable the Global Interrupts //INTERRUPT_GlobalInterruptDisable(); // Disable the Peripheral Interrupts //INTERRUPT_PeripheralInterruptDisable(); while (1) { wrBuffer[0] = 0x0A; wrBuffer[1] = 0x0B; wrBuffer[2] = 0x0C; I2C1_MasterWrite( wrBuffer, 1, 0x08, &status); while(status == I2C1_MESSAGE_PENDING); I2C1_MasterRead( rdBuffer, 3, 0x08, &status); while(status == I2C1_MESSAGE_PENDING); __delay_ms(500); } }



I2C Output waveformI2C Output waveform

I2C Output waveform


INTERRUPT_GlobalInterruptEnable() and INTERRUPT_PeripheralInterruptEnable() have to comment out due to I2C Master and I2C Slave are using interrupt service routine

I2C Pin allocation for I2C1, I2C2


Posted by KennyShin
,