51温湿度代码,lcd,报警 串口
单片机源程序如下:
- /*******************************************************************************
- * 实验名 :温度显示实验
- * 使用的IO :
- * 实验效果 :1602显示温度
- * 注意 :
- *******************************************************************************/
- typedef unsigned char U8;
- typedef unsigned short int uint16_t;
- #include<reg51.h>
- #include"lcd.h"
- #include"temp.h"
- sbit beep=P2^0;
- sbit k1=P1^4;//jia
- sbit k2=P1^5;//jian
- unsigned char outdata[14];
- uchar CNCHAR[13] = "set temp:32.0";
- U8 count, count_r=0;
- U8 str[15]={"res23205"};
- int SETTEMP;
- void LcdDisplay(int);
- void UsartConfiguration();
- SendData(U8 *a)
- {
- outdata[0] = a[0];
- outdata[1] = a[1];
- outdata[2] = a[2];
- outdata[3] = a[3];
- outdata[4] = a[4];
- outdata[5] = a[5];
- outdata[6] = a[6];
- outdata[7] = a[7];
- outdata[8] = a[8];
- outdata[9] = a[9];
- outdata[10] = a[10];
- outdata[11] = a[11];
- outdata[12] = a[12];
- outdata[13] = a[13];
-
- SBUF=outdata[0];
- count = 1;
- }
- /*******************************************************************************
- * 函数名 : LcdDisplay()
- * 函数功能 : LCD显示读取到的温度
- * 输入 : v
- * 输出 : 无
- *******************************************************************************/
- void LcdDisplay(int temp) //lcd显示
- {
- unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定义数组
- float tp;
- LcdWriteCom(0x80); //写地址 80表示初始地址
- tp=temp;
- temp=tp*0.0625*100+0.5;
- if((temp/100)>(SETTEMP-1))
- {
- beep=0;
- }
- else
- {
- beep=1;
- }
- //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
- //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
- datas[1] = temp % 10000 / 1000;
- datas[2] = temp % 1000 / 100;
- datas[3] = temp % 100 / 10;
- datas[4] = temp % 10;
-
-
- str[0]=datas[1]+'0';
- str[1]=datas[2]+'0';
- str[2]='.';
- str[3]=datas[3]+'0';
- str[4]='T';
- str[5]=SETTEMP/10+'0';
- str[6]=SETTEMP%10+'0';
- str[7]='U';
- LcdWriteCom(0x80); //写地址 80表示初始地址
- LcdWriteData('t');
- LcdWriteCom(0x81); //写地址 80表示初始地址
- LcdWriteData('e');
- LcdWriteCom(0x82); //写地址 80表示初始地址
- LcdWriteData('m');
- LcdWriteCom(0x83); //写地址 80表示初始地址
- LcdWriteData('p');
- LcdWriteCom(0x84); //写地址 80表示初始地址
- LcdWriteData(':');
- LcdWriteCom(0x85); //写地址 80表示初始地址
- LcdWriteData('0'+datas[1]); //十位
- LcdWriteCom(0x86); //写地址 80表示初始地址
- LcdWriteData('0'+datas[2]); //个位
- LcdWriteCom(0x87); //写地址 80表示初始地址
- LcdWriteData('.'); //显示 ‘.’
- LcdWriteCom(0x88); //写地址 80表示初始地址
- LcdWriteData('0'+datas[3]); //显示小数点
- LcdWriteCom(0x89); //写地址 80表示初始地址
- LcdWriteData('C'); //显示小数点
- LcdWriteCom(0x80+0x40); //写地址 80表示初始地址
- for(i=0;i<=8;i++)
- {
- LcdWriteData(CNCHAR[i]);
- }
- LcdWriteCom(0x80+0x40+13);
- LcdWriteData('C');
- // datas1[0]=SETTEMP/10;
- // datas1[1]=SETTEMP%10;
- // LcdWriteCom(0x80+0x40+9);
- // LcdWriteData(datas1[0]+'0');
- // LcdWriteCom(0x80+0x40+10);
- // LcdWriteData(datas1[1]+'0');
- LcdWriteCom(0x80+0x40+11);
- LcdWriteData('.');
- LcdWriteCom(0x80+0x40+12);
- LcdWriteData('0');
- }
- /*******************************************************************************
- * 函 数 名 :UsartConfiguration()
- * 函数功能 :设置串口
- * 输 入 : 无
- * 输 出 : 无
- *******************************************************************************/
- void UsartConfiguration()
- {
- TMOD = 0x20; //定时器T1使用工作方式2
- TH1 = 253; // 设置初值
- TL1 = 253;
- TR1 = 1; // 开始计时
- SCON = 0x50; //工作方式1,波特率9600bps,允许接收
- ES = 1;
- EA = 1; // 打开所以中断
- TI = 0;
- RI = 0;
- }
-
- void RSINTR() interrupt 4 using 2
- {
- U8 InPut3;
- if(TI==1) //发送中断
- {
- TI=0;
- if(count!=14) //发送完5位数据
- {
- SBUF= outdata[count];
- count++;
- }
- }
- if(RI==1) //接收中断
- {
- InPut3=SBUF;
- if(InPut3=='A')
- {
- SendData(str);
- }
- RI=0;
- }
- }
-
-
- unsigned char drv_uart_rx_bytes( U8* RxBuffer )
- {
- U8 l_RxLength = 0;
- uint16_t l_UartRxTimOut = 0xFFFF;
- while( l_UartRxTimOut-- ) //在超时范围内检测数据
- {
- if( 0 != RI ) //检测是否接收到数据
- {
- RI = 0; //清标志位
- *RxBuffer = SBUF; //读数据
- RxBuffer++;
- l_RxLength++;
- l_UartRxTimOut = 0x7FFF;//重置超时检测
- }
- }
-
- return l_RxLength; //等待超时,数据接收完成
- }
- /*******************************************************************************
- * 函数名 : main
- * 函数功能 : 主函数
- * 输入 : 无
- * 输出 : 无
- *******************************************************************************/
- void main()
- {
- int temp;
- int datas1[] = {0, 0, 0, 0, 0};
- UsartConfiguration();
- LcdInit(); //初始化LCD1602
- LcdWriteCom(0x85); //写地址 80表示初始地址
- LcdWriteData('C');
- SETTEMP=32;
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- while(1)
- {
- temp=Ds18b20ReadTemp();
- LcdDisplay(temp);
- Delay1ms(1000);//1s钟刷一次
- if(k1==0)
- {
- Delay1ms(10);
- SETTEMP=SETTEMP+1;
- Delay1ms(1000);//1s钟刷一次
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
- }
- if(k2==0)
- {
- Delay1ms(10);
- SETTEMP=SETTEMP-1;
- datas1[0]=SETTEMP/10;
- datas1[1]=SETTEMP%10;
- LcdWriteCom(0x80+0x40+9);
- LcdWriteData(datas1[0]+'0');
- LcdWriteCom(0x80+0x40+10);
- LcdWriteData(datas1[1]+'0');
-
- }
- }
- }
复制代码
所有资料51hei提供下载:
程序.rar
(64.85 KB, 下载次数: 19)
|