add softi2c , test failed
This commit is contained in:
72
3rd/soft_i2c.c
Normal file
72
3rd/soft_i2c.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "delay.h"
|
||||
#include "ti_msp_dl_config.h"
|
||||
|
||||
void I2C_W_SCL(uint8_t BitValue) {
|
||||
DL_GPIO_writePinsVal(SOFT_I2C_PORT, SOFT_I2C_CLK_PIN, BitValue);
|
||||
}
|
||||
|
||||
void I2C_W_SDA(uint8_t BitValue) {
|
||||
DL_GPIO_writePinsVal(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN, BitValue);
|
||||
}
|
||||
|
||||
// 读取时钟线数据
|
||||
uint8_t I2C_R_SDA(void) {
|
||||
uint8_t BitValue;
|
||||
BitValue = DL_GPIO_readPins(SOFT_I2C_PORT, SOFT_I2C_SDA_PIN);
|
||||
|
||||
return BitValue;
|
||||
}
|
||||
|
||||
void I2C_Start(void) {
|
||||
I2C_W_SCL(1);
|
||||
I2C_W_SDA(1);
|
||||
I2C_W_SDA(0);
|
||||
I2C_W_SCL(0);
|
||||
}
|
||||
|
||||
void I2C_Stop(void) {
|
||||
I2C_W_SDA(0);
|
||||
I2C_W_SCL(1);
|
||||
I2C_W_SDA(1);
|
||||
}
|
||||
|
||||
void I2C_SendByte(uint8_t Byte) {
|
||||
uint8_t i;
|
||||
|
||||
for (i = 0; i < 8; i++) {
|
||||
I2C_W_SDA(Byte & (0x80 >> i));
|
||||
I2C_W_SCL(1);
|
||||
I2C_W_SCL(0);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t I2C_RecviveData(void) {
|
||||
uint8_t i, Byte = 0x00;
|
||||
I2C_W_SDA(1);
|
||||
for (i = 0; i < 8; i++) {
|
||||
I2C_W_SCL(1);
|
||||
if (I2C_R_SDA() == 1) {
|
||||
Byte |= (0x80 >> i);
|
||||
}
|
||||
I2C_W_SCL(0);
|
||||
}
|
||||
return Byte;
|
||||
}
|
||||
|
||||
void I2C_SendAck(uint8_t AckBit) {
|
||||
|
||||
I2C_W_SDA(AckBit);
|
||||
I2C_W_SCL(1);
|
||||
I2C_W_SCL(0);
|
||||
}
|
||||
|
||||
uint8_t I2C_RecviveAck(void) {
|
||||
uint8_t AckBit;
|
||||
|
||||
I2C_W_SDA(1);
|
||||
I2C_W_SCL(1);
|
||||
AckBit = I2C_R_SDA();
|
||||
I2C_W_SCL(0);
|
||||
|
||||
return AckBit;
|
||||
}
|
||||
Reference in New Issue
Block a user