-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwm2313.c
46 lines (37 loc) · 942 Bytes
/
pwm2313.c
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
#include <avr/io.h>
#include <util/delay.h>
#define N_1 (_BV(CS00))
#define N_8 (_BV(CS01))
#define N_64 (_BV(CS01)|_BV(CS00))
#define N_256 (_BV(CS02))
#define N_1024 (_BV(CS02)|_BV(CS00))
static void pwm_init(void)
{
DDRB |= _BV(PB2); // set PWM pin as OUTPUT
TCCR0A |= _BV(COM0A1) | _BV(WGM01) | _BV(WGM00); // set timer mode to FAST PWM
}
static void pwm_set_frequency(uint32_t N)
{
TCCR0B = (TCCR0B & ~((1<<CS02)|(1<<CS01)|(1<<CS00))) | N; // set prescaler
}
static void pwm_set_duty(uint8_t duty)
{
OCR0A = duty; // set the OCRnx
}
static void pwm_stop(void)
{
TCCR0B &= ~((1<<CS02)|(1<<CS01)|(1<<CS00)); // stop the timer
}
int main(void)
{
uint8_t duty = 127; //Ranges from 0 to 254 i.e 0% t0 100% duty cycle
/* setup */
pwm_init();
pwm_set_frequency(N_1);
pwm_set_duty(duty);
/* loop */
while (1) {
//pwm_set_duty(duty++);
//_delay_ms(100);
}
}