-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLEDkontrol.c
More file actions
169 lines (146 loc) · 2.6 KB
/
LEDkontrol.c
File metadata and controls
169 lines (146 loc) · 2.6 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* spitest.c
*
* Created on: 07.07.2012
* Author: Paul Rosset
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <util/delay.h>
#include "spi.h"
/**
* Global Variables
*/
volatile int16_t framebuffer[]={0x0,0x0,0x0800,0x03F,0x00,0x00,0x00,0x0};
int8_t serialize[4];
volatile uint8_t counter;
uint8_t getRed(int16_t c16)
{
return (c16 >> 10)& 0x3E;
}
uint8_t getGreen(int16_t c16)
{
return (c16 >> 5)& 0x3F;
}
uint8_t getBlue(int16_t c16)
{
return (c16 << 1) & 0x3E;
}
/** Set the LED array to a new Image*/
void update(int h)
{
for (int i = 0; i < 8; i++)
{
//HSV colorspace
int current = (h+i * 30)%360;//46
int8_t block = (current) / 60;
int16_t hi = block <<5;
int16_t f = (current) <<5;
f /= 60;
f -= hi;
int p = 0;
int q = 31 - f;
int t = f;
int V= 31;
//Set led to color value
switch (block)
{
case 0:
case 6:
framebuffer[i]=(V <<11)|(t<<6)|p;
break;
case 1:
framebuffer[i]=(q <<11)|(V<<6)|p;
break;
case 2:
framebuffer[i]=(p <<11)|(V<<6)|t;
break;
case 3:
framebuffer[i]=(p <<11)|(q<<6)|V;
break;
case 4:
framebuffer[i]=(t <<11)|(p<<6)|V;
break;
case 5:
framebuffer[i]=(V <<11)|(p<<6)|q;
break;
}
}
}
/**
* Convert the Color value for each LED to power times
*/
void renderColor(volatile int16_t buff[], unsigned int counter, int ledcount)
{
int8_t tmp = 0;
for(int i = 0; i< ledcount ; i++)
{
int8_t bitcolor = 0;
if(getRed(buff[i]) > counter)
{
bitcolor = 4;
}
if(getGreen(buff[i])> counter)
{
bitcolor |= 2;
}
if(getBlue(buff[i]) > counter)
{
bitcolor |= 1;
}
if((i & 1) == 0)
{
tmp =((bitcolor) << 4);
}
else
{
tmp |=((bitcolor) << 1);
SPItransceiveByte(tmp);
tmp = 0;
}
}
}
/**
* Initialisation routine
* */
int main()
{
//Set the timer
//WGM10 - 13 ctc
TCCR1A = (0 << WGM10) | (0 << WGM11);
TCCR1B = (0 << WGM13) | (1 << WGM12);
// divide system clock by 64
TCCR1B |= (0 << CS12) | (1 << CS11) | (1 << CS10);
TIMSK1 = (1 << OCIE1A);
OCR1A = 40;
SPIinit();
int16_t cnt = 0;
DDRD |= (1 << PD5);
sei();
//Main loop
while(1)
{
_delay_ms(5);
PORTD ^= (1<< PD5);
update(cnt);
cnt+=1;
cnt %= 360;
}
}
ISR(TIMER1_OVF_vect){}
/**
* Timer1 Overflow Vector
* This timer is used to switch the leds on and of depending on their color value
* */
ISR(TIMER1_COMPA_vect)
{
char tmp = SREG; //backup SREG state
cli();
PORTB &= ~(1<<(SPI_SlaveSelect));
counter++;
counter %= 0x3F;
renderColor(framebuffer,counter,8);
PORTB |= (1<<SPI_SlaveSelect);
SREG = tmp;//Restore SREG
}