找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5254|回复: 0
打印 上一主题 下一主题
收起左侧

附上stm32f103控制dht11温湿度传感器的代码

[复制链接]
跳转到指定楼层
楼主
ID:438517 发表于 2018-12-27 09:08 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
实习内容,有些细节没做好

单片机源程序如下:
  1. #include <stdio.h>
  2. #include "uart.h"
  3. #include "tim.h"
  4. #include "SysTick.h"
  5. #include "TFT18.h"
  6. #include "dht11.h"
  7. #include "GUI.h"

  8. #define UART_RX_LEN                8
  9. uint8_t USART2_Buffer[UART_RX_LEN]= {0};
  10. //uint8_t Data_Receive[UART_RX_LEN]= {0};
  11. uint8_t Sendbuf[UART_RX_LEN]={0};
  12. int USART2_Rx=0;
  13. double temperature=0;  //空气温度
  14. double humidity=0;     //空气湿度
  15. //uint8_t illumination; //光照强度
  16. //uint8_t soil;         //土壤湿度
  17. uint8_t Time_3s=0;
  18. uint8_t Time_2s=0;
  19. uint8_t ch;
  20. char buffer[5];
  21. char str[8];


  22. static void SYS_init(void);
  23. static void NVIC_Configuration(void);
  24. static void lcd(void);
  25. static void kaiji(void);



  26. int main()
  27. {

  28.         SYS_init();
  29.     kaiji();
  30.         while(1){
  31. //        u8 buffer[5];
  32. //        if(Time_2s==1){
  33. //            Time_2s=0;
  34. //            if (dht11_read_data(buffer)==0){
  35. //                humidity = buffer[0] + buffer[1] / 10.0;        //空气湿度
  36. //                temperature = buffer[2] + buffer[3]/10.0;     //空气温度
  37. ////                humidity=1.2;
  38. ////                temperature=3.5;
  39. //                }
  40. //            //在屏幕显示数据
  41. //            lcd();   

  42. ///****************数据处理*********************/
  43. ///*
  44. // *回复报文格式         Data_Receive[0]                          Data_Receive[1]    Data_Receive[2]     Data_Receive[3]    Data_Receive[3]
  45. // *         (1为空气温度,2为空气湿度,3为光照强度,4为土壤湿度)    数据十位           数据个位             数据十分位          数据百分位
  46. // *
  47. // */                 
  48. //            Sendbuf[0]=2;               //标志位2位空气湿度
  49. //            Sendbuf[1]=buffer[0]/10;    //湿度的十位
  50. //            Sendbuf[2]=buffer[0]%10;    //湿度的个位
  51. //            Sendbuf[4]=buffer[1]/10;    //湿度的十分位
  52. //            Sendbuf[5]=buffer[1]%10;    //湿度的百分位
  53. //        }
  54.         }
  55. }       


  56. /*
  57. 函数功能:资源初始化
  58. */
  59. static void SYS_init(void)
  60. {
  61.     uart2_init(115200);
  62.     NVIC_Configuration();
  63.     /*tim2 10ms 中断定时*/
  64.     Tim2_Config(10000,71);
  65.     Lcd_Init();
  66.       
  67. }

  68. /*
  69. 函数功能:USART2中断控制
  70. */
  71. void NVIC_Configuration(void)
  72. {  
  73.     //配置中断控制器
  74.   NVIC_InitTypeDef NVIC_InitStructure;
  75. //  /* Enable and set USART Interrupt to the lowest priority */
  76.   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  77.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
  78.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
  79.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  80.   NVIC_Init(&NVIC_InitStructure);
  81.   
  82. }



  83. void kaiji(void)
  84. {
  85.         Lcd_Clear(GRAY0);
  86.         Gui_DrawFont_GBK16(35,50,BLUE,GRAY0,"开机中...");
  87. }
  88. void lcd(void)
  89. {
  90.         Lcd_Clear(GRAY0);
  91.         Gui_DrawFont_GBK16(18,30,BLACK,GRAY0,"智能农业大棚");

  92.         Gui_DrawFont_GBK16(10,50,BLUE,GRAY0,"空气温湿度检测");

  93.         Gui_DrawFont_GBK16(10,70,BLUE,GRAY0,"空气温度:");
  94.     sprintf(str,"%f",temperature);
  95.     Gui_DrawFont_GBK16(80,70,BLUE,GRAY0,str);   
  96.     Gui_DrawFont_GBK16(10,90,BLUE,GRAY0,"空气湿度:");
  97.     sprintf(str,"%f",humidity);
  98.     Gui_DrawFont_GBK16(80,90,BLUE,GRAY0,str);
  99.        
  100. }

  101. /*
  102. 接收中断处理函数
  103. */
  104. //串口5接收中断   

  105. void USART2_IRQHandler()
  106. {
  107. //    u32 temp = 0;  
  108. //    u16 i = 0;
  109.         if(USART_GetITStatus(USART2,USART_IT_RXNE) != RESET) //中断产生
  110.         {
  111.         Gui_DrawFont_Num32(100,50,BLACK,GRAY0,5);
  112.       
  113.                 USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志
  114.                          
  115.                 USART2_Buffer[USART2_Rx] = USART_ReceiveData(USART2);     //接收串口2数据到buff缓冲区
  116.                 USART2_Rx++;
  117.                       
  118.                 if(USART2_Buffer[USART2_Rx-1] == 0x0a || USART2_Rx == UART_RX_LEN)    //如果接收到尾标识是换行符(或者等于最大接受数就清空重新接收)
  119.                 {
  120.             //收到网关以‘9’开头的命令报文就将检测的数据组成的报文Sendbuf回复网关
  121.                         if(USART2_Buffer[0] == '9')                      //检测到头标识是我们需要的
  122.                         {
  123.                
  124.                 delay_ms(20);
  125. //                                UART_Send_Message(USART2,Sendbuf);
  126.                                 USART2_Rx=0;                                 
  127.                         }
  128.                         else
  129.                         {
  130.                                 USART2_Rx=0;                   //不是我们需要的数据或者达到最大接收数则开始重新接收
  131.                         }
  132.                 }
  133.         }
  134. }
复制代码

所有资料51hei提供下载:
空气湿度.7z (166.54 KB, 下载次数: 115)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表