Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
199 Kevin 1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
193 Kevin 2
/* ------------------------------------------------------------ */
3
/* PIC32 Configuration Settings */
4
/* ------------------------------------------------------------ */
5
/* Oscillator Settings */
6
#pragma config FNOSC     = PRIPLL   // Oscillator Selection Bits
7
#pragma config POSCMOD   = EC       // Primary Oscillator Configuration
8
#pragma config FPLLIDIV  = DIV_2    // PLL Input Divider
9
#pragma config FPLLMUL   = MUL_20   // PLL Multiplier
10
#pragma config FPLLODIV  = DIV_1    // PLL Output Divider
199 Kevin 11
#pragma config FPBDIV    = DIV_1    // Peripheral Clock Divisor (timers/UART/SPI/I2C)
193 Kevin 12
#pragma config FSOSCEN   = OFF      // Secondary Oscillator Enable
13
/* Clock Control Settings */
14
#pragma config IESO      = OFF      // Internal/External Clock Switch Over
15
#pragma config FCKSM     = CSDCMD   // Clock Switching and Monitor Selection
16
#pragma config OSCIOFNC  = OFF      // CLKO Output Signal Active on the OSCO Pin
17
/* USB Settings */
18
#pragma config UPLLEN    = ON       // USB PLL Enable
19
#pragma config UPLLIDIV  = DIV_2    // USB PLL Input Divider
20
#pragma config FVBUSONIO = OFF      // USB VBUS ON Selection
21
#pragma config FUSBIDIO  = OFF      // USB USID Selection
22
/* Other Peripheral Device Settings */
23
#pragma config FWDTEN    = OFF      // Watchdog Timer Enable
24
#pragma config WDTPS     = PS1024   // Watchdog Timer Postscaler
25
#pragma config FSRSSEL   = PRIORITY_7   // SRS Interrupt Priority
26
#pragma config FCANIO    = OFF      // CAN I/O Pin Select (default/alternate)
27
#pragma config FETHIO    = ON       // Ethernet I/O Pin Select (default/alternate)
28
#pragma config FMIIEN    = OFF      // Ethernet MII/RMII select (OFF=RMII)
29
/* Code Protection Settings */
30
#pragma config CP        = OFF      // Code Protect
31
#pragma config BWP       = OFF      // Boot Flash Write Protect
32
#pragma config PWP       = OFF      // Program Flash Write Protect
33
/* Debug Settings */
34
#pragma config ICESEL = ICS_PGx1    // ICE/ICD Comm Channel Select (on-board debugger)
35
/* ------------------------------------------------------------ */
199 Kevin 36
// </editor-fold>
193 Kevin 37
 
38
#include <xc.h>
39
#include <plib.h>
199 Kevin 40
#include <stdlib.h>
193 Kevin 41
#include "defines.h"
199 Kevin 42
#include "SPI1.h"
43
#include "TIMER5.h"
44
#include "CUBE.h"
200 Kevin 45
#include "BTN.h"
193 Kevin 46
 
200 Kevin 47
void BTN1_Interrupt(void);
48
void BTN2_Interrupt(void);
199 Kevin 49
void Animation_Solid_Colors(int iterations, int delay_ms);
50
void Animation_Layer_Alternate(int iterations, int delay_ms);
51
void Animation_Pixel_Alternate(int iterations, int delay_ms);
52
void Animation_Full_Color_Sweep(int iterations, int delay_us);
53
void Animation_Row_Column_Sweep(int iterations, int delay_ms);
54
void Animation_Pixel_Sweep(int iterations, int delay_ms);
55
void Animation_Pseudo_Random_Colors(int iterations,int delay_ms);
56
void Animation_Random_Colors(int iterations, int delay_ms);
57
void Animation_Cube_In_Cube(int iterations, int delay_ms);
58
 
59
void Delay_MS(unsigned int delay_ms) {
60
    unsigned int delay = delay_ms * MS_TO_CT_TICKS;
61
    unsigned int startTime = ReadCoreTimer();
62
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
63
}
64
 
65
void Delay_US(unsigned int delay_us) {
66
    unsigned int delay = delay_us * US_TO_CT_TICKS;
67
    unsigned int startTime = ReadCoreTimer();
68
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
69
}
70
 
