|
我是單片機(jī)小白,自己摸索了畢業(yè)設(shè)計(jì),感覺好困難,論文出了一些問題,勞煩大神指教一二 。 我們的畢業(yè)設(shè)計(jì)要求使用protues進(jìn)行電路仿真,相關(guān)系統(tǒng)原理圖中的各個(gè)器件按照參考資料都已畫好,程序也有(存在問題,編譯下來有很多的錯(cuò)誤),鄙人目前使用的軟件版本是protues7.8和keil4,來進(jìn)行聯(lián)調(diào)仿真。
老師制定的任務(wù)書如下
論文題目:基于單片機(jī)的智能家居系統(tǒng)設(shè)計(jì)
設(shè)計(jì)目的及任務(wù)
以單片機(jī)為控制核心,設(shè)計(jì)實(shí)現(xiàn)智能家居控制,該系統(tǒng)具有室內(nèi)防盜報(bào)警功能和室內(nèi)環(huán)境參數(shù)實(shí)時(shí)檢測功能。主要設(shè)計(jì)任務(wù):
1. 實(shí)現(xiàn)紅外人體感應(yīng)報(bào)警;
2. 實(shí)現(xiàn)智能檢測室內(nèi)溫度濕度;
3. 實(shí)現(xiàn)智能時(shí)鐘顯示。
設(shè)計(jì)思路
參考所查閱的資料,基本設(shè)計(jì)思路是:該系統(tǒng)AT89C52(原計(jì)劃用STC89C52,可是protues元件庫中沒有,只好用它來代替)為控制中心,加以外圍電路包括采集電路、傳輸電路、LCD1602( LM016)顯示電路、數(shù)據(jù)處理電路、報(bào)警裝置電路、時(shí)鐘電路及電路元器件,報(bào)警系統(tǒng)裝置采用發(fā)光二極管和蜂鳴器,數(shù)據(jù)采集裝置使用溫濕度傳感器(SHT10),時(shí)鐘芯片選用DS1302。單片機(jī)開發(fā)軟件采用C語言編程,實(shí)現(xiàn)室內(nèi)防盜報(bào)警功能和室內(nèi)環(huán)境參數(shù)實(shí)時(shí)檢測功能。
技術(shù)要求、預(yù)期成果
完成單片機(jī)的智能家居系統(tǒng)設(shè)計(jì),在設(shè)計(jì)過程中能熟練的使用C語言和proteus仿真軟件,掌握單片機(jī)及傳感器的技術(shù)原理和應(yīng)用,熟悉系統(tǒng)各部分電路的功能,使整個(gè)系統(tǒng)能夠完成對(duì)室內(nèi)環(huán)境的實(shí)時(shí)監(jiān)測和及時(shí)應(yīng)對(duì)。在答辯中能夠盡可能的演示設(shè)計(jì)中的環(huán)境數(shù)據(jù)采集和實(shí)時(shí)監(jiān)測的過程。
存在的主要問題:
1.我按照參考資料畫好了protue仿真的大部分器件,連接好了溫濕度傳感器(SHT10)的相關(guān)電路,結(jié)果是用掉了p0、p2、p3的大部分接口,只剩下p1口了,時(shí)鐘芯DS1302和熱釋電紅外傳感如何設(shè)計(jì)才能接到AT89C52的單片機(jī)上,還能實(shí)現(xiàn)紅外人體感應(yīng)報(bào)警和智能時(shí)鐘顯示嗎?

2.室內(nèi)防盜報(bào)警功能是如果監(jiān)測到有人闖入室內(nèi),會(huì)發(fā)出聲光報(bào)警。紅外人體感應(yīng)通過熱釋電紅外傳感器實(shí)現(xiàn),報(bào)警系統(tǒng)裝置(聲光報(bào)警電路)采用發(fā)光二極管和蜂鳴器實(shí)現(xiàn)。參考資料里采用三極管驅(qū)動(dòng)蜂鳴器報(bào)警來提醒溫濕度超過限定值,我只需要實(shí)現(xiàn)室內(nèi)環(huán)境參數(shù)實(shí)時(shí)檢測功能,溫濕度的報(bào)警電路可以去掉嗎,用室內(nèi)防盜報(bào)警電路替換,也就是用發(fā)光二極管和蜂鳴器,配合熱釋電紅外傳感器實(shí)現(xiàn)防盜報(bào)警,這個(gè)怎么更改呢?

3.智能時(shí)鐘顯示這一塊,顯示器已經(jīng)用來顯示溫濕度了,怎么再切換顯示時(shí)間呢,而且時(shí)鐘芯片僅起顯示時(shí)間的功能,不對(duì)電路做其他控制,具體如何設(shè)置呢?

