gd32f130例程iic
时间: 2023-06-05 19:01:21 浏览: 331
GD32F130是GigaDevice公司推出的一款32位单片机芯片,它拥有高效低功耗、丰富的接口和全面的内核支持等优秀特性。其中,它的IIC总线接口能够实现多个设备之间的数据通信,并具有时序稳定、线路简单等优点。
在实际开发中,为了使用该芯片的IIC功能,我们需要编写相应的IIC例程。首先,我们需要通过多个寄存器来配置IIC的主从机、传输数据的格式等参数。接着,我们需要编写IIC发送和接收数据的函数,将待传输的数据存入数据寄存器中,然后按照时序要求发送或接收数据。最终,我们需要在主函数中调用这些函数,并对传输的数据进行相应的处理。
需要注意的是,在编写IIC例程时,我们需要根据具体的硬件平台和情况进行相应的接口设置和调试,保证数据的正确性和传输的准确性。同时,我们还需要注意错误处理,确保在传输过程中出现错误时能够及时发现和处理。
相关问题
IO中断模拟IIC从机GD32F130例程,判断SDA从空闲状态到达START位置方法
您可以参考以下方法来判断SDA从空闲状态到达START位置:
1. 在程序执行之前,将SDA和SCL都拉高,并等待一段时间,通常为5个时钟周期左右。
2. 软件检测SDA和SCL的电平状态,确认它们都处于高电平状态,表示IIC总线处于空闲状态。
3. 当需要发送START信号时,先将SCL拉低,再将SDA拉低,表示开始发送START信号,然后开始发送数据。
4. 发送数据时,每个字节的传输前都要先检测SDA的状态,确认SDA已经被拉低,才继续发送数据。
5. 当需要发送STOP信号时,先将SDA拉低,再将SCL拉高,表示开始发送STOP信号,然后将SDA拉高,表示结束传输。
需要注意的是,在实际应用中,IO中断模拟IIC从机GD32F130例程的实现方法可能会有所不同,具体实现要根据硬件设计和应用场景进行相应的调整。
请给出IO中断模拟IIC从机GD32F130的例程
以下是IO中断模拟IIC从机GD32F130的例程,请参考:
```C++
#include "gd32f1x0.h"
#include "stdio.h"
#include "systick.h"
#define I2C2_SCL_PIN GPIO_PIN_6 /*!< GPIO pin of the I2C clock */
#define I2C2_SCL_GPIO_PORT GPIOB /*!< GPIO port of the I2C clock */
#define I2C2_SDA_PIN GPIO_PIN_7 /*!< GPIO pin of the I2C data */
#define I2C2_SDA_GPIO_PORT GPIOB /*!< GPIO port of the I2C data */
#define I2C2_RCC_CLK_ENABLE() rcu_periph_clock_enable(RCU_GPIOB) /*!< Enable GPIOB clock */
#define I2C2_RCC_CLK_DISABLE() rcu_periph_clock_disable(RCU_GPIOB) /*!< Disable GPIOB clock */
#define I2C2_SCL_HIGH() gpio_bit_set(I2C2_SCL_GPIO_PORT, I2C2_SCL_PIN) /*!< Set GPIOB pin 6 */
#define I2C2_SCL_LOW() gpio_bit_reset(I2C2_SCL_GPIO_PORT, I2C2_SCL_PIN) /*!< Reset GPIOB pin 6 */
#define I2C2_SDA_HIGH() gpio_bit_set(I2C2_SDA_GPIO_PORT, I2C2_SDA_PIN) /*!< Set GPIOB pin 7 */
#define I2C2_SDA_LOW() gpio_bit_reset(I2C2_SDA_GPIO_PORT, I2C2_SDA_PIN) /*!< Reset GPIOB pin 7 */
#define I2C2_SDA_READ() gpio_input_bit_get(I2C2_SDA_GPIO_PORT, I2C2_SDA_PIN) /*!< Read GPIOB pin 7 */
void delay(uint32_t count)
{
uint32_t index = 0;
for(index = count; index != 0; index--)
{
}
}
void I2C2_GPIO_Config(void)
{
rcu_periph_clock_enable(RCU_AF);
rcu_periph_clock_enable(RCU_GPIOB);
/*configure GPIO pin of I2C2*/
gpio_init(I2C2_SCL_GPIO_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, I2C2_SCL_PIN);
gpio_init(I2C2_SDA_GPIO_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, I2C2_SDA_PIN);
/*configure I2C2 as a master*/
gpio_init(I2C2_SCL_GPIO_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C2_SCL_PIN);
gpio_init(I2C2_SDA_GPIO_PORT, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, I2C2_SDA_PIN);
gpio_pin_remap_config(GPIO_SPI2_REMAP,I2C2_MAP);
}
void I2C2_Start(void)
{
I2C2_SDA_HIGH();
I2C2_SCL_HIGH();
delay(5);
I2C2_SDA_LOW();
delay(5);
I2C2_SCL_LOW();
}
void I2C2_Stop(void)
{
I2C2_SDA_LOW();
I2C2_SCL_HIGH();
delay(5);
I2C2_SDA_HIGH();
delay(5);
}
uint8_t I2C2_WaitAck(void)
{
uint8_t ucErrTime = 0;
I2C2_SDA_HIGH();
delay(1);
I2C2_SCL_HIGH();
delay(1);
while(I2C2_SDA_READ())
{
ucErrTime++;
if(ucErrTime > 50)
{
I2C2_Stop();
return 1;
}
delay(1);
}
I2C2_SCL_LOW();
return 0;
}
void I2C2_Ack(void)
{
I2C2_SCL_LOW();
I2C2_SDA_LOW();
delay(2);
I2C2_SCL_HIGH();
delay(2);
I2C2_SCL_LOW();
}
void I2C2_NAck(void)
{
I2C2_SCL_LOW();
I2C2_SDA_HIGH();
delay(2);
I2C2_SCL_HIGH();
delay(2);
I2C2_SCL_LOW();
}
void I2C2_SendByte(uint8_t ucData)
{
uint8_t ucCount = 0;
for(ucCount = 0; ucCount < 8; ucCount++)
{
if(ucData & 0x80)
I2C2_SDA_HIGH();
else
I2C2_SDA_LOW();
ucData <<= 1;
delay(2);
I2C2_SCL_HIGH();
delay(2);
I2C2_SCL_LOW();
}
}
uint8_t I2C2_ReadByte(uint8_t ucAck)
{
uint8_t ucCount = 0, ucData = 0;
I2C2_SDA_HIGH();
for(ucCount = 0; ucCount < 8; ucCount++)
{
ucData <<= 1;
I2C2_SCL_LOW();
delay(2);
I2C2_SCL_HIGH();
delay(2);
if(I2C2_SDA_READ())
ucData++;
}
if(!ucAck)
I2C2_NAck();
else
I2C2_Ack();
return ucData;
}
void GD32_I2C2_Init(void)
{
I2C2_GPIO_Config();
}
uint8_t GD32_I2C2_ReadByte(uint8_t ucAddr, uint8_t ucReg)
{
uint8_t ucData = 0;
I2C2_Start();
I2C2_SendByte(ucAddr << 1 | 0);
if(I2C2_WaitAck())
{
I2C2_Stop();
return 0;
}
I2C2_SendByte(ucReg);
I2C2_WaitAck();
I2C2_Start();
I2C2_SendByte(ucAddr << 1 | 1);
I2C2_WaitAck();
ucData = I2C2_ReadByte(0);
I2C2_Stop();
return ucData;
}
void GD32_I2C2_WriteByte(uint8_t ucAddr, uint8_t ucReg, uint8_t ucValue)
{
I2C2_Start();
I2C2_SendByte(ucAddr << 1 | 0);
if(I2C2_WaitAck())
{
I2C2_Stop();
return;
}
I2C2_SendByte(ucReg);
I2C2_WaitAck();
I2C2_SendByte(ucValue);
I2C2_WaitAck();
I2C2_Stop();
}
int main(void)
{
uint8_t ucData = 0;
GD32_I2C2_Init();
while(1)
{
ucData = GD32_I2C2_ReadByte(0x68, 0x75);
printf("WHO_AM_I is %X \r\n", ucData);
delay_1ms(1000);
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)