0,0 → 1,98 |
/*********************************************************************************************************************************** |
* File: timer.c |
* |
* Purpose: sets up a flag that is coming up every 2 ms |
* |
* Functions: void init_Timer0(void) |
* ISR(TIMER0_COMP_vect) |
* unsigned char Flag2ms() |
* |
* hardware: AVR-Ctrl experimental board without LCD display as I2C bus is used |
* |
* Created: Jan 2013 |
* |
* Version: 1.00 experimental version |
* |
* Copyright: (c)2013 dk9nw -at- darc.de |
* All rights reserved. This software is available only for non-commercial amateur radio or educational applications. |
* Other uses are prohibited. This software may be modified only if |
* the resulting code be made available publicly and the original author(s) given credit. |
* |
***********************************************************************************************************************************/ |
#include "timer.h" |
|
#include <stdlib.h> |
#include <stdio.h> |
#include <avr/io.h> |
#include <avr/pgmspace.h> |
#include <avr/interrupt.h> |
|
|
volatile unsigned char c2ms = 0; // flag that switches on every 2 milliseconds |
|
|
|
// ************************************************************************************************************* |
// Purpose: Function to init Timer0 |
// Input: none |
// Returns: none |
// -------------------------------------- |
void init_Timer0(void) |
{ |
// TCCR0 = Timer/Counter Control Register -> FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 |
// ------------------------------------------------------------------------------------- |
// WGM = Waveform Generation Mode = 10 = Clear Timer on Compare Match (CTC) mode |
TCCR0 |= (1<<WGM01); // CTC Compare Mode |
TCCR0 &= ~(1<<WGM00); // CTC Compare Mode |
|
// TCCR0 = Timer/Counter Control Register -> FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00 |
// ------------------------------------------------------------------------------------- |
// CS02 CS01 CS00 The three Clock Select bits select the clock source to be used by the Timer/Counter |
TCCR0 |= (1<<CS02); // prescale = 100 = clkI/O/256 (From prescaler) |
TCCR0 &= ~((1<<CS01)|(1<<CS00)); // prescale = 100 = clkI/O/256 (From prescaler) |
|
OCR0 = 126; // 1/(Fcpu/256) * 126 = 2.016 ms |
|
// TIMSK = Timer/Counter Interrupt Mask Register -> OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0 |
// OCIE0 = Timer/Counter0 Output Compare Match Interrupt Enable |
// |
TIMSK |= (1<<OCIE0); // Interrupt Enabled |
|
c2ms = 0; |
} |
// *** EOF: void init_Timer0(void) **************************************************************************** |
|
|
|
|
// *************************************************** |
// Purpose: ISR is called at TIMER0 Compare match |
// |
ISR(TIMER0_COMP_vect) |
{ |
c2ms = 1; |
} |
// *** EOF: ISR ************************************** |
|
|
|
|
// ********************************************************************************************** |
// Function returns 0 as long as the 2 msec Interrupt Flag of Timer0 is not set and resets it |
// Input: none |
// Returns: ck |
// |
unsigned char Flag2ms(void) |
{ |
static unsigned char ck = 0; |
|
ck = c2ms; |
c2ms = 0; |
return ck; |
} |
// *** EOF: Flag2ms() **************************************************************************** |
|
|
|
|
// ********************************** EOF: timer.c ************************************************************************** |