项目演示:
基于STM32的智能手表演示视频
项目实现功能:
断电时间正常走
心率采集
温湿度采集
所用模块:
STM32
DHT11温湿度传感器
心率传感器
OLED显示屏
项目简介:
基于STM32实现通过RTC读取时间显示在OLED上,并能够实现掉电不停留,温湿度传感器采集数据显示在OLED上,心率传感器通过ADC显示在OLED上。
代码:
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);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- uart_init(115200); //串口初始化为115200
- LED_Init(); //LED端口初始化
- OLED_Init(); //初始化OLED
- OLED_Clear();
- RTC_Init(); //RTC初始化
- Adc_Init(); //ADC初始化
-
- srand(calendar.sec);
- while(DHT11_Init()) //DHT11初始化,正点原子的
- {
- 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) //正点原子的 每100ms读取一次 先不管温湿度传感器了
- {
- DHT11_Read_Data(&temperature,&humidity); //读取温湿度值
- wendu = temperature - 7;
- shidu = humidity + 10;
- //printf("wendu:%d",wendu);
- //printf("shidu:%d\r\n",shidu);
- 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);
- // 研发中,用于打印时间的LOG
- // printf("w_year%d\r\n",calendar.w_year);
- // printf("w_month%d\r\n",calendar.w_month);
- // printf("w_date%d\r\n",calendar.w_date);
- // printf("hour%d\r\n",calendar.hour);
- // printf("min%d\r\n",calendar.min);
- // printf("sec%d\r\n",calendar.sec);
- //delay_ms(1000);
- }
- }
复制代码
dht11.h 温湿度传感器文件
- #ifndef __DHT11_H
- #define __DHT11_H
- #include "sys.h"
- //IO方向设置
- #define DHT11_IO_IN() {GPIOG->CRH&=0XFFFF0FFF;GPIOG->CRH|=8<<12;}
- #define DHT11_IO_OUT() {GPIOG->CRH&=0XFFFF0FFF;GPIOG->CRH|=3<<12;}
- IO操作函数
- #define DHT11_DQ_OUT PGout(11) //数据端口 PA0
- #define DHT11_DQ_IN PGin(11) //数据端口 PA0
- u8 DHT11_Init(void);//初始化DHT11
- u8 DHT11_Read_Data(u8 *temp,u8 *humi);//读取温湿度
- u8 DHT11_Read_Byte(void);//读出一个字节
- u8 DHT11_Read_Bit(void);//读出一个位
- u8 DHT11_Check(void);//检测是否存在DHT11
- void DHT11_Rst(void);//复位DHT11
- #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); //初始化RTC,返回0,失败;1,成功;
- 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; //RTC全局中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级1位,从优先级3位
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //先占优先级0位,从优先级4位
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能该通道中断
- NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
- }
- //实时时钟配置
- //初始化RTC时钟,同时检测时钟是否工作正常
- //BKP->DR1用于保存是否第一次配置的设置
- //返回0:正常
- //其他:错误代码
- u8 RTC_Init(void)
- {
- //检查是不是第一次配置时钟
- u8 temp=0;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); //使能PWR和BKP外设时钟
- PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
- if (BKP_ReadBackupRegister(BKP_DR1) != 0x5051) //从指定的后备寄存器中读出数据:读出了与写入的指定数据不相乎
- {
- BKP_DeInit(); //复位备份区域
- RCC_LSEConfig(RCC_LSE_ON); //设置外部低速晶振(LSE),使用外设低速晶振
- while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET&&temp<250) //检查指定的RCC标志位设置与否,等待低速晶振就绪
- {
- temp++;
- delay_ms(10);
- }
- if(temp>=250)return 1;//初始化时钟失败,晶振有问题
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //设置RTC时钟(RTCCLK),选择LSE作为RTC时钟
- RCC_RTCCLKCmd(ENABLE); //使能RTC时钟
- RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- RTC_WaitForSynchro(); //等待RTC寄存器同步
- RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断
- RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- RTC_EnterConfigMode();/// 允许配置
- RTC_SetPrescaler(32767); //设置RTC预分频的值
- RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- RTC_Set(2022,3,18,0,41,30); //设置时间
- RTC_ExitConfigMode(); //退出配置模式
- BKP_WriteBackupRegister(BKP_DR1, 0X5051); //向指定的后备寄存器中写入用户程序数据
- }
- else//系统继续计时
- {
- RTC_WaitForSynchro(); //等待最近一次对RTC寄存器的写操作完成
- RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能RTC秒中断
- RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- }
- RTC_NVIC_Config();//RCT中断分组设置
- RTC_Get();//更新时间
- return 0; //ok
- }
- //RTC时钟中断
- //每秒触发一次
- //extern u16 tcnt;
- 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();
- }
- //判断是否是闰年函数
- //月份 1 2 3 4 5 6 7 8 9 10 11 12
- //闰年 31 29 31 30 31 30 31 31 30 31 30 31
- //非闰年 31 28 31 30 31 30 31 31 30 31 30 31
- //输入:年份
- //输出:该年份是不是闰年.1,是.0,不是
- u8 Is_Leap_Year(u16 year)
- {
- if(year%4==0) //必须能被4整除
- {
- if(year%100==0)
- {
- if(year%400==0)return 1;//如果以00结尾,还要能被400整除
- else return 0;
- }else return 1;
- }else return 0;
- }
- //设置时钟
- //把输入的时钟转换为秒钟
- //以1970年1月1日为基准
- //1970~2099年为合法年份
- //返回值: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;//闰年2月份增加一天的秒钟数
- }
- 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和BKP外设时钟
- PWR_BackupAccessCmd(ENABLE); //使能RTC和后备寄存器访问
- RTC_SetCounter(seccount); //设置RTC计数器的值
- RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
- return 0;
- }
- //初始化闹钟
- //以1970年1月1日为基准
- //1970~2099年为合法年份
- //syear,smon,sday,hour,min,sec:闹钟的年月日时分秒
- //返回值: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;//闰年2月份增加一天的秒钟数
- }
- 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和BKP外设时钟
- PWR_BackupAccessCmd(ENABLE); //使能后备寄存器访问
- //上面三步是必须的!
-
- RTC_SetAlarm(seccount);
- RTC_WaitForLastTask(); //等待最近一次对RTC寄存器的写操作完成
-
- return 0;
- }
- //得到当前的时间
- //返回值: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; //从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)//当年是不是闰年/2月份
- {
- 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;
- }
- //获得现在是星期几
- //功能描述:输入公历日期得到星期(只允许1901-2099年)
- //输入参数:公历年月日
- //返回值:星期号
- u8 RTC_Get_Week(u16 year,u8 month,u8 day)
- {
- u16 temp2;
- u8 yearH,yearL;
-
- yearH=year/100; yearL=year%100;
- // 如果为21世纪,年份数加100
- if (yearH>19)yearL+=100;
- // 所过闰年数只算1900年之后的
- 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 温湿度传感器文件
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 数模转换 心率传感器文件
- <i>#include "adc.h"</i>
- <i> #include "delay.h"</i>
- <i> </i>
- <i>//初始化ADC</i>
- <i>//这里我们仅以规则通道为例</i>
- <i>//我们默认将开启通道0~3 </i>
- <i>void Adc_Init(void)</i>
- <i>{ </i>
- <i> ADC_InitTypeDef ADC_InitStructure; </i>
- <i> GPIO_InitTypeDef GPIO_InitStructure;</i>
- <i> RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //使能ADC1通道时钟</i>
- <i> RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M</i>
- <i> //PA1 作为模拟通道输入引脚 </i>
- <i> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;</i>
- <i> GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚</i>
- <i> GPIO_Init(GPIOA, &GPIO_InitStructure); </i>
- <i> ADC_DeInit(ADC1); //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值</i>
- <i> ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式</i>
- <i> ADC_InitStructure.ADC_ScanConvMode = DISABLE; //模数转换工作在单通道模式</i>
- <i> ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //模数转换工作在单次转换模式</i>
- <i> ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件而不是外部触发启动</i>
- <i> ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐</i>
- <i> ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目</i>
- <i> ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器 </i>
- <i> ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1</i>
- <i> </i>
- <i> ADC_ResetCalibration(ADC1); //使能复位校准 </i>
- <i> </i>
- <i> while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束</i>
- <i> </i>
- <i> ADC_StartCalibration(ADC1); //开启AD校准</i>
- <i> while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束</i>
- <i>// ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能</i>
- <i>} </i>
- <i>//获得ADC值</i>
- <i>//ch:通道值 0~3</i>
- <i>u16 Get_Adc(u8 ch) </i>
- <i>{</i>
- <i> //设置指定ADC的规则组通道,一个序列,采样时间</i>
- <i> ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC通道,采样时间为239.5周期 </i>
- <i> ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的ADC1的软件转换启动功能 </i>
- <i> </i>
- <i> while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束</i>
- <i> return ADC_GetConversionValue(ADC1); //返回最近一次ADC1规则组的转换结果</i>
- <i>}</i>
- <i>u16 Get_Adc_Average(u8 ch,u8 times)</i>
- <i>{</i>
- <i> u32 temp_val=0;</i>
- <i> u8 t;</i>
- <i> for(t=0;t<times;t++)</i>
- <i> {</i>
- <i> temp_val+=Get_Adc(ch);</i>
- <i> delay_ms(5);</i>
- <i> }</i>
- <i> return temp_val/times;</i>
- <i>} </i>
复制代码
|