1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#include "gd32f30x.h"
#define I2C0_SLAVE_ADDRESS7 0x72
#define I2C1_SLAVE_ADDRESS7 0x82
void rcu_config(void);
void gpio_config(void);
void i2c_config(void);
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
uint8_t i;
/* cofigure RCU */
rcu_config();
/* cofigure GPIO */
gpio_config();
/* cofigure I2C */
i2c_config();
i = 0;
/* wait until I2C bus is idle */
while(i2c_flag_get(I2C0, I2C_FLAG_I2CBSY));
/* send a start condition to I2C bus */
i2c_start_on_bus(I2C0);
/* wait until SBSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_SBSEND));
/* send slave address to I2C bus */
i2c_master_addressing(I2C0, I2C1_SLAVE_ADDRESS7, I2C_RECEIVER);
/* wait until ADDSEND bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_ADDSEND));
/* N=1,reset ACKEN bit before clearing ADDRSEND bit */
i2c_ack_config(I2C0, I2C_ACK_DISABLE);
/* clear ADDSEND bit */
i2c_flag_clear(I2C0, I2C_FLAG_ADDSEND);
/* N=1,send stop condition after clearing ADDRSEND bit */
i2c_stop_on_bus(I2C0);
/* wait until the RBNE bit is set */
while(!i2c_flag_get(I2C0, I2C_FLAG_RBNE));
/* read a data from I2C_DATA */
i2c_receiver[i++] = i2c_data_receive(I2C0);
/* wait until stop condition generate */
while(I2C_CTL0(I2C0) & 0x0200);
/* Enable Acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
while(1) {
}
}
void rcu_config(void)
{
/* enable GPIOB clock */
rcu_periph_clock_enable(RCU_GPIOB);
/* enable I2C0 clock */
rcu_periph_clock_enable(RCU_I2C0);
}
void gpio_config(void)
{
/* connect PB6 to I2C0_SCL */
/* connect PB7 to I2C0_SDA */
gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7);
}
void i2c_config(void)
{
/* cofigure I2C clock */
i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
/* cofigure I2C address */
i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C0_SLAVE_ADDRESS7);
/* enable I2C0 */
i2c_enable(I2C0);
/* enable acknowledge */
i2c_ack_config(I2C0, I2C_ACK_ENABLE);
}
|