193 Kevin 71
int main() {
72
    /* Configure the target for maximum performance at 80 MHz. */
199 Kevin 73
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
74
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 75
 
199 Kevin 76
    // Configure the interrupts for multiple vectors
193 Kevin 77
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
78
 
199 Kevin 79
    // Set all analog I/O pins to digital
80
    AD1PCFGSET = 0xFFFF;
193 Kevin 81
 
199 Kevin 82
    SPI1_DATA spi_data;
83
    SPI1_Init(&spi_data);
84
 
85
    PWM2_Init();
86
    PWM2_Start();
87
 
88
    CUBE_DATA cube_data;
200 Kevin 89
    Cube_Init(&cube_data, 0x01);
199 Kevin 90
 
91
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
200 Kevin 92
    TIMER5_Init(&Cube_Timer_Interrupt, 500);
199 Kevin 93
    TIMER5_Start();
94
 
200 Kevin 95
    BTN_Init(&BTN1_Interrupt, &BTN2_Interrupt, NULL);
96
 
97
    Cube_Set_All(0xFF,0xFF,0xFF);
98
    Delay_MS(3000);
99
 
199 Kevin 100
    // Loop through some preset animations
101
    while(1) {
102
        Animation_Solid_Colors(2,300);
103
        Animation_Layer_Alternate(2,300);
104
        Animation_Pixel_Alternate(1,200);
105
        Animation_Full_Color_Sweep(2,1000);
106
        Animation_Row_Column_Sweep(2,40);
107
        Animation_Pseudo_Random_Colors(10,300);
108
        Animation_Random_Colors(10,300);
109
        Animation_Cube_In_Cube(4,300);
110
    }
193 Kevin 111
}
112
 
200 Kevin 113
// Function call on button 1 press to change refresh rate
114
void BTN1_Interrupt(void) {
115
    static char state;
116
    state = (state == 3) ? 0 : state + 1;
117
    TIMER5_Stop();
118
    switch (state) {
119
        case 0:
120
            TIMER5_Init(&Cube_Timer_Interrupt, 500);
121
            break;
122
        case 1:
123
            TIMER5_Init(&Cube_Timer_Interrupt, 2083);
124
            break;
125
        case 2:
126
            TIMER5_Init(&Cube_Timer_Interrupt, 4166);
127
            break;
128
        case 3:
129
            TIMER5_Init(&Cube_Timer_Interrupt, 13107);
130
            break;
131
    }
132
    TIMER5_Start();
133
}
134
 
135
// Function call on button 2 press to change brightness
136
void BTN2_Interrupt(void) {
137
    static char state;
138
    state = (state == 5) ? 0 : state + 1;
139
    TIMER5_Stop();
140
    Delay_MS(1); // Need to wait for all SPI writes to complete
141
    char BC;
142
    switch (state) {
143
        case 0:
144
            BC = 0x01;
145
            break;
146
        case 1:
147
            BC = 0x08;
148
            break;
149
        case 2:
150
            BC = 0x10;
151
            break;
152
        case 3:
153
            BC = 0x20;
154
            break;
155
        case 4:
156
            BC = 0x40;
157
            break;
158
        case 5:
159
            BC = 0x70;
160
            break;
161
    }
162
    Cube_Write_DCS(BC);
163
    TIMER5_Start();
164
}
165
 
199 Kevin 166
void Animation_Solid_Colors(int iterations, int delay_ms) {
167
    int i;
168
    for (i = 0; i < iterations; i++) {
169
        Cube_Set_All(RED);
170
        Delay_MS(delay_ms);
171
        Cube_Set_All(GREEN);
172
        Delay_MS(delay_ms);
173
        Cube_Set_All(BLUE);
174
        Delay_MS(delay_ms);
175
    }
176
}
177
 
178
void Animation_Layer_Alternate(int iterations, int delay_ms) {
179
    int i,z;
180
    for (z = 0; z < iterations; z++) {
181
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
182
            if (i % 3 == 0)
183
                Cube_Set_Layer(i,RED);
184
            else if (i % 3 == 1)
185
                Cube_Set_Layer(i,GREEN);
186
            else
187
                Cube_Set_Layer(i,BLUE);
188
        }
189
        Delay_MS(delay_ms);
190
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
191
            if (i % 3 == 0)
192
                Cube_Set_Layer(i,GREEN);
193
            else if (i % 3 == 1)
194
                Cube_Set_Layer(i,BLUE);
195
            else
196
                Cube_Set_Layer(i,RED);
197
        }
198
        Delay_MS(delay_ms);
199
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
200
            if (i % 3 == 0)
201
                Cube_Set_Layer(i,BLUE);
202
            else if (i % 3 == 1)
203
                Cube_Set_Layer(i,RED);
204
            else
205
                Cube_Set_Layer(i,GREEN);
206
        }
207
        Delay_MS(delay_ms);
208
    }
209
}
210
 
211
void Animation_Pixel_Alternate(int iterations, int delay_ms) {
212
    int i,j,k,z;
213
    for (z = 0; z < iterations; z++) {
214
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
215
            Cube_Clear();
216
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
217
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
218
                    int var = (j * 8) + k;
219
                    if (var % 3 == 0)
220
                        Cube_Set_Pixel(i,j,k,RED);
221
                    else if (var % 3 == 1)
222
                        Cube_Set_Pixel(i,j,k,GREEN);
223
                    else
224
                        Cube_Set_Pixel(i,j,k,BLUE);
225
                }
226
            }
