1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include "driver/mcpwm.h" #include "soc/mcpwm_periph.h" #include "esp_attr.h"
#define SERVO_MIN_PULSEWIDTH 500 #define SERVO_MAX_PULSEWIDTH 2500 #define SERVO_MAX_DEGREE 180
static void mcpwm_example_gpio_initialize(void) { printf("initializing mcpwm servo control gpio......\n"); mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, 18); }
static uint32_t servo_per_degree_init(uint32_t degree_of_rotation) { uint32_t cal_pulsewidth = 0; cal_pulsewidth = (SERVO_MIN_PULSEWIDTH + (((SERVO_MAX_PULSEWIDTH - SERVO_MIN_PULSEWIDTH) * (degree_of_rotation)) / (SERVO_MAX_DEGREE))); return cal_pulsewidth; }
void turn_on(void *arg) { uint32_t angle; mcpwm_example_gpio_initialize(); printf("Configuring Initial Parameters of mcpwm......\n"); mcpwm_config_t pwm_config; pwm_config.frequency = 50; pwm_config.cmpr_a = 0; pwm_config.cmpr_b = 0; pwm_config.counter_mode = MCPWM_UP_COUNTER; pwm_config.duty_mode = MCPWM_DUTY_MODE_0; mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); printf("Angle of rotation: %d\n", 0); angle = servo_per_degree_init(60); printf("pulse width: %dus\n", angle); mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, angle); vTaskDelay(10); vTaskDelete(NULL); }
void turn_off(void *arg) { uint32_t angle; mcpwm_example_gpio_initialize(); printf("Configuring Initial Parameters of mcpwm......\n"); mcpwm_config_t pwm_config; pwm_config.frequency = 50; pwm_config.cmpr_a = 0; pwm_config.cmpr_b = 0; pwm_config.counter_mode = MCPWM_UP_COUNTER; pwm_config.duty_mode = MCPWM_DUTY_MODE_0; mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config); printf("Angle of rotation: %d\n", 0); angle = servo_per_degree_init(120); printf("pulse width: %dus\n", angle); mcpwm_set_duty_in_us(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_OPR_A, angle); vTaskDelay(10); vTaskDelete(NULL); }
void led_write(bool on) { if (on) { xTaskCreate(turn_on, "turn_on", 4096, NULL, 5, NULL); } else { xTaskCreate(turn_off, "turn_off", 4096, NULL, 5, NULL); } }
|