summaryrefslogtreecommitdiff
path: root/pwm.c
blob: e981e2d16cbb38415c9e03c5da6faaa314566df5 (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
/*
 * Copyright 2012 (C) Christian Franke <nobody at nowhere dot ws>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with This program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <avr/interrupt.h>
#include <avr/io.h>
#include <stdint.h>
#include <string.h>

volatile uint16_t value_red = 3072;
volatile uint16_t value_green = 2048;
volatile uint16_t value_blue = 1024;

void pwm_set_rgb(uint8_t r, uint8_t g, uint8_t b)
{
	value_red = r << 4;
	value_green = g << 4;
	value_blue = b << 4;
}

void pwm_worker_loop(void)
{
	for (;;) {
		if (value_red)
			PORTA |= (1 << PA0);
		if (value_green)
			PORTA |= (1 << PA1);
		if (value_blue)
			PORTA |= (1 << PA2);

		for (uint16_t i = 0; i < 4096; i++) {
			if (value_red < i)
				PORTA &= ~(1 << PA0);
			if (value_green < i)
				PORTA &= ~(1 << PA1);
			if (value_blue < i)
				PORTA &= ~(1 << PA2);
		}
	}
}

void pwm_init(void)
{
	PORTA = 0x00;
	DDRA = (1 << PA0) | (1 << PA1) | (1 << PA2);
}