document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  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 compteur_ph1 = 0; // Compteur Phase 1
  10. int compteur_ph2 = 4; // Compteur Phase 2
  11. int compteur_ph3 = 7; // Compteur Phase 3
  12. int switch_pola_ph1 = 0; // Switch polarité sinus Phase 1
  13. int switch_pola_ph2 = 1; // Switch polarité sinus Phase 2
  14. int switch_pola_ph3 = 0; // Switch polarité sinus Phase 3
  15. long delay_timer = 50000; // Délai PWM en microns / 30Hz = 30 / 50Hz = 18 / 115Hz = 7
  16. long unsigned timer_1 = 0;
  17. float load_pwm = 1.0; // Facteur modulation valeur PWM
  18.  
  19. 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};
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.  
  24.   // Forçage à 0 sorties PWM
  25.   char PinPWM[] = {\'PinPosPh1\', \'PinPosPh2\', \'PinPosPh3\', \'PinNegPh1\', \'PinNegPh2\', \'PinNegPh3\'};
  26.   for (int i = 0; i < 6; i++) {
  27.     pinMode(PinPWM[i], OUTPUT);
  28.     analogWrite(PinPWM[i], 0);
  29.   }
  30.  
  31.   //TCCR0B = TCCR0B & 0b11111000 | 0x01; //Timer 0 (PWM pins 5 & 6)
  32.   TCCR1B = TCCR1B & 0b11111000 | 0x03; //Timer 1 (PWM pins 9 & 10)
  33.   TCCR2B = TCCR2B & 0b11111000 | 0x03; //Timer 2 (PWM pins 3 & 11)
  34. }
  35.  
  36. void loop() {
  37.  
  38.   if (micros() >= timer_1)
  39.   {
  40.     int val_ph1 = timing_1[compteur_ph1] * load_pwm;
  41.     int val_ph2 = timing_1[compteur_ph2] * load_pwm;
  42.     int val_ph3 = timing_1[compteur_ph3] * load_pwm;
  43.  
  44.     Serial.print(val_ph1);
  45.     Serial.print(\',\');
  46.     Serial.print(val_ph2);
  47.     Serial.print(\',\');
  48.     Serial.print(val_ph3);
  49.     Serial.println();
  50.    
  51.     // Phase 1
  52.     if (switch_pola_ph1 == 0)
  53.     {
  54.       analogWrite(PinPosPh1, val_ph1);
  55.     }
  56.     else {
  57.       analogWrite(PinNegPh1, val_ph1);
  58.     }
  59.  
  60.     // Phase 2
  61.     if (switch_pola_ph2 == 0)
  62.     {
  63.       analogWrite(PinPosPh2, val_ph1);
  64.     }
  65.     else {
  66.       analogWrite(PinNegPh2, val_ph1);
  67.     }
  68.  
  69.     // Phase 3
  70.     if (switch_pola_ph3 == 0)
  71.     {
  72.       analogWrite(PinPosPh3, val_ph1);
  73.     }
  74.     else {
  75.       analogWrite(PinNegPh3, val_ph1);
  76.     }
  77.        
  78.     timer_1 = micros() + delay_timer;
  79.    
  80.     compteur_ph1++;
  81.     compteur_ph2++;
  82.     compteur_ph3++;
  83.   }
  84.  
  85.   if (compteur_ph1 == 13)
  86.   {
  87.     compteur_ph1 = 0;
  88.     switch_pola_ph1 = !switch_pola_ph1;
  89.   }
  90.  
  91.   if (compteur_ph2 == 17)
  92.   {
  93.     compteur_ph2 = 4;
  94.     switch_pola_ph2 = !switch_pola_ph2;
  95.   }
  96.  
  97.   if (compteur_ph3 == 21)
  98.   {
  99.     compteur_ph3 = 8;
  100.     switch_pola_ph3 = !switch_pola_ph3;
  101.   }
  102. }
');