51单片机数码管计数器

要求

设计一个2位10进制计数器,每秒加1

原理图

代码

方式一

#include "reg52.h"

#define THCO  0xec
#define TLCO  0x78

//共阴极数码管,0-9段码表
unsigned char code Duan[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

sbit P26=P2^6;    //数码管的位码口定义
sbit P27=P2^7;

void main()
{	
   	TMOD=0x01;		//定时器0初始化
 	TH0=THCO;
 	TL0=TLCO;
 	TR0=1;
 	ET0=1;
 	EA=1;

 	while(1)  ;
}

void timer0() interrupt 1
{
    static unsigned char Bit = 0;
    static unsigned char num = 0;	   //记录数值
    static unsigned char count = 0;    //记录中断次数
    TH0=THCO;
    TL0=TLCO;
    count++;
    Bit++;
    if (count>200)    //1s
    {
        count = 0;
        num++;
    }
    if (num >= 100) num = 0;
    /* 每5000ms刷新一次 */
    P2 |= 0xff;		//先关位码
    if (Bit>=2) Bit=0;
    switch (Bit)
    {
        case 0:
            P0 = Duan[num / 10];
            P26 = 0; 
            break;
        case 1:
            P0 = Duan[num % 10];
            P27 = 0; 
            break;
    }
}

方式二

#include "reg51.h"

#define THCO   0xee
#define TLCO   0x0
#define THC1   0xee
#define TLC1   0x0


unsigned char  code Duan[]={0x3F, 0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char  Data_Buffer[2]={0,0};

unsigned int count=0;


sbit P26=P2^6;	
sbit P27=P2^7;

void main()
{	
   	TMOD=0x11;
 	TH0=THCO;
 	TL0=TLCO;
 	TR0=1;
 	ET0=1;
	TH1=THC1;
 	TL1=TLC1;
 	TR1=1;
 	ET1=1;   
		  	

 	EA=1;

 	while(1);
}

void timer1() interrupt 3
{
	static unsigned char Bit1; 
	TH1=THC1;
 	TL1=TLC1;
    count++;	
    if(count>200)
    {
	  count=0;
	  Bit1++;
	  if(Bit1>99)Bit1 = 0;
	  Data_Buffer[0]=Bit1/10;
	  Data_Buffer[1]=Bit1%10;
    }
}

void timer0() interrupt 1
{
 static unsigned char Bit=0;

 TH0=THCO;
 TL0=TLCO;

 Bit++;		
 if(Bit>=2)Bit=0;
 P2|=0xff;
 P0=Duan[Data_Buffer[Bit]];
 switch(Bit)
 {
  case 0: P26=0;break;
  case 1: P27=0;break;
 }		
}

 

  • 10
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值