將程序中風扇的控制方式改成自動控制,要有程序注解,附 件有電路圖和源程序
電路原理圖如下:
0.png (76.83 KB, 下載次數: 30)
下載附件
2019-4-28 14:08 上傳
單片機源程序如下:
- #include "STC12C5A60S2.H"
- #include "STC12C5A60S2_EEPROM.c"
- #include <math.h>
- #include "LCD1602.H"
- #include"DS18b20.h"
- bit send_success=0;
- //----------定義排氣扇開關
- sbit Fan=P1^6;
- uchar grade=0;
- bit star=0;
- uchar scale=41,n; //轉速控制變量
- uchar cp=0;
- uint Time_50ms; //50ms變量
- int TP_H;
- uchar page_flag=0;
- int temp=0,temp1=0,temp2=0;//溫度
- bit Read_DHT_Dat=0;
- bit TP_H_flag=0;
- bit flag=0;
- uchar state=0; //轉態切換變量
- bit memory_flag=0; //存儲標志位
- //---------->定義指示燈IO口
- sbit Led1=P3^3;
- sbit Led2=P1^4;
- //---------->定義按鍵IO口
- sbit key_1=P1^0;
- sbit key_2=P1^1;
- sbit key_3=P3^2;
- bit key1_flag=0;
- bit key2_flag=0;
- bit key3_flag=0;
- //---------->定義長按按鍵變量
- unsigned char sec=0,sec1=0;
- bit s0=0;
- //----------> GSM相關變量
- bit fasong_flag_1=1;
- uchar phone[11]= "15225939196"; //手機號數組
- //-----> Uart1 And GSM Definition
- unsigned char xdata Uart1_Buff[50]; //串口1緩沖數組
- unsigned char Uart1_Count=0; //串口1累加變量
- unsigned char Quest_flag=1; //初始化標志位
- unsigned int GSM_Send_Time=0; //GSM延時變量
- unsigned char GSM_Num=0; //發送指令變量
- unsigned char GSM_Send=0; //發送標志位
- unsigned char GSM_Send_Num=0; //發送具體短信內容變量
-
- //------------> 額外
- unsigned char Timer_e=0;
- #define S2RI 0x01 //S2CON.0
- #define S2TI 0x02 //S2CON.1
- #define S2RB8 0x04 //S2CON.2
- #define S2TB8 0x08 //S2CON.3
- bit busy;
- bit A_M=1;
- void delay(uint T) //延時函數
- {
- while(T--);
- }
- //GSM相關函數begin
- void Uart1Data(unsigned char dat) //發送一個字節
- {
- SBUF=dat;
- while(!TI);
- TI=0;
- }
- void UartData_Byte(char *s) //發送一串數據
- {
- while (*s != '\0')
- {
- Uart1Data(*s++);
- }
- }
- void Uart2Data(unsigned char dat)
- {
- while (busy)
- {
- if (S2CON & S2TI)
- {
- S2CON &= ~S2TI; //Clear transmit interrupt flag
- busy = 0; //Clear transmit busy flag
- }
- }
- busy = 1;
- S2BUF=dat;
- }
- void Uart2DataByte(unsigned char *byte)
- {
- while(*byte != '\0')
- {
- Uart2Data(*byte++);
- }
- }
- void Uart2_Init(void) //串口 2 波特率配制,使用獨立波特率發生器
- {
- AUXR &= 0xF7; //波特率不倍速
- S2CON = 0x50; //8位數據,可變波特率
- AUXR &= 0xFB; //獨立波特率發生器時鐘為Fosc/12,即12T
- BRT = 0xFD; //設定獨立波特率發生器重裝值
- AUXR |= 0x10; //啟動獨立波特率發生器
- IE2 |= 0x01;
- }
-
- void memory() //存儲數據函數
- {
- unsigned char q=0;
- if(memory_flag)
- {
- memory_flag=0;
- IapEraseSector(0x0400);
- IapProgramuchar(0x0400,TP_H);
- for(q=0;q<11;q++)
- {
- IapProgramuchar(0x0401+q,phone[q]);
- }
- }
- }
- void read_memory() //讀取數據函數
- {
- unsigned char q=0;
- TP_H=IapReaduchar(0x0400);
- for(q=0;q<11;q++)
- {
- phone[q]=IapReaduchar(0x0401+q);
- }
- if(TP_H>99||TP_H<-50)
- {
- TP_H=25;
- memory_flag=1;
- }
- }
- void LCE_Display()
- {
- LCD1602_write(0,0x80);
- //顯示溫度
- LCD1602_writebyte("Temp:");
- if(temp>=0)
- {
- LCD1602_writebyte(" ");
- LCD1602_write(1,0x30+temp/100%10);
- LCD1602_write(1,0x30+temp/10%10);
- LCD1602_writebyte(".");
- LCD1602_write(1,0x30+temp%10);
- }
- else
- {
- LCD1602_writebyte("-");
- LCD1602_write(1,0x30+temp*-1/100%10);
- LCD1602_write(1,0x30+temp*-1/10%10);
- LCD1602_writebyte(".");
- LCD1602_write(1,0x30+temp*-1%10);
-
- }
- LCD1602_write(1,0xdf);
- LCD1602_writebyte("C");
- LCD1602_writebyte(" ");
- // if(A_M==1) LCD1602_writebyte("M ");
- // else LCD1602_writebyte("A ");
- LCD1602_write(0,0xC0);
- LCD1602_writebyte("TH:");
- if(state==1&&s0) LCD1602_writebyte(" ");
- else
- {
- if(TP_H>=0)
- {
- LCD1602_writebyte(" ");
- LCD1602_write(1,0x30+TP_H/10%10);
- LCD1602_write(1,0x30+TP_H%10);
- }
- else
- {
- LCD1602_writebyte("-");
- LCD1602_write(1,0x30+TP_H*-1/10%10);
- LCD1602_write(1,0x30+TP_H*-1%10);
- }
-
- }
- LCD1602_write(1,0xdf);
- LCD1602_writebyte("C ");
- LCD1602_writebyte("fan:");
- LCD1602_write(1,0x30+grade);
- LCD1602_writebyte(" ");
-
- }
- void Key_dispose()
- {
- uchar i=1;
- if(!key_1)
- {
- if(key1_flag)
- {
- key1_flag=0;
- state=(state+1)%2;
- if(state==0)
- {
- LCD1602_write(0,0x01);
- }
- }
- }
- else
- {
- key1_flag=1;
- }
- if(!key_2)
- {
- if(key2_flag||sec==0)
- {
- key2_flag=0;
- if(state==1)
- {
- if(TP_H<99)TP_H++;
- }
- else
- {
- if(TP_H_flag)
- {
- if(grade<3)
- grade++;
- }
- }
- }
- }
- else
- {
- if(key2_flag==0)
- {
- key2_flag=1;
- memory_flag=1;
- }
- sec=2;
- }
- if(!key_3)
- {
- if(key3_flag||sec1==0)
- {
- key3_flag=0;
- if(state==1)
- {
- if(TP_H>(-50))TP_H--;
- }
- else
- {
- if(TP_H_flag)
- {
- if(grade>1)
- grade--;
- }
- }
- }
- }
- else
- {
- if(key3_flag==0)
- {
- key3_flag=1;
- memory_flag=1;
- }
- sec1=2;
- }
- }
- void police() //報警函數處理
- {
- //當溫度數據高于上限值時
- if(temp>TP_H*10)
- {
- TP_H_flag=1;
- if(fasong_flag_1==1)
- {
- fasong_flag_1=0;
- GSM_Send_Num=1;
- GSM_Send=1;
- grade=1;
- }
- }
- else
- {
- Led2=1;
- TP_H_flag=0;
- fasong_flag_1=1;
- grade=0;
- }
- }
-
- void GSM_Init2()
- {
- unsigned char G_Tab[20];
- if(Quest_flag==1) //開始查詢GSM狀態//此時還沒有初始化完成,首先,先初始化 ,發AT,能夠響應上,關回顯,設置短信處理方式
- {
- switch(GSM_Num)
- {
- case 0: UartData_Byte("AT\r\n"); break; //AT
- case 1: UartData_Byte("ATE1\r\n"); break;//關回顯
- case 2: UartData_Byte("AT+CNMI=3,2,2,0,1\r\n"); break;//設置
- case 3: UartData_Byte("AT+CMGF=1\r\n"); break; //配制
- case 4: UartData_Byte("AT+COPS?\r\n"); break; //查詢網絡
- case 5: Quest_flag=0; GSM_Num=0; break; //初始化徹底結束
- }
- GSM_Send_Time=10;
- }
- else
- {
- GSM_Send_Time=40;
- if(GSM_Send==1) //發送短信標志
- {
- if(GSM_Send_Num==0) //Num 是短信編號,就是發送哪條短信,這個是返回Set End 的
- {
- switch(GSM_Num)
- {
- case 0: UartData_Byte("AT+CMGF=1\r\n"); break;
-
- case 1: UartData_Byte("AT+CMGS=\"");
- UartData_Byte(phone);
- UartData_Byte("\"\r\n"); break; //AT+CMGS="13343851798"
-
- case 2: UartData_Byte("Set End"); GSM_Num=3; break;
-
- case 3: G_Tab[0]=0x1a;
- Uart1Data(G_Tab[0]); break;
-
- case 4: GSM_Num=0; GSM_Send=0; break;
- }
- }
- else if(GSM_Send_Num==1)
- {
- switch(GSM_Num)
- {
- case 0: UartData_Byte("AT+CMGF=0\r\n"); break;
-
- case 1: UartData_Byte("AT+CMGS=35\r\n"); break;
- case 2: UartData_Byte("0011000D9168");
- G_Tab[0]=phone[1];
- G_Tab[1]=phone[0];
- G_Tab[2]=phone[3];
- G_Tab[3]=phone[2];
- G_Tab[4]=phone[5];
- G_Tab[5]=phone[4];
- G_Tab[6]=phone[7];
- G_Tab[7]=phone[6];
- G_Tab[8]=phone[9];
- G_Tab[9]=phone[8];
- G_Tab[10]='F';
- G_Tab[11]=phone[10];
- UartData_Byte(G_Tab);
- UartData_Byte("0008A914"); //末尾兩位數是 發送的短信長度
- UartData_Byte("5F53524D6E295EA68FC79AD8FF0C8BF76CE8610F");//當前溫度過高,請注意
- GSM_Num=3;
- break;
-
- case 3: G_Tab[0]=0x1a;
- Uart1Data(G_Tab[0]); break;
- case 4:
- UartData_Byte("AT+CMGF=1\r\n");
- delay(666);delay(666);
- GSM_Send = 0;
- GSM_Num=0;
- break;
- }
- }
-
- }
- else GSM_Num=0;
- }
- }
- void Uart_send()
- {
- UartData_Byte("Now Temp:");
- Uart1Data(0x30+temp/100);
- Uart1Data(0x30+temp/10%10);
- UartData_Byte(".");
- Uart1Data(0x30+temp%10);
- UartData_Byte(0xdf);
- UartData_Byte("C \r\n");
- UartData_Byte("Temp Limit:");
- Uart1Data(0x30+TP_H/10);
- Uart1Data(0x30+TP_H%10);
- UartData_Byte(0xdf);
- UartData_Byte("C \r\n");
- }
- void timer0_init()
- {
- TMOD =0x01; //定時器0配置,方式2,256自動重裝,定時器1配置成方式1,50ms 中斷一次
- TH0=0x4C;
- TL0=0x00;
- ET0= 1; //打開定時器1
- TR0= 1; //定時1開始定時
-
- TMOD=TMOD|0x20; //配置定時器0,工作在方式1,用作常規定時,定時器1用作波特率發生器
- TH1=0xFD; //裝載波特率9600
- TL1=0xFD;
- SCON=0x50;
- PCON=0;
- ES=1; //ES-串行中斷允許控制位 ES = 1 允許串行中斷。
- TR1=1; //啟動定時器開始工作
- EA=1; //打開總中斷
- }
- bit Send_Debug=1;
- void Uart2_Debug()
- {
- Uart2DataByte("\r\n當前綁定手機號:");
- phone[11]='\0';
- Uart2DataByte(phone);
- Uart2DataByte("\r\n當前溫度:");
- Uart2Data(0x30+temp/100%10);
- Uart2Data(0x30+temp/10%10);
- Uart2Data('.');
- Uart2Data(0x30+temp%10);
- Uart2DataByte("℃");
- Uart2DataByte("\r\n風扇等級:");
- Uart2Data(0x30+grade/10%10);
- Uart2Data(0x30+grade%10);
- Uart2DataByte("\r\n\r\n");
- }
- void main(void)
- {
- uchar temp_count=0;
- timer0_init();
- PS=1;
- LCD1602_cls();
- Uart2_Init();
- read_memory();
- UartData_Byte(phone);
- delay(100);
- do
- {
- temp=ready_18b20();
- }
- while(temp>=850);
- Led1=0;
- while(1)
- {
-
- if(Send_Debug==1)
- {
- Send_Debug=0;
- Uart2_Debug();
- }
-
-
- //讀取溫度
- if(page_flag==1)
- {
-
- if(Read_DHT_Dat)
- {
- Read_DHT_Dat=0;
- if(temp_count==0)
- {
- temp2=ready_18b20();
- }
- else if(temp_count==1)
- {
- temp1=ready_18b20();
- }
- else if(temp_count==2)
- {
- if(temp2>temp1)
- {
- if(temp2-temp1<50&&temp1!=0)
- {
- temp=temp1;
- }
- }
- else
- {
- if(temp1-temp2<50&&temp2!=0)
- {
- temp=temp1;
- }
- }
- }
- temp_count=(temp_count+1)%3;
- }
- }
- if(GSM_Send_Time==0)
- {
- GSM_Init2();
- }
- LCE_Display(); //顯示數據
- if(flag)
- police(); //報警處理
- memory(); //存儲數據函數
- switch(grade)
- {
- case 1:scale=25;break;
- case 2:scale=12;break;
- case 3:scale=0;break;
- case 0:scale=31;break;
- }
- }
- }
- void Timer0() interrupt 1
- {
- TH0=0xDC;
- TL0=0x00;
- n++; //每10ms n++
- if(n>=31)
- {
- n=0;
- }
- if(n<scale) //n<設置比例時,打開燈
- {
- Fan=1;
- }
- else
- if(n>=scale)//n大于等于設置比例時 關閉燈
- {
- Fan=0;
- }
- Timer_e++;
- if(Timer_e>=5)
- {
- Timer_e=0;
- Time_50ms++;
- if(GSM_Send_Time!=0) GSM_Send_Time--;
- Key_dispose();
- if(Time_50ms%4==0)
- {
- s0=~s0;
- if(TP_H_flag) Led2=~Led2; //指示燈提示 //指示燈提示
- }
- if(Time_50ms%10==0)
- {
- Read_DHT_Dat=1;
- Send_Debug=1;
- }
- if(Time_50ms%20==0)
- {
- if(sec!=0) sec--;
- if(sec1!=0) sec1--;
- flag=1;
- if(page_flag<1) page_flag++;
- Time_50ms=0;
- }
- }
- }
-
- //---->Uart1
- void Uart1() interrupt 4
- {
- uchar i=0;
- if(RI)
- {
- RI=0;
- Uart1_Buff[Uart1_Count] = SBUF;
- Uart1_Count=(Uart1_Count+1)%50;
- Uart1_Buff[Uart1_Count]=0;
- if(Quest_flag==1) //說明此時在初始化
- {
- if(GSM_Num==0||GSM_Num==1||GSM_Num==2||GSM_Num==3) //這里都是返回的 OK
- {
- if(Uart1_Count>=4&&Uart1_Buff[Uart1_Count-1]=='\n'&&Uart1_Buff[Uart1_Count-2]=='\r'&&Uart1_Buff[Uart1_Count-3]=='K'&&Uart1_Buff[Uart1_Count-4]=='O')
- {
- GSM_Num++;
- GSM_Send_Time=10;
- }
- }
- else if(GSM_Num==4) //+COPS: 0,0,"CHINA MOBILE"
- {
- if(Uart1_Count>=25&&Uart1_Buff[Uart1_Count-1]=='\n'&&Uart1_Buff[Uart1_Count-2]=='\r'&&Uart1_Buff[Uart1_Count-3]=='"'&&Uart1_Buff[Uart1_Count-4]=='E'&&Uart1_Buff[Uart1_Count-11]=='A')
- {
- GSM_Num++;
- GSM_Send_Time=10;
- }
- }
- }
- else
- {
- if(GSM_Num==0||GSM_Num==2||GSM_Num==3) //這里都是返回的 OK
- {
- if(Uart1_Count>=4&&Uart1_Buff[Uart1_Count-1]=='\n'&&Uart1_Buff[Uart1_Count-2]=='\r'&&Uart1_Buff[Uart1_Count-3]=='K'&&Uart1_Buff[Uart1_Count-4]=='O')
- {
- GSM_Num++;
- GSM_Send_Time=10;
- }
- if(Uart1_Count>=7&&Uart1_Buff[Uart1_Count-1]=='\n'&&Uart1_Buff[Uart1_Count-2]=='\r'&&Uart1_Buff[Uart1_Count-3]=='R'&&Uart1_Buff[Uart1_Count-7]=='E')
- { //ERROR
- if(GSM_Num==3)
- {
- GSM_Num=0;
- GSM_Send_Time=10;
- }
- }
- }
- else if(GSM_Num==1) //>
- {
- if(Uart1_Count>=1&&Uart1_Buff[Uart1_Count-1]=='>')
- {
- GSM_Num++;
- GSM_Send_Time=10;
- }
- }
- }
-
-
-
- if(Uart1_Count>=2&&Uart1_Buff[Uart1_Count-2]=='\r'&&Uart1_Buff[Uart1_Count-1]=='\n') //接收到回車符
- {
- //PHONE SMS:12345678911
-
- if(Uart1_Count>=23&&Uart1_Buff[Uart1_Count-23]=='P'&&Uart1_Buff[Uart1_Count-19]=='E'&&Uart1_Buff[Uart1_Count-15]=='S'&&Uart1_Buff[Uart1_Count-14]==':')
- {
- for(i=0;i<11;i++) //保存到單片機中的手機號
- {
- phone[i]=Uart1_Buff[(Uart1_Count-13)+i];
- }
- GSM_Send=1;
- GSM_Send_Num=0;
- memory_flag=1;
- }
-
-
- // GSM_Send =1;GSM_Send_Num=0;
- else
- if( Uart1_Count>=7&& //調速fan:a/b/c
- Uart1_Buff[Uart1_Count-7]=='f'&&
- Uart1_Buff[Uart1_Count-6]=='a'&&Uart1_Buff[Uart1_Count-5]=='n'&&Uart1_Buff[Uart1_Count-4]==':'&&TP_H_flag==1)
- {
- switch(Uart1_Buff[Uart1_Count-3])
- {
- case 'a': grade=1;GSM_Send =1;GSM_Send_Num=0;break;
- case 'b': grade=2;GSM_Send =1;GSM_Send_Num=0;break;
- case 'c': grade=3;GSM_Send =1;GSM_Send_Num=0;break;
- }
- }
-
- Uart1_Count=0;
- }
- }
- }
- unsigned char Uart2_Dat=0;
- void Uart_2(void) interrupt 8
- {
- if (S2CON & S2RI)
- {
- S2CON &= ~S2RI;
- Uart2_Dat=S2BUF;
- switch(Uart2_Dat)
- {
- case '0': if(TP_H_flag) {grade=0;} break;
- case '1': if(TP_H_flag) {grade=1;} break;
- case '2': if(TP_H_flag) {grade=2;} break;
- case '3': if(TP_H_flag) {grade=3;} break;
- }
- Uart2_Dat=0;
-
-
- }
- if (S2CON & S2TI)
- {
- S2CON &= ~S2TI; //Clear transmit interrupt flag
- busy = 0; //Clear transmit busy flag
- }
- }
復制代碼
原理圖.pdf
(80.19 KB, 下載次數: 12)
2019-4-28 09:38 上傳
點擊文件名下載附件
程序.docx
(36.06 KB, 下載次數: 6)
2019-4-28 09:38 上傳
點擊文件名下載附件
|