單片機源程序如下:
#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;
}
} |