基于STM32FIC8T6单片机的数字钟实物图如下:
实测48小时没有误差:
其余的子程序都在压缩包里,包括闹钟,温度检测,时间调整。
单片机源程序如下:
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "time.h"
- #include "temp.h"
- #include "key.h"
- u8 j=0;//j用来显示星期数组变量
- u16 year=2017;
- u8 month=6;
- u8 day=5;
- u16 Tabyear[4]={0,0,0,0};
- u8 Tabmonth[2]={0,0};
- u8 Tabday[2]={0,0};
- u8 sec=0;
- u8 min=29;
- u8 hou=15;
- u8 Tabsec[2]={0,0};
- u8 Tabmin[2]={0,0};
- u8 Tabhou[2]={0,0};
- u8 week[21]="SunMonTueWedThuFriSat";
- u8 TP[3]={0,0,0};
- void Display_Date()
- {
- Tabyear[0]=year/1000;
- Tabyear[1]=year%1000/100;
- Tabyear[2]=year%1000%100/10;
- Tabyear[3]=year%10;
-
- Tabmonth[0]=month/10;
- Tabmonth[1]=month%10;
-
- Tabday[0]=day/10;
- Tabday[1]=day%10;
-
- LCD1602_Display_Date(1,0,0x30+Tabyear[0]);
- LCD1602_Display_Date(2,0,0x30+Tabyear[1]);
- LCD1602_Display_Date(3,0,0x30+Tabyear[2]);
- LCD1602_Display_Date(4,0,0x30+Tabyear[3]);
- LCD1602_Display_Date(5,0,45);
- LCD1602_Display_Date(6,0,0x30+Tabmonth[0]);
- LCD1602_Display_Date(7,0,0x30+Tabmonth[1]);
- LCD1602_Display_Date(8,0,45);
- LCD1602_Display_Date(9,0,0x30+Tabday[0]);
- LCD1602_Display_Date(10,0,0x30+Tabday[1]);
- LCD1602_Display_Table(12,0,week[j]);
- LCD1602_Display_Table(13,0,week[j+1]);
- LCD1602_Display_Table(14,0,week[j+2]);
- }
- void Display_Time()
- {
- if(sec==60)
- {
- sec=0;
- min++;
- if(min==60)
- {
- min=0;
- hou++;
- if(hou==24)
- {
- hou=0;
- min=0;
- sec=0;
- j=j+3;//j用来显示星期数组变量,一天时间到,星期加一
- if(j==21)
- {
- j=0;
- }
- day++;
- if((month==2)&&(day>29))
- {
- month++;
- day=1;
- }
- if(((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12))&&(day>31))
- {
- month++;
- day=1;
- if(month>12)
- {
- year++;
- month=1;
- }
- }
- if(((month==4)||(month==6)||(month==9)||(month==11))&&(day>30))
- {
- month++;
- day=1;
- if(month>12)
- {
- year++;
- month=1;
- }
- }
- }
- }
- }
-
- Tabsec[0]=sec/10;
- Tabsec[1]=sec%10;
- Tabmin[0]=min/10;
- Tabmin[1]=min%10;
- Tabhou[0]=hou/10;
- Tabhou[1]=hou%10;
-
- LCD1602_Display_Date(1,1,0x30+Tabhou[0]);
- LCD1602_Display_Date(2,1,0x30+Tabhou[1]);
- LCD1602_Display_Date(3,1,58);
- LCD1602_Display_Date(4,1,0x30+Tabmin[0]);
- LCD1602_Display_Date(5,1,0x30+Tabmin[1]);
- LCD1602_Display_Date(6,1,58);
- LCD1602_Display_Date(7,1,0x30+Tabsec[0]);
- LCD1602_Display_Date(8,1,0x30+Tabsec[1]);
- }
- void Display_Temp(short temp)
- {
- if(temp<0)
- {
- LCD1602_Display_Date(9,1,58);
- TP[0]=temp/100;
- TP[1]=temp%100/10;
- TP[2]=temp%10;
- LCD1602_Display_Date(10,1,0x30+TP[0]);
- LCD1602_Display_Date(11,1,0x30+TP[1]);
- LCD1602_Display_Date(12,1,46);
- LCD1602_Display_Date(13,1,0x30+TP[2]);
- LCD1602_Display_Date(14,1,42);
- LCD1602_Display_Date(15,1,67);
- }
- else
- {
- TP[0]=temp/100;
- TP[1]=temp%100/10;
- TP[2]=temp%10;
- LCD1602_Display_Date(10,1,0x30+TP[0]);
- LCD1602_Display_Date(11,1,0x30+TP[1]);
- LCD1602_Display_Date(12,1,46);
- LCD1602_Display_Date(13,1,0x30+TP[2]);
- LCD1602_Display_Date(14,1,42);
- LCD1602_Display_Date(15,1,67);
- }
- }
- int main(void)
- {
- short temperature;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
sorce code.7z
(187.69 KB, 下载次数: 58)
|