为了进行环境方面的检测,对光强传感器及温度大气压传感器的驱动程序进行了移植,经验证在GD32F310开发板上进行功能验证是十分方便的,能在半小时左右完成。
对光强传感器的测试结果如图1所示,该传感器所连接的引脚为PB0和PB1,具体的对应关系为:
CLK --PB0
DIN--PB1
图1 光强检测效果
使用GPIO口模拟高低电平输出及引脚状态读取的语句定义为:
#define SCL_Clr1() gpio_bit_write(GPIOB, GPIO_PIN_0, RESET)
#define SCL_Set1() gpio_bit_write(GPIOB, GPIO_PIN_0, SET)
#define SDA_Clr1() gpio_bit_write(GPIOB, GPIO_PIN_1, RESET)
#define SDA_Set1() gpio_bit_write(GPIOB, GPIO_PIN_1, SET)
#define IIC_SDA_IN1 gpio_input_bit_get(GPIOB, GPIO_PIN_1)
对引脚进行初始化配置的函数如下:
void BH1750_Init(void)
{
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_0|GPIO_PIN_1);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1);
gpio_bit_reset(GPIOB, GPIO_PIN_0);
gpio_bit_reset(GPIOB, GPIO_PIN_1);
}
实现输入与输出模式切换的2个函数为:
void IIC_INPUT_MODE()
{
gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_PULLDOWN, GPIO_PIN_1);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}
void IIC_OUTPUT_MODE()
{
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
}
以GPIO口模拟I2C方式字节数据的函数如下:
void BH1750_SendByte(char data)
{
char i;
IIC_OUTPUT_MODE();
SCL_Clr1();
delay_us(2);
for (i=0;i<8;i++)
{
if(data&0x80) SDA_Set1();
else SDA_Clr1();
data <<= 1;
SCL_Set1();
delay_us(2);
SCL_Clr1();
delay_us(2);
}
}
实现光强检测与显示的函数为:
void Get_Sunlight_Value()
{
int dis_data=0;
float temp;
unsigned int sd;
Single_Write_BH1750(0x01);
Single_Write_BH1750(0x10);
delay_1ms(180);
Multiple_Read_BH1750();
dis_data=BUF[0];
dis_data=(dis_data<<8)+BUF[1];
temp=(float) dis_data/1.2;
sd=temp;
OLED_ShowNum(20,6,sd,5,16);
}
实现图1所示效果的主程序为:
int main(void)
{
systick_config();
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7);
gpio_bit_reset(GPIOB, GPIO_PIN_6);
gpio_bit_reset(GPIOB, GPIO_PIN_7);
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"GD32F310G",16);
OLED_ShowString(20,2,"OLED & BH1750",16);
OLED_ShowString(20,4,"Sunlight=",16);
OLED_ShowString(68,6,"lx",16);
BH1750_Init();
while(1){
Get_Sunlight_Value();
delay_1ms(1000);
}
}
以光强检测为基础,对温度大气压的检测就顺利相对,其在GPIO口的使用、输入与输出模式的切换函数、高低电平的输出及引脚状态检测均是相似的。
两者的主要差别,则是在数据读取与辅助函数方面,温度大气压的测试效果如图2所示。
图2 温度大气压检测效果
对温度大气压传感器的初始化函数为:
void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
}
实现温度大气压检测及数值转换的函数如下:
void bmp085Convert()
{
unsigned int ut;
unsigned long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = ((b5 + 8) >> 4);
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6)>>12)>>11;
x2 = (ac2 * b6)>>11;
x3 = x1 + x2;
b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2;
x1 = (ac3 * b6)>>13;
x2 = (b1 * ((b6 * b6)>>12))>>16;
x3 = ((x1 + x2) + 2)>>2;
b4 = (ac4 * (unsigned long)(x3 + 32768))>>15;
b7 = ((unsigned long)(up - b3) * (50000>>OSS));
if (b7 < 0x80000000)
p = (b7<<1)/b4;
else
p = (b7/b4)<<1;
x1 = (p>>8) * (p>>8);
x1 = (x1 * 3038)>>16;
x2 = (-7357 * p)>>16;
pressure = p+((x1 + x2 + 3791)>>4);
}
实现图2所示效果的主程序为:
int main(void)
{
systick_config();
rcu_periph_clock_enable(RCU_GPIOB);
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7);
gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7);
gpio_bit_reset(GPIOB, GPIO_PIN_6);
gpio_bit_reset(GPIOB, GPIO_PIN_7);
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"GD32F310G",16);
OLED_ShowString(20,2,"OLED & bmp085",16);
OLED_ShowString(20,4,"t= C",16);
OLED_ShowString(20,6,"p= KPa",16);
BMP085_Init();
Init_BMP085();
while(1){
bmp085Convert();
OLED_ShowNum(44,4,temperature/10,3,16);
OLED_ShowNum(44,6,pressure/100,5,16);
delay_1ms(1000);
}
}
实践证明GD32F310在功能验证方面还是十分方便的,能够将其它MCU上的功能快速移植到该芯片上来实现设计目标。
|