I2C_GenerateClock(); }
AT24C32_SDA_HIGH;
return (!I2C_GenerateClock()); }
// 读一个字节的数据,并返回该字节值 uint8_t I2C_Read(void) {
uint8_t ucData = 0; uint8_t i;
for (i = 0; i < 8; i++) {
ucData <<= 1;
if (I2C_GenerateClock()) {
ucData |= 1; } }
return ucData; }
// 设置下一步操作的地址(一字节地址) uint8_t I2C_SetAddress(uint16_t u16Addr) {
// 发送启动信号 I2C_Start_A();
// 发送访问地址
return (I2C_Send(I2C_SLAW) && I2C_Send(u16Addr)); }
// 设置下一步操作的地址(两字节地址) uint8_t I2C_SetAddress16(uint16_t u16Addr) {
// 发送启动信号 I2C_Start_A();
// 发送从器件地址
// 发送访问地址
return (I2C_Send(I2C_SLAW) && I2C_Send(u16Addr >> 8) && I2C_Send(u16Addr & 0xff)); }
// 向8位地址的器件( 如:24C02 )写数据 bAddressIs16bit = 0 // 向16位地址的器件( 如:24C32 )写数据 bAddressIs16bit = 1
void Write_Byte_24c32(uint8_t *pDat, uint16_t u16Addr, uint8_t ucNbyte, uint8_t bAddressIs16bit) {
uint8_t bWriteError = FALSE; //写数据出错标志位 uint8_t i;
uint8_t ucDataBuf = 0; uint32_t Cnt = 0;
for (i = 0; i < ucNbyte; i++) {
if (bAddressIs16bit) {
if (I2C_SetAddress16(u16Addr + i) == 0) {
bWriteError = TRUE; break; } } else {
if (I2C_SetAddress(u16Addr + i) == 0) {
bWriteError = TRUE; break; } }
ucDataBuf = *(pDat + i);
if (I2C_Send(ucDataBuf) == 0) {
bWriteError = TRUE; break; }
I2C_Stop_A();
ucDataBuf = 12; do {
I2C_Start_A();
} while ((++Cnt < 500000) && I2C_Send(I2C_SLAW) == 0);
//如果50mS内还没有响应,则进入保护性报警状态 if (Cnt == 500000) {
bWriteError = TRUE; break; }
I2C_Stop_A(); }
if (bWriteError) {
I2C_Error(); } }
// 从8位地址的器件( 如:24C02 )读数据 bAddressIs16bit = 0 // 从16位地址的器件( 如:24C32 )读数据 bAddressIs16bit = 1
void Read_nByte_24c32(uint8_t *pDat, uint16_t u16Addr, uint8_t ucNbyte, uint8_t bAddressIs16bit) {
uint8_t bReadError标志寄存器 = FALSE; //读EEPROM错误标志位 uint8_t i;
uint8_t ucDataBuf = 0;
if (bAddressIs16bit) {
if (I2C_SetAddress16(u16Addr) == 0) {
bReadError = TRUE; } } else {
if (I2C_SetAddress(u16Addr) == 0) {
bReadError = TRUE; } }
if (bReadError) {
I2C_Error(); }
I2C_Start_A();
if (I2C_Send(I2C_SLAR) == 0) {
bReadError = TRUE; }
if (bReadError) {
I2C_Error(); }
for (i = 0; i < ucNbyte; i++) {
ucDataBuf = I2C_Read();
*(pDat + i) = ucDataBuf; I2C_Ack(i != (ucNbyte - 1)); }
I2C_Stop_A(); }
uint8_t Read_Byte_24c32(uint16_t u16Addr) {
uint8_t EepromDataBuf = 0xFF;
//从16位地址的器件( 如:24C32 )读数据 bAddressIs16bit = 1
Read_nByte_24c32(&EepromDataBuf, u16Addr, 1, DEVICE_ADDR);
return EepromDataBuf; }
//修改EEPROM数据
void ModifData_24c32(uint16_t u16Addr, uint8_t *pDat, uint16_t uclen) {
//向16位地址的器件( 如:24C32 )写数据 bAddressIs16bit = 1 Write_Byte_24c32(pDat, u16Addr, uclen, DEVICE_ADDR); } 1. STM8I/O口模拟I2C所读数据不正确 2. STM8 I/O口模拟I2C 3. 4.
5. #define I2C_ERR 0 6.
7. #define I2C_CRR 1 8.
9. #define I2CDataIn 1 10.
11. #define I2CDataOut 0 12.
13. #define I2C_PORT (GPIOC) 14.
15. #define I2CSCL (GPIO_PIN_7) 16.
17. #define I2CSDA (GPIO_PIN_6) 18. 19.
20. //*************************************************************** 21.
22. // I2C Data input/output 23.
24. // 0-Output, 1-Input 25.
26. //*************************************************************** 27.
28. void I2CDataInOut(bool InOut)
29. 30. { 31.
32. if(InOut) 33. 34. { 35.
36. GPIO_Init(I2C_PORT,I2CSDA,GPIO_MODE_IN_FL_NO_IT); 37. 38. } 39.
40. else
41. 42. { 43.
44. GPIO_Init(I2C_PORT,I2CSDA,GPIO_MODE_OUT_OD_LOW_FAST); 45. 46. } 47.
48. } 49.
50. //*************************************************************** 51.
52. // Send start condition 53.
54. // ensure data is high then issue a start condition 55.
56. // see also i2c_Start() macro 57.
58. //*************************************************************** 59.
60. void I2C_Start (void) 61. 62. { 63.
64. GPIO_WriteHigh(I2C_PORT, I2CSDA); 65.
66. _delay_5us(5); 67.
68. GPIO_WriteHigh(I2C_PORT, I2CSCL); 69.
70. _delay_5us(5); 71.
72. GPIO_WriteLow(I2C_PORT, I2CSDA); 73.
74. _delay_5us(5); 75. 76. } 77.
78. //*************************************************************** 79.
80. // Send stop condition 81.
82. // data low-high while clock high 83.
84. //*************************************************************** 85.
86. void I2C_Stop (void) 87. 88. { 89.
90. GPIO_WriteLow(I2C_PORT, I2CSDA); 91.