仿真文件的下載地址:http://www.torrancerestoration.com/f/csdwpjs.rar
#include"lcd1602.h"
#define uchar unsigned char
#define uint unsigned int
uchar out[6];
sbit STAR_KEY =P3^2;//定義鍵與單片機(jī)的連接引腳
sbit L0=P1^0; //定義SFR中引腳的位
sbit L1=P1^1;
sbit L2=P1^2;
sbit L3=P1^3;
sbit L4=P1^4;
sbit L5=P1^5;
sbit L6=P1^6;
sbit L7=P1^7;
sbit MOTOR=P3^0;
uchar n[2]={1,0};
void delayms(uint x) //延時(shí)子程序
{
uchar y;
while(x--)
{
for(y=0;y<123;y++){;}
}
}
uchar Keynum() //按鍵子程序1
{
uchar key=0;
STAR_KEY=1; //置初值
if(STAR_KEY==0){delayms(10);if(STAR_KEY==0)n[0]=0;n[1]=1;} //按下STAR鍵則n[0]=0;n[1]=1
return n[1]; //返回n值
}
void xianshi(uint dispbuf) //顯示子程序
{
out[0]=dispbuf/10000+0x30;
out[1]=((dispbuf%10000)/1000)+0x30;
out[2]=((dispbuf%1000)/100)+0x30;
out[3]=((dispbuf%100)/10)+0x30;
out[4]=dispbuf%10+0x30;
out[5]='\0';
LCD_Initial(); //LCD初始化
LCD_Prints(1, 0,"The Speed Is"); //LCD輸出
LCD_Prints(10, 1,"n/min");
LCD_Prints(4, 1,out);
}
uint qiuzhi() //求用撥碼盤所置數(shù)的值的子程序
{
uchar a;
uchar b;
uchar c;
uchar d;
uchar e;
uchar f;
uchar g;
uchar h;
uint p=0;
if (L0==1) {a=1;} else {a=0;} //將電平信號(hào)變?yōu)閿?shù)字的值
if (L1==1) {b=1;} else {b=0;}
if (L2==1) {c=1;} else {c=0;}
if (L3==1) {d=1;} else {d=0;}
if (L4==1) {e=1;} else {e=0;}
if (L5==1) {f=1;} else {f=0;}
if (L6==1) {g=1;} else {g=0;}
if (L7==1) {h=1;} else {h=0;}
p=a+b*2+c*2*2+d*2*2*2+e*2*2*2*2+f*2*2*2*2*2+g*2*2*2*2*2*2+h*2*2*2*2*2*2*2;
return p; // 返回所求的P值
}
void main(void) //主程序
{
uint q;
IT0=1; //負(fù)跳變觸發(fā)
EA=1; //開總允許中斷
EX0=1; //開INTO中斷
TMOD=0X05; //置T0為計(jì)數(shù)器 方式1
TL0=0; //置計(jì)數(shù)器初值
TH0=0;
while(1) //無限循環(huán)
{
q=qiuzhi(); //調(diào)用求值子程序求出所置的數(shù)
if (MOTOR==1) {xianshi(q);} else {xianshi(TL0);Keynum();TR0=n[1];} //當(dāng)電動(dòng)機(jī)關(guān)閉時(shí)顯示用薄碼盤所置的數(shù)并停止計(jì)數(shù),電機(jī)啟動(dòng)時(shí)則開始計(jì)數(shù),并顯示計(jì)數(shù)值
if (TL0==q) {TR0=0;MOTOR=1;} else {Keynum();MOTOR=n[0];} //當(dāng)計(jì)數(shù)值與所置數(shù)相同時(shí)關(guān)閉電機(jī),停止計(jì)數(shù),當(dāng)計(jì)數(shù)值不等時(shí)則將電機(jī)啟動(dòng)與關(guān)閉交由按鍵控制,所以不存在計(jì)數(shù)值超過置數(shù)值的情況
}
}
void Int0(void) interrupt 0 //中斷服務(wù)程序,工作寄存器用0組
{
TL0=0;TR0=1; //重置計(jì)數(shù)值,重新開始計(jì)數(shù)
}