基于51单片机的中断时钟,计数器按键

文章提供了两个用C语言编写的51单片机程序,一个是按键中断计数,另一个是时间显示。第一个程序使用了T1中断来计数,第二个程序利用T0定时器每10ms更新时间并显示在LED显示器上。程序中涉及了定时器配置、中断处理和时间计算等技术。
摘要由CSDN通过智能技术生成

直接附上源代码,使用keil uVision4 软件编写。

//#include <reg52.h>

//typedef unsigned char u8;//0-255
//typedef unsigned int u16;//0-65535
//typedef unsigned char uchar;//0-255
//typedef unsigned int uint;//0-65535
//typedef unsigned long u32;//0-42

//sbit key0 = P3^0;
//sbit key1 = P3^1;
//sbit key2 = P3^2;
//sbit key3 = P3^3;

//sbit led0 = P2^0;
//sbit led1 = P2^1;
//sbit led2 = P2^2;
//sbit led3 = P2^3;

//int  flag=0;
//int count=0;
//u8 code seg[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
//                                    0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};//: 0x00


//void delayms(u16 n)//ms
//{
//    while(n--)
//    {
//        u8 c = 120;
//        while(c--);
//    }
//}

//void main()
//{
// 
//    while(1)
//    {    
//        if(key3==0)              
//        {
//            delayms(20);
//            if(key3==0)  //中断实现计数
//            {
//                 TMOD=0x09;
           TH0=0xff;
           TL0=0xfc;
//                 TH1=(65536-1000)/256;  
//            TL1=(65536-1000)%256;
//           ET1=1;   //T1中断
//          //EX0=1  //外部中断打开
//          EA=1;
//        //    IT0=1;
//          TR1=1;
//                }
//            while(!key3);
//        }
//     if(count==4)
//        {
//            P2=0x00;
//            delayms(200);
//            P2=~P2;
//            delayms(200);
//        }
//    }

//}

//void T1_int(void)  interrupt 3
//{
//  count++;  
//  EA =0;
//  
//}



  //中断时钟
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
                    0x3f,0x06,0x5b,0x4f,
                    0x66,0x6d,0x7d,0x07,
                    0x7f,0x6f,0x77,0x7c,
                    0x39,0x5e,0x79,0x71,0x40  // 0x40 为 -
                   };
uint num;
uint hour_ge,hour_shi,minute_ge, minute_shi,second_ge,second_shi;
uint Time[8];
uint now=0; //当前显示地方 s m h 
//void delay_ms(uint ms)
//{
//    uint i,j;
//    for(i=ms;i>0;i--)
//    for(j=110;j>0;j--);
//}
void display_time(uint hour_shi,uint hour_ge,uint minute_shi,uint minute_ge,uint second_shi,uint second_ge)
{
    P2=0;
    P0=table[second_ge];
    //delay_ms(1);

    
  P2=4;
    P0=table[second_shi];
     //delay_ms(1);

    P2=12;
    P0=table[minute_ge];            
    //delay_ms(1);

    P2=16;
    P0=table[minute_shi];
  //delay_ms(1);

    P2=24;
    P0=table[hour_ge];
    //delay_ms(1);
    
  P2=28;
    P0=table[hour_shi];
    //delay_ms(1);

}


void main()
{
    TMOD=0x01;
    TH0=(65536-1000)/256;   //10ms_h
    TL0=(65536-1000)%256;   //10ms_l
    //TH1=(65536-15000)/256;  //15ms刷新
    //TL1=(65536-15000)%256;

    ET0=1;
    //ET1=1;
    EA=1;
    TR0=1;
    //TR1=1;
    while(1)
    {    
        //    P2=now*4;
//            P0=table[Time[now]];
    //    display_time(hour_shi,hour_ge,minute_shi,minute_ge,second_shi,second_ge);
    }
}

void show()
{
  P2=now*4;
    P0=table[Time[now]];
    now++;
    if(now==8)
    {  now=0;}
}
void count ()
{ 
     TH0=(65536-1000)/256;
   TL0=(65536-1000)%256;
   num++;
    if(num==1000)
    {
         num=0;
        second_ge++;
    }
    if(second_ge==10)
    {
        second_ge=0;
        second_shi++;
        if(second_shi==6)
        {
            second_shi=0;
            minute_ge++;
            if(minute_ge==10)
            {
                minute_ge=0;
                minute_shi++;
                if(minute_shi==6)
                {
                    minute_shi=0;
                    hour_ge++;
                    if(hour_ge==10)
                    {
                        hour_ge=0;
                        hour_shi++;
                        if(hour_shi==2&&hour_ge==4)
                        {
                            hour_shi=0;
                            hour_ge=0;
                        }

                    }    
                }    
            }
        }
    }

    Time[0]=second_ge;
  Time[1]=second_shi;
    Time[2]=16;
    Time[3]=minute_ge;
    Time[4]=minute_shi;
    Time[5]=16;
    Time[6]=hour_ge;
    Time[7]=hour_shi;
    show();  //1ms刷新一次
}


void T0_time()interrupt 1  //T0中断计时
{
     count();    
}


//void T1_time()interrupt 3  //T1中断刷新时钟 15ms一次
//{
//    
//    TH1=(65536-15000)/256;  
//    TL1=(65536-15000)%256;
//    P2=now*4;
//    P0=table[Time[now]];
//    now++;
//    if(now==8)
//    {  now=0;}
//}



忘记注释的代码是什么用了,可能是另一种写法或者是有点错误,建议自己摸索一下!!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值