2009_0221 流水灯程序(c)语言

//以移位的方式让LED呈现流水形式
#include <reg52.h>
void delay()   //延时程序
{
    int n = 30000;
    while(–n);
}

void main()
{
    P1 = 0xfe;     //初始化P1口,实现P1.0亮其它灭
    while(1)
    {
        if(P1 == 0xfe)   //进入初始化状态
        {
            while(1)   
            {
                P1 <<= 1;   //左移内存中的数实现P1.1亮,在循环中逐渐点亮其他引脚的LED
                P1 |= 0x01;
                delay();
                if(P1 == 0x7f)break;  //左移最多能到到0x7f,此后要右移,才能实现“流水”的形式
            }
        }

        if(P1 == 0x7f) 
        {
            while(1)
            {
                P1 >>= 1;      //右移了
                P1 |= 0x80;
                delay();
                if(P1 == 0xfe)break;
            }
        }
    }
}

Leave a comment