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) {
201 Kevin 60
    // Delays the CPU for the given amount of time.
61
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
199 Kevin 62
    unsigned int delay = delay_ms * MS_TO_CT_TICKS;
63
    unsigned int startTime = ReadCoreTimer();
64
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
65
}
66
 
67
void Delay_US(unsigned int delay_us) {
201 Kevin 68
    // Delays the CPU for the given amount of time.
69
    // Note: Watch out for integer overflow!
199 Kevin 70
    unsigned int delay = delay_us * US_TO_CT_TICKS;
71
    unsigned int startTime = ReadCoreTimer();
72
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
73
}
74
 
193 Kevin 75
int main() {
76
    /* Configure the target for maximum performance at 80 MHz. */
199 Kevin 77
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
78
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 79
 
199 Kevin 80
    // Configure the interrupts for multiple vectors
193 Kevin 81
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
82
 
199 Kevin 83
    // Set all analog I/O pins to digital
84
    AD1PCFGSET = 0xFFFF;
193 Kevin 85
 
201 Kevin 86
    // Initialize peripherals
199 Kevin 87
    SPI1_DATA spi_data;
88
    SPI1_Init(&spi_data);
89
 
90
    PWM2_Init();
91
    PWM2_Start();
92
 
93
    CUBE_DATA cube_data;
200 Kevin 94
    Cube_Init(&cube_data, 0x01);
199 Kevin 95
 
96
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
201 Kevin 97
    TIMER_DATA timer_data;
98
    TIMER5_Init(&timer_data, &Cube_Timer_Interrupt, 500);
199 Kevin 99
    TIMER5_Start();
100
 
201 Kevin 101
    BTN_DATA btn_data;
102
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, NULL);
200 Kevin 103
 
201 Kevin 104
    // Begin display
200 Kevin 105
    Cube_Set_All(0xFF,0xFF,0xFF);
106
    Delay_MS(3000);
107
 
199 Kevin 108
    // Loop through some preset animations
109
    while(1) {
110
        Animation_Solid_Colors(2,300);
111
        Animation_Layer_Alternate(2,300);
112
        Animation_Pixel_Alternate(1,200);
113
        Animation_Full_Color_Sweep(2,1000);
114
        Animation_Row_Column_Sweep(2,40);
115
        Animation_Pseudo_Random_Colors(10,300);
116
        Animation_Random_Colors(10,300);
117
        Animation_Cube_In_Cube(4,300);
118
    }
193 Kevin 119
}
120
 
200 Kevin 121
// Function call on button 1 press to change refresh rate
122
void BTN1_Interrupt(void) {
123
    static char state;
124
    state = (state == 3) ? 0 : state + 1;
125
    TIMER5_Stop();
126
    switch (state) {
127
        case 0:
201 Kevin 128
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500);
200 Kevin 129
            break;
130
        case 1:
201 Kevin 131
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083);
200 Kevin 132
            break;
133
        case 2:
201 Kevin 134
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166);
200 Kevin 135
            break;
136
        case 3:
201 Kevin 137
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 13107);
200 Kevin 138
            break;
139
    }
140
    TIMER5_Start();
141
}
142
 
143
// Function call on button 2 press to change brightness
144
void BTN2_Interrupt(void) {
145
    static char state;
146
    state = (state == 5) ? 0 : state + 1;
147
    TIMER5_Stop();
148
    Delay_MS(1); // Need to wait for all SPI writes to complete
149
    char BC;
150
    switch (state) {
151
        case 0:
152
            BC = 0x01;
153
            break;
154
        case 1:
155
            BC = 0x08;
156
            break;
157
        case 2:
158
            BC = 0x10;
159
            break;
160
        case 3:
161
            BC = 0x20;
162
            break;
163
        case 4:
164
            BC = 0x40;
165
            break;
166
        case 5:
167
            BC = 0x70;
168
            break;
169
    }
170
    Cube_Write_DCS(BC);
171
    TIMER5_Start();
172
}
173
 
