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"
193 Kevin 45
 
199 Kevin 46
void Animation_Solid_Colors(int iterations, int delay_ms);
47
void Animation_Layer_Alternate(int iterations, int delay_ms);
48
void Animation_Pixel_Alternate(int iterations, int delay_ms);
49
void Animation_Full_Color_Sweep(int iterations, int delay_us);
50
void Animation_Row_Column_Sweep(int iterations, int delay_ms);
51
void Animation_Pixel_Sweep(int iterations, int delay_ms);
52
void Animation_Pseudo_Random_Colors(int iterations,int delay_ms);
53
void Animation_Random_Colors(int iterations, int delay_ms);
54
void Animation_Cube_In_Cube(int iterations, int delay_ms);
55
 
56
void Delay_MS(unsigned int delay_ms) {
57
    unsigned int delay = delay_ms * MS_TO_CT_TICKS;
58
    unsigned int startTime = ReadCoreTimer();
59
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
60
}
61
 
62
void Delay_US(unsigned int delay_us) {
63
    unsigned int delay = delay_us * US_TO_CT_TICKS;
64
    unsigned int startTime = ReadCoreTimer();
65
    while ((unsigned int)(ReadCoreTimer() - startTime) < delay) {};
66
}
67
 
193 Kevin 68
int main() {
69
    /* Configure the target for maximum performance at 80 MHz. */
199 Kevin 70
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
71
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 72
 
199 Kevin 73
    // Configure the interrupts for multiple vectors
193 Kevin 74
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
75
 
199 Kevin 76
    // Set all analog I/O pins to digital
77
    AD1PCFGSET = 0xFFFF;
193 Kevin 78
 
199 Kevin 79
    SPI1_DATA spi_data;
80
    SPI1_Init(&spi_data);
81
 
82
    PWM2_Init();
83
    PWM2_Start();
84
 
85
    CUBE_DATA cube_data;
86
    Cube_Init(&cube_data);
87
 
88
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
89
    TIMER5_Init(&Cube_Timer_Interrupt, 200);
90
    TIMER5_Start();
91
 
92
    // Loop through some preset animations
93
    while(1) {
94
        Animation_Solid_Colors(2,300);
95
        Animation_Layer_Alternate(2,300);
96
        Animation_Pixel_Alternate(1,200);
97
        Animation_Full_Color_Sweep(2,1000);
98
        Animation_Row_Column_Sweep(2,40);
99
        Animation_Pseudo_Random_Colors(10,300);
100
        Animation_Random_Colors(10,300);
101
        Animation_Cube_In_Cube(4,300);
102
    }
193 Kevin 103
}
104
 
199 Kevin 105
void Animation_Solid_Colors(int iterations, int delay_ms) {
106
    int i;
107
    for (i = 0; i < iterations; i++) {
108
        Cube_Set_All(RED);
109
        Delay_MS(delay_ms);
110
        Cube_Set_All(GREEN);
111
        Delay_MS(delay_ms);
112
        Cube_Set_All(BLUE);
113
        Delay_MS(delay_ms);
114
    }
115
}
116
 
117
void Animation_Layer_Alternate(int iterations, int delay_ms) {
118
    int i,z;
119
    for (z = 0; z < iterations; z++) {
120
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
121
            if (i % 3 == 0)
122
                Cube_Set_Layer(i,RED);
123
            else if (i % 3 == 1)
124
                Cube_Set_Layer(i,GREEN);
125
            else
126
                Cube_Set_Layer(i,BLUE);
127
        }
128
        Delay_MS(delay_ms);
129
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
130
            if (i % 3 == 0)
131
                Cube_Set_Layer(i,GREEN);
132
            else if (i % 3 == 1)
133
                Cube_Set_Layer(i,BLUE);
134
            else
135
                Cube_Set_Layer(i,RED);
136
        }
137
        Delay_MS(delay_ms);
138
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
139
            if (i % 3 == 0)
140
                Cube_Set_Layer(i,BLUE);
141
            else if (i % 3 == 1)
142
                Cube_Set_Layer(i,RED);
143
            else
144
                Cube_Set_Layer(i,GREEN);
145
        }
146
        Delay_MS(delay_ms);
147
    }
148
}
149
 
150
void Animation_Pixel_Alternate(int iterations, int delay_ms) {
151
    int i,j,k,z;
152
    for (z = 0; z < iterations; z++) {
153
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
154
            Cube_Clear();
155
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
156
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
157
                    int var = (j * 8) + k;
158
                    if (var % 3 == 0)
159
                        Cube_Set_Pixel(i,j,k,RED);
160
                    else if (var % 3 == 1)
161
                        Cube_Set_Pixel(i,j,k,GREEN);
162
                    else
163
                        Cube_Set_Pixel(i,j,k,BLUE);
164
                }
165
            }
166
            Delay_MS(delay_ms);
167
            Cube_Clear();
