各位前辈好,我是一枚刚接触单片机和C语言的小白,网上下载了郭天祥C51的教学视频,学了一段时间,昨晚遇到了一个问题,利用中断做“0~99”的计数,但十位显示不正常,只在个位数变化的时候闪一下立马就灭了,一直找不出原因,都几乎快要照搬例程了,望各位帮忙看下是哪出了问题,谢谢了
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar snap,temp;
sbit CS = P2^7;//数码管位选
sbit SEG = P2^6;//数码管段选
void delay(uchar z);
void init();//中断初始化
void display();
uchar Ten,SD;
uchar code SEGData[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
int main(void)
{
init();
snap = 0;
while(1)
{
if(temp == 20)
{
temp = 0;
snap++;//中断20次后计数+1
Ten = snap/10;//十位
SD = snap%10;//个位
display();
if(snap == 100)
snap = 0;
}
}
}
void init()
{
EA = 1;
TMOD = 0x01;
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
ET0 = 1;
TR0 = 1;
temp = 0;
}
void timer0() interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
temp++;
}
void display()
{
P0 = 0xff;
CS = 1;
P0 = 0xfe;
CS = 0;
SEG = 1;
P0 = SEGData[Ten];
SEG = 0;
delay(5);
P0 = 0xff;
CS = 1;
P0 = 0xfd;
CS = 0;
SEG = 1;
P0 = SEGData[SD];
SEG = 0;
delay(5);
}
void delay(uchar z)
{
uchar x,y;
for(x=100;x>0;x--)
for(y=z;y>0;y--);
}
|