百度知道 - 信息提示
求用C语言编程实现51单片机的LED灯移动
#include
#define uchar unsigned char
sbit c10=P1^0;
sbit c11=P1^1;
sbit c12=P1^2;
sbit c13=P1^3;
uchar led1,led3;
void delay(uchar a)
{
uchar i,j;
for(i=0;i<a;i++)
for(j=0;j<120;j++);
}
main()
{
uchar keyval=0xff;
led1=0xfe;
led3=0xf8;
while(1)
{
if(c10==0)
{
delay(10);
if(c10==0)
{
while(c10==0);
keyval=0;
}
}
if(c11==0)
{
delay(10);
if(c11==0)
{
while(c11==0);
keyval=1;
}
}
if(c12==0)
{
delay(10);
if(c12==0)
{
while(c12==0);
keyval=2;
}
}
if(c13==0)
{
delay(10);
if(c13==0)
{
while(c13==0);
keyval=3;
}
}
switch(keyval)
{
case 0:
P0=led1;
led1=(led1<<1)|0x01;
if(led1==0xff)led1=0xfe;
delay(100);
case 1:
P0=led1;
led1=(led1>>1)|0x80;
if(led1==0xff)led1=0x7f;
delay(100);
case 2:
P0=led3;
led3=(led3<<1)|0x01;
if((led3&0xf0)==0x30)led3=0x3e;
if((led3&0xf0)==0x70)led3=0x7c;
if((led3&0xf0)==0xf0)led3=0xf8;
break;
case 3:
P0=led3;
led3=(led3>>1)|0x80;
if((led3&0x0f)==0x0c)led3=0x7c;
if((led3&0x0f)==0x0e)led3=0x3e;
if((led3&0x0f)==0x0f)led3=0x1f;
break;
default:break;
}
}
}
单片机C语言编程点亮LED灯
可以通过左移函数_crol_()和右移函数_cror_()来实现LED等的来回流动。 具体实现方法可以参考如下程序: #include #include #define uint unsigned int#define uchar unsigned charvoid delay(uint a);void main(){ P1=0xfe; while(1) { // 向左循环点亮LED for(i=0; i0;x--) for(y=110;y>0;y--);}