您的位置 首页 资料

STC89C52-秒表(准确10ms)一起LED二进制计数

includereg52h>typedefunsignedcharuint8;typedefunsignedintuint16;typedefunsignedlonguint32;*数码管0到F

#include
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsignedlong uint32;
/*数码管0到F界说 */
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xa7,0xa1,0x86,0x8e};
uint8 a[6];
uint16 counter1 = 0;
uint8 counter = 0;
sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
/*守时器1初始化,并守时1ms*/
void timer1_init()
{
TMOD |= 0x10;
TMOD &= 0xdf;
TH1 = 0xFC;
TL1 = 0x67;
TR1 = 1;
}
/*中止初始化*/
void int_init()
{
ET1 = 1;
EA = 1;
}
/*改写数码管*/
void refresh_led()
{
static uint8 j = 0;
uint8 b = number[a[2]] – 0x80 ;

switch(j)
{
case 0: ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;P0 = number[a[0]];break;
case 1: ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;P0 = number[a[1]];break;
case 2: ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;P0 = b;break;
case 3: ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;P0 = number[a[3]];break;
case 4: ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;P0 = number[a[4]];break;
case 5: ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;P0 = number[a[5]];break;
default: break;
}
if(7 == j++) j = 0;
}
main()
{
ENLED = 0; ADDR3 = 1;
timer1_init();
int_init();
while(1);

}
/*中止函数*/
void interrupt_timer1() interrupt 3
{
static uint32 sec = 0;
TH1 = 0xFC;
TL1 = 0x66;
counter++;
if(counter == 10)
{
sec++;
counter = 0;
a[0] = sec%10;
a[1] = sec/10%10;
a[2] = sec/100%10;
a[3] = sec/1000%10;
a[4] = sec/10000%10;
a[5] = sec/100000%10;
if(sec%100 == 0) counter1++; //LED时刻1s
if(counter1 == 256) counter1 = 0;
}
if(counter == 9)
{ADDR0 = 0;ADDR1 = 1;ADDR2 = 1;P0 = ~counter1 ;}
refresh_led();

}

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

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

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

微信扫一扫关注我们

返回顶部