測水流量的~
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (15.7 KB, 下載次數: 83)
下載附件
2018-4-25 01:46 上傳
0.png (48.54 KB, 下載次數: 80)
下載附件
2018-4-25 01:46 上傳
單片機源程序如下:
- #include<REG52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar temp_1[]={"00.00M3/H 000.0L"}; //顯示第一行
- uchar temp_2[]={"SI:0.0L "}; //顯示第2行
-
- sbit k1=P2^0;//按鍵定義 分別4個開關
- sbit k2=P2^1;
- sbit k3=P2^2;
- uint liuliang; //變量定義
- uint shudu;
- uint M1;
- uint M2;
- uchar TEMP=01;
- //Port Definitions**********************************************************
- sbit LcdRs = P1^0;
- sbit LcdRw = P1^1;
- sbit LcdEn = P1^2;
- sfr DBPort = 0x80; //P0=0x80,P1=0x90,P2=0xA0,P3=0xB0.數據端口
- void delay(uint z)
- {
- uint a,b;
- for(a=z;a>0;a--)
- for(b=120;b>0;b--);
- }
- //內部等待函數**************************************************************************
- unsigned char LCD_Wait(void)
- {
- LcdRs=0;
- LcdRw=1; _nop_();
- LcdEn=1; _nop_();
- LcdEn=0;
- return DBPort;
- }
- //向LCD寫入命令或數據************************************************************
- #define LCD_COMMAND 0 // Command
- #define LCD_DATA 1 // Data
- #define LCD_CLEAR_SCREEN 0x01 // 清屏
- #define LCD_HOMING 0x02 // 光標返回原點
- void LCD_Write(bit style, unsigned char input)
- {
- LcdEn=0;
- LcdRs=style;
- LcdRw=0; _nop_();
- DBPort=input; _nop_();//注意順序
- LcdEn=1; _nop_();//注意順序
- LcdEn=0; _nop_();
- LCD_Wait();
- }
- //設置顯示模式************************************************************
- #define LCD_SHOW 0x04 //顯示開
- #define LCD_HIDE 0x00 //顯示關
- #define LCD_CURSOR 0x02 //顯示光標
- #define LCD_NO_CURSOR 0x00 //無光標
- #define LCD_FLASH 0x01 //光標閃動
- #define LCD_NO_FLASH 0x00 //光標不閃動
- void LCD_SetDisplay(unsigned char DisplayMode)
- {
- LCD_Write(LCD_COMMAND, 0x08|DisplayMode);
- }
- //設置輸入模式************************************************************
- #define LCD_AC_UP 0x02
- #define LCD_AC_DOWN 0x00 // default
- #define LCD_MOVE 0x01 // 畫面可平移
- #define LCD_NO_MOVE 0x00 //default
- void LCD_SetInput(unsigned char InputMode)
- {
- LCD_Write(LCD_COMMAND, 0x04|InputMode);
- }
- //初始化LCD************************************************************
- void LCD_Initial()
- {
- LcdEn=0;
- LCD_Write(LCD_COMMAND,0x38); //8位數據端口,2行顯示,5*7點陣
- delay(5);
- LCD_Write(LCD_COMMAND,0x38);
- delay(5);
- LCD_SetDisplay(LCD_SHOW|LCD_NO_CURSOR); //開啟顯示, 無光標
- delay(5);
- LCD_Write(LCD_COMMAND,LCD_CLEAR_SCREEN); //清屏
- delay(5);
- LCD_SetInput(LCD_AC_UP|LCD_NO_MOVE); //AC遞增, 畫面不動
- delay(5);
- }
- //液晶字符輸入的位置************************
- void GotoXY(unsigned char x, unsigned char y)
- {
- if(y==0)
- LCD_Write(LCD_COMMAND,0x80|x);
- if(y==1)
- LCD_Write(LCD_COMMAND,0x80|(x-0x40));
- }
- //將字符輸出到液晶顯示
- void Print(unsigned char *str)
- {
- while(*str!='\0')
- {
- LCD_Write(LCD_DATA,*str);
- str++;
- delay(1);
- }
- }
- void key()// 按鍵檢測
- {
- if(k1==0)//設置預置數加
- {
- delay(5);
- if(k1==0)//清零
- {
- M2=0;
-
- while(k1==0);
- }
- }
-
-
- if(k2==0)//設置預置數加
- {
- delay(5);
- if(k2==0)
- {
- TEMP+=1;
- if(TEMP>=99)
- TEMP=99;
- while(k2==0);
- }
- }
- if(k3==0)//設置預置數減
- {
- delay(5);
- if(k3==0)
- {
- if(TEMP>=1)
- TEMP-=1;
- else
- TEMP=0;
- while(k3==0);
- }
- }
-
-
- }
- main()
- {
- EX0=1;//開外部中斷0
- PX0=1;// 外部中斷0高優先級
- IT0=1;//觸發模式0= 電平 1=邊沿觸發
- TMOD = 0x11;
- ET1=1;
- TR1=1;
- TH1 = (65535-50000)/256;
- TL1 = (65535-50000)%256;
- EA=1;
- LCD_Initial(); //液晶初始化
- while(1)
- {
- key();// 按鍵檢測
-
- liuliang=M2*TEMP;
- temp_1[0]=shudu/1000+0x30;//轉換成液晶字符下同
- temp_1[1]=shudu/100%10+0x30;
- temp_1[3]=shudu/10%10+0x30;
- temp_1[4]=shudu%10+0x30;
-
- temp_1[10]=liuliang/1000+0x30;
- temp_1[11]=liuliang/100%10+0x30;
- temp_1[12]=liuliang/10%10+0x30;
- temp_1[14]=liuliang%10+0x30;
-
- temp_2[3]=TEMP/10+0x30;
- temp_2[5]=TEMP%10+0x30;
-
-
- GotoXY(0,0);//顯示起始位置 Y=0,第一行,Y=1 第二行//顯示第一行
- Print(temp_1);// 內容
- GotoXY(0,1);//顯示起始位置 Y=0,第一行,Y=1 第二行//顯示第一行
- Print(temp_2);// 內容
- }
- }
-
- void my_INT0(void) interrupt 0
- {
- M1++;
- M2++;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
LCD液體流量計.zip
(104.7 KB, 下載次數: 303)
2018-4-24 19:12 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|