本帖最后由 jinglixixi 于 2020-6-4 08:58 编辑
1. 温湿度检测 DHT22是一种单总线的数字式温湿度传感器,它共有3个引脚,只需占用一个GPIO口接口工作。 DHT22与MCU的连接关系为: DAT——PA7 其余两个引脚则接3.3V工作电源
定义GPIO口读取电位及输出高低电平的语句为: #define DHT22_D0_H GPIO_SetPinBits(7) //PA7 #define DHT22_D0_L GPIO_ResetPinBits(7) //PA7 #define DHT22_D0_R GPIO_GetPinValue(7) //PA7
设置GPIO为输入、输出的函数为: void DHT22_IO_IN(void)
{
GPIO_SetDir(GPIO_PA7, 0);
}
void DHT22_IO_OUT(void)
{
GPIO_SetDir(GPIO_PA7, 1);
}
DHT22的初始化函数为: u8 DHT22_Init(void)
{
GPIO_SetDir(GPIO_PA7, 1);
DHT22_Rst();
return DHT22_Check();
}
DHT22读取数据的函数为: u8 DHT22_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT22_Rst();
if(DHT22_Check()==0)
{
for(i=0;i<5;i++)
{
buf[i]=DHT22_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=(buf[0]*256+buf[1])/10;
*temp=(buf[2]*256+buf[3])/10;
}
}else return 1;
return 0;
}
实现图1与图2显示效果的主程序为: int main(void)
{
u8 senflag;
InitDebug();
InitDelay();
GPIO_SetDir(GPIO_PA0, 1);
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AC8711",16);
OLED_ShowString(0,2,"DHT11 TEST",16);
OLED_ShowString(0,4," C %RH",16);
senflag=DHT22_Init();
if(senflag) OLED_ShowString(64,0,"NO SEN!",16);
else OLED_ShowString(64,0,"SEN OK!",16);
while(1)
{
if(!senflag)
{
DHT22_Read_Data(&temperature,&humidity);
OLED_ShowNum(0,4,temperature,2,16);
OLED_ShowNum(32,4,humidity,2,16);
printf("T=%dC,H=%d%%RH \r\n\r\n",temperature,humidity);
}
mdelay(500);
}
}
图1 串口输出效果
图2 DHT22温湿度检测
2. RTC计时 I2C_OLED屏是一种借由I2C接口工作的器件,共有4个引脚,它与MCU的连接关系为: OLED_SCLK——PA4 OLED_SDIN——PA5 其余的两个引脚则接3.3V工作电源
RTC显示函数为: void RTC_ClockDisplay(uint32_t counter)
{
uint32_t tempHour = 0, tempMin = 0, tempSec = 0;
tempSec = counter % 60;
tempMin = (counter / 60) % 60;
tempHour = (counter / 3600) % 60;
OLED_ShowChar(0,6, tempHour / 10 + '0', 16);
OLED_ShowChar(8, 6, tempHour % 10 + '0', 16);
OLED_ShowString(16, 6, ":", 8);
OLED_ShowChar(24, 6, tempMin / 10 + '0', 16);
OLED_ShowChar(32, 6, tempMin % 10 + '0',16);
OLED_ShowString(40, 6, ":", 8);
OLED_ShowChar(48, 6, tempSec / 10 + '0',16);
OLED_ShowChar(56, 6, tempSec % 10 + '0',16);
}
RTC秒中断处理函数为: int32_t RTCCallBackFunc(uint32_t wparam, uint32_t lparam )
{
static uint32_t s_clkCounter = 1;
RTC_ClockDisplay(s_clkCounter);
printf("RTC CallBack Function,counter:%d\r\n", s_clkCounter++);
return 0;
}
实现图3所示效果的主程序为: int main(void)
{
uint32_t tmpMod = 99;
uint32_t tmpPrescalerValue = 0x752ff;
RTC_ConfigType RTCConfig = {0};
InitDelay();
InitDebug();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AC8711",16);
OLED_ShowString(0,2,"DHT22 TEST",16);
OLED_ShowString(0,4," C %RH",16);
senflag=DHT22_Init();
if(senflag) OLED_ShowString(64,0,"NO SEN!",16);
else OLED_ShowString(64,0,"SEN OK!",16);
RTCConfig.clockSource = 0;
RTCConfig.moduloValue = tmpMod;
RTCConfig.interruptEn = ENABLE;
RTCConfig.prescalerValue = tmpPrescalerValue;
RTCConfig.RTCOut = ENABLE;
RTC_SetEventCallback(RTCCallBackFunc);
RTC_Initialize(&RTCConfig);
while(1)
{
if(!senflag)
{
DHT22_Read_Data(&temperature,&humidity);
OLED_ShowNum(0,4,temperature,2,16);
OLED_ShowNum(32,4,humidity,2,16);
}
mdelay(500);
}
}
图3 综合运行效果
|