summaryrefslogtreecommitdiff
path: root/final_project/clock.c
blob: ce4ee11458d3fb784f46ee2386491a560e164776 (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
/*
 * clock.c
 *
 * Created: 4/3/2024 10:29:19 AM
 *  Author: bsw9xd
 */ 
#include "clock.h"

void timer_init_ctc() {
	TCCR1A = 0x00; //WGM10 = 0, WGM11 = 0 (CTC mode when combined with WGM12 in TCCR1B)
	TCCR1B = (1 << WGM12);
	TCNT1 = 0; // initialize timer at 0

}

void start_timer() {
	TCNT1 = 0;
	//OCRN1A = (seconds to wait / (prescaler / f_cpu))
	OCR1A = 0x3d09; // we'll only need to delay 1 second 
	if(TIFR1 & (1 << OCF1A)) TIFR1 |= (1 << OCF1A); //discard possible compare match
	TCCR1B |=  (1<<CS12) | (1 << CS10); //start timer with 1024 prescaler
}

//disable timer
void stop_timer() {
	if(TIFR1 & (1 << OCF1A)) TIFR1 |= (1 << OCF1A); //discard possible compare match
	TIMSK1 = (1 << WGM12); //no clock source
}

//see if a second pas passed since start_timer
bool timer_done() {
	return (TIFR1 & (1 << OCF1A)); //check if compare match
}