单片机流水灯三个点亮方向
扫描二维码
随时随地手机看文章
一、横向点亮(依次点亮P0.0~P0.1到P1.0~P1.7到P2.0~P2.7到P3.0~P3.7)
include "reg52.h" //此处使用的为89S52的芯片
void delay(unsigned int second)
{
unsigned int i,j; //定义为unsigned防止负数,习惯问题
for(i=0;i<second;i++)
{
for(j=0;j<200000;j++);
}
}
void main(void)
{
unsigned int i;
while(1)
{
for(i=0;i<8:i++)
{
P0=0x01<<i;
delay(100); //由1us=10E6,故此处100为2秒
P0=0: //使P0端全处于低电平,熄灭点亮的流水灯
}
for(i=0;i<8:i++)
{
P1=0x01<<i;
delay(100);
P1=0;
}
for(i=0;i<8:i++)
{
P2=0x01<<i;
delay(100);
P2=0;
}
for(i=0;i<8:i++)
{
P3=0x01<<i;
delay(100);
P3=0;
}
}
}
二、纵向点亮(点亮次序由P0.0~P1.0~P2.0~P3.0再到P0.1~P1.1以此类推)
include "reg52.h" //此处使用的为89S52的芯片
void delay(unsigned int second)
{
unsigned int i,j; //定义为unsigned防止负数,习惯问题
for(i=0;i<second;i++)
{
for(j=0;j<200000;j++);
}
}
void main(void)
{
unsigned i;
while(1)
{
for(i=0;i<8;i++)
{
P0=0x01<<i;
delay(100);
P0=0;
P1=0x01<<i;
delay(100);
P1=0;
P2=0x01<<i;
delay(100);
P2=0;
P3=0x01<<i;
delay(100);
P3=0:
}
}
}
三、斜向点亮(从P0~P3口斜向点亮)
include "reg52.h" //此处使用的为89S52的芯片
void delay(unsigned int second)
{
unsigned int i,j; //定义为unsigned防止负数,习惯问题
for(i=0;i<second;i++)
{
for(j=0;j<200000;j++);
}
}
void main(void)
{
unsigned i;
while(1)
{
for(i=0;i<8;i++)
{
P0=0x01<<i;
delay(100);
P0=0;
P1=0x01<<(i<7?i+1:i-7); //此处反映了当i为7时;0x01左移0位,亮P0.0的灯
delay(100);
P1=0;
P2=0x01<<(i<6?i+2:i-6); //此处反映了当i大于6时;0ix01左移i-6位,亮P0.(i-6)的灯
delay(100);
P2=0:
P3=0x01<<(i<5?i+3:i-5; //同上
delay(100);
P3=0:
}
}
}