標(biāo)題: stm8 dth22驅(qū)動代碼 [打印本頁]

作者: Tang-天空    時(shí)間: 2018-6-23 20:23
標(biāo)題: stm8 dth22驅(qū)動代碼
stm8 dth22代碼

  1. #include "main.h"

  2. u8 CMD_rx_buf[8];   //命令緩沖區(qū)
  3. u8 DATA_tx_buf[14]; //返回?cái)?shù)據(jù)緩沖區(qū)
  4. u8 CMD_ID = 0;  // 命令序號
  5. u8 Sensor_Type = 0; // 傳感器類型編號
  6. u8 Sensor_ID = 0;   // 相同類型傳感器編號
  7. u8 Sensor_Data[6];  // 傳感器數(shù)據(jù)

  8. u8 Sensor_Data_Digital = 0; // 數(shù)字類型傳感器數(shù)據(jù)
  9. u16 Sensor_Data_Analog = 0; // 模擬類型傳感器數(shù)據(jù)   
  10. u16 Sensor_Data_Threshod = 0;// 模擬傳感器閾值
  11.    

  12. void main(void)
  13. {
  14.   u8 i = 0;  
  15.   //初始化時(shí)鐘,設(shè)置內(nèi)部高速時(shí)鐘為16M
  16.   CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  17.   //串口初始化
  18.   Uart1_Init();
  19.   //LED初始化
  20.    LED_Init();
  21.   //DHT22初始化
  22.    DHT22_Init();
  23.   //初始化數(shù)據(jù)幀(EE CC 0A 01 01 XX XX XX XX XX XX XX XX FF)
  24.    for(i = 0;i < 14;i++)
  25.         DATA_tx_buf[i] = 0;
  26.     for(i = 0;i < 8;i++)
  27.         CMD_rx_buf[i] = 0;
  28.    
  29.     Sensor_Type = 0x0A;
  30.     Sensor_ID = 1;
  31.    
  32.     CMD_ID = 1;
  33.    
  34.     DATA_tx_buf[0] = 0xEE;
  35.     DATA_tx_buf[1] = 0xCC;
  36.     DATA_tx_buf[2] = Sensor_Type;
  37.     DATA_tx_buf[3] = Sensor_ID;
  38.     DATA_tx_buf[4] = CMD_ID;
  39.     DATA_tx_buf[13] = 0xFF;
  40.    
  41.     delay_ms(1000);
  42.     while (1)
  43.     {
  44.         // 獲取傳感器數(shù)據(jù)
  45.         if(DHT22_Read())
  46.         {
  47.             Sensor_Data[2] = Humidity >> 8;
  48.             Sensor_Data[3] = Humidity&0xFF;
  49.             Sensor_Data[4] = Temperature >> 8;
  50.             Sensor_Data[5] = Temperature&0xFF;
  51.         }
  52.         
  53.         // 組合數(shù)據(jù)幀
  54.         for(i = 0;i < 6;i++)
  55.             DATA_tx_buf[5+i] = Sensor_Data[i];
  56.         
  57.         // 發(fā)送數(shù)據(jù)幀
  58.         UART1_SendString(DATA_tx_buf, 14);
  59.         LED_Toggle();
  60.         delay_ms(1000);
  61.     }
  62. }

  63. #ifdef USE_FULL_ASSERT

  64. /**
  65.   * @brief  Reports the name of the source file and the source line number
  66.   *   where the assert_param error has occurred.
  67.   * @param file: pointer to the source file name
  68.   * @param line: assert_param error line source number
  69.   * @retval None
  70.   */
  71. void assert_failed(uint8_t* file, uint32_t line)
  72. {
  73.   /* User can add his own implementation to report the file name and line number,
  74.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  75.   /* Infinite loop */
  76.   while (1)
  77.   {
  78.   }
  79. }
  80. #endif
復(fù)制代碼


全部資料51hei下載地址:
DHT22練習(xí) - 副本.zip (7.06 MB, 下載次數(shù): 28)



作者: jemery1030    時(shí)間: 2019-9-16 13:07
感謝您的分享




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1