标题:
单片机数码管小程序:2组数码管分别显示加一和减一
[打印本页]
作者:
腾飞的龙
时间:
2016-1-2 08:53
标题:
单片机数码管小程序:2组数码管分别显示加一和减一
/******************************************************
功能 :二组2位数码管分别显示1秒加一和减一
单片机:STC12C5A60S2
晶振 :11.0582M
作者 :苏义江
时间 :2016-1-2
注释 :在大数码管实验板实验通过,用定时器0定时
****************************************************/
#include <reg52.h>
//#include <intrins.h>
sbit wei= P0^6; //
sbit duan = P0^7; //
#define uchar unsigned char
#define uint unsigned int
uchar code table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xBF};//共阳LED段码表
uchar code table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};//位码
uchar t,a,b,c,d,shi=90,ge=0;//定义变量
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=610;y>0;y--);
}
void init()//定时器0初始化
{
TMOD=0X01;
TH0=0X4C;//11.0592M 50ms初值
TL0=0X00;
ET0=1;
TR0=1;
EA=1;
t=0;//计数初始为0
}
void display()//显示函数
{
a=ge/10;
b=ge%10;
c=shi/10;
d=shi%10;
wei=1;
P2=0xfe;
wei=0;
duan=1;
P2=table1[a];
duan=0;
delay(5);
P2=0xff;
wei=1;
P2=0xfd;
wei=0;
duan=1;
P2=table1[b];
duan=0;
delay(5);
P2=0xff;
wei=1;
P2=0xfb;
wei=0;
duan=1;
P2=table1[c];
duan=0;
delay(5);
P2=0xff;
wei=1;
P2=0xf7;
wei=0;
duan=1;
P2=table1[d];
duan=0;
delay(5);
P2=0xff;
}
void main()
{
init();
while(1)
{
display();
}
}
void t0() interrupt 1//定时器0中断服务函数
{
TH0=0X4C;
TL0=0X00;
t++;
if(t==20)//1秒到
{
t=0;//计数清零
ge++;
shi--;
if(ge==90)
ge=0;
if(shi==0)
shi=90;
}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1