小白初学,想用GD32F310F8P6这款芯片驱动SHT30获取温湿度信息,现在卡住了,想请教一下专业人士;
代码如下:
#include "gd32f3x0.h" // Device header
#include "Serial.h"
#include "systick.h"
#define SHT30_ADDRESS 0X88 //定义温湿度传感器芯片地址
void I2C_Init(void)
{
// 硬件I2C初始化
rcu_periph_clock_enable(RCU_I2C1);//开启I2C1硬件外设时钟
rcu_periph_clock_enable(RCU_GPIOA);//开启GPIOA时钟
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_0);
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_1);
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_0);//配置引脚0
gpio_output_options_set(GPIOA,GPIO_OTYPE_OD,GPIO_OSPEED_50MHZ,GPIO_PIN_0);
gpio_mode_set(GPIOA,GPIO_MODE_AF,GPIO_PUPD_PULLUP,GPIO_PIN_1);//配置引脚1
gpio_output_options_set(GPIOA,GPIO_OTYPE_OD,GPIO_OSPEED_50MHZ,GPIO_PIN_1);
// i2c_deinit(I2C1);//重置I2C1
i2c_clock_config(I2C1,100000,I2C_DTCY_2);//配置I2C1
i2c_mode_addr_config(I2C1,I2C_I2CMODE_ENABLE,I2C_ADDFORMAT_7BITS,0x00);//配置I2C地址
i2c_ack_config(I2C1,I2C_ACK_ENABLE);
i2c_enable(I2C1);//使能
}
void SHT30_read_result(void) //获取温湿度
{
uint16_t tem,hum;
uint16_t buff[6];
float Temperature=0;
float Humidity=0;
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
i2c_start_on_bus(I2C1);//起始条件
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C1,SHT30_ADDRESS,I2C_TRANSMITTER);//发送从机地址,之后自动接收应答
while(!i2c_flag_get(I2C1, I2C_FLAG_ADDSEND)); (调试过程发现一直卡在这里,不知道什么原因)
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
i2c_data_transmit(I2C1,0x2C);//发送命令字
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
i2c_data_transmit(I2C1,0x06);
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
i2c_stop_on_bus(I2C1);//终止条件
while(I2C_CTL0(I2C1) & I2C_CTL0_STOP);
delay_1ms(50);
while(i2c_flag_get(I2C1, I2C_FLAG_I2CBSY));
i2c_start_on_bus(I2C1);//发送重复起始条件
while(!i2c_flag_get(I2C1, I2C_FLAG_SBSEND));
i2c_master_addressing(I2C1,SHT30_ADDRESS,I2C_RECEIVER);
while (i2c_flag_get(I2C1,I2C_FLAG_ADDSEND)==RESET);
i2c_flag_clear(I2C1, I2C_FLAG_ADDSEND);
while(!i2c_flag_get(I2C1, I2C_FLAG_TBE));
i2c_ack_config(I2C1,I2C_ACK_ENABLE);//提前设置应答
while(i2c_flag_get(I2C1,I2C_FLAG_RBNE)==RESET);//确定接收标志位
buff[0]=i2c_data_receive(I2C1);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);//提前设置应答
while(i2c_flag_get(I2C1,I2C_FLAG_RBNE)==RESET);//确定接收标志位
buff[1]=i2c_data_receive(I2C1);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);//提前设置应答
while(i2c_flag_get(I2C1,I2C_FLAG_RBNE)==RESET);//确定接收标志位
buff[2]=i2c_data_receive(I2C1);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);//提前设置应答
while(i2c_flag_get(I2C1,I2C_FLAG_RBNE)==RESET);//确定接收标志位
buff[3]=i2c_data_receive(I2C1);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);//提前设置应答
while(i2c_flag_get(I2C1,I2C_FLAG_RBNE)==RESET);//确定接收标志位
buff[4]=i2c_data_receive(I2C1);
i2c_ack_config(I2C1,I2C_ACK_DISABLE);//提前设置非应答
while(i2c_flag_get(I2C1,I2C_FLAG_RBNE)==RESET);//确定接收标志位
buff[5]=i2c_data_receive(I2C1);
i2c_stop_on_bus(I2C1);//设置停止位
while(I2C_CTL0(I2C1) & I2C_CTL0_STOP);
i2c_ack_config(I2C1,I2C_ACK_ENABLE);//最后恢复默认ACK=1
tem=(buff[0]<<8|buff[1]);//温度拼接
hum=(buff[3]<<8|buff[4]);//湿度拼接
//转换实际温度
Temperature=(175.0*(float)tem/65535.0-45.0);
Humidity=(100.0*(float)hum/65535.0);
Serial_Printf("温度:%f\r\n",Temperature);
Serial_Printf("湿度:%f\r\n",Humidity);
Serial_SendByte(0);
hum=0;
tem=0;
}
int main(void)
{
systick_config();
Serial_Init();
I2C_Init();
gpio_mode_set(GPIOA,GPIO_MODE_OUTPUT,GPIO_PUPD_NONE,GPIO_PIN_5);
gpio_output_options_set(GPIOA,GPIO_OTYPE_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_5);
gpio_bit_reset(GPIOA,GPIO_PIN_5);
while(1)
{
gpio_bit_set(GPIOA,GPIO_PIN_4);//设置为发送模式
SHT30_read_result();
Serial_SendByte(2);
delay_1ms(500);
}
}