Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
231 killagreg 1
#include <inttypes.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
 
5
#include "main.h"
6
 
7
volatile uint16_t CountMilliseconds = 0;
8
 
9
 
10
 
11
/*****************************************************/
12
/*              Initialize Timer 0                   */
13
/*****************************************************/
14
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
15
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
16
void TIMER0_Init(void)
17
{
18
        uint8_t sreg = SREG;
19
 
20
        // disable all interrupts before reconfiguration
21
        cli();
22
 
23
        // Timer/Counter 0 Control Register A
24
 
25
        // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
26
    // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
27
    // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
28
    TCCR0A &= ~((1<<COM0A0)|(1<<COM0B0));
29
    TCCR0A |= (1<<COM0A1)|(1<<COM0B1)|(1<<WGM01)|(1<<WGM00);
30
 
31
        // Timer/Counter 0 Control Register B
32
 
33
        // set clock devider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
34
        // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
35
        // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
36
 
37
        // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
38
        TCCR0B &= ~((1<<FOC0A)|(1<<FOC0B)|(1<<WGM02));
39
    TCCR0B = (TCCR0B & 0xF8)|(0<<CS02)|(1<<CS01)|(0<<CS00);
40
 
41
        // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
42
    OCR0A =  0;  // for PB3
43
    OCR0B = 120; // for PB4
44
 
45
        // init Timer/Counter 0 Register
46
    TCNT0 = 0;
47
 
48
        // Timer/Counter 0 Interrupt Mask Register
49
        // enable timer overflow interrupt only
50
        TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
51
        TIMSK0 |= (1<<TOIE0);
52
 
53
        SREG = sreg;
54
}
55
 
56
 
57
 
58
/*****************************************************/
59
/*          Interrupt Routine of Timer 0             */
60
/*****************************************************/
61
ISR(TIMER0_OVF_vect)    // 9.765 kHz
62
{
63
    static uint8_t cnt = 0;
64
 
65
        if(!cnt--) // every 10th run (9.765kHz/10 = 976Hz)
66
        {
67
                cnt = 9;
68
                CountMilliseconds++; // increment millisecond counter
69
        }
70
}
71
 
72
 
73
// -----------------------------------------------------------------------
74
uint16_t SetDelay (uint16_t t)
75
{
76
  return(CountMilliseconds + t - 1);
77
}
78
 
79
// -----------------------------------------------------------------------
80
int8_t CheckDelay(uint16_t t)
81
{
82
  return(((t - CountMilliseconds) & 0x8000) >> 8); // check sign bit
83
}
84
 
85
// -----------------------------------------------------------------------
86
void Delay_ms(uint16_t w)
87
{
88
 unsigned int t_stop;
89
 t_stop = SetDelay(w);
90
 while (!CheckDelay(t_stop));
91
}
92