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)
|