timer.c


1
/* Includes *******************************************************************/
2
#include <avr/io.h>
3
#include <inttypes.h>
4
#include <avr/interrupt.h>
5
#include "timer.h"
6
#include "adc.h"
7
#include "motor.h"
8
#include "defs.h"
9
10
/* Constants ******************************************************************/
11
12
/* Macros *********************************************************************/
13
14
/* Type definitions ***********************************************************/
15
16
/* Prototypes *****************************************************************/
17
18
/* Variables ******************************************************************/
19
volatile unsigned char count;
20
21
/* Functions ******************************************************************/
22
//------------------------------------------------------------------------------
23
//--- Interrupt routine
24
//------------------------------------------------------------------------------
25
ISR (TIMER1_OVF_vect)
26
{
27
    count++;              //Counter for Overflow
28
    if ( count == 20)        
29
    {
30
      ADC_Enable (ENABLE);      //enable ADC
31
      count = 0;            //reset counter
32
      TIMER_Start (STOP);
33
    }
34
}
35
36
//------------------------------------------------------------------------------
37
//--- Initialize Timer 1
38
//------------------------------------------------------------------------------
39
void TIMER_Init (void)
40
  {       
41
  TIMSK1 |= (1 << TOIE1);            
42
  }
43
44
//------------------------------------------------------------------------------
45
//--- Start / Stopp Timer 1
46
//------------------------------------------------------------------------------
47
void TIMER_Start (uint8_t start)
48
  {
49
  if (start) TCCR1B |= (1<<CS10) | ( 1 << CS12);   
50
  else TCCR1B &= ~((1<<CS10) | ( 1 << CS12)); 
51
    }