Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2203 - 1
/***********************************************************************************************************************************
2
* File:                 timer.c
3
*
4
* Purpose:              sets up a flag that is coming up every 2 ms
5
*
6
* Functions:    void init_Timer0(void)
7
*                               ISR(TIMER0_COMP_vect)
8
*                               unsigned char Flag2ms()
9
*
10
* hardware:             AVR-Ctrl experimental board without LCD display as I2C bus is used
11
*
12
* Created:              Jan 2013
13
*
14
* Version:              1.00    experimental version
15
*
16
* Copyright:    (c)2013 dk9nw -at- darc.de
17
*                               All rights reserved. This software is available only for non-commercial amateur radio or educational applications.  
18
*                               Other uses are prohibited. This software may be modified only if
19
*                               the resulting code be made available publicly and the original author(s) given credit.
20
*
21
***********************************************************************************************************************************/
22
#include "timer.h"
23
 
24
#include <stdlib.h>
25
#include <stdio.h>
26
#include <avr/io.h>
27
#include <avr/pgmspace.h>
28
#include <avr/interrupt.h> 
29
 
30
 
31
volatile unsigned char c2ms = 0;                // flag that switches on every 2 milliseconds
32
 
33
 
34
 
35
// *************************************************************************************************************
36
// Purpose:             Function to init Timer0
37
// Input:               none
38
// Returns:     none
39
// --------------------------------------
40
void init_Timer0(void)
41
{
42
        // TCCR0 = Timer/Counter Control Register -> FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
43
        // -------------------------------------------------------------------------------------
44
        // WGM = Waveform Generation Mode = 10 = Clear Timer on Compare Match (CTC) mode
45
        TCCR0 |= (1<<WGM01);    // CTC Compare Mode
46
        TCCR0 &= ~(1<<WGM00);   // CTC Compare Mode
47
 
48
        // TCCR0 = Timer/Counter Control Register -> FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
49
        // -------------------------------------------------------------------------------------
50
        // CS02 CS01 CS00 The three Clock Select bits select the clock source to be used by the Timer/Counter
51
        TCCR0 |= (1<<CS02);                             // prescale = 100 = clkI/O/256 (From prescaler)
52
        TCCR0 &= ~((1<<CS01)|(1<<CS00));        // prescale = 100 = clkI/O/256 (From prescaler)
53
 
54
        OCR0 = 126;                                                     // 1/(Fcpu/256) * 126 = 2.016 ms
55
 
56
        // TIMSK = Timer/Counter Interrupt Mask Register -> OCIE2 TOIE2 TICIE1 OCIE1A OCIE1B TOIE1 OCIE0 TOIE0
57
        // OCIE0 = Timer/Counter0 Output Compare Match Interrupt Enable
58
        //
59
        TIMSK |= (1<<OCIE0);                            // Interrupt Enabled
60
 
61
        c2ms = 0;
62
}
63
// *** EOF: void init_Timer0(void) ****************************************************************************
64
 
65
 
66
 
67
 
68
// ***************************************************
69
// Purpose:             ISR is called at TIMER0 Compare match
70
//
71
ISR(TIMER0_COMP_vect)
72
{
73
        c2ms = 1;              
74
}
75
// *** EOF: ISR **************************************
76
 
77
 
78
 
79
 
80
// **********************************************************************************************
81
// Function returns 0 as long as the 2 msec Interrupt Flag of Timer0 is not set and resets it
82
// Input:               none
83
// Returns:     ck
84
//
85
unsigned char Flag2ms(void)
86
{
87
        static unsigned char ck = 0;
88
 
89
        ck = c2ms;
90
        c2ms = 0;  
91
        return ck;
92
}
93
// *** EOF: Flag2ms() ****************************************************************************
94
 
95
 
96
 
97
 
98
// ********************************** EOF: timer.c **************************************************************************