硬件用的是天祥的实验板。
以下是在原程序的基础上加了数码管显示。
复位后,单片机还是显示 000001.说是有一次接收中断。
求大神指点
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
unsigned char flag,a;
unsigned int num=0;
sbit dula=P2^6;
sbit wela=P2^7;
void smg_num(long);
void main()
{
TMOD=0x20;//设置定时器1为工作方式2
TH1=0xfd;
TL1=0xfd;
TR1=1;
REN=1;
SM0=0;
SM1=1;
EA=1;
ES=1;
while(1)
{
/* if(RI==1)
{
RI=0;
P1=SBUF;
}*/
smg_num(num);
if(flag==1)
{
ES=0;
flag=0;
SBUF='1';//28;
while(!TI);
TI=0;
SBUF='2';//28;
while(!TI);
TI=0;
SBUF='3';//28;
while(!TI);
TI=0;
ES=1;
}
}
}
void ser() interrupt 4
{
RI=0;
// P1=SBUF;
a=SBUF;
flag=1;
num=num+1;
}
void smg_num (long num)
{
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
long num_xianshi;
uint weixuan,weixuan_temp=0xdf;
// num=6523455;//int整型最大到32767,只能用长整型long
for(weixuan=0;weixuan<6;weixuan++)
{
wela=1;
P0=weixuan_temp;
wela=0;
P0=0;
dula=1;
num_xianshi=num%10;
P0=table[num_xianshi];
dula=0;
P0=0;
weixuan_temp=weixuan_temp>>1;
weixuan_temp=weixuan_temp|0xc0;//按位或运算
num=num/10;
}
}
|