199 Kevin 174
void Animation_Solid_Colors(int iterations, int delay_ms) {
175
    int i;
176
    for (i = 0; i < iterations; i++) {
177
        Cube_Set_All(RED);
178
        Delay_MS(delay_ms);
179
        Cube_Set_All(GREEN);
180
        Delay_MS(delay_ms);
181
        Cube_Set_All(BLUE);
182
        Delay_MS(delay_ms);
183
    }
184
}
185
 
186
void Animation_Layer_Alternate(int iterations, int delay_ms) {
187
    int i,z;
188
    for (z = 0; z < iterations; z++) {
189
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
190
            if (i % 3 == 0)
191
                Cube_Set_Layer(i,RED);
192
            else if (i % 3 == 1)
193
                Cube_Set_Layer(i,GREEN);
194
            else
195
                Cube_Set_Layer(i,BLUE);
196
        }
197
        Delay_MS(delay_ms);
198
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
199
            if (i % 3 == 0)
200
                Cube_Set_Layer(i,GREEN);
201
            else if (i % 3 == 1)
202
                Cube_Set_Layer(i,BLUE);
203
            else
204
                Cube_Set_Layer(i,RED);
205
        }
206
        Delay_MS(delay_ms);
207
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
208
            if (i % 3 == 0)
209
                Cube_Set_Layer(i,BLUE);
210
            else if (i % 3 == 1)
211
                Cube_Set_Layer(i,RED);
212
            else
213
                Cube_Set_Layer(i,GREEN);
214
        }
215
        Delay_MS(delay_ms);
216
    }
217
}
218
 
219
void Animation_Pixel_Alternate(int iterations, int delay_ms) {
220
    int i,j,k,z;
221
    for (z = 0; z < iterations; z++) {
222
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
223
            Cube_Clear();
224
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
225
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
226
                    int var = (j * 8) + k;
227
                    if (var % 3 == 0)
228
                        Cube_Set_Pixel(i,j,k,RED);
229
                    else if (var % 3 == 1)
230
                        Cube_Set_Pixel(i,j,k,GREEN);
231
                    else
232
                        Cube_Set_Pixel(i,j,k,BLUE);
233
                }
234
            }
235
            Delay_MS(delay_ms);
236
            Cube_Clear();
237
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
238
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
239
                    int var = (j * 8) + k;
240
                    if (var % 3 == 0)
241
                        Cube_Set_Pixel(i,j,k,GREEN);
242
                    else if (var % 3 == 1)
243
                        Cube_Set_Pixel(i,j,k,BLUE);
244
                    else
245
                        Cube_Set_Pixel(i,j,k,RED);
246
                }
247
            }
248
            Delay_MS(delay_ms);
249
            Cube_Clear();
250
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
251
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
252
                    int var = (j * 8) + k;
253
                    if (var % 3 == 0)
254
                        Cube_Set_Pixel(i,j,k,BLUE);
255
                    else if (var % 3 == 1)
256
                        Cube_Set_Pixel(i,j,k,RED);
257
                    else
258
                        Cube_Set_Pixel(i,j,k,GREEN);
259
                }
260
            }
261
            Delay_MS(delay_ms);
262
        }
263
    }
264
}
265
 
