STC15W4K32S4单片机
指示灯系列
任务四
八路发光二极管依次点亮
参考代码
#include<stc15.h>
void delay(unsigned int i)//延时函数
{
while(i--);
}
void main()//主函数
{
P0M0=0x00;P0M1=0x00;
P2M0=0x00;P2M1=0x00;//准双向口
P27=0;
while(1)
{
P0=0xfe;
delay(50000);
P0=0xfc;
delay(50000);
P0=0xf8;
delay(50000);
P0=0xf0;
delay(50000);
P0=0xe0;
delay(50000);
P0=0xc0;
delay(50000);
P0=0x80;
delay(50000);
P0=0x00;
delay(50000);
P0=0xff;
delay(50000);
}
}
以上程序可以精简,如下:
#include<stc15.h>
void delay(unsigned int i)
{
while(i--);
}
void main()
{
unsigned char led[9]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0xff};
int a;
P0M0=0x00;P0M1=0x00;
P2M0=0x00;P2M1=0x00;
P27=0;
while(1)
{
for(a=0;a<9;a++)
{
P0=led[a];
delay(50000);
}
}
}
拓展练习:8路发光二极管从中间向两边依次点亮,再依次熄灭,循环执行。
#include<stc15.h>
void delay(unsigned int i)//延时函数
{
while(i--);
}
void main()//主函数
{
P0M0=0x00;P0M1=0x00;
P2M0=0x00;P2M1=0x00;//准双向口
P27=0;
while(1)
{
P0=0xe0;P0=0xf0;
delay(50000);
P0=0xc0;P0=0xf8;
delay(50000);
P0=0x80;P0=0xfc;
delay(50000);
P0=0x00;P0=0xfe;
delay(50000);
P0=0xff;
delay(50000);
}
}
如果发现程序存在问题,欢迎留言指正。