Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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 val_ph1 = 0; // Valeur PWM Phase 1
- int val_ph2 = 0; // Valeur PWM Phase 2
- int val_ph3 = 0; // Valeur PWM Phase 3
- int compteur_ph1 = 0; // Compteur Phase 1
- int compteur_ph2 = 0; // Compteur Phase 2
- int compteur_ph3 = 0; // Compteur Phase 3
- int switch_pola_ph1 = 0; // Switch polarité sinus Phase 1
- int switch_pola_ph2 = 0; // Switch polarité sinus Phase 2
- int switch_pola_ph3 = 0; // Switch polarité sinus Phase 3
- long delay_timer = 2000; // Délai PWM en microns / 30Hz = 30 / 50Hz = 5200 / 115Hz = ~2000
- long unsigned timer_1 = 0;
- long unsigned timer_2 = 0;
- long unsigned timer_3 = 0;
- float load_pwm = 1.0; // Facteur modulation valeur PWM
- int timing_1[15] = {0, 20, 50, 75, 120, 160, 220, 255, 220, 160, 120, 75, 50, 20, 0};
- int lg_timing = 15;
- 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 | 0x02; //Timer 0 (PWM pins 5 & 6) =>
- TCCR1B = TCCR1B & 0b11111000 | 0x01; //Timer 1 (PWM pins 9 & 10) =>
- TCCR2B = TCCR2B & 0b11111000 | 0x01; //Timer 2 (PWM pins 3 & 11) =>
- }
- void loop() {
- if (timer_1 == 0 && timer_2 == 0 && timer_3 == 0)
- {
- timer_1 = micros();
- timer_2 = micros() + (delay_timer * 10);
- timer_3 = micros() + (delay_timer * 20);
- }
- // PHASE 1
- if (micros() >= timer_1)
- {
- val_ph1 = timing_1[compteur_ph1] * load_pwm;
- if (switch_pola_ph1 == 0)
- {
- analogWrite(PinPosPh1, val_ph1);
- }
- else {
- analogWrite(PinNegPh1, val_ph1);
- }
- timer_1 = micros() + delay_timer;
- compteur_ph1++;
- }
- // PHASE 2
- if (micros() >= timer_2)
- {
- val_ph2 = timing_1[compteur_ph2] * load_pwm;
- if (switch_pola_ph2 == 0)
- {
- analogWrite(PinPosPh2, val_ph2);
- }
- else {
- analogWrite(PinNegPh2, val_ph2);
- }
- timer_2 = micros() + delay_timer;
- compteur_ph2++;
- }
- // PHASE 3
- if (micros() >= timer_3)
- {
- val_ph3 = timing_1[compteur_ph3] * load_pwm;
- if (switch_pola_ph3 == 0)
- {
- analogWrite(PinPosPh3, val_ph3);
- }
- else {
- analogWrite(PinNegPh3, val_ph3);
- }
- timer_3 = micros() + delay_timer;
- compteur_ph3++;
- }
- // Serial.print(timer_1);
- // Serial.print(',');
- // Serial.print(timer_2);
- // Serial.print(',');
- // Serial.print(timer_3);
- // Serial.println();
- // Serial.print(compteur_ph1);
- // Serial.print(',');
- // Serial.print(compteur_ph2);
- // Serial.print(',');
- // Serial.print(compteur_ph3);
- // Serial.println();
- // Serial.print(val_ph1);
- // Serial.print(',');
- // Serial.print(val_ph2);
- // Serial.print(',');
- // Serial.print(val_ph3);
- // Serial.println();
- if (compteur_ph1 >= 15)
- {
- compteur_ph1 = 0;
- switch_pola_ph1 = !switch_pola_ph1;
- }
- if (compteur_ph2 >= 15)
- {
- compteur_ph2 = 0;
- switch_pola_ph2 = !switch_pola_ph2;
- }
- if (compteur_ph3 >= 15)
- {
- compteur_ph3 = 0;
- switch_pola_ph3 = !switch_pola_ph3;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment