Aller au contenu


Photo
- - - - -

Modification de la fréquence des PWM sur Arduino Due


  • Veuillez vous connecter pour répondre
3 réponses à ce sujet

#1 Colin

Colin

    Membre

  • Membres
  • 25 messages
  • Gender:Male

Posté 23 janvier 2020 - 07:26

Bonjour,

 

Afin de ne pas subir le bruit strident de nos moteurs CC sur une plage de PWM, nous souhaitons changer la fréquence de ce dernier pour qu'il ne soit plus dans l'audible.

 

Pour l'instant nous avons trouvé cela : https://arduino.stac...for-arduino-due

 

https://folk.uio.no/...utOfPWMDue.html

 

https://forum.arduin...?topic=487649.0

 

Les pins reliés aux timers : https://github.com/i...Timer/issues/11



#2 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 934 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 23 janvier 2020 - 07:37

Lien de référence que j'ai trouvé : 

https://forum.arduin...?topic=420674.0

 

avec en particulier ce code d'exemple à tester : 

 


// --------------------------------------------
//
// PWM with timers demo
//
// Bob Cousins, August 2014
// --------------------------------------------

typedef struct {
    Tc *pTC;        // TC0, TC1, or TC2
    byte channel;   // 0-2
    byte output;    // 0 = A, 1 = B
}  tTimerInfo;

tTimerInfo timerLookup [] =
{
  {NULL,0,0}, // 0
  {NULL,0,0},
  {TC0,0,0},  // pin 2 = TIOA0
  {TC2,1,0},  // pin 3 = TIOA7
  {TC2,0,1},  // pin 4 = TIOB6
  {TC2,0,0},  // pin 5 = TIOA6
  {NULL,0,0},
  {NULL,0,0},
  {NULL,0,0},
  {NULL,0,0},
  {TC2,1,1},  // pin 10 = TIOB7
  {TC2,2,0},  // pin 11 = TIOA8
  {TC2,2,1},  // pin 12 = TIOB8
  {TC0,0,1}   // pin 13 = TIOB0
};

/**
 * \brief set a pin for PWM using a timer channel
 * \param pin       pin to use (0-13 only!)
 * \param frequency the frequency
 * \param dutyCyle  duty cycle 0-255
 * \return          this function returns a count, which is the effective PWM resolution. Returns 0 if pin is not valid
 */

uint32_t setupTimerPwm (byte pin, uint32_t frequency, unsigned dutyCycle)
{
  uint32_t count = VARIANT_MCK/2/frequency;
  tTimerInfo *pTimer = &timerLookup[pin];
 
  if (pTimer != NULL)
  {
    TC_SetRC (pTimer->pTC, pTimer->channel, count);
    if (pTimer->output == 0)
       TC_SetRA (pTimer->pTC, pTimer->channel, count * dutyCycle / 256);
    else
       TC_SetRB (pTimer->pTC, pTimer->channel, count * dutyCycle / 256);
 
    return count;
  }
  else
    return 0;
}

void setup() {
  // put your setup code here, to run once:

  // use the Arduino lib to do initial setup
  analogWrite (2, 128);
  analogWrite (13, 128);
 
  analogWrite (5, 128);
  analogWrite (4, 128);

  analogWrite (3, 128);
  analogWrite (10, 128);

  analogWrite (11, 128);
  analogWrite (12, 128);
 
  // pins 2 and 3 share the same timer so must have same frequency
  setupTimerPwm (2, 2000, 128);
  setupTimerPwm (13, 2000, 64);
 
  // pins 5 and 4 share the same timer
  setupTimerPwm (5, 3000, 128);
  setupTimerPwm (4, 3000, 64);

  // pins 3 and 10 share the same timer
  setupTimerPwm (3, 4000, 128);
  setupTimerPwm (10, 4000, 64);

  // pins 11 and 12 share the same timer
  setupTimerPwm (11, 5000, 128);
  setupTimerPwm (12, 5000, 64);

}

void loop() {
  // put your main code here, to run repeatedly:

}

On notera en particulier que les  pins partagent les même timer par paires.

(2, 13) => Timer 0
(5, 4);  => Timer 6
(3, 10) => Timer 7
(11,12, ) => Timer 8

 ... et donc auront nécessairement la même fréquence ( une seule même fréquence pour chaque paire )


Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 
Si vous souhaitez un robot pilotable par internet n'hésitez pas à visiter www.vigibot.com et à lire le sous forum dédié à vigibot!

 

Les réalisations de Mike118  

 

 

 


#3 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 934 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 30 janvier 2020 - 06:31

Sinon peut être une meilleur alternative pour les 8 channel PWM hardware qui ne sont pas sur les timer : https://github.com/antodom/pwm_lib

 

 

Sinon pour les ceux sur les timers : 
 

