Wiki

Reso-nance numérique | Arts et cultures libres

Outils du site


logiciels:arduino-timer:accueil

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
logiciels:arduino-timer:accueil [2014/05/23 15:59]
resonance [Prescaler]
logiciels:arduino-timer:accueil [2018/01/22 11:14] (Version actuelle)
resonance ancienne révision (2016/08/13 22:08) restaurée
Ligne 14: Ligne 14:
   * **Timer 0** : pins 5 et 6  : 976.5625 Hz (8 bits > 256 values)   * **Timer 0** : pins 5 et 6  : 976.5625 Hz (8 bits > 256 values)
   * **Timer 1** : pins 9 et 10 : 490.20 Hz (16 bits > 65536 values)   * **Timer 1** : pins 9 et 10 : 490.20 Hz (16 bits > 65536 values)
-  * **Timer 2** : pins 11 et 3 : 490.20 Hz (8 bits)+  * **Timer 2** : pins 3 et 11 : 490.20 Hz (8 bits) 
 + 
 +{{http://www.gammon.com.au/images/Arduino/Timer_and_Counter_pins.jpg?600}}
  
 **Arduino MEGA** :  **Arduino MEGA** : 
-  * Les mêmes timer 01 et 3 +  * **Timer 0** : pins 4 et 13  
-  * timer 3 : pins 5, 3, 2 (16 bits) +  * **Timer 1** : pins 11 et 12 
-  * timer 4 : pins 8, 7, 6 (16 bits) +  * **Timer 2** : pins 9 et 10 
-  * timer 5 : pins 46, 45 et 44 (16 bits)+  * **Timer 3** : pins 2, 3 et 5 (16 bits) 
 +  * **Timer 4** : pins 6, 7 et 8 (16 bits) 
 +  * **Timer 5** : pins 46, 45 et 44 (16 bits) 
 + 
 +Sur une Mega, chaque Tone simultané utilisera les timers dans cet ordre : 2, 3, 4, 5, 1, 0 ([[https://books.google.fr/books?id=raHyKejOBF4C&pg=PA307#v=onepage&q&f=false|source]]) 
 + 
 +**Arduino Leonardo** (no Timer 2!) : 
 +http://provideyourown.com/2012/arduino-leonardo-versus-uno-whats-new/ 
 +The Leonardo has 7 PWM pins instead of 6. ATmega32U4 also has a new timer, timer4 which has 10 bits and uses a PLL to count at 64MHz. Their mappings are different as well: 
 +  * **Timer 0** : pins 3 et 11  : 8 bits 
 +  * **Timer 1** : pins 5, 9, 10 : 16 bits  
 +  * **Timer 2** : no 
 +  * **Timer 3** : pins 5, 9, 10 : 16 bits 
 +  * **Timer 4** : pins 6, 13 (10 bits) 
 + 
 + 
 +**Tone et Leonardo** problem :https://code.google.com/p/rogue-code/issues/detail?id=13 
 + 
 + 
 +===== PWM Frequency ===== 
 +<code> 
 +TCCR0B = TCCR0B & 0b11111000 | <setting>; //Timer 0 (PWM pins 5 & 6) 
 +TCCR1B = TCCR1B & 0b11111000 | <setting>; //Timer 1 (PWM pins 9 & 10) 
 +TCCR2B = TCCR2B & 0b11111000 | <setting>; //Timer 2 (PWM pins 3 & 11) 
 +</code> 
 + 
 +| setting | 5 & 6 | 3,11 & 9,10 | 
 +| 0x01 |62500 | 31250 | 
 +| 0x02 |7812 | 3906 | 
 +| 0x03 | 976 | 488 | 
 +| 0x04 | 244 | 122 |  
 +| 0x05 | 61 | 30 |
  
 ===== Tone ===== ===== Tone =====
 When you use analogOut() to create pulsewidth modulation (PWM) on an output pin, you can change the on-off ratio of the output (also known as the duty cycle) but not the frequency. If you have a speaker connected to an output pin running analogOut(), you'll get a changing loudness, but a constant tone. To change the tone, you need to change the frequency. The tone() command does this for you.  When you use analogOut() to create pulsewidth modulation (PWM) on an output pin, you can change the on-off ratio of the output (also known as the duty cycle) but not the frequency. If you have a speaker connected to an output pin running analogOut(), you'll get a changing loudness, but a constant tone. To change the tone, you need to change the frequency. The tone() command does this for you. 
 +
 +tone() function uses at least timer2. You can’t use PWM on Pin 3,11 when you use the tone() function an Arduino and Pin 9,10 on Arduino Mega.
  
  
Ligne 42: Ligne 77:
 C'est pourquoi on ne peut dépasser 255 ! C'est pourquoi on ne peut dépasser 255 !
  
-{{http://www.societyofrobots.com/member_tutorials/files/sawtooth2.png}}+{{http://arcfn.com/images/pwm2.gif}}
  
 ===== Modes ===== ===== Modes =====
Ligne 50: Ligne 85:
   * Fast PWM (ATMega328P datasheet p152-153)   * Fast PWM (ATMega328P datasheet p152-153)
   * CTC (Clear Timer on Compare Match)   * CTC (Clear Timer on Compare Match)
 +
 +==== Fast PWM ====
 +This is useful in many power regulating circuits, or in circuits where small inductive or capacitive components are used. Atmega328 microcontroller is able to generate two types of fast PWM. In one mode Counter counts from 0 to 255 and in second mode counter counts from 0 to value stored in OCR0A.
  
 ===== Registres ===== ===== Registres =====
Ligne 58: Ligne 96:
   * TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.   * TIMSKx - Timer/Counter Interrupt Mask Register. To enable/disable timer interrupts.
   * TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt   * TIFRx - Timer/Counter Interrupt Flag Register. Indicates a pending timer interrupt
 +
 +
 +The Timer/Counter Control Registers hold several bit groups. These are:
 +  * WGM - Waveform Generation Mode bits
 +  * CS - Clock Select bits
 +  * COMnA & COMnB - Compare Match Output bits 
 +
 +{{:logiciels:arduino-timer:timer-ocoa.png|}}
  
 ===== Prescaler ===== ===== Prescaler =====
Ligne 70: Ligne 116:
 </code> </code>
  
 +{{:logiciels:arduino-timer:period-formula.png|}}
  
-Target Timer Count = (Input Frequency / (Prescaler * Target Frequency)) - 1+Période minimale : / 16 000 000 = 0,000000062 = 6,2 ns
  
 +{{:logiciels:arduino-timer:timer-formula.png|}}
 +
 +Target Timer Count = (Input Frequency / (Prescaler * Target Frequency)) - 1
  
 Target Timer Count = (16000000 / 1) -1 Target Timer Count = (16000000 / 1) -1
  
 +Those familiar with C will know that an unsigned eight bit value can store a value from 0 to 28 − 1,
 +or 255, before running out of bits to use and becoming zero again. Similarly, an unsigned 16 bit
 +value may store a value from 0 to 216 − 1, or 65535 before doing the same.
 +
 +==== Exemple : 20Khz ====
 +<code cpp>
 +// Réglage de la fréquence PWM du Timer 1
 +// vu sur : https://github.com/pololu/zumo-shield/blob/master/ZumoMotors/ZumoMotors.cpp
 +
 +int mypwm = 200; // réglage de test avec max = 400 ici
 +
 +void setup() {
 +  // PWM frequency calculation
 +  // 16MHz / 1 (prescaler) / 2 (phase-correct) / 400 (top) = 20kHz
 +  TCCR1A = 0b10100000;
 +  TCCR1B = 0b00010001;
 +  ICR1 = 400; // max
 +}
 +
 +void loop() {
 +  OCR1B = mypwm; // 0-400
 +}
 +</code>
  
 ===== Interruptions ===== ===== Interruptions =====
Ligne 108: Ligne 181:
   * http://hekilledmywire.wordpress.com/2011/05/28/introduction-to-timers-tutorial-part-6/   * http://hekilledmywire.wordpress.com/2011/05/28/introduction-to-timers-tutorial-part-6/
   * http://wiki.openmusiclabs.com/wiki/PWMDAC   * http://wiki.openmusiclabs.com/wiki/PWMDAC
 +  * http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=37871
 +  * http://www.embedds.com/8-bit-timercounter0-operation-modes-part2/
 +  * http://forum.arduino.cc/index.php/topic,191624.0.html
  
/home/resonancg/www/wiki/data/attic/logiciels/arduino-timer/accueil.1400853566.txt.gz · Dernière modification: 2014/05/23 15:59 de resonance