问题遇到的现象和发生背景
KEIL中调试时发现用数组给P0赋值时出现错误。
问题相关代码,请勿粘贴截图
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit dula=P2^6;
sbit wela=P2^7;
uchar code tabledu[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
uchar code tablewe[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
//1111 1110fe,1111 1101fd,1111 1011fb,1111 0111f7,1110 1111ef,1101 1111df
uchar table_perwe[6]={0};//tablewe的0,1,2,3,4,5位分别对应n的每一位
uint n=0,a=0;//n为计数,z为中断次数
uint x=1,y,i;//x表示当前n有多少位y=n
uchar n_du;
void main(){
TMOD=1;
TH0=(65526-50000)/256;
TL0=(65536-50000)%256;
EA=1;//开总中断
ET0=1;//开计时器0的中断
TR0=1;//开始计数
wela=1;
P0=tablewe[0];//第一个数码管亮1
wela=0;
dula=1;
P0=tabledu[0];
dula=0;
while(1){
if(a>=100){//将n每一位存到tablen中去,并计算出n有x位
a=0;
x=0;
n++;//每过一秒n+1
y=n;
while(y){
table_perwe[x]=y%10;
x++;
y=y/10;
}
}
for(i=0;i<x;i++){
n_du=table_perwe[i];//n_du表示当前位的数字对应于dula表中的第几个
wela=1;
P0=tablewe[i];
wela=0;
dula=1;
P0=tabledu[n_du];
dula=0;
}
}
}
void timer0() interrupt 1
{
TH0=(65526-50000)%256;
TL0=(65536-50000)/256;
a++;
}