#我最近在写一个IAP15F2K61S2单片机的数码管秒表程序,程序没报错也没警告,但是最后下载到单片机上运行现象不对:秒数是有,但是位置不对,而且重复出现。最右边的两个数码管总是显示0和8。
#下面分别是我的.c和.h文件
#include <stc15f2k60s2.h>
unsigned char code LedChar[] = {
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E
};
unsigned char LedBuff[8] = {
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
}; //数码管显示缓冲区,初值0xFF确保启动时不亮
unsigned char i = 0; //动态扫描的索引
unsigned int cnt = 0; //记录T0中断次数
void main()
{
unsigned long sec = 0; //记录经过的秒数
EA = 1;
TMOD = 0x01;
TH0 = 0XFC;
TL0 = 0X67;
ET0 = 1;
TR0 = 1;
while (1)
{
if (cnt >=1000)
{
cnt = 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];
LedBuff[6] = LedChar[sec/1000000%10];
LedBuff[7] = LedChar[sec/10000000%10];
}
}
}
void InterruptTime0() interrupt 1
{
TH0 = 0xFC;
TL0 = 0x67;
cnt++;
P0 = 0xFF;
P27 = 1;
P26 = 1;
P25 = 1;
P27 = 0;
P26 = 0;
P25 = 0;
switch (i)
{
case 0:P0 = 0x7F; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[0]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 1:P0 = 0xBF; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[1]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 2:P0 = 0xDF; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[2]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 3:P0 = 0xEF; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[3]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 4:P0 = 0xF7; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[4]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 5:P0 = 0xFB; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[5]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 6:P0 = 0xFD; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[6]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i++; break;
case 7:P0 = 0xFE; P27 = 1;P26 = 1;P25 = 0; P27 = 0;P26 = 0;P25 = 0;
P0 = LedBuff[7]; P27 = 1;P26 = 1;P25 = 1; P27 = 0;P26 = 0;P25 = 0;
i = 0; break;
default: break;
}
}
#ifndef __STC15_H__
#define __STC15_H__
#include <stc15f2k60s2.h>
sbit P3_6 = P4^2;
sbit P3_7 = P4^4;
#endif
#用的是KEIL和ISP烧录软件