rotary_encoder_example_main.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* PCNT example -- Rotary Encoder
  2. This example code is in the Public Domain (or CC0 licensed, at your option.)
  3. Unless required by applicable law or agreed to in writing, this
  4. software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  5. CONDITIONS OF ANY KIND, either express or implied.
  6. */
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_log.h"
  10. #include "rotary_encoder.h"
  11. static const char *TAG = "example";
  12. void app_main(void)
  13. {
  14. // Rotary encoder underlying device is represented by a PCNT unit in this example
  15. uint32_t pcnt_unit = 0;
  16. // Create rotary encoder instance
  17. rotary_encoder_config_t config = ROTARY_ENCODER_DEFAULT_CONFIG((rotary_encoder_dev_t)pcnt_unit, 14, 15);
  18. rotary_encoder_t *encoder = NULL;
  19. ESP_ERROR_CHECK(rotary_encoder_new_ec11(&config, &encoder));
  20. // Filter out glitch (1us)
  21. ESP_ERROR_CHECK(encoder->set_glitch_filter(encoder, 1));
  22. // Start encoder
  23. ESP_ERROR_CHECK(encoder->start(encoder));
  24. // Report counter value
  25. while (1) {
  26. ESP_LOGI(TAG, "Encoder value: %d", encoder->get_counter_value(encoder));
  27. vTaskDelay(pdMS_TO_TICKS(1000));
  28. }
  29. }