CHQ1838引脚示意图
源程序如下:
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "freertos/queue.h"
- #include "driver/gpio.h"
- #include "esp_log.h"
- #include "esp_system.h"
- static const char *TAG = "main";
- #define GPIO_INPUT_IO_2 2 // D4
- #define GPIO_INPUT_PIN_SEL (1ULL<<GPIO_INPUT_IO_2)
- static xQueueHandle gpio_evt_queue = NULL;
- extern uint32_t esp_get_time(void); // 获取当前启动毫秒数
- static void gpio_task_example(void *arg)
- {
- uint32_t interval_us,ms;
- for (;;) {
- if (xQueueReceive(gpio_evt_queue, &interval_us, portMAX_DELAY)) {
- // ESP_LOGI(TAG, "GPIO failling edge interval time: %d\n", interval_us);
- // printf("data is: ");
- ms = interval_us / 100;
- if (ms <= 150 && ms >= 120 ) // 在 12 - 15 ms之间认为是前导码
- {
- printf("\n start ");
- }
- else if (ms < 20 && ms >= 10) // 在 1 - 2 ms之间认为是0
- {
- printf("0 ");
- }
- else if (ms <= 30 && ms >= 20) // 在2 - 3 ms之间认为是1
- {
- printf("1 ");
- }
- }
- }
- }
- uint32_t first_time = 0, second_time = 0,interval_time = 0;
- static void monitor_chq1838_break(void *arg){
- first_time = second_time;
- second_time = esp_get_time();
- interval_time = second_time - first_time;
- xQueueSendFromISR(gpio_evt_queue, &interval_time, NULL);
- }
- void app_main(void)
- {
- gpio_config_t io_conf;
- io_conf.intr_type = GPIO_INTR_NEGEDGE;
- io_conf.mode = GPIO_MODE_INPUT;
- io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
- io_conf.pull_down_en = 0;
- io_conf.pull_up_en = 1;
- gpio_config(&io_conf);
- gpio_evt_queue = xQueueCreate(100, sizeof(uint32_t));
- xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
- gpio_install_isr_service(0);
- gpio_isr_handler_add(GPIO_INPUT_IO_2, monitor_chq1838_break, (void *) GPIO_INPUT_IO_2);
- while (1) {
- vTaskDelay(1000 / portTICK_RATE_MS);
- }
- }
复制代码
手册下载:
CHQ1838.PDF
(806.72 KB, 下载次数: 13)
|