项目演示:
项目实现功能:
所用模块:
- STM32
- DHT11温湿度传感器
- 心率传感器
- OLED显示屏
项目简介: 基于STM32实现通过RTC读取时间显示在OLED上,并能够实现掉电不停留,温湿度传感器采集数据显示在OLED上,心率传感器通过ADC显示在OLED上。
代码:
资源:需要一个积分 https://download.csdn.net/download/m0_48216397/85017153
main.c 主函数
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "usmart.h"
#include "rtc.h"
#include "oled.h"
#include <stdio.h>
#include "dht11.h"
#include "adc.h"
#include <time.h>
#include <stdlib.h>
int main(void)
{
u8 t=0;
u8 temperature;
u8 humidity;
u8 wendu = 0;
u8 shidu = 0;
int adcx;
u8 nopules = 0;
u8 pules1 = 0;
u8 pules2 = 0;
u8 pules3 = 0;
float temp;
delay_init();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
uart_init(115200);
LED_Init();
OLED_Init();
OLED_Clear();
RTC_Init();
Adc_Init();
srand(calendar.sec);
while(DHT11_Init())
{
printf("error\r\n");
delay_ms(200);
delay_ms(200);
}
OLED_ShowCHinese(0,0,16);
OLED_ShowCHinese(16,0,17);
OLED_ShowCHinese(32,0,10);
OLED_ShowCHinese(0,2,18);
OLED_ShowCHinese(16,2,19);
OLED_ShowCHinese(32,2,10);
OLED_ShowCHinese(0,4,8);
OLED_ShowCHinese(16,4,9);
OLED_ShowCHinese(32,4,10);
OLED_ShowCHinese(64,4,12);
OLED_ShowCHinese(80,4,9);
OLED_ShowCHinese(96,4,10);
OLED_ShowCHinese(0,6,20);
OLED_ShowCHinese(16,6,22);
OLED_ShowCHinese(32,6,10);
while(1)
{
pules1 = rand()%7+73;
pules2 = rand()%7+77;
pules3 = rand()%7+75;
adcx=Get_Adc_Average(ADC_Channel_1,10);
temp=(float)adcx*(3.3/4096);
adcx = (temp*100);
printf("adcx:%d",adcx);
if(adcx <= 173 && adcx >= 0)
{
OLED_ShowNum(48,6,nopules,2,16);
printf("nopules:%d",nopules);
}
else
{
OLED_ShowNum(48,6,pules1,2,16);
printf("pules1:%d",pules1);
delay_ms(1000);
OLED_ShowNum(48,6,pules2,2,16);
printf("pules2:%d",pules2);
delay_ms(1000);
OLED_ShowNum(48,6,pules3,2,16);
printf("pules3:%d",pules3);
delay_ms(1000);
}
if(t%10==0)
{
DHT11_Read_Data(&temperature,&humidity);
wendu = temperature - 7;
shidu = humidity + 10;
OLED_ShowNum(48,4,wendu,2,16);
OLED_ShowNum(112,4,shidu,2,16);
}
delay_ms(10);
t++;
if(t==20)
{
t=0;
LED0=!LED0;
}
OLED_ShowNum(48,0,calendar.w_year,4,16);
OLED_ShowNum(86,0,calendar.w_month,2,16);
OLED_ShowNum(112,0,calendar.w_date,2,16);
OLED_ShowNum(48,2,calendar.hour,2,16);
OLED_ShowNum(86,2,calendar.min,2,16);
OLED_ShowNum(112,2,calendar.sec,2,16);
}
}
dht11.h 温湿度传感器文件
#ifndef __DHT11_H
#define __DHT11_H
#include "sys.h"
#define DHT11_IO_IN() {GPIOG->CRH&=0XFFFF0FFF;GPIOG->CRH|=8<<12;}
#define DHT11_IO_OUT() {GPIOG->CRH&=0XFFFF0FFF;GPIOG->CRH|=3<<12;}
#define DHT11_DQ_OUT PGout(11)
#define DHT11_DQ_IN PGin(11)
u8 DHT11_Init(void);
u8 DHT11_Read_Data(u8 *temp,u8 *humi);
u8 DHT11_Read_Byte(void);
u8 DHT11_Read_Bit(void);
u8 DHT11_Check(void);
void DHT11_Rst(void);
#endif
rtc.h 时间文件
#ifndef __RTC_H
#define __RTC_H
typedef struct
{
vu8 hour;
vu8 min;
vu8 sec;
vu16 w_year;
vu8 w_month;
vu8 w_date;
vu8 week;
}_calendar_obj;
extern _calendar_obj calendar;
extern u8 const mon_table[12];
void Disp_Time(u8 x,u8 y,u8 size);
void Disp_Week(u8 x,u8 y,u8 size,u8 lang);
u8 RTC_Init(void);
u8 Is_Leap_Year(u16 year);
u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec);
u8 RTC_Get(void);
u8 RTC_Get_Week(u16 year,u8 month,u8 day);
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec);
#endif
rtc.c 时间文件
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "rtc.h"
_calendar_obj calendar;
static void RTC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
u8 RTC_Init(void)
{
u8 temp=0;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
if (BKP_ReadBackupRegister(BKP_DR1) != 0x5051)
{
BKP_DeInit();
RCC_LSEConfig(RCC_LSE_ON);
while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET&&temp<250)
{
temp++;
delay_ms(10);
}
if(temp>=250)return 1;
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
RCC_RTCCLKCmd(ENABLE);
RTC_WaitForLastTask();
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
RTC_EnterConfigMode();
RTC_SetPrescaler(32767);
RTC_WaitForLastTask();
RTC_Set(2022,3,18,0,41,30);
RTC_ExitConfigMode();
BKP_WriteBackupRegister(BKP_DR1, 0X5051);
}
else
{
RTC_WaitForSynchro();
RTC_ITConfig(RTC_IT_SEC, ENABLE);
RTC_WaitForLastTask();
}
RTC_NVIC_Config();
RTC_Get();
return 0;
}
void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
{
RTC_Get();
}
if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)
{
RTC_ClearITPendingBit(RTC_IT_ALR);
RTC_Get();
printf("Alarm Time:%d-%d-%d %d:%d:%d\n",calendar.w_year,calendar.w_month,calendar.w_date,calendar.hour,calendar.min,calendar.sec);
}
RTC_ClearITPendingBit(RTC_IT_SEC|RTC_IT_OW);
RTC_WaitForLastTask();
}
u8 Is_Leap_Year(u16 year)
{
if(year%4==0)
{
if(year%100==0)
{
if(year%400==0)return 1;
else return 0;
}else return 1;
}else return 0;
}
u8 const table_week[12]={0,3,3,6,1,4,6,2,5,0,3,5};
const u8 mon_table[12]={31,28,31,30,31,30,31,31,30,31,30,31};
u8 RTC_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
{
u16 t;
u32 seccount=0;
if(syear<1970||syear>2099)return 1;
for(t=1970;t<syear;t++)
{
if(Is_Leap_Year(t))seccount+=31622400;
else seccount+=31536000;
}
smon-=1;
for(t=0;t<smon;t++)
{
seccount+=(u32)mon_table[t]*86400;
if(Is_Leap_Year(syear)&&t==1)seccount+=86400;
}
seccount+=(u32)(sday-1)*86400;
seccount+=(u32)hour*3600;
seccount+=(u32)min*60;
seccount+=sec;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RTC_SetCounter(seccount);
RTC_WaitForLastTask();
return 0;
}
u8 RTC_Alarm_Set(u16 syear,u8 smon,u8 sday,u8 hour,u8 min,u8 sec)
{
u16 t;
u32 seccount=0;
if(syear<1970||syear>2099)return 1;
for(t=1970;t<syear;t++)
{
if(Is_Leap_Year(t))seccount+=31622400;
else seccount+=31536000;
}
smon-=1;
for(t=0;t<smon;t++)
{
seccount+=(u32)mon_table[t]*86400;
if(Is_Leap_Year(syear)&&t==1)seccount+=86400;
}
seccount+=(u32)(sday-1)*86400;
seccount+=(u32)hour*3600;
seccount+=(u32)min*60;
seccount+=sec;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
PWR_BackupAccessCmd(ENABLE);
RTC_SetAlarm(seccount);
RTC_WaitForLastTask();
return 0;
}
u8 RTC_Get(void)
{
static u16 daycnt=0;
u32 timecount=0;
u32 temp=0;
u16 temp1=0;
timecount=RTC_GetCounter();
temp=timecount/86400;
if(daycnt!=temp)
{
daycnt=temp;
temp1=1970;
while(temp>=365)
{
if(Is_Leap_Year(temp1))
{
if(temp>=366)temp-=366;
else {temp1++;break;}
}
else temp-=365;
temp1++;
}
calendar.w_year=temp1;
temp1=0;
while(temp>=28)
{
if(Is_Leap_Year(calendar.w_year)&&temp1==1)
{
if(temp>=29)temp-=29;
else break;
}
else
{
if(temp>=mon_table[temp1])temp-=mon_table[temp1];
else break;
}
temp1++;
}
calendar.w_month=temp1+1;
calendar.w_date=temp+1;
}
temp=timecount%86400;
calendar.hour=temp/3600;
calendar.min=(temp%3600)/60;
calendar.sec=(temp%3600)%60;
calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);
return 0;
}
u8 RTC_Get_Week(u16 year,u8 month,u8 day)
{
u16 temp2;
u8 yearH,yearL;
yearH=year/100; yearL=year%100;
if (yearH>19)yearL+=100;
temp2=yearL+yearL/4;
temp2=temp2%7;
temp2=temp2+day+table_week[month-1];
if (yearL%4==0&&month<3)temp2--;
return(temp2%7);
}
dht11.c 温湿度传感器文件
#include "dht11.h"
#include "delay.h"
void DHT11_Rst(void)
{
DHT11_IO_OUT();
DHT11_DQ_OUT=0;
delay_ms(20);
DHT11_DQ_OUT=1;
delay_us(30);
}
u8 DHT11_Check(void)
{
u8 retry=0;
DHT11_IO_IN();
while (DHT11_DQ_IN&&retry<100)
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
else retry=0;
while (!DHT11_DQ_IN&&retry<100)
{
retry++;
delay_us(1);
};
if(retry>=100)return 1;
return 0;
}
u8 DHT11_Read_Bit(void)
{
u8 retry=0;
while(DHT11_DQ_IN&&retry<100)
{
retry++;
delay_us(1);
}
retry=0;
while(!DHT11_DQ_IN&&retry<100)
{
retry++;
delay_us(1);
}
delay_us(40);
if(DHT11_DQ_IN)return 1;
else return 0;
}
u8 DHT11_Read_Byte(void)
{
u8 i,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}
}else return 1;
return 0;
}
u8 DHT11_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_SetBits(GPIOG,GPIO_Pin_11);
DHT11_Rst();
return DHT11_Check();
}
adc.h 数模转换 心率传感器文件
#ifndef __ADC_H
#define __ADC_H
#include "sys.h"
void Adc_Init(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch,u8 times);
u16 OLED_Get_Pules(void);
#endif
adc.c 数模转换 心率传感器文件
#include "adc.h"
#include "delay.h"
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE );
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Get_Adc(u8 ch)
{
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 );
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
return ADC_GetConversionValue(ADC1);
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
|