#define PIN_PWM_1 2
#define PIN_PWM_2 4
uint slice_num_1, slice_num_2;
int shiftNum = 10; // сколько циклов сдвигов фазы при сдвиге
auto &mySerial = Serial1; //Чтобы быстро менять на другой Serial
void setup() {
// put your setup code here, to run once:
mySerial.begin(115200);
mySerial.setTimeout(10);
mySerial.println("===START===");
mySerial.print("shiftNum = "), mySerial.println(shiftNum);
setupPWM();
}
void loop() {
if (mySerial.available()) {
int i = mySerial.parseInt(); // (если надо 0, то передаем -1)
if (i != 0) { // 0 возвращается и в случае ошибки. Поэтому если надо передать 0, то передаем его как -1
if (i == -1) // 0 передаем как -1
i = 0;
shiftNum = i;
mySerial.print("New shiftNum = "), mySerial.println(shiftNum); // Сообщаем принятое значение
}
}
uint32_t micr = micros();
for (int i = 0; i < shiftNum; i++)
pwm_advance_count(slice_num_2);
mySerial.println(micros() - micr);
delay(1000);
}
void setupPWM() {
// Tell GPIO 0 and 1 they are allocated to the PWM
gpio_set_function(PIN_PWM_1, GPIO_FUNC_PWM);
gpio_set_function(PIN_PWM_2, GPIO_FUNC_PWM);
// Find out which PWM slice is connected to PIN_PWM_1
slice_num_1 = pwm_gpio_to_slice_num(PIN_PWM_1);
//pwm_set_clkdiv(slice_num_1, 125); // pwm clock should now be running at 1MHz
pwm_set_clkdiv(slice_num_1, 125); //
// Set period N+1
pwm_set_wrap(slice_num_1, 1023);
// Set channel A output high for M cycle before dropping
pwm_set_chan_level(slice_num_1, PWM_CHAN_A, 128);
// Find out which PWM slice is connected to PIN_PWM_2
slice_num_2 = pwm_gpio_to_slice_num(PIN_PWM_2);
pwm_set_clkdiv(slice_num_2, 125); // pwm clock should now be running at 1MHz
// Set period N+1
pwm_set_wrap(slice_num_2, 1023);
// Set channel A output high for M cycle before dropping
pwm_set_chan_level(slice_num_2, PWM_CHAN_A, 256);
// Set the PWM running
pwm_set_enabled(slice_num_1, true);
pwm_set_enabled(slice_num_2, true);
}