Advertisement
Guest User

Untitled

a guest
May 27th, 2023
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include "driver/mcpwm.h"
  3. #include "soc/mcpwm_reg.h"
  4. #include "soc/mcpwm_struct.h"
  5. #include "wiring_private.h" // pinPeripheral() function
  6.  
  7.  
  8. // MCPWM Pins
  9. #define GPIO_PWM0A_OUT 12
  10. #define GPIO_PWM0B_OUT 13
  11. float pwm1 = 12; // 100 = more voltage, 0 = less
  12.  
  13. static void setup_mcpwm_pins() {
  14. Serial.println("initializing mcpwm control gpio...n");
  15. mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_PWM0A_OUT);
  16. mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_PWM0B_OUT);
  17. } // setup_pins()
  18.  
  19. static void setup_mcpwm() {
  20. setup_mcpwm_pins();
  21.  
  22. mcpwm_config_t pwm_config;
  23. pwm_config.frequency = 50000; // frequency = 20000Hz
  24. pwm_config.cmpr_a = 95.0; // duty cycle of PWMxA = 50.0%
  25. pwm_config.cmpr_b = 95.0; // duty cycle of PWMxB = 50.0%
  26. pwm_config.counter_mode =
  27. MCPWM_UP_DOWN_COUNTER; // MCPWM_UP_DOWN_COUNTER; // Up-down counter (triangle
  28. // wave)
  29. pwm_config.duty_mode = MCPWM_DUTY_MODE_0; // Active HIGH
  30.  
  31. mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); // Configure PWM0A & PWM0B with above settings
  32. mcpwm_deadtime_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 1, 1);
  33. mcpwm_timer_set_resolution(MCPWM_UNIT_0, MCPWM_TIMER_0, 8);
  34. mcpwm_set_timer_sync_output(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SWSYNC_SOURCE_SYNCIN);
  35. delayMicroseconds(1000);
  36. } // setup_mcpwm
  37.  
  38. void set_duty(int mode, float duty) {
  39. mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, duty);
  40. mcpwm_set_duty(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_B, duty);
  41. }
  42.  
  43.  
  44. void setup() {
  45. setup_mcpwm();
  46. set_duty(1, 50);
  47. }
  48.  
  49. void loop() {
  50. // put your main code here, to run repeatedly:
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement