-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFast PWM.c
More file actions
40 lines (32 loc) · 787 Bytes
/
Fast PWM.c
File metadata and controls
40 lines (32 loc) · 787 Bytes
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
/*
* Percobaan 1.c
* Created: 15/09/2022 23.29.09
* Author : Kelompok 1
*/
#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
double duty = 50; // Deklarasi variabel duty tipe double
int main(void)
{
DDRB = (1 << PORTB3); // PORT B3 sebagai output
TCCR0 = (1 << WGM00)|(1 << WGM01)|(1 << COM01); // Mode fast PWM
TIMSK = (1 << OCIE0); // Interrupt aktif
//OCR0 = (duty/100)*255; // Menghitung nilai OCR
sei(); // Global interrupt
TCCR0 |= (1 << CS02)|(1 << CS00); // Prescaler 1024
while (1)
{
_delay_ms(2000); // Tunda 2 detik
duty++; // Increment 1
if (duty > 100)
{
duty = 50;
}
}
}
ISR(TIMER0_COMP_vect) // Interrupt servis rutin timer 0
{
OCR0 = (duty/100)*255; // Menghitung nilai OCR
}