168
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
169
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
170
                    int var = (j * 8) + k;
171
                    if (var % 3 == 0)
172
                        Cube_Set_Pixel(i,j,k,GREEN);
173
                    else if (var % 3 == 1)
174
                        Cube_Set_Pixel(i,j,k,BLUE);
175
                    else
176
                        Cube_Set_Pixel(i,j,k,RED);
177
                }
178
            }
179
            Delay_MS(delay_ms);
180
            Cube_Clear();
181
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
182
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
183
                    int var = (j * 8) + k;
184
                    if (var % 3 == 0)
185
                        Cube_Set_Pixel(i,j,k,BLUE);
186
                    else if (var % 3 == 1)
187
                        Cube_Set_Pixel(i,j,k,RED);
188
                    else
189
                        Cube_Set_Pixel(i,j,k,GREEN);
190
                }
191
            }
192
            Delay_MS(delay_ms);
193
        }
194
    }
195
}
196
 
197
void Animation_Full_Color_Sweep(int iterations, int delay_us) {
198
    int i,z;
199
    for (z = 0; z < iterations; z++) {
200
        for (i = 0; i < 0x0FF; i+=2) {
201
            Cube_Set_All(i,0,0);
202
            Delay_US(delay_us);
203
        }
204
        for (i = 0; i < 0x0FF; i+=2) {
205
            Cube_Set_All(0x0FF,i,0);
206
            Delay_US(delay_us);
207
        }
208
        for (i = 0x0FF; i >= 0; i-=2) {
209
            Cube_Set_All(i,0x0FF,0);
210
            Delay_US(delay_us);
211
        }
212
        for (i = 0; i < 0x0FF; i+=2) {
213
            Cube_Set_All(0,0x0FF,i);
214
            Delay_US(delay_us);
215
        }
216
        for (i = 0; i < 0x0FF; i+=2) {
217
            Cube_Set_All(i,0x0FF,0x0FF);
218
            Delay_US(delay_us);
219
        }
220
        for (i = 0x0FF; i >= 0; i-=2) {
221
            Cube_Set_All(0x0FF,i,0x0FF);
222
            Delay_US(delay_us);
223
        }
224
        for (i = 0x0FF; i >= 0; i-=2) {
225
            Cube_Set_All(i,0,0x0FF);
226
            Delay_US(delay_us);
227
        }
228
        for (i = 0x100; i >= 0; i-=2) {
229
            Cube_Set_All(0,0,i);
230
            Delay_US(delay_us);
231
        }
232
    }
233
}
234
 
235
void Animation_Row_Column_Sweep(int iterations, int delay_ms) {
236
    int i,j,k,a,z;
237
    for (z = 0; z < iterations; z++) {
238
        for (i = 0; i < 3; i++) {
239
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
240
                Cube_Clear();
241
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
242
                    if (i % 3 == 0)
243
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
244
                            Cube_Set_Pixel(a,j,k,RED);
245
                    else if (i % 3 == 1)
246
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
247
                            Cube_Set_Pixel(a,j,k,GREEN);
248
                    else
249
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
250
                            Cube_Set_Pixel(a,j,k,BLUE);
251
                Delay_MS(delay_ms);
252
            }
253
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
254
                Cube_Clear();
255
                for (k = 0; k < CUBE_COLUMN_COUNT; k++)
256
                    if (i % 3 == 0)
257
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
258
                            Cube_Set_Pixel(a,k,j,RED);
259
                    else if (i % 3 == 1)
260
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
261
                            Cube_Set_Pixel(a,k,j,GREEN);
262
                    else
263
                        for (a = 0; a < CUBE_LAYER_COUNT; a++)
264
                            Cube_Set_Pixel(a,k,j,BLUE);
265
                Delay_MS(delay_ms);
266
            }
267
            for (j = CUBE_LAYER_COUNT-1; j >= 0; j--) {
268
                Cube_Clear();
269
                if (i % 3 == 0) {
270
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
271
                        if (k == j)
272
                            Cube_Set_Layer(k,RED);
273
                } else if (i % 3 == 1) {
274
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
275
                        if (k == j)
276
                            Cube_Set_Layer(k,GREEN);
277
                } else {
278
                    for (k = 0; k < CUBE_LAYER_COUNT; k++)
279
                        if (k == j)
280
                            Cube_Set_Layer(k,BLUE);
281
                }
282
                Delay_MS(delay_ms);
283
            }
284
        }
285
    }
286
}
287
 
288
void Animation_Pixel_Sweep(int iterations, int delay_ms) {
289
    int i,j,k,z,a;
290
    for (z = 0; z < iterations; z++) {
291
        for (a = 0; a < 3; a++) {
292
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
293
                for (j = 0; j < CUBE_ROW_COUNT; j++) {
294
                    for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
295
                        Cube_Clear();
296
                        if (a % 3 == 0) {
297
                            Cube_Set_Pixel(i,j,k,RED);
298
                        } else if (a % 3 == 1) {
299
                            Cube_Set_Pixel(i,j,k,GREEN);
300
                        } else {
301
                            Cube_Set_Pixel(i,j,k,BLUE);
302
                        }
303
                        Delay_MS(delay_ms);
304
                    }
305
                }
306
            }
307
        }