4.原資料的程序編譯下來有很多錯(cuò)誤,無法生成.hex文件,求大神結(jié)合我要實(shí)現(xiàn)的功能更改一下我的源程序。
原參考資料仿真圖
原參考資料仿真圖
我參考畫的
熱釋電紅外傳感器內(nèi)部電路圖
熱
聲光報(bào)警模塊電路
聲光報(bào)警模塊電路
參考資料源程序(本身好像有錯(cuò)還是不完整,編譯之后無法生成.hex文件)
- //*********主函數(shù)*****************
- void main(void)
- {
- value humi_val,temp_val;
- unsigned char error,checksum;
- unsigned int wendu,shidu;
- LCD_init();
- s_connectionreset();
- LCD_disp_str(0,1,"TE ");
- LCD_disp_str(0,2,"RH ");
- //*********初始化溫度顯示區(qū)*********
- LCD_disp_str(2,1,"TTT.T C");
- //*********初始化濕度顯示區(qū)*********
- LCD_disp_str(2,2,"RRR.R%");
- delay_n10us(20000); //延時(shí)0.2s
- while(1)
- { error=0;
- error+=s_measure((unsigned char*)&humi_val.i,&checksum,HUMI);
- error+=s_measure((unsigned char*)&temp_val.i,&checksum,TEMP);
- if(error!=0) s_connectionreset();
- else
- { humi_val.f=(float)humi_val.i;
- temp_val.f=(float)temp_val.i;
- calc_SHT10(&humi_val.f,&temp_val.f);
- wendu=10*temp_val.f;
- LCD_disp_char(2,1,wendu/1000+'0'); //顯示溫度百位
- LCD_disp_char(3,1,(wendu%1000)/100+'0'); //顯示溫度十位
- LCD_disp_char(4,1,(wendu%100)/10+'0'); //顯示溫度個(gè)位
- LCD_disp_char(6,1,(wendu%10)+'0'); //顯示溫度小數(shù)點(diǎn)后第一位
- shidu=10*humi_val.f;
- LCD_disp_char(2,2,shidu/1000+'0'); //顯示濕度百位
- LCD_disp_char(3,2,(shidu%1000)/100+'0'); //顯示濕度十位
- LCD_disp_char(4,2,(shidu%100)/10+'0'); //顯示濕度個(gè)位
- LCD_disp_char(6,2,(shidu%10)+'0'); //顯示濕度小數(shù)點(diǎn)后第一位
- }
-
- delay_n10us(80000); //延時(shí)約0.8s
- }
- }
- 系統(tǒng)部分程序:
- /*************定義接口********************
- P0------DB0~DB7 (LCD1602)
- P2.4------RS (LCD1602)
- P2.5------RW (LCD1602)
- P2.6------E (LCD1602)
- P3.5------SOUNDER (蜂鳴器)
- P3.6------SCK (SHT10) //時(shí)鐘
- P3.7------DATA (SHT10) //數(shù)據(jù)
- *****************************************/
- #include <AT89x51.h>
- #include <intrins.h>
- #include <math.h> //Keil library
- #include <stdio.h> //Keil library
- #define TEMPUP 24 //溫度上限, 達(dá)到該溫度蜂鳴器報(bào)警
- typedef unsigned char BYTE;
- typedef unsigned int WORD;
- typedef bit BOOL ;
- /*******************************************/
- sbit buzzer = P3^5; //蜂鳴器
- BOOL start;
- /******************************************/
- //************第一部分LCD1602設(shè)置******
- START*************************************/
- #define LCD_DB P0
- sbit LCD_RS=P2^5; //P2^5是p2.5
- sbit LCD_RW=P2^6; //P2^6是p2.6
- sbit LCD_E=P2^7; //P2^7是p2.7
- /******定義函數(shù)****************/
- #define uchar unsigned char
- #define uint unsigned int
- void LCD_init(void); //初始化函數(shù)
- void LCD_write_command(ucharcommand); //寫指令函數(shù)
- void LCD_write_data(uchardat); //寫數(shù)據(jù)函數(shù)
- void LCD_disp_char(ucharx,uchar y,uchar dat);//在某個(gè)屏幕位置上顯示一個(gè)字符,X(0-15),y(1-2)
- void LCD_disp_str(ucharx,uchar y,uchar *str); //LCD1602顯示字符串函數(shù)
- void delay_n10us(uintn); //延時(shí)函數(shù)
- /*--------------------------------------
- ;初始化LCD1602
- ;-------------------------------------*/
- void LCD_init(void)
- {
- delay_n10us(10);
- LCD_write_command(0x38);//設(shè)置8位格式,2行,5x7
- delay_n10us(10);
- LCD_write_command(0x0c);//整體顯示,關(guān)光標(biāo),不閃爍
- delay_n10us(10);
- LCD_write_command(0x06);//設(shè)定輸入方式,增量不移位
- delay_n10us(10);
- LCD_write_command(0x01);//清除屏幕顯示
- delay_n10us(100); //延時(shí)清屏,延時(shí)函數(shù),延時(shí)約n個(gè)10us
- }
- /*--------------------------------------
- ;LCD1602寫指令函數(shù)
- ;-------------------------------------*/
- void LCD_write_command(uchardat)
- {
- delay_n10us(10);
- LCD_RS=0; //指令
- LCD_RW=0; //寫入
- LCD_E=1; //允許
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- /*--------------------------------------
- ;LCD1602寫數(shù)據(jù)函數(shù)
- ;-------------------------------------*/
- void LCD_write_data(uchardat)
- {
- delay_n10us(10);
- LCD_RS=1; //數(shù)據(jù)
- LCD_RW=0; //寫入
- LCD_E=1; //允許
- LCD_DB=dat;
- delay_n10us(10);
- LCD_E=0;
- delay_n10us(10);
- }
- /*--------------------------------------
- ;LCD1602顯示一個(gè)字符函數(shù),在某個(gè)屏幕位置上顯示一個(gè)字符,X(0-15),y(1-2)。
- ;-------------------------------------*/
- void LCD_disp_char(ucharx,uchar y,uchar dat)
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- LCD_write_data(dat);
- }
- /*--------------------------------------
- ;LCD1602顯示字符串函數(shù),在某個(gè)屏幕起始位置{X(0-15),y(1-2)}上顯示一個(gè)字符串。
- ;-------------------------------------*/
- void LCD_disp_str(ucharx,uchar y,uchar *str)
- {
- uchar address;
- if(y==1)
- address=0x80+x;
- else
- address=0xc0+x;
- LCD_write_command(address);
- while(*str!='\0')
- {
- LCD_write_data(*str);
- str++;
- }
- }
- /*--------------------------------------
- ;延時(shí)函數(shù),延時(shí)約n個(gè)10us
- ;-------------------------------------*/
- void delay_n10us(uintn) //延時(shí)n個(gè)10us@12M晶振
- {
- uint i;
- for(i=n;i>0;i--)
- {
- _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- }
- }
- //***************第一部分LCD1602設(shè)置*******
- END****************************************
- //*************第二部分SHT10設(shè)置*************
- START****************************************
- sbit SCK = P3^6; //定義通訊時(shí)鐘端口
- sbit DATA = P3^7; //定義通訊數(shù)據(jù)端口
- typedef union
- { unsigned int i; //定義了兩個(gè)共用體
- float f;
- } value;
- enum {TEMP,HUMI}; //TEMP=0,HUMI=1
- #define noACK 0 //用于判斷是否結(jié)束通訊
- #define ACK 1 //結(jié)束數(shù)據(jù)傳輸
- //adr command r/w
- #define STATUS_REG_W0x06 //000 0011 0
- #define STATUS_REG_R0x07 //000 0011 1
- #define MEASURE_TEMP0x03 //000 0001 1
- #define MEASURE_HUMI0x05 //000 0010 1
- #define RESET 0x1e //000 1111 0
- /****************定義函數(shù)****************/
- voids_transstart(void); //啟動(dòng)傳輸函數(shù)
- voids_connectionreset(void); //連接復(fù)位函數(shù)
- char s_write_byte(unsignedchar value);//SHT10寫函數(shù)
- char s_read_byte(unsignedchar ack); //SHT10讀函數(shù)
- char s_measure(unsigned char*p_value, unsigned char *p_checksum, unsigned char mode);//測量溫濕度函數(shù)
- void calc_SHT10(float*p_humidity ,float *p_temperature);//溫濕度補(bǔ)償
- /*--------------------------------------
- ;啟動(dòng)傳輸函數(shù)
- ;-------------------------------------*/
- void s_transstart(void)
- // generates a transmissionstart
- // _____ ________
- // DATA: |_______|
- // ___ ___
- // SCK : ___| |___| |______
- {
- DATA=1; SCK=0; //Initial state
- _nop_();
- SCK=1;
- _nop_();
- DATA=0;
- _nop_();
- SCK=0;
- _nop_();_nop_();_nop_();
- SCK=1;
- _nop_();
- DATA=1;
- _nop_();
- SCK=0;
- }
- /*--------------------------------------
- ;連接復(fù)位函數(shù)
- ;-------------------------------------*/
- void s_connectionreset(void)
- // communication reset:DATA-line=1 and at least 9 SCK cycles followed by transstart
- // _____________________________________________________ ________
- // DATA: |_______|
- // _ _ _ _ _ _ _ _ _ ___ ___
- // SCK : __| |__| |__| |__| |__||__| |__| |__| |__| |______| |___| |______
- {
- unsigned char i;
- DATA=1; SCK=0; //Initial state
- for(i=0;i<9;i++) //9 SCK cycles
- {
- SCK=1;
- SCK=0;
- }
- s_transstart(); //transmission start
- }
- /*--------------------------------------
- ;SHT10寫函數(shù)
- ;-------------------------------------*/
- char s_write_byte(unsignedchar value)
- //----------------------------------------------------------------------------------
- // writes a byte on theSensibus and checks the acknowledge
- {
- unsigned char i,error=0;
- for (i=0x80;i>0;i/=2) //shift bit for masking
- {
- if (i & value) DATA=1; //masking value with i , write toSENSI-BUS
- elseDATA=0;
- SCK=1; //clk for SENSI-BUS
- _nop_();_nop_();_nop_(); //pulswith approx. 3 us
- SCK=0;
- }
- DATA=1; //release DATA-line
- SCK=1; //clk #9for ack
- error=DATA; //check ack (DATA willbe pulled down by SHT10),DATA在第9個(gè)上升沿將被SHT10自動(dòng)下拉為低電
- _nop_();_nop_();_nop_();
- SCK=0;
- DATA=1; //release DATA-line
- return error; //error=1 in case of noacknowledge //返回:0成功,1失敗
- }
- /*--------------------------------------
- ;SHT10讀函數(shù)
- ;-------------------------------------*/
- char s_read_byte(unsignedchar ack)
- // reads a byte form theSensibus and gives an acknowledge in case of "ack=1"
- {
- unsigned char i,val=0;
- DATA=1; //release DATA-line
- for (i=0x80;i>0;i/=2) //shift bit for masking
- { SCK=1; //clk for SENSI-BUS
- if (DATA) val=(val | i); //read bit
- _nop_();_nop_();_nop_(); //pulswith approx. 3 us
- SCK=0;
- }
- if(ack==1)DATA=0; //in case of"ack==1" pull down DATA-Line
- else DATA=1; //如果是校驗(yàn)(ack==0),讀取完后結(jié)束通訊
- _nop_();_nop_();_nop_(); //pulswith approx. 3 us
- SCK=1; //clk #9 for ack
- _nop_();_nop_();_nop_(); //pulswith approx. 3 us
- SCK=0;
- _nop_();_nop_();_nop_(); //pulswith approx. 3 us
- DATA=1; //release DATA-line
- return val;
- }
- /*--------------------------------------
- ;測量溫濕度函數(shù)
- ;-------------------------------------*/
- char s_measure(unsigned char*p_value, unsigned char *p_checksum, unsigned char mode)
- // makes a measurement(humidity/temperature) with checksum
- {
- unsigned error=0;
- unsigned int i;
- s_transstart(); //transmission start
- switch(mode){ //send command to sensor
- caseTEMP :error+=s_write_byte(MEASURE_TEMP); break;
- case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
- default : break;
- }
- for (i=0;i<65535;i++) if(DATA==0) break;//wait until sensor has finished the measurement
- if(DATA) error+=1; // or timeout (~2 sec.) isreached
- *(p_value) =s_read_byte(ACK); //read thefirst byte (MSB)
- *(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
- *p_checksum =s_read_byte(noACK); //read checksum
- return error;
- }
- /*--------------------------------------
- ;溫濕度補(bǔ)償函數(shù)
- ;-------------------------------------*/
- void calc_SHT10(float*p_humidity ,float *p_temperature)
- // calculates temperature[C] and humidity [%RH]
- // input : humi [Ticks] (12 bit)
- // temp [Ticks] (14 bit)
- // output: humi [%RH]
- // temp [C]
- { const float C1=-4.0; // for 12 Bit
- const float C2=+0.0405; // for 12 Bit
- const float C3=-0.0000028; // for 12 Bit
- const float T1=+0.01; // for 14 Bit @ 5V
- const float T2=+0.00008; // for 14 Bit @ 5V
- float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
- float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
- float rh_lin; // rh_lin: Humidity linear
- float rh_true; // rh_true: Temperaturecompensated humidity
- float t_C; // t_C : Temperature [C]
- t_C=t*0.01 - 40; //calc. temperature fromticks to [C]
- rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
- rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity[%RH]
- if(rh_true>100)rh_true=100; //cut if the value is outside of
- if(rh_true<0.1)rh_true=0.1; //the physical possible range
- *p_temperature=t_C; //return temperature [C]
- *p_humidity=rh_true; //return humidity[%RH]
- }
- //**********第二部分SHT10設(shè)置***************
- //END****************************************
復(fù)制代碼
|
|