// Output 50% duty cycle PWM at 10kHz on digital pins D4 and D5 using TC6
void setup()
{
  REG_PMC_PCER1 |= PMC_PCER1_PID33;                 // Enable peripheral TC6 (TC2 Channel 0)
  REG_PIOC_ABSR |= PIO_ABSR_P26 | PIO_ABSR_P25;     // Switch the multiplexer to peripheral B for TIOA6 and TIOB6
  REG_PIOC_PDR |= PIO_PDR_P26 | PIO_PDR_P25;        // Disable the GPIO on the corresponding pins

  REG_TC2_CMR0 = TC_CMR_BCPC_SET |                  // Set TIOB on counter match with RC0
                 TC_CMR_ACPC_SET |                  // Set TIOA on counter match with RC0
                 TC_CMR_BCPB_CLEAR |                // Clear TIOB on counter match with RB0
                 TC_CMR_ACPA_CLEAR |                // Clear TIOA on counter match with RA0
                 TC_CMR_WAVE |                      // Enable wave mode
                 TC_CMR_WAVSEL_UP_RC |              // Count up with automatic trigger on RC compare
                 TC_CMR_EEVT_XC0 |                  // Set event selection to XC0 to make TIOB an output
                 TC_CMR_TCCLKS_TIMER_CLOCK1;        // Set the timer clock to TCLK1 (MCK/2 = 84MHz/2 = 48MHz)

  REG_TC2_RA0 = 2100;                               // Load the RA0 register
  REG_TC2_RB0 = 2100;                               // Load the RB0 register
  REG_TC2_RC0 = 4200;                               // Load the RC0 register
 
  REG_TC2_CCR0 = TC_CCR_SWTRG | TC_CCR_CLKEN;       // Enable the timer TC6
}

void loop() {}

Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 
Si vous souhaitez un robot pilotable par internet n'hésitez pas à visiter www.vigibot.com et à lire le sous forum dédié à vigibot!

 

Les réalisations de Mike118  

 

 

 


#4 Mike118

Mike118

    Staff Robot Maker

  • Administrateur
  • PipPipPipPipPip
  • 9 934 messages
  • Gender:Male
  • Location:Anglet
  • Interests:Robotique, Entrepreneuriat, Innovation, Programmation, Résolution de problème, Recherche de solutions, Mécanique, Electronique, Créer, Concevoir

Posté 30 janvier 2020 - 08:59

// Output 50% duty cycle PWM at 10kHz on digital pins D9 and D10 using TC8
void setup()
{
  REG_PMC_PCER1 |= PMC_PCER1_PID35;                 // Enable peripheral TC8 (TC2 Channel 2)
  REG_PIOC_ABSR |= PIO_ABSR_P30 | PIO_ABSR_P29;     // Switch the multiplexer to peripheral B for TIOA8 and TIOB8
  REG_PIOC_PDR |= PIO_PDR_P30 | PIO_PDR_P29;        // Disable the GPIO on the corresponding pins

  REG_TC2_CMR2 = TC_CMR_BCPC_SET |                  // Set TIOB on counter match with RC2
                 TC_CMR_ACPC_SET |                  // Set TIOA on counter match with RC2
                 TC_CMR_BCPB_CLEAR |                // Clear TIOB on counter match with RB2
                 TC_CMR_ACPA_CLEAR |                // Clear TIOA on counter match with RA2
                 TC_CMR_WAVE |                      // Enable wave mode
                 TC_CMR_WAVSEL_UP_RC |              // Count up with automatic trigger on RC compare
                 TC_CMR_EEVT_XC0 |                  // Set event selection to XC0 to make TIOB an output
                 TC_CMR_TCCLKS_TIMER_CLOCK1;        // Set the timer clock to TCLK1 (MCK/2 = 84MHz/2 = 48MHz)

  REG_TC2_RA2 = 2100;                               // Load the RA2 register
  REG_TC2_RB2 = 2100;                               // Load the RB2 register
  REG_TC2_RC2 = 4200;                               // Load the RC2 register
 
  REG_TC2_CCR2 = TC_CCR_SWTRG | TC_CCR_CLKEN;       // Enable the timer TC8
}

void loop() {}

Pour les pins sur le Timer 8 ...


Si mon commentaire vous a plus laissez nous un avis  !  :thank_you:

Nouveau sur Robot Maker ? 

Jetez un oeil aux blogs, aux tutoriels, aux ouvrages, au robotscope  aux articles,  à la boutique  et aux différents services disponible !
En attendant qu'une bibliothèque de fichiers 3D soit mise en place n'hésitez pas à demander si vous avez besoin du fichier 3D d'un des produits de la boutique... On l'a peut être ! 
Si vous souhaitez un robot pilotable par internet n'hésitez pas à visiter www.vigibot.com et à lire le sous forum dédié à vigibot!

 

Les réalisations de Mike118  

 

 

 





0 utilisateur(s) li(sen)t ce sujet

0 members, 0 guests, 0 anonymous users