266
void Animation_Full_Color_Sweep(int iterations, int delay_us) {
267
    int i,z;
268
    for (z = 0; z < iterations; z++) {
269
        for (i = 0; i < 0x0FF; i+=2) {
270
            Cube_Set_All(i,0,0);
271
            Delay_US(delay_us);
272
        }
273
        for (i = 0; i < 0x0FF; i+=2) {
274
            Cube_Set_All(0x0FF,i,0);
275
            Delay_US(delay_us);
276
        }
277
        for (i = 0x0FF; i >= 0; i-=2) {
278
            Cube_Set_All(i,0x0FF,0);
279
            Delay_US(delay_us);
280
        }
281
        for (i = 0; i < 0x0FF; i+=2) {
282
            Cube_Set_All(0,0x0FF,i);
283
            Delay_US(delay_us);
284
        }
285
        for (i = 0; i < 0x0FF; i+=2) {
286
            Cube_Set_All(i,0x0FF,0x0FF);
287
            Delay_US(delay_us);
288
        }
289
        for (i = 0x0FF; i >= 0; i-=2) {
290
            Cube_Set_All(0x0FF,i,0x0FF);
291
            Delay_US(delay_us);
292
        }
293
        for (i = 0x0FF; i >= 0; i-=2) {
294
            Cube_Set_All(i,0,0x0FF);
295
            Delay_US(delay_us);
296
        }
297
        for (i = 0x100; i >= 0; i-=2) {
298
            Cube_Set_All(0,0,i);
299
            Delay_US(delay_us);
300
        }
301
    }
302
}
303
 
304
void Animation_Row_Column_Sweep(int iterations, int delay_ms) {
305
    int i,j,k,a,z;
306
    for (z = 0; z < iterations; z++) {
307
        for (i = 0; i < 3; i++) {
308
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
309
                Cube_Clear();
310
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
311
                    if (i % 3 == 0)
312
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
313
                            Cube_Set_Pixel(a,j,k,RED);
314
                    else if (i % 3 == 1)
315
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
316
                            Cube_Set_Pixel(a,j,k,GREEN);
317
                    else
318
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
319
                            Cube_Set_Pixel(a,j,k,BLUE);
320
                Delay_MS(delay_ms);
321
            }
322
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
323
                Cube_Clear();
324
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
325
                    if (i % 3 == 0)
326
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
327
                            Cube_Set_Pixel(a,k,j,RED);
328
                    else if (i % 3 == 1)
329
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
330
                            Cube_Set_Pixel(a,k,j,GREEN);
331
                    else
332
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
333
                            Cube_Set_Pixel(a,k,j,BLUE);
334
                Delay_MS(delay_ms);
335
            }
336
            for (j = CUBE_LAYER_COUNT-1; j >= 0; j--) {
337
                Cube_Clear();
338
                if (i % 3 == 0) {
339
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
340
                        if (k == j)
341
                            Cube_Set_Layer(k,RED);
342
                } else if (i % 3 == 1) {
343
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
344
                        if (k == j)
345
                            Cube_Set_Layer(k,GREEN);
346
                } else {
347
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
348
                        if (k == j)
349
                            Cube_Set_Layer(k,BLUE);
350
                }
351
                Delay_MS(delay_ms);
352
            }
353
        }
354
    }
355
}
356
 
357
void Animation_Pixel_Sweep(int iterations, int delay_ms) {
358
    int i,j,k,z,a;
359
    for (z = 0; z < iterations; z++) {
360
        for (a = 0; a < 3; a++) {
361
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
362
                for (j = 0; j < CUBE_ROW_COUNT; j++) {
363
                    for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
364
                        Cube_Clear();
365
                        if (a % 3 == 0) {
366
                            Cube_Set_Pixel(i,j,k,RED);
367
                        } else if (a % 3 == 1) {
368
                            Cube_Set_Pixel(i,j,k,GREEN);
369
                        } else {
370
                            Cube_Set_Pixel(i,j,k,BLUE);
371
                        }
372
                        Delay_MS(delay_ms);
373
                    }
374
                }
375
            }
376
        }
377
    }
378
}
379
 
