blob: 5b33a1950daea01686058c9461d61e645b1eac9a (
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/*
* src/pwm.c
* (c) 2022 Jonas Gunz <himself@jonasgunz.de>
* License: MIT
*/
#include "pwm.h"
#include <stdint.h>
uint8_t t0_ovf_cnt = 0;
uint8_t pwm_duty[PWM_PINS];
uint8_t pwm_target[PWM_PINS];
ISR(TIMER0_OVF_vect) {
uint8_t i;
cli();
/*TCNT0 = (1<<7);*/ /* Hack-increase Interrupt trigger freq */
t0_ovf_cnt ++;
for( i=0; i<PWM_PINS; i++) {
if(t0_ovf_cnt <= pwm_duty[i])
PWM_PORT |= (1<<i);
else
PWM_PORT &= ~(1<<i);
}
sei();
}
ISR(TIMER2_OVF_vect) {
uint8_t i;
cli();
for( i=0; i<PWM_PINS; i++) {
if (pwm_duty[i] < pwm_target[i])
pwm_duty[i]++;
else if (pwm_duty[i] > pwm_target[i])
pwm_duty[i]--;
}
sei();
}
void pwm_init() {
uint8_t i;
PWM_DDR |= (0xff >> (8-PWM_PINS));
PWM_PORT &= ~(0xff >> (8-PWM_PINS));
for ( i=0; i<PWM_PINS; i++ )
pwm_duty[i] = pwm_target[i] = 0;
/* Enable TIMER0 with interrupt, no prescaler */
TCCR0 |= (1<<CS00);
TIMSK |= (1<<TOIE0);
/* Enable TIMER2 with interrupt, Clk divide by 1024 */
TCCR2 |= (7<<CS20);
TIMSK |= (1<<TOIE2);
}
void pwm_set_pin(uint8_t _pin, uint8_t _duty) {
if(_pin < PWM_PINS) {
pwm_duty[_pin] = _duty;
pwm_target[_pin] = _duty;
}
}
void pwm_fade_pin(uint8_t _pin, uint8_t _duty) {
if(_pin < PWM_PINS)
pwm_target[_pin] = _duty;
}
|