- /**
- * @defgroup DHT11_Public_Functions dht11 Public Functions
- * [url=home.php?mod=space&uid=247401]@brief[/url] BSP DHT11 Functions
- * @verbatim
- ===============================================================================
- ##### BSP DHT11 Functions #####
- ===============================================================================
- [..] This section provides functions allowing to:
- (+) Initialize bsp dht11.
- (+) get dht11 data.
- @endverbatim
- * @{
- */
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] get dht11 data
- * @retval result 1 or 0
- */
- uint8_t dht11_read_data(uint8_t *temp, uint8_t *humi)
- {
- uint8_t buf[5] = {0};
- uint8_t i;
- dht11_reset();
- if (dht11_check() == 0)
- {
- for (i = 0; i < 5; i++)
- buf[i] = dht11_read_byte();
- if ((buf[0] + buf[1] + buf[2] + buf[3]) == buf[4])
- {
- *humi = buf[0];
- *temp = buf[2];
- }
- }
- else
- return 1;
- return 0;
- }
- /**
- * @brief init dht11
- * @retval result 1 or 0
- */
- uint8_t dht11_init(void)
- {
- dht11_pin_init();
- ald_delay_ms(100);
- dht11_reset();
- return dht11_check();
- }
rtc.c
- /**
- * @brief rtc module initialize function
- * @retval None
- */
- void rtc_init(void *rtc_param)
- {
- rtc_param_t *p_rtc = (rtc_param_t *)rtc_param;
- /* Initialize RTC */
- p_rtc->init.asynch_pre_div = 0;
- p_rtc->init.synch_pre_div = 32767;
- p_rtc->init.hour_format = RTC_HOUR_FORMAT_24;
- p_rtc->init.output = RTC_OUTPUT_DISABLE;
- p_rtc->init.output_polarity = RTC_OUTPUT_POLARITY_HIGH;
- ald_rtc_init(&p_rtc->init);
- /* Set current time and date */
- p_rtc->time.hour = 18;
- p_rtc->time.minute = 31;
- p_rtc->time.second = 43;
- p_rtc->time.sub_sec = 123;
- p_rtc->date.day = 15;
- p_rtc->date.month = 12;
- p_rtc->date.year = 19;
- p_rtc->date.week = 7;
- ald_rtc_set_time(&p_rtc->time, RTC_FORMAT_DEC);
- ald_rtc_set_date(&p_rtc->date, RTC_FORMAT_DEC);
- /* Enable second interrupt */
- ald_rtc_interrupt_config(RTC_IT_SEC, ENABLE);
- }
【二】OLED显示屏模块:
我这里使用了OLED12864显示液晶,采用模拟IIC进行驱动,便于程序的移植。
- int main()
- {
- /* Initialize ALD */
- ald_cmu_init();
- /* Configure system clock */
- ald_cmu_pll1_config(CMU_PLL1_INPUT_HOSC_3, CMU_PLL1_OUTPUT_48M);
- ald_cmu_clock_config(CMU_CLOCK_PLL1, 48000000);
-
- /* Initialize tx_buf */
- memset(tx_buf, 0x55, sizeof(tx_buf));
- /* Initialize uart_usb */
- uart_usb_init();
-
- /* Initialize led */
- led_pin_init();
-
- /* Initialize led */
- dht11_init();
-
- /* Initialize oled */
- OLED_Init();
-
- /* Initialize rtc */
- rtc_init(&rtc_h);
- while (1) {
- /* Send a message */
- //sprintf((char *)tx_buf, "es32 uart test!\n");
- //ald_uart_send(&h_uart, tx_buf, sizeof("es32 uart test!\n"), 1000);
-
- /* LED灯测试 */
- cnt++;
- if(cnt == 5)
- {
- ald_gpio_write_pin(LED1_PORT, LED1_PIN, 0);
- ald_gpio_write_pin(LED2_PORT, LED2_PIN, 0);
- sprintf((char *)tx_buf, "LED ON ");
- OLED_P8x16Str(0u,4u,(uint8_t *)tx_buf);
- //ald_delay_ms(1000);
- }else if(cnt == 10)
- {
- ald_gpio_write_pin(LED1_PORT, LED1_PIN, 1);
- ald_gpio_write_pin(LED2_PORT, LED2_PIN, 1);
- sprintf((char *)tx_buf, "LED OFF");
- OLED_P8x16Str(0u,4u,(uint8_t *)tx_buf);
- //ald_delay_ms(1000);
- cnt = 0;
- }
-
- /* DHT11温湿度传感器测试 */
- dht11_read_data(&temp, &humi);
- sprintf((char *)tx_buf, "Temp: %d Humi: %d \n", temp, humi);
- ald_uart_send(&h_uart, tx_buf, sizeof("Temp: %d Humi: %d \n"), 1000);
-
- /* RTC测试 */
- ald_rtc_get_time(&rtc_v, RTC_FORMAT_DEC);
- sprintf((char *)tx_buf, "Time:%02d-%02d-%02d", rtc_v.hour, rtc_v.minute, rtc_v.second);
- OLED_P8x16Str(0u,6u,(uint8_t *)tx_buf);
-
- /* OLED显示屏测试 */
- OLED_P8x16Str(0u,0u,(uint8_t *)"ES32 PDS BOARD");
- sprintf((char *)tx_buf, "Temp:%d Humi:%d", temp, humi);
- OLED_P8x16Str(0u,2u,(uint8_t *)tx_buf);
- //OLED_P8x16Str(0u,6u,(uint8_t *)"Date:2019-12-15");
- }
- }
OLED显示效果和串口调试打印输出如下:
完整工程源代码: