Guest User

Untitled

a guest
May 4th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Attributions des pins PWM
  2. int PinPosPh1 = 3; // Timer 2
  3. int PinNegPh1 = 11; // Timer 2
  4. int PinPosPh2 = 5; // Timer 0
  5. int PinNegPh2 = 6; // Timer 0
  6. int PinPosPh3 = 9; // Timer 1
  7. int PinNegPh3 = 10; // Timer 1
  8.  
  9. int val_ph1 = 0; // Valeur PWM Phase 1
  10. int val_ph2 = 0; // Valeur PWM Phase 2
  11. int val_ph3 = 0; // Valeur PWM Phase 3
  12. int compteur_ph1 = 0; // Compteur Phase 1
  13. int compteur_ph2 = 0; // Compteur Phase 2
  14. int compteur_ph3 = 0; // Compteur Phase 3
  15. int switch_pola_ph1 = 0; // Switch polarité sinus Phase 1
  16. int switch_pola_ph2 = 0; // Switch polarité sinus Phase 2
  17. int switch_pola_ph3 = 0; // Switch polarité sinus Phase 3
  18. long delay_timer = 2000; // Délai PWM en microns / 30Hz = 30 / 50Hz = 5200 / 115Hz = ~2000
  19. long unsigned timer_1 = 0;
  20. long unsigned timer_2 = 0;
  21. long unsigned timer_3 = 0;
  22. float load_pwm = 1.0; // Facteur modulation valeur PWM
  23.  
  24. int timing_1[15] = {0, 20, 50, 75, 120, 160, 220, 255, 220, 160, 120, 75, 50, 20, 0};
  25. int lg_timing = 15;
  26.  
  27. void setup() {
  28.   Serial.begin(9600);
  29.  
  30.   // Forçage à 0 sorties PWM
  31.   char PinPWM[] = {'PinPosPh1', 'PinPosPh2', 'PinPosPh3', 'PinNegPh1', 'PinNegPh2', 'PinNegPh3'};
  32.   for (int i = 0; i < 6; i++) {
  33.     pinMode(PinPWM[i], OUTPUT);
  34.     analogWrite(PinPWM[i], 0);
  35.   }
  36.  
  37.   TCCR0B = TCCR0B & 0b11111000 | 0x02; //Timer 0 (PWM pins 5 & 6) =>
  38.   TCCR1B = TCCR1B & 0b11111000 | 0x01; //Timer 1 (PWM pins 9 & 10) =>
  39.   TCCR2B = TCCR2B & 0b11111000 | 0x01; //Timer 2 (PWM pins 3 & 11) =>
  40. }
  41.  
  42. void loop() {
  43.  
  44.  if (timer_1 == 0 && timer_2 == 0 && timer_3 == 0)
  45.  {
  46.   timer_1 = micros();
  47.   timer_2 = micros() + (delay_timer * 10);
  48.   timer_3 = micros() + (delay_timer * 20);
  49.  }
  50.  
  51.   // PHASE 1
  52.   if (micros() >= timer_1)
  53.   {
  54.     val_ph1 = timing_1[compteur_ph1] * load_pwm;
  55.  
  56.     if (switch_pola_ph1 == 0)
  57.     {
  58.       analogWrite(PinPosPh1, val_ph1);
  59.     }
  60.     else {
  61.       analogWrite(PinNegPh1, val_ph1);
  62.     }
  63.  
  64.     timer_1 = micros() + delay_timer;
  65.    
  66.     compteur_ph1++;
  67.   }
  68.  
  69.   // PHASE 2
  70.   if (micros() >= timer_2)
  71.   {
  72.     val_ph2 = timing_1[compteur_ph2] * load_pwm;
  73.  
  74.     if (switch_pola_ph2 == 0)
  75.     {
  76.       analogWrite(PinPosPh2, val_ph2);
  77.     }
  78.     else {
  79.       analogWrite(PinNegPh2, val_ph2);
  80.     }
  81.  
  82.     timer_2 = micros() + delay_timer;
  83.    
  84.     compteur_ph2++;
  85.   }
  86.  
  87.   // PHASE 3
  88.   if (micros() >= timer_3)
  89.   {
  90.     val_ph3 = timing_1[compteur_ph3] * load_pwm;
  91.    
  92.     if (switch_pola_ph3 == 0)
  93.     {
  94.       analogWrite(PinPosPh3, val_ph3);
  95.     }
  96.     else {
  97.       analogWrite(PinNegPh3, val_ph3);
  98.     }
  99.  
  100.     timer_3 = micros() + delay_timer;
  101.  
  102.     compteur_ph3++;
  103.   }
  104.  
  105. //    Serial.print(timer_1);
  106. //    Serial.print(',');
  107. //    Serial.print(timer_2);
  108. //    Serial.print(',');
  109. //    Serial.print(timer_3);
  110. //    Serial.println();
  111. //    Serial.print(compteur_ph1);
  112. //    Serial.print(',');
  113. //    Serial.print(compteur_ph2);
  114. //    Serial.print(',');
  115. //    Serial.print(compteur_ph3);
  116. //    Serial.println();
  117. //    Serial.print(val_ph1);
  118. //    Serial.print(',');
  119. //    Serial.print(val_ph2);
  120. //    Serial.print(',');
  121. //    Serial.print(val_ph3);
  122. //    Serial.println();
  123.  
  124.  
  125.   if (compteur_ph1 >= 15)
  126.   {
  127.     compteur_ph1 = 0;
  128.     switch_pola_ph1 = !switch_pola_ph1;
  129.   }
  130.  
  131.   if (compteur_ph2 >= 15)
  132.   {
  133.     compteur_ph2 = 0;
  134.     switch_pola_ph2 = !switch_pola_ph2;
  135.   }
  136.  
  137.   if (compteur_ph3 >= 15)
  138.   {
  139.     compteur_ph3 = 0;
  140.     switch_pola_ph3 = !switch_pola_ph3;
  141.   }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment