单片机源程序如下:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit aa=P2^0;
sbit bb=P2^1;
sbit cc=P2^2;//74HC138位扫描控制位
uchar cnt=0;//定时器0中断计数
uchar i=0;//位扫描索引
uint sum=0;//定时器1中断计数
uchar k;
uchar temp=0xfe;//流水灯赋初值,第1个灯点亮
unsigned long sec=765432;
uchar code Ledchar[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//数码管码表
uchar Ledbuff[]={0x00,0x00,0x00,0x00,0x00,0x00};//启动时数码管不亮
void Leddisplay();
void LedSL();
void main()
{
TMOD=0x11;
EA=1;
ET0=1;
TR0=1;
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET1=1;
TR1=1;
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
P3=temp;
while(1)
{
Leddisplay();
LedSL();
}
}
void Leddisplay()
{
if(cnt==10)//每0.1s刷新一次
{
cnt=0;
sec--;
if(sec==765398) //当sec=765398时关闭定时器0
{
TR0=0;
}
}
Ledbuff[0]=Ledchar[sec%10];
Ledbuff[1]=Ledchar[sec/10%10];
Ledbuff[2]=Ledchar[sec/100%10];
Ledbuff[3]=Ledchar[sec/1000%10];
Ledbuff[4]=Ledchar[sec/10000%10];
Ledbuff[5]=Ledchar[sec/100000%10];
}
void LedSL()//流水灯程序
{
if(sum==1000)
{
sum=0;
temp=temp<<1;
P3=temp;
}
}
void T0_time() interrupt 1//定时10ms
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
cnt++;
}
void T1_time() interrupt 3//定时1ms
{
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
sum++;
P0=0x00;
switch(i)
{
case 0 :aa=1;bb=1;cc=1;i++;P0=Ledbuff[0];break;
case 1 :aa=0;bb=1;cc=1;i++;P0=Ledbuff[1];break;
case 2 :aa=1;bb=0;cc=1;i++;P0=Ledbuff[2];break;
case 3 :aa=0;bb=0;cc=1;i++;P0=Ledbuff[3];break;
case 4 :aa=1;bb=1;cc=0;i++;P0=Ledbuff[4];break;
case 5 :aa=0;bb=1;cc=0;i=0;P0=Ledbuff[5];break;
default:break;
}
} |