標題:
單片機四模式運行流水燈源程序
[打印本頁]
作者:
13384329059
時間:
2020-3-13 09:39
標題:
單片機四模式運行流水燈源程序
#include<reg51.h> //包含頭文件
#define uchar unsigned char
#define uint unsigned int //宏定義
uchar ModeNo; //燈閃爍的模式
uint Speed; //閃爍的速度
uchar tCount=0; //進入定時器控制速度
uchar Idx; //調(diào)速變量
uchar mb_Count=0;
bit Dirtect=1;
uchar code DSY_CODE[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uint code sTable[]={0,1,3,5,7,9,15,30,50,100,200,230,280,300,350};
void Delay(uint x) //延時函數(shù)
{
uchar i;
while (x--)
for(i=0;i<120;i++);
}
uchar GetKey() //按鍵函數(shù)
{
uchar K;
if(P2==0xFF) //沒有按鍵按下
return 0; //返回0
Delay(10); //延時
switch(P2) //有按鍵按下
{
case 0xFE: K=1; break; //k1按下
case 0xFD: K=2; break;
case 0xFB: K=3; break;
default: K=0; //其他狀態(tài),k=0
}
while(P2!=0xFF); //按鍵釋放
return K; //返回按鍵信息
}
void Led_Demo(uint Led16) //LED狀態(tài)右移
{
P1=(uchar)(Led16 & 0x00FF);
P0=(uchar)(Led16 >>8);
}
void T0_TNT() interrupt 1 //定時器工作
{
if (++tCount < Speed) return; //對應不同的速度
tCount=0;
switch (ModeNo)
{
case 0: Led_Demo(0x0001 << mb_Count);break; //模式0,最右側的燈亮,然后向左移
case 1: Led_Demo(0x8000 >> mb_Count);break; //模式1,最左側的燈亮,然后向右移
case 2: if(Dirtect) Led_Demo(0x000F << mb_Count); //模式2,四個滅燈左右移
else Led_Demo(0xF000 >> mb_Count);
if(mb_Count==15) Dirtect =!Dirtect;
break;
case 3: if(Dirtect) Led_Demo(~(0x000F << mb_Count)); //模式3,四個亮燈左右移
else Led_Demo(~(0xF000 >> mb_Count));
if(mb_Count==15) Dirtect =!Dirtect;
break;
if(mb_Count==15) Dirtect =!Dirtect;
break;
}
mb_Count=(mb_Count+1)%16;
}
void KeyProcess(uchar Key) //切換速度
{
switch(Key) //按鍵按下
{
case 1: Dirtect=1; //切換模式
mb_Count=0;
ModeNo=(ModeNo+1)%8;
P3=DSY_CODE[ModeNo];
break;
case 2: if(Idx>1) //速度減
Speed=sTable[--Idx];
break;
case 3: if(Idx<15)
Speed=sTable[++Idx]; //速度加
}
}
void main() //主函數(shù)
{
uchar Key;
P0=P1=P2=P3=0xFF; //IO口都復位
ModeNo=0; //變量清零
Idx=4; //速度默認開機4
Speed=sTable[Idx]; //4檔速度對應的延時數(shù)
P3=DSY_CODE[ModeNo]; //P3口賦值初始狀態(tài)
IE=0x82; //定義寄存器
TMOD=0x00; //定時器工作狀態(tài)
TR0=1; //開啟定時器0
while(1) //進入while循環(huán)
{
Key=GetKey(); //讀取按鍵狀態(tài)
if(Key!=0) //如果有按鍵按下
KeyProcess(Key); //根據(jù)按鍵狀態(tài)切換速度
}
}
復制代碼
改過的四模式運行流水燈.zip
2020-3-13 09:39 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
32.99 KB, 下載次數(shù): 10, 下載積分: 黑幣 -5
四模式流水燈
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1