您的位置 首页 硬件

单片机C言语电子时钟加日历显现编程

一、任务和要求设计制作一个电子表并且能够显示日历,用LED数码管直接显示,并且能够通过按键调整时间、年月日,功能为按第一个键被调整

一、 使命和要求
规划制造一个电子表而且能够显现日历,用LED数码管直接显现,而且能够经过按键调整时刻、年月日,功用为按第一个键被调整的相应调整区域开端闪耀,第二个键加一,第三个减一,第四个承认,终究一个在时分秒和年月日中心彼此切换。

二、计划证明
咱们在这里选用了C8051F单片机,它是彻底集成的混合信号体系级芯片(SoC),具有与8051兼容的高速CIP-51内核,与MCS-51指令集彻底兼容,片内集成了数据收集和控制体系中常用的模仿、数字外设及其他功用部件;内置FLASH程序存储器、内部RAM,大部分器材内部还有坐落外部数据存储器空间的RAM,即XRAM。C8051F单片机具有片内调试电路,经过4脚的JTAG接口能够进行非侵入式、全速的在体系调试。

三、 体系硬件电路规划
该体系分为两个部分,主控电路和扫描电路,其间键盘扫描和LED扫描为往后节约I/O口线,我将键盘的扫描和LED的扫描共用,在扫描LED的一起也扫描键盘,这样不光便利,而且为往后的扩展留下了相当大的地步。
3.1主控制器
单片机运用c8051f310,而且运用它的开发板易于烧录,改动程序和外围电路。
3.2显现电路
显现板由于常用,是我自己画出来交给工厂做的,选用4个两位共阳数码管,从P0口输出,8位段码别离和P0相接,经过定时器完成秒钟的加一,又由于310有推挽输出,所以不用再加外部电路对数码管进行驱动,直接将它们与P0口相连就能够
3.3键盘电路
键盘是是与数码管的位选相连,经过二极管使它们具有单向性,一起将最初终究经过三极管接单片机的P3.4,只需有按键按下,P3.4就一定会发生改变,经过这个改变在去扫描位就能够得出是哪个按键了。
附电路图:

四、体系程序规划
体系和程序首要包含:显现程序,键盘扫描程序,主动加秒,分,时,日,月,年,
附程序:

#include “c8051f310.h”
#define uchar unsigned char
#define unchar unsigned char
uchar code duan[10] ={0xa0,0xbd,0x26,0x2c,0x39,0x68,0x60,0xad,0x20,0x28};
//段码表 0 1 2 3 4 5 6 7 8 9
uchar code wei[8]={0x02,0x01,0x08,0x04,0x20,0x10,0x80,0x40};//位码表
uchar code week1[7]={0xbd,0x26,0x2c,0x39,0x68,0x60,0xad} ;//星期一到周日
uchar dataa,sec,min,hour,ymd,year,year1=9,year2=20,month=8,day=22,week=6;//初始值是7-00:00:00,日期是2009-08-22;
uchar n,cnt,sw1=0,sw2,sw3,cnt1,sw5;
uchar key1,key2,key3,key4=0,key5=0;
sbit P3_4=P3^4;
void scan_key(void);
void delay(ms);

void init(void)
{
IE =0x8A;
TCON =0x50;
TMOD =0x11;
IP =0x0A;
TH0 =(65536-1270)/256;
TL0 =(65536-1270)%256;
TH1 =(65536-500)/256;
TL1 =(65536-500)%256;
}

