This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| de:examples:digi:timer [2009/04/27 16:01] – nierhoff | de:examples:digi:timer [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Timer ====== | ||
| + | Bei der Programmierung von Mikrocontrollern werden häufig Timer eingesetzt, z.B. um innerhalb des Codes Zeit zu messen oder um einen Interrupt zu erzeugen, wenn bis zu einem bestimmten Wert gezählt wurde. Mögliche Anwendungsfälle sind das Messen der Zeit zwischen zwei erfassten Impulsen oder das fungieren als Zähler von vom Sensor wahrgenommenen Ereignissen. Auch der Einsatz eines Timers als Absolutzeitgeber ist gebräuchlich, | ||
| + | ===== Blinking LED example ===== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Labor 2 example | ||
| + | |||
| + | Blinking LED using timer | ||
| + | Raivo Sell 2008 | ||
| + | |||
| + | LED = 0 (ON) LED = 1 (OFF) | ||
| + | PORT direction: 1-output 0-input | ||
| + | */ | ||
| + | |||
| + | #define F_CPU 14745600UL | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | static int i; //globale variable | ||
| + | int SAGEDUS = 10; // blinking frequency | ||
| + | |||
| + | #define INV(x) ^=(1<< | ||
| + | #define LED 4 // Blinking 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> | ||
| + | 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 | ||
| + | } | ||
| + | </ | ||