电子开发网

电子开发网电子设计 | 电子开发网Rss 2.0 会员中心 会员注册
搜索: 您现在的位置: 电子开发网 >> 电子开发 >> 单片机 >> 正文

51单片机4位数码管电子钟源程序

作者:佚名    文章来源:本站原创    点击数:    更新时间:2018-03-08

/***************************************************************************

标题:     定时器中断 24小时时钟程序

来源网站:http://www.dzkfw.com.cn/index.html

效果:     数码管显示24小时制时钟(具有调时功能)

说明:     使用12M晶振        ,key1=P3^7; key2=P3^6; key3=P3^5;key4=P3^4;

                  数码管位选P1.0~P1.3   ,段选P0

******************************************************************************/

 

#include

 

#define uchar unsigned char;

#define uint unsigned int;

                                             

 

uchar position;          

uchar tt;            

uchar second;      

uchar minute;

uchar hour;      

 

 

uchar code table[]=

                            {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//0--9

 

                 

sbit smg_q = P1^0;  //数码管千位引脚

sbit smg_b = P1^1;

sbit smg_s = P1^2;

sbit smg_g = P1^3;

 

sbit key1=P3^7;                 //按键定义

sbit key2=P3^6;

sbit key3=P3^5;

sbit key4=P3^4;

 

 

void keyscan();                   //函数声明

void display(unsigned char hour,unsignedchar minute);

void delay(unsigned int timer);

void init();                                             


/***********************************************************

                                     主函数

***********************************************************/

void main()

{

         init();                                   

         while(1)

         {

                   if(tt==20)

                   {                         

                            tt=0;

                            second++;

                            if(second==60)

                            {

                                     second=0;

                                     minute++;

                                     if(minute==60)

                                     {      

                                               minute=0;

                                               hour++;

                                               if(hour==24)

                                                        hour=0;

                                     }

                            }

                   }

 

                   keyscan();

                   display(hour,minute);     

                   delay(1);

         }

}

/***********************************************************

                                     调时

***********************************************************/

void keyscan()

{

         if(key1==0)        //小时加1

         {

                   TR0=0;

                   hour++;

                            if(hour==24)

                                     hour=0;

                   delay(200)        ;

                   TR0=1;

         }

         if(key2==0)                 // 小时减

         {

                   TR0=0;

                   hour--;

                            if(hour<0)

                                     hour=23;

                   delay(200)        ;

                   TR0=1;

         }

 

         if(key3==0)                   // 分钟加

         {

                   TR0=0;

                   minute++;

                            if(minute==60)

                                     minute=0;

                   delay(200)        ;

                   TR0=1;

         }

 

                   if(key4==0)                    //        分钟减

         {

                   TR0=0;

                   minute--;

                            if(minute<0)

                                     minute=59;

                   delay(200)        ;

                   TR0=1;

         }

}

 

/***********************************************************

                                                初始化

***********************************************************/

void init()

{

         tt=0;

         position=0;              

         second=0;

         minute=0;       

         hour=0;  

         smg_q=1;

         smg_b=1;

         smg_s=1;

         smg_g=1;

         key1=1;

         key2=1;

 

         TMOD=0X01; 

         TH0=(65536-50000)/256;       

         TL0=(65536-50000)%256;

         EA=1;               

         ET0=1;             

         TR0=1;             

}

 

/***********************************************************

                                                  定时器TO中断

***********************************************************/

void t0() interrupt 1

{

         TH0=(65536-50000)/256;       

         TL0=(65536-50000)%256;

         tt++;                                             

}

 

/***********************************************************

                                          显示

***********************************************************/

void display(unsigned char hour,unsignedchar minute)

{

         P0=0XFF;                            

         switch(position)

         {

         case0:             

                   smg_g=1;

                   smg_q=0;

                   P0=table[hour/10];

                   break;

 

         case1:

                   smg_q=1;       

                   smg_b=0;       

                   P0=table[hour%10];

 

                   if(tt>=10)

                            P0&=0x7f;                  //小数点以半秒的时间闪烁

                   break;

 

         case2:   

                   smg_b=1;                 

                   smg_s=0;                 

                   P0=table[minute/10];

                   break;

         case3:                      

                   smg_s=1;

                   smg_g=0;

                   P0=table[minute%10];

                   break;

         }

         position++;              

         if(position>3)

                   position=0;     

}

 

/***********************************************************

                                                 延时

***********************************************************/

void delay(unsigned int time)

{               

         unsignedint x,y;

         for(x=time;x>0;x--)

                   for(y=110;y>0;y--);

}
关键字:51单片机  4位数码管  电子钟

Tags:51单片机,单片机,4位数码管电子时钟,c程序  
责任编辑:admin
请文明参与讨论,禁止漫骂攻击,不要恶意评论、违禁词语。 昵称:
1分 2分 3分 4分 5分

还可以输入 200 个字
[ 查看全部 ] 网友评论
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 在线帮助 - 文章列表
返回顶部
刷新页面
下到页底
晶体管查询