STM8 I/O口模拟I2C
来源:互联网 发布:怎样金融网络投资 编辑:程序博客网 时间:2024/06/05 02:13
- STM8I/O口模拟I2C所读数据不正确
- STM8 I/O口模拟I2C
-
-
- #define I2C_ERR 0
-
- #define I2C_CRR 1
-
- #define I2CDataIn 1
-
- #define I2CDataOut 0
-
- #define I2C_PORT (GPIOC)
-
- #define I2CSCL (GPIO_PIN_7)
-
- #define I2CSDA (GPIO_PIN_6)
-
-
-
-
-
-
-
-
-
-
- void I2CDataInOut(bool InOut)
-
- {
-
- if(InOut)
-
- {
-
- GPIO_Init(I2C_PORT,I2CSDA,GPIO_MODE_IN_FL_NO_IT);
-
- }
-
- else
-
- {
-
- GPIO_Init(I2C_PORT,I2CSDA,GPIO_MODE_OUT_OD_LOW_FAST);
-
- }
-
- }
-
-
-
-
-
-
-
-
-
-
-
- void I2C_Start (void)
-
- {
-
- GPIO_WriteHigh(I2C_PORT, I2CSDA);
-
- _delay_5us(5);
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- GPIO_WriteLow(I2C_PORT, I2CSDA);
-
- _delay_5us(5);
-
- }
-
-
-
-
-
-
-
-
-
- void I2C_Stop (void)
-
- {
-
- GPIO_WriteLow(I2C_PORT, I2CSDA);
-
- _delay_5us(5);
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- GPIO_WriteHigh(I2C_PORT, I2CSDA);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
- void I2C_Rack(void)
-
- {
-
- GPIO_WriteLow(I2C_PORT, I2CSDA);
-
- _delay_5us(5);
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- GPIO_WriteLow(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- void I2C_nAck (void)
-
- {
-
- GPIO_WriteHigh(I2C_PORT, I2CSDA);
-
- _delay_5us(5);
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- GPIO_WriteLow(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- }
-
-
-
-
-
-
-
-
-
- bool SendByte(UCHAR I2cData)
-
- {
-
- UCHAR i;
-
- bool I2CStatus;
-
-
- for(i=0; i<8; i++)
-
- {
-
- GPIO_WriteLow(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- if(I2cData & 0x80)
-
- {GPIO_WriteHigh(I2C_PORT, I2CSDA);}
-
- else
-
- {GPIO_WriteLow(I2C_PORT, I2CSDA);}
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- I2cData <<= 1;
-
- }
-
- GPIO_WriteLow(I2C_PORT, I2CSCL);
-
- GPIO_WriteHigh(I2C_PORT, I2CSDA);
-
-
-
- I2CDataInOut(I2CDataIn);
-
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- if(GPIO_ReadInputPin(I2C_PORT,I2CSDA) == 0)
-
- {
-
- I2CStatus = I2C_CRR;
-
- }
-
- else
-
- {
-
- I2CStatus = I2C_ERR;
-
- }
-
- I2CDataInOut(I2CDataOut);
-
- return I2CStatus;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- UCHAR RcvByte(void)
-
- {
-
- UCHAR i;
-
- UCHAR ReadByte=0;
-
- GPIO_WriteHigh(I2C_PORT, I2CSDA);
-
-
- I2CDataInOut(I2CDataIn);
-
- _delay_5us(10);
-
- for(i=0; i<8; i++)
-
- {
-
- ReadByte <<= 1;
-
- GPIO_WriteLow(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
- GPIO_WriteHigh(I2C_PORT, I2CSCL);
-
- _delay_5us(5);
-
-
- if(GPIO_ReadInputPin(I2C_PORT,I2CSDA) == 1)
-
- {ReadByte |= 0x01;}
-
- _delay_5us(5);
-
- }
-
- GPIO_WriteLow(I2C_PORT, I2CSCL);
-
- I2CDataInOut(I2CDataOut);
-
- _delay_5us(10);
-
- return ReadByte;
-
- }
-
-
- /*******************************************************
-
- 读N个数据,参数:?
-
- wrDAdr: write device-address 写器件地址??
-
- wordAdr: word address 读数据的寄存器地址??
-
- rdDAdr: read device-address 读器件地址
-
- *pRdDat: p->read data 读数据指针
-
- num: number 需要读数据的个数
-
- *******************************************************/
-
- bool I2C_Read(UCHAR wrDAdr,UCHAR wordAdr,UCHAR *pRdDat,UCHAR num)
-
- {
-
- bool I2CAck;
-
- UCHAR i=0;
-
- UCHAR rdDAdr;
-
- rdDAdr = wrDAdr+1;
-
- I2C_Start();
-
- _delay_5us(5);
-
- I2CAck = SendByte(wrDAdr);
-
- if(!I2CAck)
-
- {
-
- return I2C_ERR;
-
- }
-
- I2CAck = SendByte(wordAdr);
-
- if(!I2CAck)
-
- {
-
- return I2C_ERR;
-
- }
-
-
- I2C_Start();
-
- I2CAck = SendByte(rdDAdr);
-
- if(!I2CAck)
-
- {
-
- return I2C_ERR;
-
- }
-
- _delay_5us(5);
-
- for(i=0;i<num-1;i++)
-
- {
-
- *(pRdDat+i) = RcvByte();
-
- I2C_Rack();
-
- }
-
- if(i==num-1)
-
- {
-
- *(pRdDat+i) = RcvByte();
-
- I2C_nAck();
-
- }
-
- I2C_Stop();
-
- return I2C_CRR;
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- bool I2C_Write(UCHAR wrDAdr,UCHAR wordAdr,UCHAR *pWrDat,UCHAR num)
-
- {
-
- bool I2CAck;
-
- unsigned char i;
-
-
- I2C_Start();
-
- _delay_5us(5);
-
- I2CAck = SendByte(wrDAdr);
-
- if(!I2CAck)
-
- {
-
- return I2C_ERR;
-
- }
-
- I2CAck = SendByte(wordAdr);
-
- if(!I2CAck)
-
- {
-
- return I2C_ERR;
-
- }
-
- for(i=0;i<num-1;i++)
-
- {
-
- I2CAck = SendByte(*(pWrDat+i));
-
- if(!I2CAck)
-
- {return I2C_ERR;}
-
- }
-
- if(i==num-1)
-
- {
-
- I2CAck = SendByte(*(pWrDat+num-1));
-
- if(!I2CAck)
-
- {return I2C_ERR;}
-
- }
-
- I2C_Stop();
-
- return I2CAck;
-
- }
以上代码不知为何读出数据不正确,用示波器看我发送的器件地址,寄存器地址这些都对,器件输出的数据是0xFF,程序中读出返回的数据是0x00,弄了一天没对相当郁闷,同志们指点下
终于搞定了, 应该跟我的器件有关,另外if(GPIO_ReadInputPin(I2C_PORT,I2CSDA) == 1)这种不能写成==1,要直接写成if(GPIO_ReadInputPin(I2C_PORT,I2CSDA)),太郁闷了