]> the.earth.li Git - temper-clone.git/blob - timer.c
Remove unused read_temp() function
[temper-clone.git] / timer.c
1 /*
2  * Timer1 functions for ATTiny85
3  *
4  * Heavily based on code from Ardunio:
5  * https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring.c
6  *
7  * Copyright (c) 2005-2006 David A. Mellis
8  * Copyright 2018 Jonathan McDowell <noodles@earth.li>
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <avr/interrupt.h>
26 #include <avr/io.h>
27 #include <util/delay.h>
28
29 #include <avr/pgmspace.h>
30
31 #include "timer.h"
32
33 #define clockCyclesPerMicrosecond() (F_CPU / 1000000L)
34 /* Prescaler of 64 from CPU_CLK */
35 #define MICROSECONDS_PER_TIMER1_OVERFLOW (64 * 256 / clockCyclesPerMicrosecond())
36 #define MILLIS_INC (MICROSECONDS_PER_TIMER1_OVERFLOW / 1000)
37 #define FRACT_INC ((MICROSECONDS_PER_TIMER1_OVERFLOW % 1000) >> 3)
38 #define FRACT_MAX (1000 >> 3)
39
40 volatile unsigned long timer1_overflow_count = 0;
41 volatile unsigned long timer1_millis = 0;
42 static unsigned char timer1_fract = 0;
43
44 void timer_init(void)
45 {
46         /*
47          * bit 7:   0  (CTC1: Disabled CTC)
48          * bit 6/5: 00 (COM1A1/COM1A0: Disconnect counter from OC1A output)
49          * bit 3-0: 0111 (CS13/CS12/CS11/CS10: CPU_CK/64)
50          */
51         TCCR1 = (1 << CS12) | (1 << CS11) | (1 << CS10);
52
53         /* Initialise counter */
54         TCNT1 = 0;
55
56         /* Disable Timer 1 output compare match interrupt */
57         TIMSK &= ~(OCIE1A | OCIE1B);
58         /* Enable Timer 1 overflow interrupt */
59         TIMSK |= _BV(TOIE1);
60 }
61
62 unsigned long micros(void)
63 {
64         unsigned long m;
65         uint8_t oldSREG = SREG, t;
66
67         cli();
68         m = timer1_overflow_count;
69         t = TCNT1;
70
71         if ((TIFR & _BV(TOV1)) & (t < 255))
72                 m++;
73
74         SREG = oldSREG;
75
76         return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
77 }
78
79 unsigned long millis(void)
80 {
81         unsigned long m;
82         uint8_t oldSREG = SREG;
83
84         cli();
85         m = timer1_millis;
86         SREG = oldSREG;
87
88         return m;
89 }
90
91 SIGNAL(TIMER1_OVF_vect)
92 {
93         unsigned long m = timer1_millis;
94         unsigned char f = timer1_fract;
95
96         m += MILLIS_INC;
97         f += FRACT_INC;
98         if (f >= FRACT_MAX) {
99                 f -= FRACT_MAX;
100                 m++;
101         }
102
103         timer1_fract = f;
104         timer1_millis = m;
105         timer1_overflow_count++;
106 }