forked from esden/open-bldc-mk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
59 lines (48 loc) · 1.63 KB
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Open-BLDC Firmware, Firmware for BrushLess Drive Controllers
* Copyright (C) 2009 Piotr Esden-Tempski <piotr at esden.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <avr/interrupt.h>
#include <stdint.h>
#include "timer.h"
uint16_t timer_sw_counter = 0;
//#define TIMER_VAL (((uint16_t)TCNT0)|(((uint16_t)timer_overflow_counter)<<8))
#define TIMER_VAL TCNT0
void timer_init_0();
void timer_init() {
timer_init_0();
}
void timer_init_0() {
TIMSK = (1 << TOIE0); // Timer/Counter0 Overflow Interrupt Enable
TCCR0 = (0 << CS02) | (1 << CS01) | (0 << CS00); // clk/8 => 1 MHz
}
void timer_wait(uint8_t time_in_us) {
time_in_us = (uint8_t) TCNT0 + time_in_us;
while ((TCNT0 - time_in_us) & 0x80) {
asm("nop");
}
}
/* start new stopwatch */
uint16_t timer_new_sw_ms(uint16_t time_in_ms) {
return (timer_sw_counter + (time_in_ms * 1000L / 256));
}
uint16_t timer_sw_check(uint16_t sw_in_ms) {
return ((sw_in_ms - timer_sw_counter) & 0x8000) >> 8;
}
ISR(TIMER0_OVF_vect)
{
timer_sw_counter++;
}