用89C52单片机控制32个LED流水灯的C语言程序,

2024年12月03日 01:25
有4个网友回答
网友(1):

/***32灯流水——火柴天堂作品-20130520***/
/***52单片机,12MHz晶振,P0-P3 接32颗LED,低电平驱动***/
#include"REG52.h"//包含52头文件
#define TRUE 1//定义布尔量'1':真
#define FALSE 0//定义布尔量'0':假
#define uchar unsigned char//定义 无符号字符型数据 简称
#define uint unsigned int//定义 无符号整型数据 简称

#define th0 0xb1
#define tl0 0xe0//20ms at 12MHz Fosc in Model 1

#define LED_Port1 P0
#define LED_Port2 P1
#define LED_Port3 P2
#define LED_Port4 P3
#define LED_AllOff LED_Port1=LED_Port2=LED_Port3=LED_Port4=0xff
#define LED_AllOn LED_Port1=LED_Port2=LED_Port3=LED_Port4=0
#define LED_Status 43//1步全亮+32步单亮+10步闪烁
#define LED_Marquee 32

bit T20msFlag=0;
void Timer0() interrupt 1
{
TL0=tl0;
TH0=th0;
T20msFlag=TRUE;
}
void TimerInit()
{
TMOD=0x01;
TH0=th0;
TL0=tl0;
TR0=1;
ET0=1;
EA=1;
}
void LED_Out()
{
static uchar led_status=0;
if(T20msFlag)
{
T20msFlag=FALSE;
led_status=++led_status%LED_Status;
}
if(led_status && led_status<=LED_Marquee)
{
LED_AllOn;
switch((led_status-1)>>3)
{
case 0:LED_Port1=1<<((led_status-1)%8);break;
case 1:LED_Port2=1<<((led_status-1)%8);break;
case 2:LED_Port3=1<<((led_status-1)%8);break;
case 3:LED_Port4=1<<((led_status-1)%8);break;
default:led_status=0;break;
}
}
else
{
if(!led_status && led_status%2) LED_AllOn;
else LED_AllOff;
}
}
void main()
{
TimerInit();
while(1)
{
LED_Out();
}
}

网友(2):

#include

#define uchar unsigned char
uchar led1,led2,led3,led4;
void delay(uchar a)
{
uchar i,j;
for(i=0;i for(j=0;j<120;j++);
}
main()
{
uchar num;
unsigned long int led;
while(1)
{
led=0x00000001;
num=0;
while(num<32)
{
led1=(led<>24;
led2=(led<>16;
led3=(led<>8;
led4=led< P0=led1;
P1=led2;
P2=led3;
P3=led4;
delay(20);
num++;
}
for(num=0;num<5;num++)
{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00;
delay(20);
P0=0xff;
P0=0xff;
P0=0xff;
P0=0xff;
delay(20);
}

}
}

网友(3):

#include

#define uchar unsigned char
uchar led1,led2,led3,led4;
void delay(uchar a)
{
uchar i,j;
for(i=0;ifor(j=0;j<120;j++);
}
main()
{
uchar num;
unsigned long int led;
while(1)
{
led=0x00000001;
num=0;
while(num<32)
{
led1=(led<>24;
led2=(led<>16;
led3=(led<>8;
led4=led<P0=led1;
P1=led2;
P2=led3;
P3=led4;
delay(20);
num++;
}
for(num=0;num<5;num++)
{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00;
delay(20);
P0=0xff;
P0=0xff;
P0=0xff;
P0=0xff;
delay(20);
}

}
}

网友(4):

I/O口连接方式呢?