小白自备笔记···
····
数码管1S左移,6位数码管从999999每一秒钟减一倒着计数,点阵9-0倒计时显示。
#include //头文件
#include //库函数头文件
#define uint unsigned int //宏定义
#define uchar unsigned char
#define ulong unsigned long
uchar flag=0; //定义变量
static uint index=0;
sbit LED=P0^0;
sbit ADDR0=P1^0; //位声明
sbit ADDR1=P1^1;
sbit ADDR2=P1^2;
sbit ADDR3=P1^3;
sbit ENLED=P1^4;
uchar code ledchar[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E}; //数码管真值表
uchar ledbuff[7]={0xff,0xff,0xff,0xff,0xff,0xff,0xff};
uchar code image[10][8]={0xFF,0xC3,0xDB,0xDB,0xC3,0xDF,0xC3,0xFF, //9
0xFF,0xC3,0xDB,0xC3,0xDB,0xDB,0xC3,0xFF,
0xFF,0xC3,0xDF,0xEF,0xF7,0xF7,0xF7,0xFF,
0xFF,0xC3,0xFB,0xC3,0xDB,0xDB,0xC3,0xFF,
0xFF,0xC3,0xFB,0xC3,0xDF,0xDF,0xC3,0xFF,
0xEF,0xE7,0xEB,0xED,0x81,0xEF,0xEF,0xFF,
0xFF,0xC3,0xDF,0xC3,0xDF,0xDF,0xC3,0xFF,
0xFF,0xC3,0xDF,0xDF,0xC3,0xFB,0xC3,0xFF,
0xFF,0xF3,0xF7,0xF7,0xF7,0xF7,0xE3,0xFF,
0xFF,0xE7,0xDB,0xDB,0xDB,0xDB,0xE7,0xFF};
void main()
{
static uchar j=0;
ulong sec=999999;
EA=1; //使能总中断
ENLED=0; //74HC138的4、5脚低电平有效
TMOD=0x10;
TH1=0xFC;
TL1=0x67;//定时一毫秒
ET1=1;
TR1=1;
while(1)
{
if(flag==1) //判断一秒定时标志
{
flag=0; //S秒定时清0标志
sec--; //秒定时加1
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];
ledbuff[6]=~(0x01<<j);
j++;
if(j>=8)
{
j=0;
}
index++;
if(index>=10)
{
index=0;
}
}
}
}
void interruptT0() interrupt 3
{
static uint cnt=0;
static uchar i=0;
TH1=0xFC;
TL1=0x67;
switch(i)
{
case 0 : ADDR3=1; ADDR2=0;ADDR1=0;ADDR0=0;i++;P0=ledbuff[0];break; //数码管刷新
case 1 :ADDR3=1; ADDR2=0;ADDR1=0;ADDR0=1;i++;P0=ledbuff[1];break;
case 2 :ADDR3=1; ADDR2=0;ADDR1=1;ADDR0=0;i++;P0=ledbuff[2];break;
case 3 :ADDR3=1; ADDR2=0;ADDR1=1;ADDR0=1;i++;P0=ledbuff[3];break;
case 4 :ADDR3=1; ADDR2=1;ADDR1=0;ADDR0=0;i++;P0=ledbuff[4];break;
case 5 :ADDR3=1; ADDR2=1;ADDR1=0;ADDR0=1;i++;P0=ledbuff[5];break;
case 6:ADDR3=1; ADDR2=1;ADDR1=1;ADDR0=0;i++;P0=ledbuff[6];break; //led流水刷新
case 7 : ADDR3=0; ADDR2=0;ADDR1=0;ADDR0=0;i++;P0=image[index][0];break; //点阵刷新
case 8 :ADDR3=0; ADDR2=0;ADDR1=0;ADDR0=1;i++;P0=image[index][1];break;
case 9 :ADDR3=0; ADDR2=0;ADDR1=1;ADDR0=0;i++;P0=image[index][2];break;
case 10 :ADDR3=0; ADDR2=0;ADDR1=1;ADDR0=1;i++;P0=image[index][3];break;
case 11 :ADDR3=0; ADDR2=1;ADDR1=0;ADDR0=0;i++;P0=image[index][4];break;
case 12 :ADDR3=0; ADDR2=1;ADDR1=0;ADDR0=1;i++;P0=image[index][5];break;
case 13:ADDR3=0; ADDR2=1;ADDR1=1;ADDR0=0;i++;P0=image[index][6];break;
case 14:ADDR3=0; ADDR2=1;ADDR1=1;ADDR0=1;i=0;P0=image[index][7];break;
default:break;
}
cnt++;
if(cnt>=1000)
{
cnt=0;
flag=1;
}
}