RTC实时实验 - 按键+2个闹钟 +触屏
单片机源程序如下:
- #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"
- _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;
- }
- LCD_ShowNum(65,185,calendar.hour,2,24);
- LCD_ShowNum(115,185,calendar.min,2,24);
- LCD_ShowNum(170,185,calendar.sec,2,24);
- LCD_ShowNum(65,255,alarm.hour1,2,24);
- LCD_ShowNum(115,255,alarm.min1,2,24);
- LCD_ShowNum(170,255,alarm.sec1,2,24);
- LCD_ShowNum(65,280,alarm.hour2,2,24);
- LCD_ShowNum(115,280,alarm.min2,2,24);
- LCD_ShowNum(170,280,alarm.sec2,2,24);
- }
- }
- } ;
- }
复制代码
所有资料51hei提供下载:
RTC实时实验 - 按键+2个闹钟 +触屏.7z
(304.77 KB, 下载次数: 47)
|