微控制器51c語言程式例項(可調控的跑馬燈)+Proteus模擬+原始碼
阿新 • • 發佈:2019-02-02
//Ãû³Æ£º¿ÉÒÔµ÷¿ØµÄ×ßÂíµÆ
//˵Ã÷£ºµ¥»÷ģʽ°´¼ü¿ÉÔÚ0-7ºÅģʽÖÐÑ¡Ôñ£¬¼ÓËٺͼõËÙ°´¼ü¿Éµ÷ÕûLEDÁ÷¶¯
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar ModeNo; //ģʽ±àºÅ
uchar Speed;
uchar tCount = 0;
uchar rdx; //ËÙ¶ÈÈ¡ÖµË÷Òý
uchar mb_Count =0; //Òƶ¯Î»Êý
bit Direct= 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)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//¼ì²â°´¼ü
uchar Getkey()
{
uchar K;
if(P2==0xFF)return 0;
Delay(10);
switch(P2)
{
case 0xFE:K=1;break;
case 0xFD:K=2;break;
case 0xFB:K=3;break;
default : K=0;
}
while(P2!=0xFF); //µÈ´ýÊÍ·Å°´¼ü
return K;
}
//16Ö»LEDÏÔʾ
void Led_Demo(uint Led16)
{
P1=(uchar)(Led16 & 0x00FF);//ÏÔʾµÍ8λ
P0=(uchar)(Led16 >>8);//ÏÔʾ¸ß8λ
}
//¶¨Ê±Æ÷2
void T0_INT() interrupt 1
{
if(++tCount < Speed) return;
tCount = 0;
switch (ModeNo)
{
case 0:Led_Demo(~0x0001<<mb_Count);break;
case 1:Led_Demo(~0xB000>>mb_Count);break;
case 2:if(Direct)Led_Demo(~0x000F<<mb_Count);
else Led_Demo(~0xF000>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 3:if(Direct)Led_Demo(-(0x000F<<mb_Count));
elseLed_Demo(-(0xF000>>mb_Count));
if(mb_Count==15)Direct=!Direct;
break;
case 4:if(Direct)Led_Demo(0x003F<<mb_Count);
elseLed_Demo(0xFC00>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 5:if(Direct)Led_Demo(0x0001<<mb_Count);
elseLed_Demo(0x8000>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 6:if(Direct)Led_Demo(-(0x0001<<mb_Count));
elseLed_Demo(-(0x8000>>mb_Count));
if(mb_Count==15) Direct=!Direct;
break;
case 7:if(Direct)Led_Demo(0xFFFE<<mb_Count);
elseLed_Demo(0x7FFF>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
}
mb_Count=(mb_Count+1)%16;
}
//¼üÅÌ°´¼ü´¦Àí
void KeyProcess(uchar Key)
{
switch(Key)
{
case 1: //ÖØÉèģʽ
Direct=1;mb_Count=0;
ModeNo=(ModeNo+1)%8;
P3=DSY_CODE[ModeNo];
break;
case 2://¼ÓËÙ
if(rdx>1)Speed=sTable[--rdx];break;
case 3://¼õËÙ
if(rdx<15)Speed=sTable[++rdx];break;
}
}
//Ö÷³ÌÐò
void main()
{
uchar Key;
P0=P1=P2=P3=0xFF;
ModeNo=0;rdx=4;
Speed=sTable[rdx];
P3=DSY_CODE[ModeNo];
IE=0xB2;
TMOD=0X00;
TR0=1;
while(1)
{
Key=Getkey();
if(Key!=0) KeyProcess(Key);
}
}
//˵Ã÷£ºµ¥»÷ģʽ°´¼ü¿ÉÔÚ0-7ºÅģʽÖÐÑ¡Ôñ£¬¼ÓËٺͼõËÙ°´¼ü¿Éµ÷ÕûLEDÁ÷¶¯
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar ModeNo; //ģʽ±àºÅ
uchar Speed;
uchar tCount = 0;
uchar rdx; //ËÙ¶ÈÈ¡ÖµË÷Òý
uchar mb_Count =0; //Òƶ¯Î»Êý
bit Direct= 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)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
}
}
//¼ì²â°´¼ü
uchar Getkey()
{
uchar K;
if(P2==0xFF)return 0;
Delay(10);
switch(P2)
{
case 0xFE:K=1;break;
case 0xFD:K=2;break;
case 0xFB:K=3;break;
default : K=0;
}
while(P2!=0xFF); //µÈ´ýÊÍ·Å°´¼ü
return K;
}
//16Ö»LEDÏÔʾ
void Led_Demo(uint Led16)
{
P1=(uchar)(Led16 & 0x00FF);//ÏÔʾµÍ8λ
P0=(uchar)(Led16 >>8);//ÏÔʾ¸ß8λ
}
//¶¨Ê±Æ÷2
void T0_INT() interrupt 1
{
if(++tCount < Speed) return;
tCount = 0;
switch (ModeNo)
{
case 0:Led_Demo(~0x0001<<mb_Count);break;
case 1:Led_Demo(~0xB000>>mb_Count);break;
case 2:if(Direct)Led_Demo(~0x000F<<mb_Count);
else Led_Demo(~0xF000>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 3:if(Direct)Led_Demo(-(0x000F<<mb_Count));
elseLed_Demo(-(0xF000>>mb_Count));
if(mb_Count==15)Direct=!Direct;
break;
case 4:if(Direct)Led_Demo(0x003F<<mb_Count);
elseLed_Demo(0xFC00>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 5:if(Direct)Led_Demo(0x0001<<mb_Count);
elseLed_Demo(0x8000>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
case 6:if(Direct)Led_Demo(-(0x0001<<mb_Count));
elseLed_Demo(-(0x8000>>mb_Count));
if(mb_Count==15) Direct=!Direct;
break;
case 7:if(Direct)Led_Demo(0xFFFE<<mb_Count);
elseLed_Demo(0x7FFF>>mb_Count);
if(mb_Count==15) Direct=!Direct;
break;
}
mb_Count=(mb_Count+1)%16;
}
//¼üÅÌ°´¼ü´¦Àí
void KeyProcess(uchar Key)
{
switch(Key)
{
case 1: //ÖØÉèģʽ
Direct=1;mb_Count=0;
ModeNo=(ModeNo+1)%8;
P3=DSY_CODE[ModeNo];
break;
case 2://¼ÓËÙ
if(rdx>1)Speed=sTable[--rdx];break;
case 3://¼õËÙ
if(rdx<15)Speed=sTable[++rdx];break;
}
}
//Ö÷³ÌÐò
void main()
{
uchar Key;
P0=P1=P2=P3=0xFF;
ModeNo=0;rdx=4;
Speed=sTable[rdx];
P3=DSY_CODE[ModeNo];
IE=0xB2;
TMOD=0X00;
TR0=1;
while(1)
{
Key=Getkey();
if(Key!=0) KeyProcess(Key);
}
}