TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
timer.c
Go to the documentation of this file.
1#include <avr/io.h>
2
3#include "timer.h"
4
5// PWM, OC1A 50% dutycycle
6int tim1_PWM_init(void) {
7 // Set OC1A on PD5 as output
8 DDRD |= (1 << PD5);
9
10 // Fast PWM,
11 // Set Compare Output Mode
12 TCCR1A = (1 << COM1A1);
13 // Set Clock to PWM fast mode, and prescaler 10(CS10)
14 TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS10);
15 TCCR1A |= (1 << WGM11);
16
17 ICR1L = 0x04; // frequency = fclk/(N*(1+ICR1)) 4 ~983khz
18 ICR1H = ((0x04) >> 8);
19 OCR1AL = 0x04 / 2; // 50% duty cycle
20 OCR1AH = (0x04 / 2 >> 8);
21
22 return 0;
23}
24
25// CTC timer, OC1A
26int tim1_CTC_init(void) {
27 // Set OC1A on PD5 as output
28 DDRD |= (1 << PD5);
29
30 // CTC mode, toggle OC1A on compare
31 TCCR1A = (1 << COM1A0);
32 TCCR1B = (1 << WGM12) | (1 << CS10);
33
34 OCR1AL = 0x01; // Compare value
35 OCR1AH = 0x01 >> 8;
36 // With 4915200 Hz clk, N=8, value = 4915200/(2*8*(1+f_desierd)) = 1 MHz
37 // 1 = ~1.23Mhz
38
39 return 0;
40}
int tim1_CTC_init(void)
Initialize Timer1 in CTC mode on OC1A.
Definition timer.c:26
int tim1_PWM_init(void)
Initialize Timer1 for Fast PWM on OC1A.
Definition timer.c:6
Timer driver (for ADC clock).