227
            Delay_MS(delay_ms);
228
            Cube_Clear();
229
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
230
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
231
                    int var = (j * 8) + k;
232
                    if (var % 3 == 0)
233
                        Cube_Set_Pixel(i,j,k,GREEN);
234
                    else if (var % 3 == 1)
235
                        Cube_Set_Pixel(i,j,k,BLUE);
236
                    else
237
                        Cube_Set_Pixel(i,j,k,RED);
238
                }
239
            }
240
            Delay_MS(delay_ms);
241
            Cube_Clear();
242
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
243
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
244
                    int var = (j * 8) + k;
245
                    if (var % 3 == 0)
246
                        Cube_Set_Pixel(i,j,k,BLUE);
247
                    else if (var % 3 == 1)
248
                        Cube_Set_Pixel(i,j,k,RED);
249
                    else
250
                        Cube_Set_Pixel(i,j,k,GREEN);
251
                }
252
            }
253
            Delay_MS(delay_ms);
254
        }
255
    }
256
}
257
 
258
void Animation_Full_Color_Sweep(int iterations, int delay_us) {
259
    int i,z;
260
    for (z = 0; z < iterations; z++) {
261
        for (i = 0; i < 0x0FF; i+=2) {
262
            Cube_Set_All(i,0,0);
263
            Delay_US(delay_us);
264
        }
265
        for (i = 0; i < 0x0FF; i+=2) {
266
            Cube_Set_All(0x0FF,i,0);
267
            Delay_US(delay_us);
268
        }
269
        for (i = 0x0FF; i >= 0; i-=2) {
270
            Cube_Set_All(i,0x0FF,0);
271
            Delay_US(delay_us);
272
        }
273
        for (i = 0; i < 0x0FF; i+=2) {
274
            Cube_Set_All(0,0x0FF,i);
275
            Delay_US(delay_us);
276
        }
277
        for (i = 0; i < 0x0FF; i+=2) {
278
            Cube_Set_All(i,0x0FF,0x0FF);
279
            Delay_US(delay_us);
280
        }
281
        for (i = 0x0FF; i >= 0; i-=2) {
282
            Cube_Set_All(0x0FF,i,0x0FF);
283
            Delay_US(delay_us);
284
        }
285
        for (i = 0x0FF; i >= 0; i-=2) {
286
            Cube_Set_All(i,0,0x0FF);
287
            Delay_US(delay_us);
288
        }
289
        for (i = 0x100; i >= 0; i-=2) {
290
            Cube_Set_All(0,0,i);
291
            Delay_US(delay_us);
292
        }
293
    }
294
}
295
 
296
void Animation_Row_Column_Sweep(int iterations, int delay_ms) {
297
    int i,j,k,a,z;
298
    for (z = 0; z < iterations; z++) {
299
        for (i = 0; i < 3; i++) {
300
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
301
                Cube_Clear();
302
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
303
                    if (i % 3 == 0)
304
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
305
                            Cube_Set_Pixel(a,j,k,RED);
306
                    else if (i % 3 == 1)
307
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
308
                            Cube_Set_Pixel(a,j,k,GREEN);
309
                    else
310
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
311
                            Cube_Set_Pixel(a,j,k,BLUE);
312
                Delay_MS(delay_ms);
313
            }
314
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
315
                Cube_Clear();
316
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
317
                    if (i % 3 == 0)
318
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
319
                            Cube_Set_Pixel(a,k,j,RED);
320
                    else if (i % 3 == 1)
321
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
322
                            Cube_Set_Pixel(a,k,j,GREEN);
323
                    else
324
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
325
                            Cube_Set_Pixel(a,k,j,BLUE);
326
                Delay_MS(delay_ms);
327
            }
328
            for (j = CUBE_LAYER_COUNT-1; j >= 0; j--) {
329
                Cube_Clear();
330
                if (i % 3 == 0) {
331
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
332
                        if (k == j)
333
                            Cube_Set_Layer(k,RED);
334
                } else if (i % 3 == 1) {
335
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
336
                        if (k == j)
337
                            Cube_Set_Layer(k,GREEN);
338
                } else {
339
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
340
                        if (k == j)
341
                            Cube_Set_Layer(k,BLUE);
342
                }
343
                Delay_MS(delay_ms);
344
            }
345
        }
346
    }
347
}
348
 
