標(biāo)題:
51單片機(jī)流水燈及計數(shù)器程序
[打印本頁]
作者:
weilexuexi
時間:
2017-5-10 17:31
標(biāo)題:
51單片機(jī)流水燈及計數(shù)器程序
無標(biāo)題.png
(71.28 KB, 下載次數(shù): 62)
下載附件
2017-5-10 17:31 上傳
#include<reg51.h>
unsigned char led[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned charled_mod[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char j=0;
unsigned char LCD_Status;
bit dir=0,run=0;
sbit RS=P1^6;
sbit RW=P1^7;
void delay(unsigned int count) //延時函數(shù)
{ unsigned char i;
while(count--)
for(i=0;i<120;i++);
}
unsigned char Busy_Check() //檢查忙函數(shù)
{ RS=0;RW=1;
P2=0x4f;
delay(2);
LCD_Status=P0; //讀出LCD的狀態(tài)
delay(2);
P2=0x8f;
return LCD_Status;
}
void wcmd(unsigned char cmd) //寫命令函數(shù)
{ while((Busy_Check() &0x80)==0x80);
RS=0;RW=0;
P2=0x4f;
P0=cmd; //命令由P0口送入LCD
delay(2);
P2=0x8f;
}
void wdat(unsigned char dat) //寫數(shù)據(jù)函數(shù)
{ while((Busy_Check() &0x80)==0x80);
RS=1;RW=0;
P2=0x4f;
P0=dat; //數(shù)據(jù)由P0口送入LCD
delay(2);
P2=0x8f;
}
void LCD_init() //初始化函數(shù)
{ wcmd(0x38); //38H=0011 1000,使用8位,用5×7的字型,2行
delay(20);
wcmd(0x01); //01H=0000 0001,清屏
delay(20);
wcmd(0x06); //06H=0000 0110,字符不動,光標(biāo)自動右移一格
delay(20);
wcmd(0x0e); //0eH=0000 1110,開顯示,有光標(biāo),字符不閃爍
delay(20);
}
void main(){ //主函數(shù),流水燈運(yùn)行
signed char j;
IT1=1;
EX1=1;
EA=1;
PX1=0;
while(1){
switch(P3&0x3c){
case 0x38:run=1;dir=1;break;
case 0x34:run=0;dir=0;break;
case 0x2c:dir=0;break;
case 0x1c:dir=1;break;}
if(run==1)
if(dir==1)
for(j=0;j<=7;j++){
P2=led[j];
delay(500);
}
else
for(j=7;j>=0;j--){
P2=led[j];
delay(500);
}
else P2=0xff;
}}
K2() interrupt 2{ //中斷函數(shù),LCD顯示,計數(shù)開始
LCD_init();
wcmd(0x80+00);
wdat('1');
wdat('1');
wdat('1');
wdat('2');
wdat('2');
wdat('2');
wdat('3');
wdat('3');
wdat('3');
wdat('4');
wdat('4');
wdat('4');
wcmd(0x80+0x40);
wdat('B');
wdat('1');
wdat('1');
wdat('2');
wdat('2');
wdat('3');
wdat('3');
wdat('4');
wdat('4');
for(j=0;j<=99;j++)
{P2=0x8f;
P0=led_mod[j/10];
P2=0x1f;
P0=led_mod[j%10];
delay(500);
P0=0;}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1