日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]1 /**2 * @brief 寫(xiě)一個(gè)字節(jié)到I2C設(shè)備中3 * @param 4 * @arg pBuffer:緩沖區(qū)指針5 * @arg WriteAddr:寫(xiě)地址6 * @retval 正常返回1,異常返回07 */8 uint8_t I2C_ByteWrite(u8 pBuffer, u8 WriteAddr)9

1 /**

2 * @brief 寫(xiě)一個(gè)字節(jié)到I2C設(shè)備中

3 * @param

4 * @arg pBuffer:緩沖區(qū)指針

5 * @arg WriteAddr:寫(xiě)地址

6 * @retval 正常返回1,異常返回0

7 */

8 uint8_t I2C_ByteWrite(u8 pBuffer, u8 WriteAddr)

9 {

10 /* Send STRAT condition */

11 I2C_GenerateSTART(macI2Cx, ENABLE);

12

13 I2CTimeout = I2CT_FLAG_TIMEOUT;

14

15

16 /* Test on EV5 and clear it */

17 //啟動(dòng)信號(hào)發(fā)出之后要等待狀態(tài)寄存器SR1的位0(SB=1),狀態(tài)寄存器SR2的位1(BUSY=1)和位0(MSL=1),此時(shí)表明主模式下,起始條件已發(fā)送,總線處于忙狀態(tài);確保IIC通訊正確

18 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_MODE_SELECT))

19 {

20 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

21 }

22

23 /* Send slave address for write */

24 I2C_Send7bitAddress(macI2Cx, MPU6050_SLAVE_ADDRESS, I2C_Direction_Transmitter);//7bit slave address + read/write (0write,1 read)

25

26 I2CTimeout = I2CT_FLAG_TIMEOUT;

27

28 /* Test on EV6 and clear it */

29 //從機(jī)地址發(fā)出之后,等待 BUSY, MSL, ADDR, TXE and TRA flags標(biāo)志位

30 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

31 {

32 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

33 }

34

35 /* Send the slave's internal address to write to */

36 I2C_SendData(macI2Cx, WriteAddr);

37

38 I2CTimeout = I2CT_FLAG_TIMEOUT;

39 /* Test on EV8 and clear it */

40 /* TRA, BUSY, MSL, TXE and BTF flags */

41 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))

42 {

43 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

44 }

45

46 /* Send the byte to be written */

47 I2C_SendData(macI2Cx, pBuffer);

48

49 I2CTimeout = I2CT_FLAG_TIMEOUT;

50

51 /* Test on EV8 and clear it */

52 /* TRA, BUSY, MSL, TXE and BTF flags */

53 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))

54 {

55 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

56 }

57

58 /* Send STOP condition */

59 I2C_GenerateSTOP(macI2Cx, ENABLE);

60

61 return 1; //正常返回1

62 }


IIC事件檢測(cè):498頁(yè)。STM32的硬件IIC通信非常嚴(yán)格,每一步都要檢測(cè)相應(yīng)的標(biāo)志位是否正確,確保通信不會(huì)出錯(cuò)。



1 /**

2 * @brief 從I2C設(shè)備里面讀取一塊數(shù)據(jù)

3 * @param

4 * @arg pBuffer:存放從slave讀取的數(shù)據(jù)的緩沖區(qū)指針

5 * @arg WriteAddr:接收數(shù)據(jù)的從設(shè)備的地址

6 * @arg NumByteToWrite:要從從設(shè)備讀取的字節(jié)數(shù)

7 * @retval 正常返回1,異常返回0

8 */

9 uint8_t I2C_BufferRead(u8* pBuffer, u8 ReadAddr, u16 NumByteToRead)

10 {

11 I2CTimeout = I2CT_LONG_TIMEOUT;

12

13 while(I2C_GetFlagStatus(macI2Cx, I2C_FLAG_BUSY)) // Added by Najoua 27/08/2008

14 {

15 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

16 }

17

18 I2C_GenerateSTART(macI2Cx, ENABLE);

19

20 I2CTimeout = I2CT_FLAG_TIMEOUT;

21

22 /* Test on EV5 and clear it */

23 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_MODE_SELECT))

24 {

25 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

26 }

27

28 /* Send slave address for write */

29 I2C_Send7bitAddress(macI2Cx, MPU6050_SLAVE_ADDRESS, I2C_Direction_Transmitter);

30

31 I2CTimeout = I2CT_FLAG_TIMEOUT;

32

33 /* Test on EV6 and clear it */

34 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))

35 {

36 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

37 }

38

39 /* Clear EV6 by setting again the PE bit */

40 I2C_Cmd(macI2Cx, ENABLE);

41

42 /* Send the slave's internal address to write to */

43 I2C_SendData(macI2Cx, ReadAddr);

44

45 I2CTimeout = I2CT_FLAG_TIMEOUT;

46

47 /* Test on EV8 and clear it */

48 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED))

49 {

50 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

51 }

52

53 /* Send STRAT condition a second time */

54 I2C_GenerateSTART(macI2Cx, ENABLE);

55

56 I2CTimeout = I2CT_FLAG_TIMEOUT;

57 /* Test on EV5 and clear it */

58 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_MODE_SELECT))

59 {

60 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

61 }

62

63 /* Send slave address for read */

64 I2C_Send7bitAddress(macI2Cx, MPU6050_SLAVE_ADDRESS, I2C_Direction_Receiver);

65

66 I2CTimeout = I2CT_FLAG_TIMEOUT;

67

68 /* Test on EV6 and clear it */

69 while(!I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))

70 {

71 if((I2CTimeout--) == 0) return I2C_TIMEOUT_UserCallback();

72 }

73

74 /* While there is data to be read */

75 while(NumByteToRead)

76 {

77 if(NumByteToRead == 1)

78 {

79 /* Disable Acknowledgement */

80 I2C_AcknowledgeConfig(macI2Cx, DISABLE);

81

82 /* Send STOP Condition */

83 I2C_GenerateSTOP(macI2Cx, ENABLE);

84 }

85

86 /* Test on EV7 and clear it */

87 if(I2C_CheckEvent(macI2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED))

88 {

89 /* Read a byte from the slave */

90 *pBuffer = I2C_ReceiveData(macI2Cx);

91

92 /* Point to the next location where the byte read will be saved */

93 pBuffer++;

94

95 /* Decrement the read bytes counter */

96 NumByteToRead--;

97 }

98 }

99

100 /* Enable Acknowledgement to be ready for another reception */

101 I2C_AcknowledgeConfig(macI2Cx, ENABLE);

102

103 return 1; //正常,返回1

104 }


本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