基于51的水温水位检测调节,采用SR04、12864、DS1820
单片机源程序如下:
- #include<reg52.h>
- #include "12864.h"
- #include "delay.h"
- #include "sr04.h"
- #include "ds18b20.h"
- #include "delay.h"
- #include "beep.h"
- sbit s1=P1^2;
- sbit s2=P3^7;
- sbit KEY1=P1^3;
- sbit KEY2=P1^4;
- sbit KEY3=P1^5;
- extern uchar table1[];
- extern uint time,s;
- extern uchar code table5[];
- extern uchar code table6[];
- uchar num,num2;
- uchar j,mode;
- uchar weidu=25;
- uchar shuiwei=5;
- uchar ge,si;
- uchar set_w=25;
- uchar set_s=2;
- bit res;
- int temp; //读取到的当前温度值
- int intT, decT; //温度值的整数和小数部分
- unsigned char len;
- unsigned char str[12];
- void get_temp();
- unsigned char IntToString(unsigned char *str, int dat);
- void judge();
- void keyscan();
- void main()
- {
- s1=0;s2=0; // 继电器关
- Start18B20(); //启动DS18B20
- lcd_init(); //液晶初始
- TMOD=0X10; // 定时器
- TH1=(65536-45872)/256;//50ms
- TL1=(65536-45872)%256;
- EA=1;
- ET1=1;
- TR1=1;
-
- while(1) // 死循环
- {
-
- if((num==10)&&(mode==0)) // 500ms刷新时间和显示界面0
- {
- num=0;
- get_temp(); sr04();// 获得温度 距离
- lcd_pos(0,3); j=0; // 显示距离
- while(table1[j]!='\0' )
- {
- write_dat(table1[j]); j++;
- }
-
- }
- if(mode==0)
- {judge();} // 水位水温调节
- keyscan();
- }
- }
- void judge()
- {
- if(s<set_s) // 水位判断
- {
- s2=1; lcd_pos(1,5); j=0;
- while(table6[j]!='\0' )
- {
- write_dat(table6[j]); j++;
- }
- BEEP=1;
- }
- else
- {
- s2=0; lcd_pos(1,5); j=0;
- while(table5[j]!='\0' )
- {
- write_dat(table5[j]); j++;
- }
- BEEP=0;
- }
- if(intT<set_w) // 温度判断
- {
- s1=1;lcd_pos(3,6); j=0;
- while(table6[j]!='\0' )
- {
- write_dat(table6[j]); j++;
- }
- }
- else
- {
- s1=0;lcd_pos(3,6); j=0;
- while(table5[j]!='\0' )
- {
- write_dat(table5[j]); j++;
- }
- }
- }
- void T1_time() interrupt 3
- {
- TH1=(65536-45872)/256;
- TL1=(65536-45872)%256;
- num++;
- }
- /* 整型数转换为字符串,str-字符串指针,dat-待转换数,返回值-字符串长度 */
- unsigned char IntToString(unsigned char *str, int dat)
- {
- signed char i = 0;
- unsigned char len = 0;
- unsigned char buf[6];
-
- if (dat < 0) //如果为负数,首先取绝对值,并在指针上添加负号
- {
- dat = -dat;
- *str++ = '-';
- len++;
- }
- do { //先转换为低位在前的十进制数组
- buf[i++] = dat % 10;
- dat /= 10;
- } while (dat > 0);
- len += i; //i最后的值就是有效字符的个数
- while (i-- > 0) //将数组值转换为ASCII码反向拷贝到接收指针上
- {
- *str++ = buf[i] + '0';
- }
- *str = '\0'; //添加字符串结束符
-
- return len; //返回字符串长度
- }
- void get_temp()
- {
- res = Get18B20Temp(&temp); //读取当前温度
- if (res) //读取成功时,刷新当前温度显示
- {
- intT = temp >> 4; //分离出温度值整数部分
- decT = temp & 0xF; //分离出温度值小数部分
- len = IntToString(str, intT); //整数部分转换为字符串
- str[len++] = '.'; //添加小数点
- decT = (decT*10) / 16; //二进制的小数部分转换为1位十进制位
- str[len++] = decT + '0'; //十进制小数位再转换为ASCII字符
- while (len < 6) //用空格补齐到6个字符长度
- {
- str[len++] = ' ';
- }
- str[len] = '\0'; //添加字符串结束符
- lcd_pos(2,3); j=0;
- while(str[j]!='\0' ) // 显示温度
- {
- write_dat(str[j]);
- j++;
- }
- }
- else //读取失败时,提示错误信息
- {
- //LcdShowStr(0, 0, "error!");
- }
- Start18B20(); //重新启动下一次转换
- }
- void keyscan()
- {
- if(KEY1==0) // set
- {
- delay(5);
- if(KEY1==0)
- {
- while(!KEY1);
- mode++;
- }
-
- }
- if(mode==1) //界面1
- {
- clear(); //界面1显示
- if(KEY2==0)
- {
- delay(5);
- if(KEY2==0)
- {
- shuiwei++;
- if(shuiwei>9)
- {shuiwei=9;}
-
- }
- }
- if(KEY3==0)
- {
- delay(5);
- if(KEY3==0)
- {
- shuiwei--;
- if(shuiwei==255)
- {shuiwei=0;}
- }
- }
- lcd_pos(2,5);
- write_dat(shuiwei+0x30);
- write_dat( );
- }
- if(mode==2) //界面2
- {
-
- mode2();// 界面显示
- if(KEY2==0)
- {
- delay(5);
- if(KEY2==0)
- { while(!KEY2);
- weidu++;
- if(weidu>50)
- {weidu=50;}
-
- }
- }
- if(KEY3==0)
- {
- delay(5);
- if(KEY3==0)
- { while(!KEY3);
- weidu--;
- if(weidu==255)
- {weidu=0;}
- }
- }
- ge=weidu%10;
- si=weidu/10;
- lcd_pos(2,5);
- write_dat(si+0x30);
- write_dat(ge+0x30);
- }
- if(mode==3)
- {
- mode=0;lcd_init();// 界面0 及 显示
- set_w=weidu;
- set_s=shuiwei;
- }
- }
复制代码
所有资料51hei提供下载:
1.程序.zip
(76.12 KB, 下载次数: 37)
|