㈠ 51單片機C程序怎樣把LED燈從左到右點亮然後又從右到左點亮然後循環
#include<reg51.h>
unsigned char ledtab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe}
void delay(unsigned char a)
{
unsigned char i,j;
for(i=0;i<a;i++)
for(j=0;j<120;j++);
}
main()
{
unsigned char i;
while(1)
{
for(i=0;i<8;i++){P0=ledtab[i];delay(100);}
for(i=0;i<8;i++){P0=ledtab[7-i];delay(100);}
}
}
㈡ 基於51單片機16個彩燈從左到右再從右左循環閃爍的匯編程序
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int //16位
#define uchar unsigned char//8位
sbit led=P2^0;
sbit k0=P1^0;
sbit k1=P1^1;
void delay(uint z);
void youyi();
void zuoyi();
uchar i;
uchar temp1=0x01;//0000 0001
uchar temp2=0x80;//1000 0000
uchar flag0=0;
uchar flag1=0;
void main()
{
while(1)
{
if(k0==0)//左移
{
flag0=1;
}
if(k1==0)//右移
{
flag1=1;
}
if(flag0==1)
{
zuoyi();
temp2=0x80;
}
if(flag1==1)
{
youyi();
temp1=0x01;
}
}
}
void zuoyi()
{
for(i=0;i<7;i++)//i++ i=i+1
{
P2=temp1;
temp1=_crol_(temp1,1);//0000 0010
delay(500);
}
}
void youyi()
{
for(i=0;i<7;i++)//i++ i=i+1
{
P2=temp2;
temp2=_cror_(temp2,1);//1000 0000
delay(500);
}
}
void delay(uint z)
{
uint x , y;
for(x=110;x>0;x--)
for(y=z;y>0;y--);
㈢ 求16個流水燈程序,有模式還可以調節快慢的,急求,謝謝。
《基於8051+Proteus模擬》案例
第01 篇基礎程序設計
01 閃爍的LED
/* 名稱:閃爍的LED
說明:LED按設定的時間間隔閃爍
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED=P1^0;
//延時
void DelayMS(uint x)
{
uchari;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
while(1)
{
LED=~LED;
DelayMS(150);
}
}
02 從左到右的流水燈
/* 名稱:從左到右的流水燈
說明:接在P0口的8個LED從左到右循環依次點亮,產生走馬燈效果
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延時
void DelayMS(uint x)
{
uchari;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
P0=0xfe;
while(1)
{
P0=_crol_(P0,1);//P0的值向左循環移動
DelayMS(150);
}
}
03 8隻LED左右來回點亮
/* 名稱:8隻LED左右來回點亮
說明:程序利用循環移位函數_crol_和_cror_形成來回滾動的效果
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延時
void DelayMS(uint x)
{
uchari;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
uchari;
P2=0x01;
while(1)
{
for(i=0;i<7;i++)
{
P2=_crol_(P2,1);//P2的值向左循環移動
DelayMS(150);
}
for(i=0;i<7;i++)
{
P2=_cror_(P2,1);//P2的值向右循環移動
DelayMS(150);
}
}
}
04 花樣流水燈
/* 名稱:花樣流水燈
說明:16隻LED分兩組按預設的多種花樣變換顯示
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code Pattern_P0[]=
{
0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,
0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
uchar code Pattern_P2[]=
{
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,
0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,
0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,
0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
//延時
void DelayMS(uint x)
{
uchari;
while(x--)
{
for(i=0;i<120;i++);
}
}
//主程序
void main()
{
uchari;
while(1)
{ //從數組中讀取數據送至P0和P2口顯示
for(i=0;i<136;i++)
{
P0=Pattern_P0[i];
P2=Pattern_P2[i];
DelayMS(100);
}
}
}
05 LED模擬交通燈
/* 名稱:LED模擬交通燈
說明:東西向綠燈亮若干秒,黃燈閃爍5次後紅燈亮, 紅燈亮後,南北向由紅燈變為綠燈,若干秒後南北向黃燈閃爍5此後變紅燈,東西向變綠燈,如此重復。
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit RED_A=P0^0; //東西向燈
sbit YELLOW_A=P0^1;
sbit GREEN_A=P0^2;
sbit RED_B=P0^3; //南北向燈
sbit YELLOW_B=P0^4;
sbit GREEN_B=P0^5;
uchar Flash_Count=0,Operation_Type=1; //閃爍次數,操作類型變數
//延時
void DelayMS(uint x)
{
uchari;
while(x--)for(i=0;i<120;i++);
}
//交通燈切換
void Traffic_Light()
{
switch(Operation_Type)
{
case1: //東西向綠燈與南北向紅燈亮
RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
DelayMS(2000);
Operation_Type=2;
break;
case2: //東西向黃燈閃爍,綠燈關閉
DelayMS(300);
YELLOW_A=~YELLOW_A;GREEN_A=1;
if(++Flash_Count!=10)return; //閃爍5次
Flash_Count=0;
Operation_Type=3;
break;
case3: //東西向紅燈,南北向綠燈亮
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;
DelayMS(2000);
Operation_Type=4;
break;
case4: //南北向黃燈閃爍5次
DelayMS(300);
YELLOW_B=~YELLOW_B;GREEN_B=1;
if(++Flash_Count!=10)return;
Flash_Count=0;
Operation_Type=1;
}
}
//主程序
void main()
{
while(1)Traffic_Light();
}
06 單只數碼管循環顯示0~9
/* 名稱:單只數碼管循環顯示0~9
說明:主程序中的循環語句反復將0~9的段碼送至P0口,使數字0~9循環顯示
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
//延時
void DelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
void main()
{
uchari=0;
P0=0x00;
while(1)
{/* for(;i<11;i++){ P0=~DSY_CODE[i]; DelayMS(300);} //注:另一方案 */
P0=~DSY_CODE[i];
i=(i+1)%10;
DelayMS(300);
}
}
07 8隻數碼管滾動顯示單個數字
/* 名稱:8隻數碼管滾動顯示單個數字
說明:數碼管從左到右依次滾動顯示0~7,程序通過每次僅循環選通一隻數碼管
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//延時
void DelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
void main()
{
uchari,wei=0x80;
while(1)
{
for(i=0;i<8;i++)
{
P2=0xff; //關閉顯示
wei=_crol_(wei,1);
P0=DSY_CODE[i]; //發送數欄位碼
P2=wei; //發送位碼
DelayMS(300);
}
}
}
08 8隻數碼管動態顯示多個不同字元
電路如上圖
/* 名稱:8隻數碼管動態顯示多個不同字元
說明:數碼管動態掃描顯示0~7。
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//延時
void DelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
void main()
{
uchari,wei=0x80;
while(1)
{
for(i=0;i<8;i++)
{
P2=0xff;
P0=DSY_CODE[i]; //發送段碼
wei=_crol_(wei,1);
P2=wei; //發送位碼
DelayMS(2);
}
}
}
09 8隻數碼管閃爍顯示數字串
電路如上圖
/* 名稱:8隻數碼管閃爍顯示數字串
說明:數碼管閃爍顯示由0~7構成的一串數字
本例用動態刷新法顯示一串數字,在停止刷新時所有數字顯示消失。
*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//段碼表
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//位碼表
uchar codeDSY_IDX[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
//延時
void DelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
void main()
{
uchari,j;
while(1)
{
for(i=0;i<30;i++)
{
for(j=0;j<8;j++)
{
P0=0xff;
P0=DSY_CODE[j]; //發送段碼
P2=DSY_IDX[j]; //發送位碼
DelayMS(2);
}
}
P2=0x00; //關閉所有數碼管並延時
DelayMS(1000);
}
}
10 8隻數碼管滾動顯示數字串
電路如上圖
/* 名稱:8隻數碼管滾動顯示數字串
說明:數碼管向左滾動顯示3個字元構成的數字串
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//段碼表
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};
//下面數組看作環形隊列,顯示從某個數開始的8個數(10表示黑屏)
uchar Num[]={10,10,10,10,10,10,10,10,2,9,8};
//延時
void DelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
void main()
{
uchari,j,k=0,m=0x80;
while(1)
{ //刷新若干次,保持一段時間的穩定顯示
for(i=0;i<15;i++)
{
for(j=0;j<8;j++)
{ //發送段碼,採用環形取法,從第k個開始取第j個
P0=0xff;
P0=DSY_CODE[Num[(k+j)%11]];
m=_crol_(m,1);
P2=m; //發送位碼
DelayMS(2);
}
}
k=(k+1)%11; //環形隊列首支針k遞增,Num下標范圍0~10,故對11取余
}
}
11 K1-K4 控制LED移位
/* 名稱:K1-K4 控制LED移位
說明:按下K1時,P0口LED上移一位;
按下K2時,P0口LED下移一位;
按下K3時,P2口LED上移一位;
按下K4時,P2口LED下移一位;
*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延時
void DelayMS(uint x)
{
uchari;
while(x--)for(i=0;i<120;i++);
}
//根據P1口的按鍵移動LED
void Move_LED()
{
if ((P1&0x10)==0) P0=_cror_(P0,1); //K1
elseif((P1&0x20)==0) P0=_crol_(P0,1); //K2
elseif((P1&0x40)==0) P2=_cror_(P2,1);//K3
elseif((P1&0x80)==0) P2=_crol_(P2,1); //K4
}
//主程序
void main()
{
ucharRecent_Key; //最近按鍵
P0=0xfe;
P2=0xfe;
P1=0xff;
Recent_Key=0xff;
while(1)
{
if(Recent_Key!=P1)
{
Recent_Key=P1; //保存最近按鍵
Move_LED();
DelayMS(10);
}
}
}