308
    }
309
}
310
 
311
void Animation_Pseudo_Random_Colors(int iterations, int delay_ms) {
312
    int i,j,k,z;
313
    for (z = 0; z < iterations; z++) {
314
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
315
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
316
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
317
                    unsigned int a = rand();
318
                    if (a % 5 == 0)
319
                        Cube_Set_Pixel(i,j,k,RED);
320
                    else if (a % 5 == 1)
321
                        Cube_Set_Pixel(i,j,k,GREEN);
322
                    else if (a % 5 == 2)
323
                        Cube_Set_Pixel(i,j,k,BLUE);
324
                    else if (a % 5 == 3)
325
                        Cube_Set_Pixel(i,j,k,PURPLE);
326
                    else if (a % 5 == 4)
327
                        Cube_Set_Pixel(i,j,k,YELLOW);
328
                    else
329
                        Cube_Set_Pixel(i,j,k,ORANGE);
330
                }
331
            }
332
        }
333
        Delay_MS(delay_ms);
334
    }
335
}
336
 
337
void Animation_Random_Colors(int iterations, int delay_ms) {
338
    int i,j,k,z;
339
    for (z = 0; z < iterations; z++) {
340
        for (i = 0; i < CUBE_LAYER_COUNT; i++) {
341
            for (j = 0; j < CUBE_ROW_COUNT; j++) {
342
                for (k = 0; k < CUBE_COLUMN_COUNT; k++) {
343
                    Cube_Set_Pixel(i,j,k,rand()&0x0FF,rand()&0x0FF,rand()&0x0FF);
344
                }
345
            }
346
        }
347
        Delay_MS(delay_ms);
348
    }
349
}
350
 
351
void Animation_Cube_In_Cube(int iterations, int delay_ms) {
352
    int z,x,i,j,k;
353
    for (z = 0; z < iterations; z++) {
354
        for (x = 0; x < 5; x++) {
355
            Cube_Clear();
356
            for (i = 0; i < CUBE_LAYER_COUNT; i++) {
357
                if ((x == 0 || x == 4)&&(i == 0 || i == 7)) {
358
                    Cube_Set_Layer(i,RED);
359
                } else if ((x == 1 || x == 4)&&(i == 1 || i == 6)) {
360
                    for (j = 1; j < CUBE_ROW_COUNT-1; j++)
361
                        for (k = 1; k < CUBE_COLUMN_COUNT-1; k++)
362
                            Cube_Set_Pixel(i,j,k,YELLOW);
363
                } else if ((x == 2 || x == 4)&&(i == 2 || i == 5)) {
364
                    for (j = 2; j < CUBE_ROW_COUNT-2; j++)
365
                        for (k = 2; k < CUBE_COLUMN_COUNT-2; k++)
366
                            Cube_Set_Pixel(i,j,k,GREEN);
367
                } else if ((x == 3 || x == 4)&&(i == 3 || i == 4)) {
368
                    for (j = 3; j < CUBE_ROW_COUNT-3; j++)
369
                        for (k = 3; k < CUBE_COLUMN_COUNT-3; k++)
370
                            Cube_Set_Pixel(i,j,k,BLUE);
371
                }
372
 
373
                if ((x == 0 || x == 4)&&(i > 0 && i < 8)) {
374
                    for (j = 0; j < 8; j++) {
375
                        Cube_Set_Pixel(i,j,0,RED);
376
                        Cube_Set_Pixel(i,j,7,RED);
377
                        Cube_Set_Pixel(i,0,j,RED);
378
                        Cube_Set_Pixel(i,7,j,RED);
379
                    }
380
                }
381
                if ((x == 1 || x == 4)&&(i > 1 && i < 7)) {
382
                    for (j = 1; j < 7; j++) {
383
                        Cube_Set_Pixel(i,j,1,YELLOW);
384
                        Cube_Set_Pixel(i,j,6,YELLOW);
385
                        Cube_Set_Pixel(i,1,j,YELLOW);
386
                        Cube_Set_Pixel(i,6,j,YELLOW);
387
                    }
388
                }
389
                if ((x == 2 || x == 4)&&(i > 2 && i < 6)) {
390
                    for (j = 2; j < 6; j++) {
391
                        Cube_Set_Pixel(i,j,2,GREEN);
392
                        Cube_Set_Pixel(i,j,5,GREEN);
393
                        Cube_Set_Pixel(i,2,j,GREEN);
394
                        Cube_Set_Pixel(i,5,j,GREEN);
395
                    }
396
                }
397
            }
398
            Delay_MS(delay_ms);
399
        }
400
    }
401
}