|
|
51溫濕度代碼,lcd,報(bào)警 串口
單片機(jī)源程序如下:
- /*******************************************************************************
- * 實(shí)驗(yàn)名 :溫度顯示實(shí)驗(yàn)
- * 使用的IO :
- * 實(shí)驗(yàn)效果 :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;
- }
- /*******************************************************************************
- * 函數(shù)名 : LcdDisplay()
- * 函數(shù)功能 : LCD顯示讀取到的溫度
- * 輸入 : v
- * 輸出 : 無
- *******************************************************************************/
- void LcdDisplay(int temp) //lcd顯示
- {
- unsigned char i, datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
- float tp;
- LcdWriteCom(0x80); //寫地址 80表示初始地址
- tp=temp;
- temp=tp*0.0625*100+0.5;
- if((temp/100)>(SETTEMP-1))
- {
- beep=0;
- }
- else
- {
- beep=1;
- }
- //留兩個(gè)小數(shù)點(diǎn)就*100,+0.5是四舍五入,因?yàn)镃語言浮點(diǎn)數(shù)轉(zhuǎn)換為整型的時(shí)候把小數(shù)點(diǎn)
- //后面的數(shù)自動(dòng)去掉,不管是否大于0.5,而+0.5之后大于0.5的就是進(jìn)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]); //個(gè)位
- LcdWriteCom(0x87); //寫地址 80表示初始地址
- LcdWriteData('.'); //顯示 ‘.’
- LcdWriteCom(0x88); //寫地址 80表示初始地址
- LcdWriteData('0'+datas[3]); //顯示小數(shù)點(diǎn)
- LcdWriteCom(0x89); //寫地址 80表示初始地址
- LcdWriteData('C'); //顯示小數(shù)點(diǎn)
- 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');
- }
- /*******************************************************************************
- * 函 數(shù) 名 :UsartConfiguration()
- * 函數(shù)功能 :設(shè)置串口
- * 輸 入 : 無
- * 輸 出 : 無
- *******************************************************************************/
- void UsartConfiguration()
- {
- TMOD = 0x20; //定時(shí)器T1使用工作方式2
- TH1 = 253; // 設(shè)置初值
- TL1 = 253;
- TR1 = 1; // 開始計(jì)時(shí)
- SCON = 0x50; //工作方式1,波特率9600bps,允許接收
- ES = 1;
- EA = 1; // 打開所以中斷
- TI = 0;
- RI = 0;
- }
-
- void RSINTR() interrupt 4 using 2
- {
- U8 InPut3;
- if(TI==1) //發(fā)送中斷
- {
- TI=0;
- if(count!=14) //發(fā)送完5位數(shù)據(jù)
- {
- 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-- ) //在超時(shí)范圍內(nèi)檢測數(shù)據(jù)
- {
- if( 0 != RI ) //檢測是否接收到數(shù)據(jù)
- {
- RI = 0; //清標(biāo)志位
- *RxBuffer = SBUF; //讀數(shù)據(jù)
- RxBuffer++;
- l_RxLength++;
- l_UartRxTimOut = 0x7FFF;//重置超時(shí)檢測
- }
- }
-
- return l_RxLength; //等待超時(shí),數(shù)據(jù)接收完成
- }
- /*******************************************************************************
- * 函數(shù)名 : main
- * 函數(shù)功能 : 主函數(shù)
- * 輸入 : 無
- * 輸出 : 無
- *******************************************************************************/
- 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');
-
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
程序.rar
(64.85 KB, 下載次數(shù): 19)
2018-6-19 21:09 上傳
點(diǎn)擊文件名下載附件
51代碼 下載積分: 黑幣 -5
|
|