中级会员
 
- 积分
- 400
- 金钱
- 400
- 注册时间
- 2015-10-16
- 在线时间
- 85 小时
|

楼主 |
发表于 2018-5-3 15:36:49
|
显示全部楼层
原子哥,IIC两个管脚是这样的
// I2C IO-Pins
// SDA on port B, bit 6
#define SDA1_LOW() (GPIOB->BSRR = 0x00400000) // set SDA to low
#define SDA1_OPEN() (GPIOB->BSRR = 0x00000040) // set SDA to open-drain
#define SDA1_READ (GPIOB->IDR & 0x0040) // read SDA
// SCL on port B, bit 7
#define SCL1_LOW() (GPIOB->BSRR = 0x00800000) // set SCL to low
#define SCL1_OPEN() (GPIOB->BSRR = 0x00000080) // set SCL to open-drain
#define SCL1_READ (GPIOB->IDR & 0x0080) // read SCL
etError I2c1_WriteByte(uint8 txByte)
{
etError error = NO_ERROR;
uint8 mask;
for(mask = 0x80; mask > 0; mask >>= 1)// shift bit for masking (8 times)
{
if((mask & txByte) == 0) SDA1_LOW(); // masking txByte, write bit to SDA-Line
else SDA1_OPEN();
DelayMicroSeconds(1); // data set-up time (t_SU;DAT)
SCL1_OPEN(); // generate clock pulse on SCL
DelayMicroSeconds(5); // SCL high time (t_HIGH)
SCL1_LOW();
DelayMicroSeconds(1); // data hold time(t_HD;DAT)
}
SDA1_OPEN(); // release SDA-line
SCL1_OPEN(); // clk #9 for ack
DelayMicroSeconds(5); // data set-up time (t_SU;DAT)
if(SDA1_READ) error = ACK_ERROR; // check ack from i2c slave
SCL1_LOW();
DelayMicroSeconds(20); // wait to see byte package on scope
return error; // return error code
}
//------------------------------------------------------------------------
uint8 I2c1_ReadByte(etI2cAck ack)
{
uint8 mask;
uint8 rxByte = NO_ERROR;
SDA1_OPEN(); // release SDA-line
for(mask = 0x80; mask > 0; mask >>= 1) // shift bit for masking (8 times)
{
SCL1_OPEN(); // start clock on SCL-line
DelayMicroSeconds(3); // SCL high time (t_HIGH)
if(SDA1_READ) rxByte = rxByte | mask; // read bit
SCL1_LOW();
DelayMicroSeconds(1); // data hold time(t_HD;DAT)
}
if(ack == ACK) SDA1_LOW(); // send acknowledge if necessary
else SDA1_OPEN();
DelayMicroSeconds(1); // data set-up time (t_SU;DAT)
SCL1_OPEN(); // clk #9 for ack
DelayMicroSeconds(5); // SCL high time (t_HIGH)
SCL1_LOW();
SDA1_OPEN(); // release SDA-line
DelayMicroSeconds(20); // wait to see byte package on scope
return rxByte; // return with no error
}
麻烦帮我看看,有错误吗?我今天遇到一个板子,这两个脚都是1V,我电路上上拉到5V
|
|