dsPIC33 EVM 보드의 I2C는 RG2(SCL), RG3(SDA)에 연결되어 있다.
dsPIC33 I2C 초기화 코드
void i2c_init(unsigned int Speed)
{
OpenI2C1( I2C_ON, I2C_BRG );
}
dsPIC33 I2C Read/Write 소스 코드
unsigned char i2c_read_byte(unsigned char Addr, unsigned char reg){unsigned char data = 0;unsigned char SlaveAddress = Addr;char i2cData[10];int DataSz;int Index = 0;i2cData[0] = (SlaveAddress << 1) | 0;i2cData[1] = reg;DataSz = 2;StartI2C1();IdleI2C1();while( DataSz ){MasterWriteI2C1( i2cData[Index++] );IdleI2C1();DataSz--;if( I2C1STATbits.ACKSTAT )break;}RestartI2C1();i2c_wait(10);IdleI2C1();MasterWriteI2C1( (SlaveAddress << 1) | 1 );IdleI2C1();data = MasterReadI2C1();StopI2C1();IdleI2C1();return data;}void i2c_write_byte(unsigned char Addr, unsigned char reg, unsigned char data){unsigned char SlaveAddress = Addr;char i2cData[10];int DataSz;int Index = 0;char status;i2cData[0] = (SlaveAddress << 1) | 0;i2cData[1] = reg;i2cData[2] = data;DataSz = 3;StartI2C1();IdleI2C1();while( DataSz ){MasterWriteI2C1( i2cData[Index++] );IdleI2C1();DataSz--;if( I2C1STATbits.ACKSTAT )break;}StopI2C1();IdleI2C1();}
dsPIC33 EVM을 이용한 I2C 온습도 센서 테스트 예제
반응형