我给你做个例子吧,把接LED灯的io口赋值0,灯亮;把接LED灯的io口赋值1,灯灭
因为一般接io口的LED灯有个上拉高电平,io这端给他个低电平(0),造成电压差,就有电流通过,灯就亮了;反之,io给高电平(1),没有压差,无电流,灯灭。
下面的你自己应该可以写了,锻炼下手。
void delay(void)
{
int i=10000; //这个值调整亮灯灭等快慢,越小越快越大越慢
while(i--)
return;
}
//从上到下亮灯
void LED_a(void)
{
LED_IO1 = 0;
delay();
LED_IO2 = 0;
delay();
LED_IO3 = 0;
delay();
LED_IO4 = 0;
delay();
LED_IO5 = 0;
delay();
LED_IO6= 0;
delay();
LED_IO7= 0;
delay();
LED_IO8= 0;
delay();
}
//开始灯全灭
void LED_dengmie(void)
{
LED_IO1 = 1;
LED_IO2 = 1;
LED_IO3 = 1;
LED_IO4 = 1;
LED_IO5 = 1;
LED_IO6= 1;
LED_IO7= 1;
LED_IO8= 1;
}
main()
{
LED_dengmie();
LED_a();
}
#include
unsigned char P1;
void delay05s(void)
{
unsignedchar i,j,k;
for(i=50;i>0;i--)
for(j=20;j>0;j--)
for(k=248;k>0;k--);
}
void main(void)
{
while(1)
{
//自上而下
P1=0xfe;delay05s();P1=0xfd;delay05s();P1=0xfb;delay05s();
P1=0xf7;delay05s();
//自下而上
P1=0xf7;delay05s();P1=0xfb;delay05s();P1=0xfd;delay05s();
P1=0xfe;delay05s();
//交替
P1=0xfC;delay05s();P1=0xf3;delay05s();
//一个个灭
P1=0xf0;delay05s();P1=0xf1;delay05s();P1=0xf3;delay05s();
P1=0xf7;delay05s();P1=0xff;delay05s();
}
}
#include
#define uchar unsigned char
uchar led;
void delay()
{
uchar i,j;
for(i=0;i<100;i++)
for(j=0;j<125;j++);
}
main()
{
uchar i;
while(1)
{
led=0xfe;
i=8;
while(i--)
{
P0=led;
delay();
led<<=1;
led|=0x01;
}
i=8;
led=0x7f;
while(i--)
{
P0=led;
delay();
led>>=1;
led|=0x80;
}
i=8;
led=0x0f;
while(i--)
{
P0=led;
delay();
led>>~led;
}
i=8;
led=0x00;
P0=led;
delay();
while(i--)
{
led|=0x80
P0=led;
delay();
led>>=1;
}
}
}