|
實(shí)習(xí)內(nèi)容,有些細(xì)節(jié)沒(méi)做好
單片機(jī)源程序如下:
- #include <stdio.h>
- #include "uart.h"
- #include "tim.h"
- #include "SysTick.h"
- #include "TFT18.h"
- #include "dht11.h"
- #include "GUI.h"
- #define UART_RX_LEN 8
- uint8_t USART2_Buffer[UART_RX_LEN]= {0};
- //uint8_t Data_Receive[UART_RX_LEN]= {0};
- uint8_t Sendbuf[UART_RX_LEN]={0};
- int USART2_Rx=0;
- double temperature=0; //空氣溫度
- double humidity=0; //空氣濕度
- //uint8_t illumination; //光照強(qiáng)度
- //uint8_t soil; //土壤濕度
- uint8_t Time_3s=0;
- uint8_t Time_2s=0;
- uint8_t ch;
- char buffer[5];
- char str[8];
- static void SYS_init(void);
- static void NVIC_Configuration(void);
- static void lcd(void);
- static void kaiji(void);
- int main()
- {
- SYS_init();
- kaiji();
- while(1){
- // u8 buffer[5];
- // if(Time_2s==1){
- // Time_2s=0;
- // if (dht11_read_data(buffer)==0){
- // humidity = buffer[0] + buffer[1] / 10.0; //空氣濕度
- // temperature = buffer[2] + buffer[3]/10.0; //空氣溫度
- //// humidity=1.2;
- //// temperature=3.5;
- // }
- // //在屏幕顯示數(shù)據(jù)
- // lcd();
- ///****************數(shù)據(jù)處理*********************/
- ///*
- // *回復(fù)報(bào)文格式 Data_Receive[0] Data_Receive[1] Data_Receive[2] Data_Receive[3] Data_Receive[3]
- // * (1為空氣溫度,2為空氣濕度,3為光照強(qiáng)度,4為土壤濕度) 數(shù)據(jù)十位 數(shù)據(jù)個(gè)位 數(shù)據(jù)十分位 數(shù)據(jù)百分位
- // *
- // */
- // Sendbuf[0]=2; //標(biāo)志位2位空氣濕度
- // Sendbuf[1]=buffer[0]/10; //濕度的十位
- // Sendbuf[2]=buffer[0]%10; //濕度的個(gè)位
- // Sendbuf[4]=buffer[1]/10; //濕度的十分位
- // Sendbuf[5]=buffer[1]%10; //濕度的百分位
- // }
- }
- }
- /*
- 函數(shù)功能:資源初始化
- */
- static void SYS_init(void)
- {
- uart2_init(115200);
- NVIC_Configuration();
- /*tim2 10ms 中斷定時(shí)*/
- Tim2_Config(10000,71);
- Lcd_Init();
-
- }
- /*
- 函數(shù)功能:USART2中斷控制
- */
- void NVIC_Configuration(void)
- {
- //配置中斷控制器
- NVIC_InitTypeDef NVIC_InitStructure;
- // /* Enable and set USART Interrupt to the lowest priority */
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- }
- void kaiji(void)
- {
- Lcd_Clear(GRAY0);
- Gui_DrawFont_GBK16(35,50,BLUE,GRAY0,"開機(jī)中...");
- }
- void lcd(void)
- {
- Lcd_Clear(GRAY0);
- Gui_DrawFont_GBK16(18,30,BLACK,GRAY0,"智能農(nóng)業(yè)大棚");
- Gui_DrawFont_GBK16(10,50,BLUE,GRAY0,"空氣溫濕度檢測(cè)");
- Gui_DrawFont_GBK16(10,70,BLUE,GRAY0,"空氣溫度:");
- sprintf(str,"%f",temperature);
- Gui_DrawFont_GBK16(80,70,BLUE,GRAY0,str);
- Gui_DrawFont_GBK16(10,90,BLUE,GRAY0,"空氣濕度:");
- sprintf(str,"%f",humidity);
- Gui_DrawFont_GBK16(80,90,BLUE,GRAY0,str);
-
- }
- /*
- 接收中斷處理函數(shù)
- */
- //串口5接收中斷
- void USART2_IRQHandler()
- {
- // u32 temp = 0;
- // u16 i = 0;
- if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //中斷產(chǎn)生
- {
- Gui_DrawFont_Num32(100,50,BLACK,GRAY0,5);
-
- USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中斷標(biāo)志
-
- USART2_Buffer[USART2_Rx] = USART_ReceiveData(USART2); //接收串口2數(shù)據(jù)到buff緩沖區(qū)
- USART2_Rx++;
-
- if(USART2_Buffer[USART2_Rx-1] == 0x0a || USART2_Rx == UART_RX_LEN) //如果接收到尾標(biāo)識(shí)是換行符(或者等于最大接受數(shù)就清空重新接收)
- {
- //收到網(wǎng)關(guān)以‘9’開頭的命令報(bào)文就將檢測(cè)的數(shù)據(jù)組成的報(bào)文Sendbuf回復(fù)網(wǎng)關(guān)
- if(USART2_Buffer[0] == '9') //檢測(cè)到頭標(biāo)識(shí)是我們需要的
- {
-
- delay_ms(20);
- // UART_Send_Message(USART2,Sendbuf);
- USART2_Rx=0;
- }
- else
- {
- USART2_Rx=0; //不是我們需要的數(shù)據(jù)或者達(dá)到最大接收數(shù)則開始重新接收
- }
- }
- }
- }
復(fù)制代碼
所有資料51hei提供下載:
空氣濕度.7z
(166.54 KB, 下載次數(shù): 116)
2018-12-28 02:49 上傳
點(diǎn)擊文件名下載附件
dth11驅(qū)動(dòng)
|
評(píng)分
-
查看全部評(píng)分
|