新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 51單片機(jī)流水燈C語(yǔ)言源程序

51單片機(jī)流水燈C語(yǔ)言源程序

作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
**************************************************************
文件名稱(chēng):flash_led.c
文件說(shuō)明:流水燈C程序
編寫(xiě)日期:2006年10月5日
程序說(shuō)明:MCU采用AT89S51,外接12M晶振,P1口輸出
*************************************************************/
#include //51系列單片機(jī)定義文件
#define uchar unsigned char //定義無(wú)符號(hào)字符
#define uint unsigned int //定義無(wú)符號(hào)整數(shù)
void delay(uint); //聲明延時(shí)函數(shù)
void main(void)
{
uint i;
uchar temp;
while(1)
{
temp=0x01;
for(i=0;i<8;i++) //8個(gè)流水燈逐個(gè)閃動(dòng)
{
P1=~temp;
delay(100); //調(diào)用延時(shí)函數(shù)
temp<<=1;
}
temp=0x80;
for(i=0;i<8;i++) //8個(gè)流水燈反向逐個(gè)閃動(dòng)
{
P1=~temp;
delay(100); //調(diào)用延時(shí)函數(shù)
temp>>=1;
}
temp=0xFE;
for(i=0;i<8;i++) //8個(gè)流水燈依次全部點(diǎn)亮
{
P1=temp;
delay(100); //調(diào)用延時(shí)函數(shù)
temp<<=1;
}
temp=0x7F;
for(i=0;i<8;i++) //8個(gè)流水燈依次反向全部點(diǎn)亮
{
P1=temp;
delay(100); //調(diào)用延時(shí)函數(shù)
temp>>=1;
}
}
}
void delay(uint t) //定義延時(shí)函數(shù)
{
register uint bt;
for(;t;t--)
for(bt=0;bt<255;bt++);
}
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define TimeDelay 60000
void delay(uint n);
void Flash0a(void);
void Flash0b(void);
void Flash1a(void);
void Flash1b(void);
void Flash2a(void);
void Flash2b(void);
void Flash3a(void);
void Flash3b(void);
void main(void)
{
while(1)
{
Flash1b();
Flash1a();
delay(TimeDelay);
delay(TimeDelay);
Flash0b();
Flash0a();
Flash2b();
Flash2a();
Flash1b();
Flash1a();
Flash3b();
Flash3a();
}
}
void delay(uint n)
{
while(n!=0)
{
n--;
}
}
void Flash0a(void)
{
uchar i;
uchar Temp;
Temp = 0x80;
for(i=0;i<8;i++)
{
P0 = ~Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P0 = 0xff;
}
void Flash0b(void)
{
uchar i;
uchar Temp;
Temp = 0x80;
for(i=0;i<8;i++)
{
P0 = Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P0 = 0;
}
void Flash1a(void)
{
uchar i;
uchar Temp;
P1 = 1;
Temp = 0x80;
for(i=0;i<8;i++)
{
P1 = ~Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P1 = 0xff;
}
void Flash1b(void)
{
uchar i;
uchar Temp;
P1 = 1;
Temp = 0x80;
for(i=0;i<8;i++)
{
P1 = Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P1 = 0;
}
void Flash2a(void)
{
uchar i;
uchar Temp;
P2 = 1;
Temp = 0x80;
for(i=0;i<8;i++)
{
P2 = ~Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P2 = 0xff;
}
void Flash2b(void)
{
uchar i;
uchar Temp;
P2 = 1;
Temp = 0x80;
for(i=0;i<8;i++)
{
P2 = Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P2 = 0;
}
void Flash3a(void)
{
uchar i;
uchar Temp;
P3 = 1;
Temp = 0x80;
for(i=0;i<8;i++)
{
P3 = ~Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P3 = 0xff;
}
void Flash3b(void)
{
uchar i;
uchar Temp;
P3 = 1;
Temp = 0x80;
for(i=0;i<8;i++)
{
P3 = Temp;
delay(TimeDelay);
Temp = Temp >> 1;
}
P3 = 0;
}
===========
;P1口八個(gè)LED燈做三路流水燈實(shí)驗(yàn)
;***************************************************************
;* Date : 2004.4.20 *
;***************************************************************
ORG 0000H
LJMP MAIN
ORG 030H
MAIN:MOV P1,#0DBH ;11011011--0為亮,1為滅
ACALL DELAY
MOV P1,#06DH ;01101101
ACALL DELAY
MOV P1,#0B6H ;10110110
ACALL DELAY
AJMP MAIN
DELAY:MOV R7,#255 ;延時(shí)子程序
D1: MOV R6,#255
D2: DJNZ R6,D2
DJNZ R7,D1
RET
END


評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