| 1 |
/* timers.c -- routines for handling the timer |
| 2 |
|
| 3 |
Copyright (C) 2008 Ajith Kumar, Inter-University Accelerator Centre, |
| 4 |
New Delhi. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 3, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
This program is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with this program; if not, write to the Free Software Foundation, |
| 18 |
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
| 19 |
|
| 20 |
|
| 21 |
#include <avr/io.h> |
| 22 |
|
| 23 |
|
| 24 |
#ifndef BV |
| 25 |
#define BV(bit) (1 << (bit)) |
| 26 |
#endif |
| 27 |
|
| 28 |
#define CPU_CLOCK 8000000 // 8 MHz clock is assumed |
| 29 |
#define FLIMIT (CPU_CLOCK/2) |
| 30 |
|
| 31 |
static uint16_t f[] = {1,8,32,64,128,256,1024}; |
| 32 |
|
| 33 |
uint32_t set_frequency(uint32_t freq) |
| 34 |
{ |
| 35 |
uint32_t tmp; |
| 36 |
uint8_t ocr, k; |
| 37 |
|
| 38 |
DDRD |= 0x80; // Make PD7 as output |
| 39 |
if(freq == 0) { |
| 40 |
TCCR2 = BV(WGM21) | BV(COM20); // CTC mode, clock disabled |
| 41 |
return 0; |
| 42 |
} else if(freq <= 15) { |
| 43 |
TCCR2 = BV(WGM21) | BV(COM20) | 7; // CTC mode, ck/1024 |
| 44 |
OCR2 = 255; |
| 45 |
return 15; |
| 46 |
} else if(freq >= FLIMIT) { |
| 47 |
TCCR2 = BV(WGM21) | BV(COM20) | 1; // CTC mode, ck/1 |
| 48 |
OCR2 = 0; |
| 49 |
return FLIMIT; |
| 50 |
} else { |
| 51 |
k = 0; |
| 52 |
while(k < 7) { |
| 53 |
tmp = FLIMIT / f[k]; // maximum value for the chosen prescaler |
| 54 |
if (tmp/freq <= 256) { |
| 55 |
TCCR2 = BV(WGM21) | BV(COM20) | (k+1); // CTC mode |
| 56 |
ocr = tmp/freq; |
| 57 |
tmp = tmp/ocr; // the value actually set |
| 58 |
if (ocr) |
| 59 |
--ocr; |
| 60 |
OCR2 = ocr; |
| 61 |
return tmp; |
| 62 |
} |
| 63 |
k = k + 1; |
| 64 |
} |
| 65 |
} |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
#define MTIME 50000 // We count the pulses for 50 milliseconds only |
| 71 |
|
| 72 |
uint32_t measure_frequency(void) |
| 73 |
{ |
| 74 |
/* |
| 75 |
Count the input of TC0 for 50000 microseconds (using TC1). |
| 76 |
multiply by 20 to get the counts/second. |
| 77 |
*/ |
| 78 |
uint32_t low = 0; |
| 79 |
|
| 80 |
TCCR1B = BV(CS11); // Feed Clock/8 (1 MHz ) to TCNT1 |
| 81 |
TCCR0 = 7; // TC0 counts External clock |
| 82 |
TIFR |= BV(TOV0); // Clear TCC0 OVF flag |
| 83 |
TCNT0 = 0; // Reset TCNT0 |
| 84 |
TCNT1 = 0; // to keep time with clk/8 |
| 85 |
while(TCNT1 < MTIME) |
| 86 |
{ |
| 87 |
if(TIFR & BV(TOV0)) // TC0 overflow after 255 counts |
| 88 |
{ |
| 89 |
++low; |
| 90 |
TIFR |= BV(TOV0); // Clear OVF flag |
| 91 |
} |
| 92 |
} |
| 93 |
TCCR0 = 0; |
| 94 |
return (low * 256 + TCNT0)*20; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
/* PWM DAC on counter 2, PD0 (OC2) */ |
| 99 |
void set_voltage(uint8_t val) |
| 100 |
{ |
| 101 |
DDRD |= 0x80; // make PD0 as output |
| 102 |
OCR2 = val; |
| 103 |
// Fast PWM mode |
| 104 |
TCCR2 = BV(WGM21) | BV(WGM20) | BV(COM21) | BV(CS20); |
| 105 |
TCNT2 = 0; |
| 106 |
} |
| 107 |
|