blob: 1caff1c1c1736d0ee75f1ca8966691df85ac1a9b (
plain)
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
  | 
/**
 * 
 * Author: Dylan Muller
 * Copyright (c) 2025 
 * All rights reserved.
 * 
 * - Commercial/IP use prohibited.
 * - Attribution required.
 * See License.txt
 *
 */
#include "setup.h"
#include "peripheral/timer.h"
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/atomic.h>
uint32_t millis_count = 0;
void timer_init(void)
{
    uint32_t ctc_overflow;
    ctc_overflow = ((F_CPU / 1000) / 8); 
    TCCR1B |= (1 << WGM12) | (1 << CS11);
    OCR1AH = (ctc_overflow >> 8);
    OCR1AL = ctc_overflow;
    TIMSK1 |= (1 << OCIE1A);
}
void timer_reset(void)
{
    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
        millis_count = 0;
    }
}
uint32_t timer_millis(void)
{   
    uint32_t millis;
    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
    millis = millis_count;
  }
  return millis;
}
ISR(TIMER1_COMPA_vect)
{
    millis_count++;
}
  |