单片机流水灯,以及延时
#include<reg52.h>
sbit led=P1^2; //定义端口为P1.2
void delay(unsigned int x) //延时函数,x为传递参数
{
unsigned char i;
while(x--)
{
for(i=0;i<120;i++);
}
}
void main(void) //单个灯闪烁
{
while(1)
{
led=0; //灯亮
delay(500); //延时500ms
led=1; //灯灭
delay(500);
}
====================================================8个灯依次闪烁===========
#include<reg52.h>
void delay(unsigned int x)
{ unsigned char i;
while(x--)
{
for(i=0;i<120;i++);
}
}
unsigned char a[8]={0xFE,0xFD,0xFB,0xF7,0xEF
,0xDF,0xBF,0x7F}; //根据二进制进行
void main(void)
{
while(1)
{
unsigned char i;
for(i=0;i<8;i++)
{
P1=a[i]; //定义P1端口
delay(500);
}
}
===================================流水灯程序============================================
#include<reg52.h>
void delay(unsigned int x)
{
unsigned char i;
while(x--)
{
for(i=0;i<120;i++);
}
}
unsigned char a[]={0xaa,0x55,0xf0,0xf,0x0,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void main(void)
{
unsigned char i;
while(1)
{
for(i=0;i<sizeof(a);i++)
{
delay(500);
P1=a[i];
}
}
}