- #include <reg51.h>
- #include "lcd.h"
- #define uchar unsigned char
- #define uint unsigned int
- sbit key1=P2^1;
- uint z=0,count=0,msec=0,zhuan;
- uchar display[]={"speed=XXXXr/s "}; //定義顯示參數(shù)
- /*-----------------------毫秒延時------------------------*/
- void delay(uint ms)
- {
- uint m,j;
- for (j=0;j<ms;j++)
- for (m=0;m<120;m++);
- }
- void displaytolcd()
- {
- uint i;
- LcdWriteCom(0x80); //向LCD1602寫命令80H ,定義光標(biāo)位置為首位
- for (i=0;i<sizeof(display);i++)
- {
- LcdWriteData(display[i]);
- delay(10);
- }
- }
- void disp_count()
- {
- display[6]=(zhuan/1000+'0'); //轉(zhuǎn)換轉(zhuǎn)速的千位
- display[7]=(zhuan/100%10+'0'); //轉(zhuǎn)換轉(zhuǎn)速的百位
- display[8]=(zhuan/10%10+'0'); //轉(zhuǎn)換轉(zhuǎn)速的十位
- display[9]=(zhuan%10+'0'); //轉(zhuǎn)換轉(zhuǎn)速的個位
- }
- /*-------------------外部中斷0計數(shù)程序-------------------*/
- void counter() interrupt 0
- {
- if(!key1){count+=1;
- if(count==2) //2次循環(huán)為發(fā)動機(jī)轉(zhuǎn)一圈
- {
- z+=1; //轉(zhuǎn)圈計數(shù)加1
- count=1; //初始化計數(shù)
- }
- }
- }
- /*-----------------內(nèi)部中斷1計時計數(shù)程序-----------------*/
- void Timer_0() interrupt 1
- {
- TH0=0xDC; //10ms定時
- TL0=0x00;
- msec+=1;
- }
- void int_all()
- {
- z=0; //初始化z的值
- count=0; //初始化count的值
- zhuan=0; //初始化轉(zhuǎn)的值
- TMOD=0x01; //內(nèi)部中斷定時器選擇
- TH0=0xDC; //10ms定時
- TL0=0x00;
- EA=1; //開中斷總開關(guān)
- ET0=1; //開內(nèi)部中斷0
- TR0=1; //計時器開始工作
- IT0=1; //外部中斷0為下降沿觸發(fā)
- EX0=1; //開外部中斷0
- EX1=0;
- IT1=0;
- }
- /**********************************
- 函數(shù)聲明
- **********************************/
- void LcdWriteCom(uchar com); /*LCD1602寫入8位命令子函數(shù) */
- void LcdWriteData(uchar dat); /*LCD1602寫入8位數(shù)據(jù)子函數(shù)*/
- void LcdInit(); /*LCD1602初始化子程序*/
- void main()
- {
- int_all();
- LcdInit();
- while(1)
- {
- if(msec==10)
- {
- zhuan=10*z;
- /////z=0;
- msec=0;
- }
- display[13]=z%10+'0';
- disp_count(); //數(shù)據(jù)處理
- displaytolcd(); //LCD顯示
- }
- }
復(fù)制代碼
程序如上,自己從網(wǎng)上查資料然后修修改改就這樣,其中l(wèi)cd初始化什么的程序就沒有寫在這里了。
其中/*-------------------外部中斷0計數(shù)程序-------------------*/
void counter() interrupt 0 { if(!key1){count+=1; if(count==2) //2次循環(huán)為發(fā)動機(jī)轉(zhuǎn)一圈 { z+=1; //轉(zhuǎn)圈計數(shù)加1 count=1; //初始化計數(shù) } } } 這一部分程序不知道錯在哪里,z的值總是0,這導(dǎo)致轉(zhuǎn)速顯示出來老是0。也許是程序其它的問題,我看了一整天還沒發(fā)現(xiàn)。求解答~!
|