基于stm32MINI的RTC实时闹钟,闹钟值可调,到时间屏幕闪烁
实验目的:
学习STM32自带RTC的使用
硬件资源:
1,DS0(连接在PA8)
2,串口1(波特率:9600,PA9/PA10连接在板载USB转串口芯片CH340上面)
3,ALIENTEK 2.8/3.5/4.3/7寸TFTLCD模块(通过GPIO驱动,连接关系见lcd.h)
4,RTC(实时时钟)
实验现象:
本实验通过TFTLCD显示RTC时间,并可以通过usmart设置RTC时间,从而调节时间,以便大家验证
RTC功能。
注意事项:
1,4.3寸和7寸屏需要比较大电流,USB供电可能不足,请用外部电源供电(5V电源,接VOUT2即可).
2,本例程在LCD_Init函数里面(在ILI93xx.c),用到了printf,如果不初始化串口1,将导致液晶无法显示!!
3,LCD模块在开发板的LCD接口上面,必须靠右插
3,串口波特率为9600
单片机源程序如下:
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "usmart.h"
- #include "rtc.h"
- #include "timer.h"
- #include "key.h"
- #include "24cxx.h"
- #include "myiic.h"
- #include "touch.h"
- //ALIENTEK Mini STM32开发板范例代码13
- //RTC实时时钟实验
- _calendar_obj calendar_buf; //存放设置完成时的值
- _alarm_obj alarm_buf; //存放设置完成时的值
- int main(void)
- {
- u8 t=0;
- u8 t1=0;
- u8 temp=0;
- u8 move=0; //move为移动的偏移量
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- delay_init(); //延时函数初始化
- uart_init(9600); //串口初始化为9600
- LED_Init(); //初始化与LED连接的硬件接口
- LCD_Init(); //初始化LCD
- KEY_Init();
- TIM2_Init(799,89);
- TIM3_Init(799,89);
- usmart_dev.init(72); //初始化USMART
- tp_dev.init(); //触摸屏初始
- POINT_COLOR=RED;//设置字体为红色
- while(RTC_Init()) //RTC初始化 ,一定要初始化成功
- {
- LCD_ShowString(60,130,200,16,16,"RTC ERROR! ");
- delay_ms(800);
- LCD_ShowString(60,130,200,16,16,"RTC Trying...");
- }
- //显示时间
- POINT_COLOR=BLUE;//设置字体为蓝色
- LCD_ShowString(0,10,200,24,24," Date ");
- LCD_ShowString(0,45,200,24,24," - - ");
- LCD_ShowString(0,80,200,24,24," Week ");
- LCD_ShowString(0,150,200,24,24," Time ");
- LCD_ShowString(0,185,200,24,24," : : ");
- LCD_ShowString(0,220,200,24,24," Alarm time"); //按键调整时间
- LCD_ShowString(0,255,200,24,24," : : ");
- LCD_ShowString(0,280,200,24,24," : : ");
- while(1)
- {
- t1=move;
- if(TIMChange1&(0x01<<7)) //每5ms读一次键值
- {
- TIMChange1&=(~(0x01<<7));
- tp_dev.scan(0);
- if(tp_dev.sta&TP_PRES_DOWN) //触摸屏被按下
- {
- delay_ms(100);
- if(tp_dev.x[0]<lcddev.width&&tp_dev.y[0]<lcddev.height)
- {
- if(tp_dev.x[0]>(lcddev.width-230)&&tp_dev.y[0]<290)
- Press_KEY_SUB(move); //数值减(下键)
- }
- }
- }
- if(TIMChange&(0x01<<6)) //每5ms读一次键值
- {
- TIMChange&=(~(0x01<<6));
- temp=KEY_Scan(0); //读键值,按移动键(右键)时不支持连按
- if(temp==KEY_MOVE)
- {
- if(move==12) move=1; //偏移量循环
- else move++; //移动偏移量加一
- timcnt=0; //移动一次就把闪烁计数值复位
- }
- if(move) //处于时间设置模式中
- {
- Press_KEY_MOVE(move); //调用子程序,进行闪烁和移动处理
- RTC_ITConfig(RTC_IT_SEC, DISABLE); // 关闭RTC 秒中断
- }
- if(temp==KEY_ADD)Press_KEY_ADD(move); //按数值加键(上键),数值加一,不支持连按
- //if(temp==KEY_SUB)Press_KEY_SUB(move); //数值减(下键),支持连按
- if(temp==KEY_SURE&&move) //确定键(左键)
- {
- move=0; //move复位
- calendar_buf=calendar; //把设置完的值放入缓冲区
- alarm_buf=alarm; //把设置完的值放入缓冲区
- RTC_ITConfig(RTC_IT_SEC, ENABLE); //使能 RTC 秒中断
- delay_ms(1); //关闭秒中断时,RTC还是在走的,开启秒中断后要延时一段时间,把旧值覆盖掉
- calendar=calendar_buf; //用设置后的值覆盖旧值
- alarm=alarm_buf; //用设置后的值覆盖旧值
- RTC_Set(calendar.year,calendar.month,calendar.date,calendar.hour,calendar.min,calendar.sec);//写入新值
- Set_Alarm(calendar.year,calendar.month,calendar.date,alarm.hour1,alarm.min1,alarm.sec1,alarm.hour2,alarm.min2,alarm.sec2);//写入新值
- }
- if((t!=calendar.sec&&move==0)||t1!=move) //秒更新或者按下移位键,显示时间
- {
- t=calendar.sec;
- POINT_COLOR=BLUE;
- LCD_ShowNum(55,45,calendar.year,4,24);
- LCD_ShowNum(125,45,calendar.month,2,24);
- LCD_ShowNum(180,45,calendar.date,2,24);
- switch(calendar.week)
- {
- case 0:
- LCD_ShowString(60,115,200,24,24,"Sunday ");
- break;
- case 1:
- LCD_ShowString(60,115,200,24,24,"Monday ");
- break;
- case 2:
- LCD_ShowString(60,115,200,24,24,"Tuesday ");
- break;
- case 3:
- LCD_ShowString(60,115,200,24,24,"Wednesday");
- break;
- case 4:
- LCD_ShowString(60,115,200,24,24,"Thursday ");
- break;
- case 5:
- LCD_ShowString(60,115,200,24,24,"Friday ");
- break;
- case 6:
- LCD_ShowString(60,115,200,24,24,"Saturday ");
- break;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
RTC实时实验 - 按键 2个闹钟 触屏.rar
(5.17 MB, 下载次数: 349)
|