LED.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <stdio.h>
  2. #include "LED.h"
  3. #include "esp_log.h"
  4. static const char *LOG_TAG = "LED";
  5. //uint8_t led[6]={0x04,0x20,0x08,0x10,0x02,0x40 };
  6. //uint8_t led[6]={0x20,0x02,0x04,0x08,0x10,0x40 };
  7. //dailiao baoyang tunxing fengcun guzhang tingji
  8. uint8_t led[6]={0x04,0x40,0x20,0x10,0x08,0x02 };
  9. #include <stdio.h>
  10. #include "esp_err.h"
  11. static void example_ledc_init(void)
  12. {
  13. // Prepare and then apply the LEDC PWM timer configuration
  14. ledc_timer_config_t ledc_timer = {
  15. .speed_mode = LEDC_MODE,
  16. .timer_num = LEDC_TIMER,
  17. .duty_resolution = LEDC_DUTY_RES,
  18. .freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
  19. .clk_cfg = LEDC_AUTO_CLK
  20. };
  21. ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
  22. // Prepare and then apply the LEDC PWM channel configuration
  23. ledc_channel_config_t ledc_channel = {
  24. .speed_mode = LEDC_MODE,
  25. .channel = LEDC_CHANNEL,
  26. .timer_sel = LEDC_TIMER,
  27. .intr_type = LEDC_INTR_DISABLE,
  28. .gpio_num = LEDC_OUTPUT_IO,
  29. .duty = 0, // Set duty to 0%
  30. .hpoint = 0
  31. };
  32. ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
  33. }
  34. void led_init(void)
  35. {
  36. #if 0
  37. gpio_config_t led_pin_cfg = {};
  38. led_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  39. led_pin_cfg.mode = GPIO_MODE_OUTPUT;
  40. led_pin_cfg.pin_bit_mask =LED_OUTPUT_PIN_SEL;
  41. led_pin_cfg.pull_down_en = 0;
  42. led_pin_cfg.pull_up_en = 1;
  43. gpio_config(&led_pin_cfg);
  44. LED_SCLK_0;
  45. LED_DATA_0;
  46. LED_LCLK_0;
  47. LED_RST_0;
  48. LED_RST_1;
  49. for(int i=0;i<6;i++)
  50. led_set(i,0);
  51. #else
  52. //void app_main(void)
  53. {
  54. // Set the LEDC peripheral configuration
  55. example_ledc_init();
  56. // Set duty to 50%
  57. ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 0));
  58. // Update duty to apply the new value
  59. ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
  60. }
  61. #endif
  62. }
  63. static void _74HC595_WriteByte(unsigned char Byte)
  64. {
  65. unsigned char i;
  66. for(i=0;i<8;i++)
  67. {
  68. uint8_t bit = (Byte >> (i)) & 0x01;
  69. gpio_set_level(LED_DATA_PIN,bit);
  70. LED_LCLK_0 ;
  71. LED_LCLK_1 ;
  72. }
  73. LED_SCLK_0;
  74. LED_SCLK_1;
  75. }
  76. void led_set(uint8_t led_index,uint8_t led_status)
  77. {
  78. ESP_LOGI(LOG_TAG,"led_set %s",led_status ? "open":"close");
  79. if(led_status)
  80. {
  81. _74HC595_WriteByte(led[led_index]);
  82. }else
  83. {
  84. _74HC595_WriteByte(0x00);
  85. }
  86. }