刚开始学习51单片机..一步一步照着书上来..
按照书上的程序写的 按书上的应该是秒表计时的效果...可我的为什么是流水灯效果...
希望各位能帮我看看~ 万分感激~
#include<reg52.h>
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
unsigned char code LedChar[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char LedBuff[6] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
unsigned char i = 0;
unsigned int cnt = 0; //记录T0中断次数
unsigned char flag1s = 0; // 1s定时标志
void main()
{
unsigned long sec = 0;
EA = 1;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x67;
ET0 = 1;
TR0 = 1;
while(1)
{
if(flag1s == 1) // 判断1s定时标志
{
flag1s = 0;
sec++;
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 InterruptTimer0() interrupt 1
{
if(TF0 == 1)
{
TF0 = 0;
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
}
if(cnt>=1000)
{
cnt = 0;
flag1s = 1;
}
P0 = 0xFF;
switch(i)
{
case 0:LSA = 0;LSB = 0;LSC = 0;i++;P0 = LedBuff[0];break;/*Y0*/
case 1:LSA = 1;LSB = 0;LSC = 0;i++;P0 = LedBuff[1];break;/*Y1*/
case 2:LSA = 0;LSB = 1;LSC = 0;i++;P0 = LedBuff[2];break;/*Y2*/
case 3:LSA = 1;LSB = 1;LSC = 0;i++;P0 = LedBuff[3];break;/*Y3*/
case 4:LSA = 0;LSB = 0;LSC = 1;i++;P0 = LedBuff[4];break;/*Y4*/
case 5:LSA = 1;LSB = 0;LSC = 1;i=0;P0 = LedBuff[5];break;/*Y5*/
default:break;
}
}
|