void time0(void) interrupt 1
{
TH0 =(65536-1270)/256;
TL0 =(65536-1270)%256;
dataa++;
if(sw1!=0&&key4==0) //第一个按键按下,且第四个没有按下
{
if(++cnt1==40)
{
cnt=0;
if(sw5==0&&sw1==1)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xf3; //闪耀程序
P0 =duan[sec%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[sec/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[hour%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[hour/10];
P1 =wei[5];
delay(8);
P1=0x00;
P0 =0x7f;
P1 =wei[6];
delay(8);
P1=0x00;
P0 =week1[week];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==0&&sw1==2)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xcf;
P0 =duan[sec%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[sec/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[min%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[min/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =0x7f;
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =week1[week];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==0&&sw1==3)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0x7f;
P0 =duan[sec%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[sec/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[min%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[min/10];
P1 =wei[3];
delay(8);
P1=0x00;
P0=duan[hour%10];
P1=wei[4];
delay(8);
P1 =0x00;
P0 =duan[hour/10];
P1 =wei[5];
delay(8);
P1 =0x00;
P0 =0x7f;
P1 =wei[6];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==9)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0x3f;
P0 =duan[month%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[month/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =duan[year1%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[year1/10];
P1 =wei[5];
delay(8);
P1 =0x00;
P0 =duan[year2%10];
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =duan[year2/10];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==8)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xf3;
P0 =duan[day%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[day/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[year1%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[year1/10];
P1 =wei[5];
delay(8);
P1 =0x00;
P0 =duan[year2%10];
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =duan[year2/10];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==7)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xc3;
P0 =duan[day%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[day/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[month%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[month/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =duan[year2%10];
P1 =wei[6];
delay(8);
P1 =0x00;
P0 =duan[year2/10];
P1 =wei[7];
delay(8);
}
scan_key();
}
if(sw5==1&&sw1==6)
{
for(n=0;n<20;n++)
{
P1 =0x00;
P1 =P1&0xc3;
P0 =duan[day%10];
P1 =wei[0];
delay(8);
P1 =0x00;
P0 =duan[day/10];
P1 =wei[1];
delay(8);
P1 =0x00;
P0 =duan[month%10];
P1 =wei[2];
delay(8);
P1 =0x00;
P0 =duan[month/10];
P1 =wei[3];
delay(8);
P1 =0x00;
P0 =duan[year1%10];
P1 =wei[4];
delay(8);
P1 =0x00;
P0 =duan[year1/10];
P1 =wei[5];
delay(8);
}
scan_key();
}
}

}
}

void time1(void) interrupt 3
{
TH1 =(65536-500)/256;
TL1 =(65536-500)%256;
if(++cnt>8) cnt=0;
if(sw5==0) //显现时分秒
{switch(cnt)
{
case 0: P1 =0x00;
P0 =duan[sec%10];
P1 =wei[cnt]; break;
case 1: P1 =0x00;
P0 =duan[sec/10];
P1 =wei[cnt];break;
case 2: P1 =0x00;
P0 =duan[min%10];
P1 =wei[cnt]; break;
case 3: P1 =0x00;
P0 =duan[min/10];
P1 =wei[cnt];
scan_key(); break;
case 4: P1 =0x00;
P0 =duan[hour%10];
P1 =wei[cnt];
scan_key();break;
case 5: P1 =0x00;
P0 =duan[hour/10];
P1 =wei[cnt];
scan_key();break;
case 6: P1 =0x00;
P0 =0x7f;
P1 =wei[cnt];
scan_key(); break;
case 7: P1 =0x00;
P0 =week1[week];
P1 =wei[cnt];
scan_key();break;
default: break;
}
}
if(sw5==1) //显现年月日
{switch(cnt)
{
case 0: P1 =0x00;
P0 =duan[day%10];
P1 =wei[cnt]; break;
case 1: P1 =0x00;
P0 =duan[day/10];
P1 =wei[cnt];break;
case 2: P1 =0x00;
P0 =duan[month%10];
P1 =wei[cnt]; break;
case 3: P1 =0x00;
P0 =duan[month/10];
P1 =wei[cnt];
scan_key(); break;
case 4: P1 =0x00;
P0 =duan[year1%10];
P1 =wei[cnt];
scan_key();break;
case 5: P1 =0x00;
P0 =duan[year1/10];
P1 =wei[cnt];
scan_key();break;
case 6: P1 =0x00;
P0 =duan[year2%10];
P1 =wei[cnt];
scan_key(); break;
case 7: P1 =0x00;
P0 =duan[year2/10];
P1 =wei[cnt];
scan_key();break;
default: break;
}
}
}

void conv(void)
{
if(dataa>=201)
{dataa=0;sec++;}
if(sec>=60)
{sec=0;min++;}
if(sw1==0&&min>=60)
{min=0;hour++;}
if(sw1==0&&hour>=24)
{
if(year%4==0)
{
if(month==2)
{
if(day==29)
{
day =1;
month++;
}
else day++;
}
else
{
if(month==4||month==6||month==9||month==11)
{
if(day==30)
{
day =1;
month++;
}
else day++;
}
else
{
if(day==31)
{
day =1;
month++;
if(month==13)
{
month =1;
year++;
}
}
else day++;
}
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/qianrushi/yingjian/258344.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部