TTK4155 Lab Node 1
Term project in the NTNU course TTK4155 Embedded and Industrial Computer Systems Design
Loading...
Searching...
No Matches
Timer

Functions

int tim1_PWM_init (void)
 Initialize Timer1 for Fast PWM on OC1A.
int tim1_CTC_init (void)
 Initialize Timer1 in CTC mode on OC1A.

Detailed Description

Function Documentation

◆ tim1_CTC_init()

int tim1_CTC_init ( void )

#include <timer.h>

Initialize Timer1 in CTC mode on OC1A.

Configures PD5 as output and sets up CTC toggling with a compare value that yields roughly 1–1.2 MHz output depending on clock and prescaler.

Returns
0 on success.

Definition at line 26 of file timer.c.

26 {
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}

Referenced by main().

◆ tim1_PWM_init()

int tim1_PWM_init ( void )

#include <timer.h>

Initialize Timer1 for Fast PWM on OC1A.

Configures PD5 as output, sets Fast PWM mode with ICR1 as top, and generates ~50% duty cycle at approximately 983 kHz (with ICR1 = 4).

Returns
0 on success.

Definition at line 6 of file timer.c.

6 {
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}