之前学校比赛,于是在网上收集资料,于是做了一个OLED显示的万年历
可通过按键调节时间,需要四个按键
单片机源程序如下:
- #include <REGX52.H>
- #include "LCD1602.h"
- #include "DS1302.h"
- #include "Key.h"
- #include "Timer0.h"
- #include "oled.h"
- #include "bmp.h"
- #include "Delay.h"
- #include "Buzzer.h"
- unsigned char KeyNum,MODE,TimeSetSelect,TimeSetFlashFlag;
- void TimeShow(void)//时间显示功能
- {
- DS1302_ReadTime();//读取时间
- OLED_ShowNum(0,4,DS1302_Time[0],2,16);
- OLED_ShowNum(38,4,DS1302_Time[1],2,16);
- OLED_ShowNum(73,4,DS1302_Time[2],2,16);
- OLED_ShowNum(0,6,DS1302_Time[3],2,16);
- OLED_ShowNum(38,6,DS1302_Time[4],2,16);
- OLED_ShowNum(73,6,DS1302_Time[5],2,16);
- }
- void TimeSet(void)//时间设置功能
- {
- Buzzer_Time();
- if(KeyNum==2)//按键2按下
- {
- TimeSetSelect++;//设置选择位加1
- TimeSetSelect%=6;//越界清零
- }
- if(KeyNum==3)//按键3按下
- {
- Buzzer_Time();
- DS1302_Time[TimeSetSelect]++;//时间设置位数值加1
- if(DS1302_Time[0]>99){DS1302_Time[0]=0;}//年越界判断
- if(DS1302_Time[1]>12){DS1302_Time[1]=1;}//月越界判断
- if( DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 ||
- DS1302_Time[1]==8 || DS1302_Time[1]==10 || DS1302_Time[1]==12)//日越界判断
- {
- if(DS1302_Time[2]>31){DS1302_Time[2]=1;}//大月
- }
- else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
- {
- if(DS1302_Time[2]>30){DS1302_Time[2]=1;}//小月
- }
- else if(DS1302_Time[1]==2)
- {
- if(DS1302_Time[0]%4==0)
- {
- if(DS1302_Time[2]>29){DS1302_Time[2]=1;}//闰年2月
- }
- else
- {
- if(DS1302_Time[2]>28){DS1302_Time[2]=1;}//平年2月
- }
- }
- if(DS1302_Time[3]>23){DS1302_Time[3]=0;}//时越界判断
- if(DS1302_Time[4]>59){DS1302_Time[4]=0;}//分越界判断
- if(DS1302_Time[5]>59){DS1302_Time[5]=0;}//秒越界判断
- }
- if(KeyNum==4)//按键3按下
- {
- Buzzer_Time();
- DS1302_Time[TimeSetSelect]--;//时间设置位数值减1
- if(DS1302_Time[0]<0){DS1302_Time[0]=99;}//年越界判断
- if(DS1302_Time[1]<1){DS1302_Time[1]=12;}//月越界判断
- if( DS1302_Time[1]==1 || DS1302_Time[1]==3 || DS1302_Time[1]==5 || DS1302_Time[1]==7 ||
- DS1302_Time[1]==8 || DS1302_Time[1]==10 || DS1302_Time[1]==12)//日越界判断
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=31;}//大月
- if(DS1302_Time[2]>31){DS1302_Time[2]=1;}
- }
- else if(DS1302_Time[1]==4 || DS1302_Time[1]==6 || DS1302_Time[1]==9 || DS1302_Time[1]==11)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=30;}//小月
- if(DS1302_Time[2]>30){DS1302_Time[2]=1;}
- }
- else if(DS1302_Time[1]==2)
- {
- if(DS1302_Time[0]%4==0)
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=29;}//闰年2月
- if(DS1302_Time[2]>29){DS1302_Time[2]=1;}
- }
- else
- {
- if(DS1302_Time[2]<1){DS1302_Time[2]=28;}//平年2月
- if(DS1302_Time[2]>28){DS1302_Time[2]=1;}
- }
- }
- if(DS1302_Time[3]<0){DS1302_Time[3]=23;}//时越界判断
- if(DS1302_Time[4]<0){DS1302_Time[4]=59;}//分越界判断
- if(DS1302_Time[5]<0){DS1302_Time[5]=59;}//秒越界判断
- }
- //更新显示,根据TimeSetSelect和TimeSetFlashFlag判断可完成闪烁功能
- if(TimeSetSelect==0 && TimeSetFlashFlag==1){OLED_ShowString(0,4," ",16);}
- else {OLED_ShowNum(0,4,DS1302_Time[0],2,16);}
- if(TimeSetSelect==1 && TimeSetFlashFlag==1){OLED_ShowString(38,4," ",16);}
- else {OLED_ShowNum(38,4,DS1302_Time[1],2,16);}
- if(TimeSetSelect==2 && TimeSetFlashFlag==1){OLED_ShowString(73,4," ",16);}
- else {OLED_ShowNum(73,4,DS1302_Time[2],2,16);}
- if(TimeSetSelect==3 && TimeSetFlashFlag==1){OLED_ShowString(0,6," ",16);}
- else {OLED_ShowNum(0,6,DS1302_Time[3],2,16);}
- if(TimeSetSelect==4 && TimeSetFlashFlag==1){OLED_ShowString(38,6," ",16);}
- else {OLED_ShowNum(38,6,DS1302_Time[4],2,16);}
- if(TimeSetSelect==5 && TimeSetFlashFlag==1){OLED_ShowString(73,6," ",16);}
- else {OLED_ShowNum(73,6,DS1302_Time[5],2,16);}
- }
- void main()
- {
-
- OLED_Init();//
- OLED_ColorTurn(0);//
- OLED_DisplayTurn(0);//
- // DS1302_Init();
- Timer0Init();
-
-
- DS1302_SetTime();//设置时间
- OLED_DrawBMP(0,0,128,64,BMP1);
-
- Delay(200);
- OLED_Clear();
- Delay(20);
- OLED_ShowString(0,1,"Time is money",16);
- // OLED_ShowString(0,4, " - - ",16);
- // OLED_ShowString(0,6, " - - ",16);
- OLED_ShowChinese(18,4,0,16);//?D
- OLED_ShowChinese(55,4,1,16);//?°
- OLED_ShowChinese(92,4,2,16);//?°
- OLED_ShowChinese(18,6,3,16);//μ?
- OLED_ShowChinese(55,6,4,16);//×ó
- OLED_ShowChinese(92,6,5,16);//??
- while(1)
- {
- KeyNum=Key();//读取键码
- if(KeyNum==1)//按键1按下
- {
- Buzzer_Time();
- if(MODE==0){MODE=1;TimeSetSelect=0;}//功能切换
- else if(MODE==1){MODE=0;DS1302_SetTime();}
- }
- switch(MODE)//根据不同的功能执行不同的函数
- {
- case 0:TimeShow();break;
- case 1:TimeSet();break;
- }
- }
-
-
- }
- void Timer0_Routine() interrupt 1
- {
- static unsigned int T0Count;
- TL0 = 0x66; //设置定时初值
- TH0 = 0xFC; //设置定时初值
- T0Count++;
- if(T0Count>=20)//每20ms进入一次
- {
- T0Count=0;
- TimeSetFlashFlag=!TimeSetFlashFlag;//闪烁标志位取反
- }
- }
复制代码
上图所有代码51hei附件下载:
DS1302可调时钟(OLED) (测试).zip
(99.36 KB, 下载次数: 116)
|