1 /**
2 * @brief 寫一個字節(jié)到I2C設(shè)備中
3 * @param
4 * @arg pBuffer:緩沖區(qū)指針
5 * @arg WriteAddr:寫地址
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 //啟動信號發(fā)出之后要等待狀態(tài)寄存器SR1的位0(SB=1),狀態(tài)寄存器SR2的位1(BUSY=1)和位0(MSL=1),此時表明主模式下,起始條件已發(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 //從機地址發(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事件檢測:498頁。STM32的硬件IIC通信非常嚴(yán)格,每一步都要檢測相應(yīng)的標(biāo)志位是否正確,確保通信不會出錯。
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 }