/* * File: pic16f_i2c.c * Author: www.henteko.org * * Created on 2021/07/29, 17:15 */ #include #include "pic16f_i2c.h" void InitI2C() { SSP1CLKPPS = 0b10100; // RC5 SSP1DATPPS = 0b10101; // RC4 SSP1CON1 = 0b00101000; // I2C master : clock = FOSC / (4 * (SPP1ADD+1)) SSP1CON2 = 0; SSP1STAT = 0b10000000; // I2C standard speed mode SSP1ADD = 0x13; // 19 : 32000000 / (4 * (19 + 1)) = 400000 } void StartI2C() { SSP1CON2bits.SEN = 1; WaitI2C(); } void StopI2C() { SSP1CON2bits.PEN = 1; WaitI2C(); } void WriteI2C(unsigned char data) { SSP1BUF = data; WaitI2C(); } void WaitI2C() { while((SSP1STAT & 0x04) || (SSP1CON2 & 0x1F)); }