OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4210|回复: 1

简单时钟管理程序源代码——ALIENTEK MINISTM32 实验11 RTC实时时钟实验学习扩展

[复制链接]

2

主题

7

帖子

0

精华

初级会员

Rank: 2

积分
89
金钱
89
注册时间
2013-10-28
在线时间
11 小时
发表于 2014-11-3 18:07:34 | 显示全部楼层 |阅读模式

简单时钟管理程序源代码——ALIENTEK MINISTM32 实验11 RTC实时时钟实验学习扩展

  搂主在学习正点原子版主的RTC范例程(ALIENTEK MINISTM32 实验11 RTC实时时钟实验)时,在理解其原理后,顺手添了一点代码,将该范例扩展为一个简单实用的时钟管理程序,现把代码贴出来分享给和我一样正在学习STM32编程的朋友,希望对大家有帮助,共同进步。

   本程序在MDK3.3上面编译的,编译之前需要在原来的范例(实验11)的驱动文件夹(HARDWARE)里面增加遥控器驱动(REMOTE)文件夹,从遥控器实例中拷贝过来即可。

 
  
开发板内部时间设置程序简单使用说明:

1、使用遥控器上下左右4个键来控制时间的显示和调整;

2、左右控制模式切换,可切换的模式有显示时间、修改年、修改月,修改日、修改小时、修改分钟、修改秒,总共7种模式;

3、在各时间单位的修改模式下,对应的时间单位闪烁,例如修改年份时,2014闪烁;

4、使用遥控器的上下按钮分别用来增加和减少修改各时间的数值。


修改后的主程序代码如下:

#include "sys.h"
#include "usart.h"       
#include "delay.h"   
#include "led.h"
#include "key.h"
#include "exti.h"
#include "wdg.h"
#include "timer.h"
#include "lcd.h"     
#include "rtc.h"
#include "remote.h"  

 //Mini STM32开发板范例代码11
 //RTC实时时钟 实验
 //正点原子@ALIENTEK
 //技术论坛:www.openedv.com    

const u8 *COMPILED_DATE=__DATE__;//获得编译日期
const u8 *COMPILED_TIME=__TIME__;//获得编译时间

