|
下面是下位機(jī)的主機(jī)源代碼:
- /***************************************************************************
- 函數(shù)名稱: AM2301多機(jī)通信主機(jī)部分
- 主控芯片: STC12C5A60S2,晶振11.0592MHz
- 功 能: 和從機(jī)及上位機(jī)通信,接收從機(jī)發(fā)送過來的溫濕度,用1620顯示出來并
- 將數(shù)據(jù)傳送到上位機(jī)
- 創(chuàng)建日期: 2011.5.22
- 修改日志: 無
- ****************************************************************************/
- //#include <reg52.h>
- #include <STC12C5A.h>
- #include <intrins.h>
- #include "LCD1602.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define _Nop() _nop_()
- sbit RDE =P3^2; // 485控制輸入輸出控制(與從機(jī))
- sbit RDE2=P1^4; //485控制輸入輸出控制(與上位機(jī))
- #define SlaveNum 2 //從機(jī)數(shù)量
- uchar code dis1[]={"H: % T: C"} ;
- uchar code dis2[]={"h: % t: c"} ;
- #define num 8 //緩沖數(shù)據(jù)的長度
- char Revdata[num];//接收到的數(shù)據(jù)緩存
- uchar SLAVE_ID = 1;//從機(jī)地址
- uchar rc;//串口1接收到和數(shù)據(jù)緩存
- uchar Rev;//串口2接收數(shù)據(jù)
- //----------------------------------------
- uchar state = 0;//狀態(tài)標(biāo)志位。1-進(jìn)入等待從機(jī)回應(yīng),2-更新顯示數(shù)據(jù)
- uchar recv_length = 0;
- bit recving_flag = 0;
- bit recfinish=0;
- bit recflag=0;
- bit minusflag=0,addflag=0,stopflag=0;
- //----------------------------------------
- ////////////////////////////////////////////////////////////////////////
- //////////////////延時(shí)函數(shù)///////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- void Delay(unsigned int t)
- {
- while(t--);
- }
- /////////////////////////////////
- void Delay_10us(unsigned int us)
- {
- do{
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }while(--us);
- }
- //////////////////////////////////
- void Delay_ms(unsigned int ms)
- {
- do Delay_10us(131);
- while(--ms);
- }
- ////////////////////////////////////////////////////////////////////////
- //////////////////////串口1發(fā)送地址函數(shù)//////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- void senda(unsigned char adra) //發(fā)送地址
- {
- TB8 = 1;//發(fā)送地址幀
- RDE = 1;//允許發(fā)送
- Delay_10us(50);
- ES=0; //關(guān)串1口中斷
- TI = 0; //清標(biāo)志位
- SBUF = adra;
- while(!TI);
- TI = 0;//等待發(fā)送完畢
- ES=1; //開串口1中斷
- RDE = 0;//485允許接收
- Delay_10us(50);
- }
- ////////////////////////////////////////////////////////////////////////
- //////////////////////串口1發(fā)送數(shù)據(jù)函數(shù)//////////////////////////////
- //////////////////////////////////////////////////////////////////////
- /**/
- void sendata(unsigned char tdata) //發(fā)送地址
- {
- TB8=0;//發(fā)送數(shù)據(jù)幀
- RDE = 1;//允許發(fā)送
- TI = 0;
- SBUF = tdata;
- while(!TI);
- TI=0;
- RDE = 0;//485允許接收
- }
- //////////////////////////////////////////////////////////////////////
- //////////////////////串口2發(fā)送數(shù)據(jù)函數(shù)///////////////////////////////
- //////////////////////////////////////////////////////////////////////
- void Uart2_senbyte(uchar val)
- {
- RDE2=1;//485發(fā)送狀態(tài)
- // Delay(50);
- IE2=0x00;//關(guān)串口2中斷
- S2BUF=val;
- while ((S2CON & 0x02) == 0); //等待發(fā)送完畢
- S2CON &= 0xFD; //清標(biāo)志位(S2TI=0)
- IE2=0x01;//開串口2中斷
- RDE2=0;
- // Delay(50);
- }
- ////////////////////////////////////////////////////////////////////////
- ///////////////////////////////顯示函數(shù)/////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- ///////////顯示字符///////
- void showchar()
- { uchar string;
- //====顯示字符1======
- string=0;
- writecmd(0x80);
- while(dis1[string] != '\0')//顯示字符
- {
- writedata(dis1[string]) ;
- string++ ;
- }string = 0 ;
- //=====顯示字符2=====
- writecmd(0xc0);
- while(dis2[string] != '\0')//顯示字符
- {
- writedata(dis2[string]) ;
- string++ ;
- }string = 0 ;
- }
- //////////////顯示數(shù)據(jù)/////////////
- void showdata()
- {
- if(SLAVE_ID==1)
- {
- //=====顯示濕度=====
- writecmd(0x82);
- writedata(Revdata[1]+0x30);
- writedata(Revdata[2]+0x30);
- writedata('.');
- writedata(Revdata[3]+0x30);
- //=====顯示溫度=====
- writecmd(0x8b);
- writedata(Revdata[4]+0x30);
- writedata(Revdata[5]+0x30);
- writedata('.');
- writedata(Revdata[6]+0x30);
- Delay_ms(400);
- }
- if(SLAVE_ID==2)
- {
- //=====顯示濕度=====
- writecmd(0xc2);
- writedata(Revdata[1]+0x30);
- writedata(Revdata[2]+0x30);
- writedata('.');
- writedata(Revdata[3]+0x30);
- //=====顯示溫度=====
- writecmd(0xcb);
- writedata(Revdata[4]+0x30);
- writedata(Revdata[5]+0x30);
- writedata('.');
- writedata(Revdata[6]+0x30);
- Delay_ms(1000);
- }
-
- }
- ////////////////////////////////////////////////////////////////////////
- ///////////////////串口初始化函數(shù)////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- void UARTinit()
- {
- //串口1初始化/////////
- TMOD|=0x21;//定時(shí)器1工作在方式2,定時(shí)器0工作在方式1
- TH1=0xfa;TL1=0xfa; //波特率9600
- SCON=0x50;//SCON:SM0,SM1,SM2,REN,TB8,RB8,TI,RI
- PCON|=0x80;//波特率倍增
- TR1=1;//開定時(shí)器1
- EA=1;//開總中斷
- ES=1;//串口1中斷
- //--------------------//
- //串口2初始化///////////
- BRT = 0XDC; //設(shè)置獨(dú)立的波特率發(fā)生器的重載數(shù)值,此數(shù)值的波特率為9600
- AUXR = 0X14; //設(shè)置波特率的發(fā)生方式(允許波特率發(fā)生器運(yùn)行,波特率不加倍,每個(gè)時(shí)鐘計(jì)數(shù)一次)
- S2CON|=0x50;//串口2,方式1,允許接收
- IE2|=0X01;//允許串口2中斷
- EA=1;//開總中斷
- //--------------------//
- Delay(10000); //熱機(jī)時(shí)間
- init_LCD(); //初始化液晶
- }
- //==========================================
- ////////////////////////////////////////////////////////////////////////
- ///////////////////////////////主函數(shù)///////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- void main()
- {
- // uchar i;
- UARTinit();//串口初始化
- Delay(40000);//熱機(jī)
-
- while(1)
- {
- WDT_CONTR=0x3c;//啟動看門狗,超過該時(shí)間未能喂狗則系統(tǒng)復(fù)位,
- //0011.1011---WDT_FLAG=0,EN_WDT=1,CLR_WDT=1,IDLE_WDT=0,PS2=1,PS1=1,PS0=0
- //IDLE_WDT=1時(shí),"空閑模式"計(jì)數(shù),IDLE_WDT=0時(shí)"空閑模式"不計(jì)數(shù)
- //0x38-71.1ms,0x39-142.2ms, 0x3a-284.4ms, 0x3b-568.8ms,
- //0x3c-1.1377s,0x3d-2.2755s,0x3e-4.5511s,0x3f-9.1022s
- showchar();//顯示字符
- if(state == 0)
- {
-
- senda(SLAVE_ID++);
- if(SLAVE_ID>SlaveNum)SLAVE_ID = 1;
- if(addflag==1&&SLAVE_ID==1){sendata(0xf1);addflag=0;}//發(fā)送指令給從機(jī)1
- if(minusflag==1&&SLAVE_ID==2){sendata(0xf2);minusflag=0;}//發(fā)送指令給從機(jī)2
- if((stopflag==1&&SLAVE_ID==1)||(stopflag==1&&SLAVE_ID==2))
- {stopflag=0,sendata(0xf0);} // 發(fā)哦能夠停止指令給從機(jī)1、2
-
- Delay_ms(30);
- state = 1;// 狀態(tài)轉(zhuǎn)為等待從機(jī)應(yīng)答,在串口中斷
- /* if(recfinish==1)
- { recfinish=0;
-
-
- if(recflag == 0)
- {// recflag =0;
- // senda(SLAVE_ID);
- senda(SLAVE_ID++); Delay_ms(4000);
- if(SLAVE_ID>SlaveNum)
- {
- SLAVE_ID = 1;
- senda(SLAVE_ID);
- Delay_ms(4000);
- }
-
- }//end of recving_flag =0
- }//end of recfinish=1
- */
- } //end of state=0
-
- if(state == 2) // 狀態(tài) 轉(zhuǎn)為 顯示
- {
- showdata();//更新數(shù)據(jù)顯示
- Delay_ms(800);
- //發(fā)送數(shù)據(jù)到上位機(jī)
- Uart2_senbyte('R');//上位機(jī)識別濕度的標(biāo)志
- Uart2_senbyte(Revdata[1]+0x30);//
- Uart2_senbyte(Revdata[2]+0x30);
- Uart2_senbyte('.');
- Uart2_senbyte(Revdata[3]+0x30);
- Uart2_senbyte('T');//上位機(jī)識別溫度的標(biāo)志
- Uart2_senbyte(Revdata[4]+0x30);
- Uart2_senbyte(Revdata[5]+0x30);
- Uart2_senbyte('.');
- Uart2_senbyte(Revdata[6]+0x30);
- if(SLAVE_ID==1)Uart2_senbyte('A');//上位機(jī)識別從機(jī)的標(biāo)志
- if(SLAVE_ID==2)Uart2_senbyte('B');
- /* ////////////////////////////////////////////////////////////////
- for(i = 1; i<num-1; i++)
- {
-
-
- senda(Revdata[i]); //把收到的數(shù)據(jù)往PC機(jī)回送(串口助手調(diào)試用)
- Delay_ms(20);
- }
- */ ////////////////////////////////////////////////////////////////
- Delay_ms(20);
- state = 0; // 狀態(tài)轉(zhuǎn)為0,重新查詢從機(jī)
- }//end of state=2
- WDT_CONTR=0x3c;//喂狗,若超過時(shí)間不喂狗,則系統(tǒng)復(fù)位
- }//end of while(1)
-
-
- }//end of main()
- ////////////////////////////////////////////////////////////////////////
- ///////////////////串口 1接收 中斷 函數(shù)//////////////////////////////////
- ////////////////////////////////////////////////////////////////////////
- void Recv_int(void)interrupt 4 //using 2
- {
- RDE=0;
- if(RI)
- {
- RI=0;
- rc=SBUF;
-
- if(state==1)
- {
-
- if((recv_length == 0)&&(rc == 0xf7 ) ) //這里判斷發(fā)送的第一個(gè)字節(jié)
- {
- recving_flag = 1;
- recflag=1;
- }
-
- if(recving_flag == 1)
- {
- Revdata[recv_length++] = rc;
- if(rc == 0xf3)//判斷結(jié)束幀
- {
- recv_length = 0;
- recving_flag = 0;
- recfinish=1;
- state = 2; //接收完畢,置標(biāo)志位,準(zhǔn)備顯示
- }//end of rc=0xf3
-
- }//end of recving_flag = 1
-
- }//end of state=1
-
- }//end of RI
-
- }//end of interrupt
- //////////////////串口2中斷服務(wù)子程序////////////////////////////
- void Uart2() interrupt 8 //using 1
- {
- RDE2=0;//485保持為接收狀態(tài)
- if (S2CON & S2RI)
- {
- S2CON &= ~S2RI; //清除接收完成標(biāo)志
- Rev = S2BUF;
- switch(Rev)
- {
- case 'a': //加大濕度
- addflag=1;
- break;
- case 'm': //降低濕度
- minusflag=1;
- break;
- case 's': //停止
- stopflag=1;
- break;
- default:
- break;
- }//end of swtich
- } //end of if
-
- RDE2=1;//485允許發(fā)送
-
- }
- ////////////////////////////////End/////////////////////////////////////
復(fù)制代碼 |
|