Rev 289 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 289 | Rev 291 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CAN RGB LED. This application is used to control a RGB LED. |
2 | * CAN RGB LED. This application is used to control a RGB LED. |
| 3 | * Still under heavy development |
3 | * Still under heavy development |
| 4 | * |
4 | * |
| 5 | * @date 2007-02-04 |
5 | * @date 2007-02-04 |
| 6 | * @author Martin Nordén |
6 | * @author Martin Nordén |
| 7 | * |
7 | * |
| 8 | */ |
8 | */ |
| 9 | 9 | ||
| 10 | /*----------------------------------------------------------------------------- |
10 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
11 | * Includes |
| 12 | *---------------------------------------------------------------------------*/ |
12 | *---------------------------------------------------------------------------*/ |
| 13 | /* system files */ |
13 | /* system files */ |
| 14 | #include <avr/io.h> |
14 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
15 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
16 | #include <avr/wdt.h> |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| 18 | /* lib files */ |
18 | /* lib files */ |
| 19 | #include <can.h> |
19 | #include <can.h> |
| 20 | #include <serial.h> |
20 | #include <serial.h> |
| 21 | #include <timebase.h> |
21 | #include <timebase.h> |
| 22 | #include <math.h> |
22 | #include <math.h> |
| 23 | 23 | ||
| 24 | /*----------------------------------------------------------------------------- |
24 | /*----------------------------------------------------------------------------- |
| 25 | * Variables |
25 | * Variables |
| 26 | *---------------------------------------------------------------------------*/ |
26 | *---------------------------------------------------------------------------*/ |
| 27 | uint32_t timeStamp; //TimeStamp to keep track of time |
27 | uint32_t timeStamp; //TimeStamp to keep track of time |
| 28 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
28 | uint32_t dimStamp; //Timestamp to keep track of dimmer changes |
| 29 | 29 | ||
| 30 | uint8_t currR; //Current Red 0->255 |
30 | uint8_t currR; //Current Red 0->255 |
| 31 | uint8_t currG; //Current Green 0->255 |
31 | uint8_t currG; //Current Green 0->255 |
| 32 | uint8_t currB; //Current Blue 0->255 |
32 | uint8_t currB; //Current Blue 0->255 |
| 33 | 33 | ||
| 34 | uint8_t destR; //Destination Red |
34 | uint8_t destR; //Destination Red |
| 35 | uint8_t destG; //Destination Green |
35 | uint8_t destG; //Destination Green |
| 36 | uint8_t destB; //Destination Blue |
36 | uint8_t destB; //Destination Blue |
| 37 | 37 | ||
| 38 | uint8_t currIntens; //Current intensity. 0->255 |
38 | uint8_t currIntens; //Current intensity. 0->255 |
| 39 | uint8_t destIntens; //Destination intensity. 0->255 |
39 | uint8_t destIntens; //Destination intensity. 0->255 |
| 40 | 40 | ||
| 41 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
41 | uint16_t fadeSpeed; //Fadingspeed. 16bit for reaaaally slow fading. Sets the speed of colorchange. |
| 42 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
42 | uint8_t dimSpeed; //Dimmer speed. Sets the speed of intensitychange. |
| 43 | 43 | ||
| 44 | uint8_t program; //This variable keeps track of what program is currently running. |
44 | uint8_t program; //This variable keeps track of what program is currently running. |
| 45 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
45 | uint8_t programstate; //This variable keeps track of where in a program we currently are. Will probably be removed later on. |
| 46 | 46 | ||
| 47 | /*---------------------------------------------------------------------------- |
47 | /*---------------------------------------------------------------------------- |
| 48 | * Functions |
48 | * Functions |
| 49 | * -------------------------------------------------------------------------*/ |
49 | * -------------------------------------------------------------------------*/ |
| 50 | void updateLED(); |
50 | void updateLED(); |
| 51 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
51 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B); |
| 52 | uint8_t fade(uint8_t current, uint8_t destination); |
52 | uint8_t fade(uint8_t current, uint8_t destination); |
| 53 | 53 | ||
| 54 | 54 | ||
| 55 | /*----------------------------------------------------------------------------- |
55 | /*----------------------------------------------------------------------------- |
| 56 | * Main Program |
56 | * Main Program |
| 57 | *---------------------------------------------------------------------------*/ |
57 | *---------------------------------------------------------------------------*/ |
| 58 | int main(void) { |
58 | int main(void) { |
| 59 | Timebase_Init(); |
59 | Timebase_Init(); |
| 60 | Serial_Init(); |
60 | Serial_Init(); |
| 61 | Can_Init(); |
61 | Can_Init(); |
| 62 | 62 | ||
| 63 | //Setting up OC1A, 16 bit counter used as 8 bit. |
63 | //Setting up OC1A, 16 bit counter used as 8 bit. |
| 64 | /* Use OC1A */ |
64 | /* Use OC1A */ |
| 65 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
65 | TCCR1A |= (1<<COM1A1) | (1<<WGM11); /* Set OC1A at TOP, mode 14 */ |
| 66 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
66 | TCCR1B |= (1<<WGM13) | (1<<WGM12) | (1<<CS11); /* Timer uses main system clock with 1/8 prescale */ |
| 67 | ICR1 = 256; //8 bit precision to match the other two counters. |
67 | ICR1 = 256; //8 bit precision to match the other two counters. |
| 68 | OCR1A = |
68 | OCR1A = 1; //50% as standard |
| 69 | DDRB |= (1<<PB1); /* Enable pin as output */ |
69 | DDRB |= (1<<PB1); /* Enable pin as output */ |
| - | 70 | ||
| - | 71 | /* Use OC0A */ |
|
| - | 72 | TCCR0A |= (1<<COM0A1)|(1<<WGM01)|(1<<WGM00); /* Set OC0A at TOP, Mode 7 */ |
|
| - | 73 | TCCR0B |= (1<<CS02);//(1<<CS02)|(1<<CS00); /* Prescaler 1/1024 */ |
|
| - | 74 | OCR0A = 40; |
|
| - | 75 | DDRD |= (1<<PD6); |
|
| - | 76 | ||
| - | 77 | /* Use OC0B */ |
|
| - | 78 | TCCR0A |= (1<<COM0B1); |
|
| - | 79 | OCR0B = 40; |
|
| - | 80 | DDRD |= (1<<PD5); |
|
| 70 | 81 | ||
| 71 | //Initialize variables |
82 | //Initialize variables |
| 72 | fadeSpeed = 250; |
83 | fadeSpeed = 250; |
| 73 | dimSpeed = 255; |
84 | dimSpeed = 255; |
| 74 | timeStamp = 0; |
85 | timeStamp = 0; |
| 75 | dimStamp = 0; |
86 | dimStamp = 0; |
| 76 | 87 | ||
| 77 | Can_Message_t rxMsg; |
88 | Can_Message_t rxMsg; |
| 78 | currR = |
89 | currR = 0x64; |
| 79 | currG = 0x15; |
90 | currG = 0x15; |
| 80 | currB = 0x63; |
91 | currB = 0x63; |
| 81 | 92 | ||
| 82 | sei(); |
93 | sei(); |
| 83 | 94 | ||
| 84 | reformatRGB(currR,currG,currB); |
95 | reformatRGB(currR,currG,currB); |
| 85 | 96 | ||
| 86 | destR = 0xff; |
- | |
| 87 | currIntens = |
97 | currIntens = 20; |
| 88 | destIntens = |
98 | destIntens = 200; |
| 89 | 99 | ||
| 90 | /* main loop */ |
100 | /* main loop */ |
| 91 | while (1) { |
101 | while (1) { |
| 92 | /* service the CAN routines */ |
102 | /* service the CAN routines */ |
| 93 | Can_Service(); |
103 | Can_Service(); |
| 94 | 104 | ||
| 95 | /* Time to fade color? */ |
105 | /* Time to fade color? */ |
| 96 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
106 | if (Timebase_PassedTimeMillis(timeStamp) >= fadeSpeed) { |
| 97 | currR = fade(currR, destR); |
107 | currR = fade(currR, destR); |
| 98 | currG = fade(currG, destG); |
108 | currG = fade(currG, destG); |
| 99 | currB = fade(currB, destB); |
109 | currB = fade(currB, destB); |
| 100 | 110 | ||
| 101 | timeStamp = Timebase_CurrentTime(); |
111 | timeStamp = Timebase_CurrentTime(); |
| 102 | updateLED(); |
112 | updateLED(); |
| 103 | } |
113 | } |
| 104 | 114 | ||
| 105 | 115 | ||
| 106 | /* Time to fade intensity? */ |
116 | /* Time to fade intensity? */ |
| 107 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
117 | if (Timebase_PassedTimeMillis(dimStamp) >= dimSpeed) { |
| 108 | currIntens = fade(currIntens, destIntens); |
118 | currIntens = fade(currIntens, destIntens); |
| 109 | dimStamp = Timebase_CurrentTime(); |
119 | dimStamp = Timebase_CurrentTime(); |
| 110 | updateLED(); |
120 | updateLED(); |
| 111 | } |
121 | } |
| 112 | 122 | ||
| 113 | 123 | ||
| 114 | 124 | ||
| 115 | /* check if any messages have been received */ |
125 | /* check if any messages have been received */ |
| 116 | /* A lot of remote control crap going on here now for testing */ |
126 | /* A lot of remote control crap going on here now for testing */ |
| 117 | while (Can_Receive(&rxMsg) == CAN_OK) { |
127 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 118 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
128 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x20)){ |
| 119 | program = 1; |
129 | program = 1; |
| 120 | fadeSpeed = 0; |
130 | fadeSpeed = 0; |
| 121 | } |
131 | } |
| 122 | 132 | ||
| 123 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
133 | if ((rxMsg.Data.bytes[0] == 0x0)&(rxMsg.Data.bytes[3] == 0x21)){ |
| 124 | program = 2; |
134 | program = 2; |
| 125 | fadeSpeed = 0; |
135 | fadeSpeed = 0; |
| 126 | } |
136 | } |
| 127 | } |
137 | } |
| 128 | 138 | ||
| 129 | 139 | ||
| 130 | } |
140 | } |
| 131 | 141 | ||
| 132 | return 0; |
142 | return 0; |
| 133 | } |
143 | } |
| 134 | 144 | ||
| 135 | 145 | ||
| 136 | 146 | ||
| 137 | 147 | ||
| 138 | 148 | ||
| 139 | void updateLED(){ |
149 | void updateLED(){ |
| 140 | 150 | ||
| 141 |
|
151 | // OCR1A = currR * currIntens / 255; |
| 142 | //TODO: Här ska givetvis de andra två färgerna också uppdateras. |
152 | //TODO: Här ska givetvis de andra två färgerna också uppdateras. |
| 143 | 153 | ||
| 144 | 154 | ||
| 145 | //Bara för debugcrap |
155 | //Bara för debugcrap |
| 146 | Can_Message_t txMsg; |
156 | Can_Message_t txMsg; |
| 147 | txMsg.Id = 0x55; |
157 | txMsg.Id = 0x55; |
| 148 | txMsg.RemoteFlag = 0; |
158 | txMsg.RemoteFlag = 0; |
| 149 | txMsg.ExtendedFlag = 1; |
159 | txMsg.ExtendedFlag = 1; |
| 150 | txMsg.DataLength = 4; |
160 | txMsg.DataLength = 4; |
| 151 | 161 | ||
| 152 | txMsg.Data.bytes[0] = currR * currIntens / 255; |
162 | txMsg.Data.bytes[0] = currR * currIntens / 255; |
| 153 | txMsg.Data.bytes[1] = currG * currIntens / 255; |
163 | txMsg.Data.bytes[1] = currG * currIntens / 255; |
| 154 | txMsg.Data.bytes[2] = currB * currIntens / 255; |
164 | txMsg.Data.bytes[2] = currB * currIntens / 255; |
| 155 | txMsg.Data.bytes[3] = currIntens; |
165 | txMsg.Data.bytes[3] = currIntens; |
| 156 | 166 | ||
| 157 | Can_Send(&txMsg); |
167 | Can_Send(&txMsg); |
| 158 | 168 | ||
| 159 | 169 | ||
| 160 | 170 | ||
| 161 | //Red |
171 | //Red |
| 162 | //Green |
172 | //Green |
| 163 | //Blue |
173 | //Blue |
| 164 | } |
174 | } |
| 165 | - | ||
| 166 | 175 | ||
| 167 | 176 | ||
| 168 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
177 | void reformatRGB(uint8_t R, uint8_t G, uint8_t B){ |
| 169 | uint8_t maxvalue; |
178 | uint8_t maxvalue; |
| 170 | uint16_t tempcontainer; |
179 | uint16_t tempcontainer; |
| 171 | 180 | ||
| 172 | if ((R>G)&(R>B)){ |
181 | if ((R>G)&(R>B)){ |
| 173 | maxvalue = R; |
182 | maxvalue = R; |
| 174 | } else if ((G>R)&(G>B)){ |
183 | } else if ((G>R)&(G>B)){ |
| 175 | maxvalue = G; |
184 | maxvalue = G; |
| 176 | } else { |
185 | } else { |
| 177 | maxvalue = B; |
186 | maxvalue = B; |
| 178 | } |
187 | } |
| 179 | 188 | ||
| 180 | tempcontainer = R * 255; |
189 | tempcontainer = R * 255; |
| 181 | currR = tempcontainer/maxvalue; |
190 | currR = tempcontainer/maxvalue; |
| 182 | tempcontainer = G * 255; |
191 | tempcontainer = G * 255; |
| 183 | currG = tempcontainer/maxvalue; |
192 | currG = tempcontainer/maxvalue; |
| 184 | tempcontainer = B * 255; |
193 | tempcontainer = B * 255; |
| 185 | currB = tempcontainer/maxvalue; |
194 | currB = tempcontainer/maxvalue; |
| 186 | 195 | ||
| 187 | currIntens = maxvalue; |
196 | currIntens = maxvalue; |
| 188 | } |
197 | } |
| 189 | 198 | ||
| 190 | uint8_t fade(uint8_t current, uint8_t destination){ |
199 | uint8_t fade(uint8_t current, uint8_t destination){ |
| 191 | if (current > destination){ |
200 | if (current > destination){ |
| 192 | return(current - 1); |
201 | return(current - 1); |
| 193 | } else if (current < destination){ |
202 | } else if (current < destination){ |
| 194 | return(current + 1); |
203 | return(current + 1); |
| 195 | } |
204 | } |
| 196 | return(current); |
205 | return(current); |
| 197 | } |
206 | } |
| 198 | 207 | ||