|
智能水杯-
- /*連線說(shuō)明: 藍(lán)牙: RXD--P43
- 液晶 RXD--P31
- DHT11 DATA--P20
- */
- /*************** 用戶定義參數(shù) *****************************/
- #define MAIN_Fosc 11059200L //define main clock
- #define Baudrate1 9600 //define the baudrate, 如果使用BRT做波特率發(fā)生器,則波特率跟串口2一樣
- //12T mode: 600~115200 for 22.1184MHZ, 300~57600 for 11.0592MHZ
- #define Baudrate2 9600 //define the baudrate2,
- //12T mode: 600~115200 for 22.1184MHZ, 300~57600 for 11.0592MHZ
- #define BUF_LENTH 1 //定義串口接收緩沖長(zhǎng)度
- /**********************************************************/
- #include <STC12C5A60S2.h>
- #include <DS18B20.h>
- #define U8 unsigned char
- #define U16 unsigned int
- unsigned char uart1_wr; //寫(xiě)指針
- unsigned char uart1_rd; //讀指針
- unsigned char xdata RX1_Buffer[BUF_LENTH];
- bit B_TI,flag;
- unsigned char uart2_wr; //寫(xiě)指針
- unsigned char uart2_rd; //讀指針
- unsigned char xdata RX2_Buffer[BUF_LENTH];
- bit B_TI2;
- sbit JDQ=P3^7;
- bit sec;
- U8 state,cnt;
- int TempThreshold,TH;
- /****************** 編譯器自動(dòng)生成,用戶請(qǐng)勿修改 ************************************/
- #define T1_TimerReload (256 - MAIN_Fosc / 192 / Baudrate1) //Calculate the timer1 reload value at 12T mode
- #define BRT_Reload (256 - MAIN_Fosc / 192 / Baudrate2) //Calculate BRT reload value
- #define TimeOut1 (28800 / (unsigned long)Baudrate1 + 2)
- #define TimeOut2 (28800 / (unsigned long)Baudrate2 + 2)
- #define TI2 (S2CON & 0x02) != 0
- #define RI2 (S2CON & 0x01) != 0
- #define CLR_TI2() S2CON &= ~0x02
- #define CLR_RI2() S2CON &= ~0x01
- /******************** 本地函數(shù)聲明 ***************/
- void uart1_init(void);
- void uart2_init(void);
- void UART1_TxByte(unsigned char dat);
- void UART2_TxByte(unsigned char dat);
- void PrintString1(unsigned char *puts);
- void PrintString2(unsigned char *puts);
- /**********************************************************/
- //液晶相關(guān)函數(shù),利用串口2完成數(shù)據(jù)收發(fā)功能
- void UART1_TxByte(unsigned char dat)
- {
- B_TI = 0;
- SBUF = dat;
- while(!B_TI);
- B_TI = 0;
- }
- void UART2_TxByte(unsigned char dat)
- {
- B_TI2 = 0;
- S2BUF = dat;
- while(!B_TI2);
- B_TI2 = 0;
- }
- void PrintString1(unsigned char *puts) //發(fā)送一串字符串
- {
- for (; *puts != 0; puts++) UART1_TxByte(*puts); //遇到停止符0結(jié)束
- }
- void PrintString2(unsigned char *puts) //發(fā)送一串字符串
- {
- for (; *puts != 0; puts++) UART2_TxByte(*puts); //遇到停止符0結(jié)束
- }
- void uart1_init(void)
- {
- PCON |= 0x80; //UART0 Double Rate Enable
- SCON = 0x50; //UART0 set as 10bit , UART0 RX enable
- TMOD &= ~(1<<6); //Timer1 Set as Timer, 12T
- TMOD = (TMOD & ~0x30) | 0x20; //Timer1 set as 8 bits auto relaod
- TH1 = T1_TimerReload; //Load the timer
- TR1 = 1;
- ES = 1;
- EA = 1;
- }
- /**********************************************/
- void UART0_RCV (void) interrupt 4
- {
- if(RI)
- {
- RI = 0;
- RX1_Buffer[uart1_wr] = SBUF;
- if(++uart1_wr >= BUF_LENTH) uart1_wr = 0;
- }
- if(TI)
- {
- TI = 0;
- B_TI = 1;
- }
- }
- /**********************************************/
- void uart2_init(void)
- {
- AUXR |= (1 << 3); //串口2波特率加倍
- S2CON = (S2CON & 0x3f) | (1<<6); //串口2模式1,8位UART,(2^S2SMOD / 32) * BRT溢出率
- S2CON |= 1 << 4; //允許串2接收
- AUXR |= 1 << 4; //baudrate use BRT
- BRT = BRT_Reload;
- IE2 |= 1; //允許串口2中斷
- }
- /**********************************************/
- void UART2_RCV (void) interrupt 8
- {
- if(RI2)
- {
- CLR_RI2();
- RX2_Buffer[uart2_wr] = S2BUF;
- flag=1;
- if(++uart2_wr >= BUF_LENTH) uart2_wr = 0;
- }
- if(TI2)
- {
- CLR_TI2();
- B_TI2 = 1;
- }
- }
- /**********************************************************/
- void InitT0()
- {
- TMOD|=0X01;
- IE|=0X82;
- TR0=1;
- }
- void T0Ser() interrupt 1
- {
- if(++cnt==16)
- {
- cnt=0;
- sec=1;
- }
- }
- void Uart2Enter()
- {
- UART2_TxByte(0x0d);
- UART2_TxByte(0x0a);
- }
- void Uart1Enter()
- {
- UART1_TxByte(0x0d);
- UART1_TxByte(0x0a);
- }
- void dis_LanYa(void)
- {
- char temp[]="TH=25.0C";
- temp[3]=TempThreshold/100+48;
- temp[4]=TempThreshold%100/10+48;
- temp[6]=TempThreshold%10+48;
- UART2_TxByte('T');
- UART2_TxByte('=');
- UART2_TxByte(TH%1000/100+0x30);
- UART2_TxByte(TH%100/10+0x30);
- UART2_TxByte('.');
- UART2_TxByte(TH%10+0x30);
- PrintString2("C");
- Uart2Enter();
-
- PrintString2(temp);
- Uart2Enter();
- Uart2Enter();
- }
- void dis_LanYa2(void)
- {
- char temp[]="TH=25.0C";
- temp[3]=TempThreshold/100+48;
- temp[4]=TempThreshold%100/10+48;
- temp[6]=TempThreshold%10+48;
- UART1_TxByte('T');
- UART1_TxByte('=');
- UART1_TxByte(TH%1000/100+0x30);
- UART1_TxByte(TH%100/10+0x30);
- UART1_TxByte('.');
- UART1_TxByte(TH%10+0x30);
- PrintString1("C");
- PrintString1(temp);
- Uart1Enter();
- Uart1Enter();
-
- }
- void main(void)
- {
- unsigned int oldT,oldTh;
- InitT0();
- uart1_rd = 0;
- uart1_wr = 0;
- uart2_rd = 0;
- uart2_wr = 0;
- // AUXR |= 0x01; //串口1使用獨(dú)立波特率發(fā)生器, 波特率跟串口2一樣
- AUXR1 |= (1<<4); //將UART2從P1口切換到 RXD2--P1.2切換到P4.2 TXD2---P1.3切換到P4.3
-
- uart1_init();
- uart2_init();
-
- state=0; //==0 正常模式,==1設(shè)置模式
- TempThreshold=400;
-
- P3M1&=0X7F; //強(qiáng)推挽模式,推動(dòng)繼電器
- P3M0|=0X80;
- PrintString1("Hello");
- while(1)
- {
- if(flag) //串行口2(藍(lán)牙)接收到數(shù)據(jù) A(溫度增加) B(溫度減少)
- {
- flag=0;
- if(RX2_Buffer[0]=='A' || RX2_Buffer[0]=='a')
- {
- TempThreshold+=10; //目標(biāo)溫度++
- if(TempThreshold>800)
- {
- PrintString2("Warning,you can't set the value greater than 80C");
- Uart2Enter();
- TempThreshold=800;
- }
- }
- else if(RX2_Buffer[0]=='B' || RX2_Buffer[0]=='b')
- {
- TempThreshold-=10; //目標(biāo)溫度--
- if(TempThreshold<40)
- {
- PrintString2("Warning,you can't set the value less than 4C");
- Uart2Enter();
- TempThreshold=40;
- }
- }
- }
- if(sec)
- {
- sec=0;
- TH=Get_temp();
-
- if((oldT!=TH)||(TempThreshold!=oldTh))//如果溫度有變化或者設(shè)置的閾值有變化,才更新顯示
- {
- oldT=TH;
- oldTh=TempThreshold;
- dis_LanYa();
- dis_LanYa2();
-
- if(TH>=TempThreshold-1) //還差1度就停止加熱
- {
- PrintString2("Stop heat");
- Uart2Enter();
- JDQ=0;//關(guān)閉加熱裝置
- }
- else if(TH<TempThreshold-1) //相差1攝氏度再開(kāi)啟加熱
- {
- JDQ=1;// 打開(kāi)加熱裝置
- PrintString2("Heating");
- Uart2Enter();
- }
- }
-
- }
- }
- }
復(fù)制代碼
|
-
-
智能水杯.7z
2019-8-27 15:35 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
29.96 KB, 下載次數(shù): 7, 下載積分: 黑幣 -5
|