// Attributions des pins PWM
int PinPosPh1 = 3; // Timer 2
int PinNegPh1 = 11; // Timer 2
int PinPosPh2 = 5; // Timer 0
int PinNegPh2 = 6; // Timer 0
int PinPosPh3 = 9; // Timer 1
int PinNegPh3 = 10; // Timer 1
int compteur_ph1 = 0; // Compteur Phase 1
int compteur_ph2 = 4; // Compteur Phase 2
int compteur_ph3 = 7; // Compteur Phase 3
int switch_pola_ph1 = 0; // Switch polarité sinus Phase 1
int switch_pola_ph2 = 1; // Switch polarité sinus Phase 2
int switch_pola_ph3 = 0; // Switch polarité sinus Phase 3
long delay_timer = 50000; // Délai PWM en microns / 30Hz = 30 / 50Hz = 18 / 115Hz = 7
long unsigned timer_1 = 0;
float load_pwm = 1.0; // Facteur modulation valeur PWM
int timing_1[] = {0, 20, 60, 120, 160, 220, 255, 220, 160, 120, 60, 20, 0, 20, 60, 120, 160, 220, 255, 220, 160, 120, 60, 20, 0};
void setup() {
Serial.begin(9600);
// Forçage à 0 sorties PWM
char PinPWM[] = {\'PinPosPh1\', \'PinPosPh2\', \'PinPosPh3\', \'PinNegPh1\', \'PinNegPh2\', \'PinNegPh3\'};
for (int i = 0; i < 6; i++) {
pinMode(PinPWM[i], OUTPUT);
analogWrite(PinPWM[i], 0);
}
//TCCR0B = TCCR0B & 0b11111000 | 0x01; //Timer 0 (PWM pins 5 & 6)
TCCR1B = TCCR1B & 0b11111000 | 0x03; //Timer 1 (PWM pins 9 & 10)
TCCR2B = TCCR2B & 0b11111000 | 0x03; //Timer 2 (PWM pins 3 & 11)
}
void loop() {
if (micros() >= timer_1)
{
int val_ph1 = timing_1[compteur_ph1] * load_pwm;
int val_ph2 = timing_1[compteur_ph2] * load_pwm;
int val_ph3 = timing_1[compteur_ph3] * load_pwm;
Serial.print(val_ph1);
Serial.print(\',\');
Serial.print(val_ph2);
Serial.print(\',\');
Serial.print(val_ph3);
Serial.println();
// Phase 1
if (switch_pola_ph1 == 0)
{
analogWrite(PinPosPh1, val_ph1);
}
else {
analogWrite(PinNegPh1, val_ph1);
}
// Phase 2
if (switch_pola_ph2 == 0)
{
analogWrite(PinPosPh2, val_ph1);
}
else {
analogWrite(PinNegPh2, val_ph1);
}
// Phase 3
if (switch_pola_ph3 == 0)
{
analogWrite(PinPosPh3, val_ph1);
}
else {
analogWrite(PinNegPh3, val_ph1);
}
timer_1 = micros() + delay_timer;
compteur_ph1++;
compteur_ph2++;
compteur_ph3++;
}
if (compteur_ph1 == 13)
{
compteur_ph1 = 0;
switch_pola_ph1 = !switch_pola_ph1;
}
if (compteur_ph2 == 17)
{
compteur_ph2 = 4;
switch_pola_ph2 = !switch_pola_ph2;
}
if (compteur_ph3 == 21)
{
compteur_ph3 = 8;
switch_pola_ph3 = !switch_pola_ph3;
}
}