stm8 dth22代碼
- #include "main.h"
- u8 CMD_rx_buf[8]; //命令緩沖區
- u8 DATA_tx_buf[14]; //返回數據緩沖區
- u8 CMD_ID = 0; // 命令序號
- u8 Sensor_Type = 0; // 傳感器類型編號
- u8 Sensor_ID = 0; // 相同類型傳感器編號
- u8 Sensor_Data[6]; // 傳感器數據
- u8 Sensor_Data_Digital = 0; // 數字類型傳感器數據
- u16 Sensor_Data_Analog = 0; // 模擬類型傳感器數據
- u16 Sensor_Data_Threshod = 0;// 模擬傳感器閾值
-
- void main(void)
- {
- u8 i = 0;
- //初始化時鐘,設置內部高速時鐘為16M
- CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
- //串口初始化
- Uart1_Init();
- //LED初始化
- LED_Init();
- //DHT22初始化
- DHT22_Init();
- //初始化數據幀(EE CC 0A 01 01 XX XX XX XX XX XX XX XX FF)
- for(i = 0;i < 14;i++)
- DATA_tx_buf[i] = 0;
- for(i = 0;i < 8;i++)
- CMD_rx_buf[i] = 0;
-
- Sensor_Type = 0x0A;
- Sensor_ID = 1;
-
- CMD_ID = 1;
-
- DATA_tx_buf[0] = 0xEE;
- DATA_tx_buf[1] = 0xCC;
- DATA_tx_buf[2] = Sensor_Type;
- DATA_tx_buf[3] = Sensor_ID;
- DATA_tx_buf[4] = CMD_ID;
- DATA_tx_buf[13] = 0xFF;
-
- delay_ms(1000);
- while (1)
- {
- // 獲取傳感器數據
- if(DHT22_Read())
- {
- Sensor_Data[2] = Humidity >> 8;
- Sensor_Data[3] = Humidity&0xFF;
- Sensor_Data[4] = Temperature >> 8;
- Sensor_Data[5] = Temperature&0xFF;
- }
-
- // 組合數據幀
- for(i = 0;i < 6;i++)
- DATA_tx_buf[5+i] = Sensor_Data[i];
-
- // 發送數據幀
- UART1_SendString(DATA_tx_buf, 14);
- LED_Toggle();
- delay_ms(1000);
- }
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
復制代碼
全部資料51hei下載地址:
DHT22練習 - 副本.zip
(7.06 MB, 下載次數: 28)
2018-6-23 20:23 上傳
點擊文件名下載附件
stm8 下載積分: 黑幣 -5
|