380
void Animation_Pseudo_Random_Colors(int iterations, int delay_ms) {
381
    int i,j,k,z;
382
    for (z = 0; z < iterations; z++) {
383
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
384
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
385
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
386
                    unsigned int a = rand();
387
                    if (a % 5 == 0)
388
                        Cube_Set_Pixel(i,j,k,RED);
389
                    else if (a % 5 == 1)
390
                        Cube_Set_Pixel(i,j,k,GREEN);
391
                    else if (a % 5 == 2)
392
                        Cube_Set_Pixel(i,j,k,BLUE);
393
                    else if (a % 5 == 3)
394
                        Cube_Set_Pixel(i,j,k,PURPLE);
395
                    else if (a % 5 == 4)
396
                        Cube_Set_Pixel(i,j,k,YELLOW);
397
                    else
398
                        Cube_Set_Pixel(i,j,k,ORANGE);
399
                }
400
            }
401
        }
402
        Delay_MS(delay_ms);
403
    }
404
}
405
 
406
void Animation_Random_Colors(int iterations, int delay_ms) {
407
    int i,j,k,z;
408
    for (z = 0; z < iterations; z++) {
409
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
410
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
411
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
412
                    Cube_Set_Pixel(i,j,k,rand()&0x0FF,rand()&0x0FF,rand()&0x0FF);
413
                }
414
            }
415
        }
416
        Delay_MS(delay_ms);
417
    }
418
}
419
 
420
void Animation_Cube_In_Cube(int iterations, int delay_ms) {
421
    int z,x,i,j,k;
422
    for (z = 0; z < iterations; z++) {
423
        for (x = 0; x < 5; x++) {
424
            Cube_Clear();
425
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
426
                if ((x == 0 || x == 4)&&(i == 0 || i == 7)) {
427
                    Cube_Set_Layer(i,RED);
428
                } else if ((x == 1 || x == 4)&&(i == 1 || i == 6)) {
429
                    for (j = 1; j < CUBE_ROW_COUNT-1; j++)
430
                        for (k = 1; k < CUBE_COLUMN_COUNT-1; k++)
431
                            Cube_Set_Pixel(i,j,k,YELLOW);
432
                } else if ((x == 2 || x == 4)&&(i == 2 || i == 5)) {
433
                    for (j = 2; j < CUBE_ROW_COUNT-2; j++)
434
                        for (k = 2; k < CUBE_COLUMN_COUNT-2; k++)
435
                            Cube_Set_Pixel(i,j,k,GREEN);
436
                } else if ((x == 3 || x == 4)&&(i == 3 || i == 4)) {
437
                    for (j = 3; j < CUBE_ROW_COUNT-3; j++)
438
                        for (k = 3; k < CUBE_COLUMN_COUNT-3; k++)
439
                            Cube_Set_Pixel(i,j,k,BLUE);
440
                }
441
 
442
                if ((x == 0 || x == 4)&&(i > 0 && i < 8)) {
443
                    for (j = 0; j < 8; j++) {
444
                        Cube_Set_Pixel(i,j,0,RED);
445
                        Cube_Set_Pixel(i,j,7,RED);
446
                        Cube_Set_Pixel(i,0,j,RED);
447
                        Cube_Set_Pixel(i,7,j,RED);
448
                    }
449
                }
450
                if ((x == 1 || x == 4)&&(i > 1 && i < 7)) {
451
                    for (j = 1; j < 7; j++) {
452
                        Cube_Set_Pixel(i,j,1,YELLOW);
453
                        Cube_Set_Pixel(i,j,6,YELLOW);
454
                        Cube_Set_Pixel(i,1,j,YELLOW);
455
                        Cube_Set_Pixel(i,6,j,YELLOW);
456
                    }
457
                }
458
                if ((x == 2 || x == 4)&&(i > 2 && i < 6)) {
459
                    for (j = 2; j < 6; j++) {
460
                        Cube_Set_Pixel(i,j,2,GREEN);
461
                        Cube_Set_Pixel(i,j,5,GREEN);
462
                        Cube_Set_Pixel(i,2,j,GREEN);
463
                        Cube_Set_Pixel(i,5,j,GREEN);
464
                    }
465
                }
466
            }
467
            Delay_MS(delay_ms);
468
        }
469
    }
470
}