消えゆく世界と流れる未来に最後の灯を since 2006/4/3
2021/1/10現在、MCPWM.hをいじる必要があり。逆回転も問題なさげ pic.twitter.com/8n3SQkb9qf
— オウバク (@phellodendron) January 10, 2021
#include <M5Stack.h>
#include "driver/mcpwm.h"
#include "soc/mcpwm_reg.h"
#include "soc/mcpwm_struct.h"
uint32_t time_on=0;
boolean clockwise=false;
boolean button_on=false;
boolean button_on_complete=false;
void setup() {
// put your setup code here, to run once:
M5.begin();
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_NUM_3);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_NUM_1);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1A, GPIO_NUM_16);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM1B, GPIO_NUM_17);
mcpwm_config_t pwm_config;
pwm_config.frequency = 50;
pwm_config.cmpr_a = 50;
pwm_config.cmpr_b = pwm_config.cmpr_a;
pwm_config.counter_mode = MCPWM_UP_DOWN_COUNTER;
pwm_config.duty_mode = MCPWM_DUTY_MODE_0;
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_0, &pwm_config);
mcpwm_init(MCPWM_UNIT_0, MCPWM_TIMER_1, &pwm_config);
mcpwm_deadtime_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 80, 80);
mcpwm_deadtime_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, 80, 80);
//mcpwm.h line 144-147 added
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_0, MCPWM_SELECT_INT1, 0);
mcpwm_sync_enable(MCPWM_UNIT_0, MCPWM_TIMER_1, MCPWM_SELECT_INT1, 500);
MCPWM0.timer[0].sync.out_sel = 1;
}
// M5.BtnB.wasPressed()使えば簡単に再現できたので、下側のloop()はコメントアウトした。
void loop() {
// put your main code here, to run repeatedly:
M5.update();
if (M5.BtnA.wasPressed()) {
if (motorMoving) {
M5.Lcd.println("Motor Stops");
mcpwm_stop(MCPWM_UNIT_0, MCPWM_TIMER_0);
mcpwm_stop(MCPWM_UNIT_0, MCPWM_TIMER_1);
} else {
M5.Lcd.println("Motor Starts");
mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_0);
mcpwm_start(MCPWM_UNIT_0, MCPWM_TIMER_1);
}
motorMoving = !motorMoving;
}
if (M5.BtnB.wasPressed()) {
if (clockwise) {
M5.Lcd.println("to counterwise");
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_NUM_3);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_NUM_1);
} else {
M5.Lcd.println("to clockwise");
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_NUM_1);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_NUM_3);
}
clockwise = !clockwise;
MCPWM0.timer[0].sync.out_sel = 1;
}
}
/*void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(39)==0) {
if (!button_on) {
button_on = true;
button_on_complete = false;
time_on = millis();
} else {
if (!button_on_complete) {
if (millis()-time_on>30) {
button_on_complete=true;
if (clockwise) {
M5.Lcd.printf("to counterwise\n");
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_NUM_3);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_NUM_1);
clockwise=false;
} else {
M5.Lcd.printf("to clockwise\n");
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0A, GPIO_NUM_1);
mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM0B, GPIO_NUM_3);
clockwise=true;
}
MCPWM0.timer[0].sync.out_sel = 1;
}
}
}
} else {
button_on = false;
}
}*/
typedef enum {
MCPWM_SELECT_INT0 =0,
MCPWM_SELECT_INT1,
MCPWM_SELECT_INT2,
MCPWM_SELECT_INT3,
MCPWM_SELECT_SYNC0, /*!<Select SYNC0 as input*/
MCPWM_SELECT_SYNC1, /*!<Select SYNC1 as input*/
MCPWM_SELECT_SYNC2, /*!<Select SYNC2 as input*/
} mcpwm_sync_signal_t;