Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
fr:examples:digi:timer [2009/03/25 23:32] sdeniaudfr:examples:digi:timer [2020/07/20 12:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Timer ======
 +Les //timers// sont très souvent utilisés lorsqu'on programme des contrôleurs. Ils peuvent être utilisés de manières variées, telles que compter le temps à l'intérieur d'un code ou fournir une interruption quand le //timer// a atteint une certaine valeur haute ou basse. Par exemple, la durée entre 2 impulsions externes peut être détectée, ou encore les //timers// peuvent être utilisés comme compteurs de pulsations provenant d'un capteur. Une des manières classiques d'utiliser un //timer// est l'horloge temps réel. La tâche du //timer// est alors de fournir une interruption avec un intervalle spécifique. A chaque fois qu'une interruption se produit, //as part of the interrupt routine// vous pourrez, par exemple, échantillonner les capteurs. De cette manière, vous serez toujours sûr d'avoir la même durée entre 2 échantillons du même capteur.
  
 +===== Exemple de LED clignotante =====
 +
 +
 +<code c>
 +/* 
 +Labor 2 example
 +
 +Blincking LED using timer
 +Raivo Sell 2008
 +
 +LED = 0 (ON)  LED = 1 (OFF)
 +PORT direction: 1-output 0-input
 + */
 +
 +#include <inttypes.h>
 +#include <avr/io.h>
 +#include <avr/interrupt.h>
 +
 +static int i; //globale variable
 +int SAGEDUS = 10;  // blincking frequency
 +
 +#define INV(x) ^=(1<<x) //inversting bit in PORT x
 +#define LED 4 // Blincking LED (in this example LED2 (yellow)
 +
 +// Interrupt function - starts on timer buffer overrun
 +
 +ISR (TIMER1_OVF_vect){
 + i++; // increments every time when function is executed
 + if (i>SAGEDUS){ // LED port is inverted when i reaches constant SAGEDUS
 +     PORTC INV(LED); // Inverting LED
 + i=0; // repeater is set to zero
 + }
 +}
 +
 +int main (void) {
 + // Timer control regiters
 + TCCR1A = 0x00;
 + TCCR1B = _BV(CS10); //clk/1 (No prescaling)
 +  
 +    DDRC  = 0x38;  // DDRC  0bXX111000
 +    PORTC = 0x3F;  // PORTC 0bXX111111
 +
 +    TIMSK = _BV (TOIE1); // Overrun interrupt is enabled
 +    sei (); // Global interrupts are enabled
 +
 +    while(1); // endless loop
 +// No action here, LED is inverted through the interrupt function
 +}
 +</code>
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0