349
void Animation_Pixel_Sweep(int iterations, int delay_ms) {
350
    int i,j,k,z,a;
351
    for (z = 0; z < iterations; z++) {
352
        for (a = 0; a < 3; a++) {
353
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
354
                for (j = 0; j < CUBE_ROW_COUNT; j++) {
355
                    for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
356
                        Cube_Clear();
357
                        if (a % 3 == 0) {
358
                            Cube_Set_Pixel(i,j,k,RED);
359
                        } else if (a % 3 == 1) {
360
                            Cube_Set_Pixel(i,j,k,GREEN);
361
                        } else {
362
                            Cube_Set_Pixel(i,j,k,BLUE);
363
                        }
364
                        Delay_MS(delay_ms);
365
                    }
366
                }
367
            }
368
        }
369
    }
370
}
371
 
372
void Animation_Pseudo_Random_Colors(int iterations, int delay_ms) {
373
    int i,j,k,z;
374
    for (z = 0; z < iterations; z++) {
375
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
376
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
377
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
378
                    unsigned int a = rand();
379
                    if (a % 5 == 0)
380
                        Cube_Set_Pixel(i,j,k,RED);
381
                    else if (a % 5 == 1)
382
                        Cube_Set_Pixel(i,j,k,GREEN);
383
                    else if (a % 5 == 2)
384
                        Cube_Set_Pixel(i,j,k,BLUE);
385
                    else if (a % 5 == 3)
386
                        Cube_Set_Pixel(i,j,k,PURPLE);
387
                    else if (a % 5 == 4)
388
                        Cube_Set_Pixel(i,j,k,YELLOW);
389
                    else
390
                        Cube_Set_Pixel(i,j,k,ORANGE);
391
                }
392
            }
393
        }
394
        Delay_MS(delay_ms);
395
    }
396
}
397
 
398
void Animation_Random_Colors(int iterations, int delay_ms) {
399
    int i,j,k,z;
400
    for (z = 0; z < iterations; z++) {
401
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
402
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
403
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
404
                    Cube_Set_Pixel(i,j,k,rand()&0x0FF,rand()&0x0FF,rand()&0x0FF);
405
                }
406
            }
407
        }
408
        Delay_MS(delay_ms);
409
    }
410
}
411
 
412
void Animation_Cube_In_Cube(int iterations, int delay_ms) {
413
    int z,x,i,j,k;
414
    for (z = 0; z < iterations; z++) {
415
        for (x = 0; x < 5; x++) {
416
            Cube_Clear();
417
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
418
                if ((x == 0 || x == 4)&&(i == 0 || i == 7)) {
419
                    Cube_Set_Layer(i,RED);
420
                } else if ((x == 1 || x == 4)&&(i == 1 || i == 6)) {
421
                    for (j = 1; j < CUBE_ROW_COUNT-1; j++)
422
                        for (k = 1; k < CUBE_COLUMN_COUNT-1; k++)
423
                            Cube_Set_Pixel(i,j,k,YELLOW);
424
                } else if ((x == 2 || x == 4)&&(i == 2 || i == 5)) {
425
                    for (j = 2; j < CUBE_ROW_COUNT-2; j++)
426
                        for (k = 2; k < CUBE_COLUMN_COUNT-2; k++)
427
                            Cube_Set_Pixel(i,j,k,GREEN);
428
                } else if ((x == 3 || x == 4)&&(i == 3 || i == 4)) {
429
                    for (j = 3; j < CUBE_ROW_COUNT-3; j++)
430
                        for (k = 3; k < CUBE_COLUMN_COUNT-3; k++)
431
                            Cube_Set_Pixel(i,j,k,BLUE);
432
                }
433
 
434
                if ((x == 0 || x == 4)&&(i > 0 && i < 8)) {
435
                    for (j = 0; j < 8; j++) {
436
                        Cube_Set_Pixel(i,j,0,RED);
437
                        Cube_Set_Pixel(i,j,7,RED);
438
                        Cube_Set_Pixel(i,0,j,RED);
439
                        Cube_Set_Pixel(i,7,j,RED);
440
                    }
441
                }
442
                if ((x == 1 || x == 4)&&(i > 1 && i < 7)) {
443
                    for (j = 1; j < 7; j++) {
444
                        Cube_Set_Pixel(i,j,1,YELLOW);
445
                        Cube_Set_Pixel(i,j,6,YELLOW);
446
                        Cube_Set_Pixel(i,1,j,YELLOW);
447
                        Cube_Set_Pixel(i,6,j,YELLOW);
448
                    }
449
                }
450
                if ((x == 2 || x == 4)&&(i > 2 && i < 6)) {
451
                    for (j = 2; j < 6; j++) {
452
                        Cube_Set_Pixel(i,j,2,GREEN);
453
                        Cube_Set_Pixel(i,j,5,GREEN);
454
                        Cube_Set_Pixel(i,2,j,GREEN);
455
                        Cube_Set_Pixel(i,5,j,GREEN);
456
                    }
457
                }
458
            }
459
            Delay_MS(delay_ms);
460
        }
461
    }
462
}