const u8 *Week[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
const u8 *ModeName[7]={"Display Datetime","SetYear","SetMonth","SetDate","SetHour","SetMinite","SetSecond"};

int main(void)
{      
     u8 t=0;
     u8 mode=0;//置初始状态 mode=0为显示模式,mode=1为设置年模式, ... mode=7为设置秒模式  
     u8 disp=0;//disp=0 不显示  disp=1 显示 用于闪烁
     u8 remotekey;//遥控器键值    
     Stm32_Clock_Init(9);//系统时钟设置
     delay_init(72);      //延时初始化
     uart_init(72,9600); //串口1初始化   
     LED_Init();              
     LCD_Init();   
     RTC_Init();     
     Remote_Init();
     POINT_COLOR=RED;//设置字体为红色 
     LCD_ShowString(60,50,"Mini STM32");    
     LCD_ShowString(60,70,"RTC TEST");  
     LCD_ShowString(60,90,"ATOM@ALIENTEK");
     LCD_ShowString(60,110,ModeName[0]);    
     //显示时间
     POINT_COLOR=BLUE;//设置字体为蓝色
     LCD_ShowString(60,130,"    -  -  ");      
     LCD_ShowString(60,162,"  :  :  ");         
     remotekey=0;
     mode=1;
     while(1)
     {   
        if(t!=timer.sec)
        {
            t=timer.sec;
            remotekey=0;
            if(Remote_Rdy)
            {
               remotekey=Remote_Process();
               LCD_ShowString(60,110,"            ");
               switch(remotekey)//乒乓开关
               {
                   case key_last://模式增量切换
                      if (mode>1)
                      {
                          mode--;
                      }
                      else
                      {
                          mode=7;
                      };
                      //清空状态显示
                      LCD_ShowString(60,110,"                    ");   
                      LCD_ShowString(60,110,ModeName[mode-1]);  
                      break;
                   case key_next://模式减量切换
                      if (mode==7)
                      {   mode=1;
                      }
                      else
                      {
                          mode++;
                      } ;
                      LCD_ShowString(60,110,"                    ");   
                      LCD_ShowString(60,110,ModeName[mode-1]);                       
                      break;
                   case key_volpp://音量++
                      //根据mode决定增加对应时间单位的值
                      switch(mode)
                      {
                          case 2:
                              RTC_Set(timer.w_year+1,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec);  
                             break;
                          case 3:
                              RTC_Set(timer.w_year,timer.w_month+1,timer.w_date,timer.hour,timer.min,timer.sec);  
                             break;
                          case 4:
                              RTC_Set(timer.w_year,timer.w_month,timer.w_date+1,timer.hour,timer.min,timer.sec);  
                             break;
                          case 5:
                              RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour+1,timer.min,timer.sec);  
                             break;
                          case 6:
                              RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min+1,timer.sec);  
                             break;
                          case 7:
                              RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec+1);  
                             break;
                      };
                      break;
                   case key_volcc://音量--
                      //根据mode决定减少对应的时间单位值  
                      switch(mode)
                      {
                          case 2:
                              RTC_Set(timer.w_year-1,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec);  
                             break;
                          case 3:
                             if (timer.w_month>1)
                                 RTC_Set(timer.w_year,timer.w_month-1,timer.w_date,timer.hour,timer.min,timer.sec);  
                             break;
                          case 4:
                             if (timer.w_date>1)
                                 RTC_Set(timer.w_year,timer.w_month,timer.w_date-1,timer.hour,timer.min,timer.sec);  
                             break;
                          case 5:
                             if (timer.hour>0)
                                 RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour-1,timer.min,timer.sec);  
                             break;
                          case 6:
                             if (timer.min>1)
                                 RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min-1,timer.sec);  
                             break;
                          case 7:
                             if (timer.sec>1)
                                 RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec-1);  
                             break;
                      };
                      break;
                   default:
                      break;
               }
            }
            //else delay_ms(2)
            ;
            switch(mode)
            {
               case 1://显示状态
                   LCD_ShowNum(60,130,timer.w_year,4,16);
                   LCD_ShowNum(100,130,timer.w_month,2,16);
                   LCD_ShowNum(124,130,timer.w_date,2,16);   
                   LCD_ShowNum(60,162,timer.hour,2,16);                                 
                   LCD_ShowNum(84,162,timer.min,2,16);                                  
                   LCD_ShowNum(108,162,timer.sec,2,16);                  
                   //timer.week
                   LCD_ShowString(60,148,Week[timer.week]);
                   break;
               case 2://设置年
                   LCD_ShowNum(100,130,timer.w_month,2,16);
                   LCD_ShowNum(124,130,timer.w_date,2,16);   
                   LCD_ShowNum(60,162,timer.hour,2,16);                                 
                   LCD_ShowNum(84,162,timer.min,2,16);                                  
                   LCD_ShowNum(108,162,timer.sec,2,16);                  
                   if (disp==1)
                   {
                      LCD_ShowString(60,130,"    ");
                      disp=0;
                   }
                   else
                   {   
                      LCD_ShowNum(60,130,timer.w_year,4,16);
                      disp=1;
                   };
                   break;  
               case 3://设置月
                   LCD_ShowNum(60,130,timer.w_year,4,16);
                   LCD_ShowNum(124,130,timer.w_date,2,16);   
                   LCD_ShowNum(60,162,timer.hour,2,16);                                 
                   LCD_ShowNum(84,162,timer.min,2,16);                                  
                   LCD_ShowNum(108,162,timer.sec,2,16);                  
                   if (disp==1)
                   {
                      LCD_ShowString(100,130,"  ");
                      disp=0;
                   }
                   else
                   {   
                      LCD_ShowNum(100,130,timer.w_month,2,16);
                      disp=1;
                   };                   
                   break;
               case 4://设置日
                   LCD_ShowNum(60,130,timer.w_year,4,16);
                   LCD_ShowNum(100,130,timer.w_month,2,16);
                   LCD_ShowNum(60,162,timer.hour,2,16);                                 
                   LCD_ShowNum(84,162,timer.min,2,16);                                  
                   LCD_ShowNum(108,162,timer.sec,2,16);                  
                   if (disp==1)
                   {
                      LCD_ShowString(124,130,"  ");
                      disp=0;
                   }
                   else
                   {   
                      LCD_ShowNum(124,130,timer.w_date,2,16);    
                      disp=1;
                   };                   
                   break;
               case 5://设置时
                   LCD_ShowNum(60,130,timer.w_year,4,16);
                   LCD_ShowNum(100,130,timer.w_month,2,16);
                   LCD_ShowNum(124,130,timer.w_date,2,16);   
                   LCD_ShowNum(84,162,timer.min,2,16);                                  
                   LCD_ShowNum(108,162,timer.sec,2,16);                  
                   if (disp==1)
                   {
                      LCD_ShowString(60,162,"  ");
                      disp=0;
                   }
                   else
                   {   
                      LCD_ShowNum(60,162,timer.hour,2,16);                                 
                      disp=1;
                   };                   
                   break;
               case 6://设置分
                   LCD_ShowNum(60,130,timer.w_year,4,16);
                   LCD_ShowNum(100,130,timer.w_month,2,16);
                   LCD_ShowNum(124,130,timer.w_date,2,16);   
                   LCD_ShowNum(60,162,timer.hour,2,16);                                 
                    LCD_ShowNum(108,162,timer.sec,2,16);                  
                   if (disp==1)
                   {
                      LCD_ShowString(84,162,"  ");
                      disp=0;
                   }
                   else
                   {   
                      LCD_ShowNum(84,162,timer.min,2,16);                                  
                      disp=1;
                   };                   
                   break;
               case 7://设置秒
                   LCD_ShowNum(60,130,timer.w_year,4,16);
                   LCD_ShowNum(100,130,timer.w_month,2,16);
                   LCD_ShowNum(124,130,timer.w_date,2,16);   
                   LCD_ShowNum(60,162,timer.hour,2,16);                                 
                   LCD_ShowNum(84,162,timer.min,2,16);                                  
                    if (disp==1)
                   {
                      LCD_ShowString(108,162,"  ");
                      disp=0;
                   }
                   else
                   {   
                      LCD_ShowNum(108,162,timer.sec,2,16);                                 
                      disp=1;
                   };                   
                   break;
               default:
                   LCD_ShowString(60,110,"Error Mode");
                   break; 
            };
            LED0=!LED0;
        }//每秒变化一次   
        delay_ms(10);
     };  
}

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165309
金钱
165309
注册时间
2010-12-1
在线时间
2108 小时
发表于 2014-11-3 18:56:18 | 显示全部楼层
不错,谢谢分享。最好上图给大家show一下,呵呵。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2024-11-23 13:04

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表