summaryrefslogtreecommitdiff
path: root/pwm.c
blob: 0641a80a1d2dfb4b6b3f2b3b4ee8deb2b67380bb (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
/*
 * 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>
#include "shiftbrite.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 = (uint16_t)r * r >> 4;
	value_green = (uint16_t)g * g >> 4;
	value_blue = (uint16_t)b * b >> 4;
}

void pwm_tick(void)
{
    for (uint16_t i = 0; i < 4096; i++) {
        /* this should take about the same time for each iteration, regardless of pulse width */
        PORTA= (PORTA & ~((1<<PA0)|(1<<PA1)|(1<<PA2))) |
            (value_red   > i? (1<<PA0): 0) |
            (value_green > i? (1<<PA1): 0) |
            (value_blue  > i? (1<<PA2): 0);
    }
}

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