RuiViana

ESP32_LEDc.ino

Sep 27th, 2020
167
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. / Oscilador inicial, criado pelo GUSTAVO
  2.  
  3. // ESP32 DEVKIT / Arduino IDE 1.8.9.  / ESP32 versao 1.0.4
  4. // Based on  https://portal.vidadesilicio.com.br/controle-de-potencia-via-pwm-esp32/
  5. //           https://randomnerdtutorials.com/esp32-pwm-arduino-ide/
  6. //           https://techtutorialsx.com/2017/06/15/esp32-arduino-led-pwm-fading/
  7. // Max Freq = 40 MHz / bit resolution   (Bit resolution = 1 to 16 bit)
  8. // Duty cycle = decimal * 100 / (2 ^ bit resolution)
  9. // Reference  https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.c
  10. //            https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/ledc.html
  11. // Exemplo: para um duty cycle de 50% com 10bits (2^10 = 1024) de resolução, devemos escrever 512.
  12.  
  13. //  resolBits = log((80000000 / frequencia)) + 1;
  14. //  dutyCycle = (pow(2, resolBits)) / 2;
  15.  
  16. //-------------------------------------------------------------------
  17. void setup()
  18. {
  19.   Serial.begin(115200);
  20.   pinMode(18, OUTPUT);             // GPIO_18 as Output
  21.   ledcAttachPin(18, 1);            // GPIO_18 attached to PWM Channel 1
  22.   ledcSetup(1, 250000, 5);         // Channel 1 , freq 250 KHz , 5 bit resolution = 32
  23.   ledcWrite(1, 8);                 // Enable frequency with dutty cycle 25%
  24. }
  25. //-------------------------------------------------------------------
  26. void loop()
  27. {
  28.   ;
  29. }
RAW Paste Data