51单片机流水灯问题
#define uchar unsigned char
uchar i,j,z,tt,num;
uchar code table[]={
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x01
};
void delay(uchar z)
{
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void main( )
{
while(1)
{
if(table!=0x01)
{
P1=table;
i++;
delay(200);
}
else
i=0;
}
}
我调试的时候,为什么P1一直是0xfe, 却不读0xfd,0xfd......
只想问明白这个点。
#include<reg52.h>
#define uchar unsigned char #define uint unsigned char
uchar table[]={0xfe,0xfd,0xfb,0xf7,
0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,
0xf7,0xfb,0xfd,0xf};
void delay(uint z)
{ uint i,j;
for(i=z;i>0;i--)
for(j=110;j>0;j--);
}
void main( )
{
uchar i;
while(1)
{
for (i=0;i>16;i++)
{
P1=table;
delay(200);
}
}
}
在Keil 51中,code修饰的变量放在ROM中,而const修饰的变量为只读(不可修改),放在RAM中!可以用“code const”修饰变量,表示变量存储在ROM中,且为只读,由此看来“code”与“code const”修饰变量有一样的功用。
1、table!=0x01 改为 table[i]!=0x01;或者改为i!=16
2、“P1=table”改为“P1=table[i]”
你的这个程序错的地方还比较多。
(1)i尽量不要同时出现在两个函数里,完全可以在延时程序里定义一个其他字母m之类的;
(2)在main函数中,首先要给i赋初值,然后将你程序中的if(table!=0x01)改为if(table!=0x01),然后P1的值才会不断变化,你修改一下试试
嗯嗯,谢谢!错 i 变量上了,改了一下就好了~第二个错误是发帖时候敲错啦!
全局变量有问题
应该是 i 变量有问题
