Subversion Repositories Code-Repo

Rev

Rev 193 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 193 Rev 199
Line -... Line 1...
-
 
1
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
1
/* ------------------------------------------------------------ */
2
/* ------------------------------------------------------------ */
2
/* PIC32 Configuration Settings */
3
/* PIC32 Configuration Settings */
3
/* ------------------------------------------------------------ */
4
/* ------------------------------------------------------------ */
4
/* Oscillator Settings */
5
/* Oscillator Settings */
5
#pragma config FNOSC     = PRIPLL   // Oscillator Selection Bits
6
#pragma config FNOSC     = PRIPLL   // Oscillator Selection Bits
6
#pragma config POSCMOD   = EC       // Primary Oscillator Configuration
7
#pragma config POSCMOD   = EC       // Primary Oscillator Configuration
7
#pragma config FPLLIDIV  = DIV_2    // PLL Input Divider
8
#pragma config FPLLIDIV  = DIV_2    // PLL Input Divider
8
#pragma config FPLLMUL   = MUL_20   // PLL Multiplier
9
#pragma config FPLLMUL   = MUL_20   // PLL Multiplier
9
#pragma config FPLLODIV  = DIV_1    // PLL Output Divider
10
#pragma config FPLLODIV  = DIV_1    // PLL Output Divider
10
#pragma config FPBDIV    = DIV_8    // Peripheral Clock Divisor (timers/UART/SPI/I2C)
11
#pragma config FPBDIV    = DIV_1    // Peripheral Clock Divisor (timers/UART/SPI/I2C)
11
#pragma config FSOSCEN   = OFF      // Secondary Oscillator Enable
12
#pragma config FSOSCEN   = OFF      // Secondary Oscillator Enable
12
/* Clock Control Settings */
13
/* Clock Control Settings */
13
#pragma config IESO      = OFF      // Internal/External Clock Switch Over
14
#pragma config IESO      = OFF      // Internal/External Clock Switch Over
14
#pragma config FCKSM     = CSDCMD   // Clock Switching and Monitor Selection
15
#pragma config FCKSM     = CSDCMD   // Clock Switching and Monitor Selection
15
#pragma config OSCIOFNC  = OFF      // CLKO Output Signal Active on the OSCO Pin
16
#pragma config OSCIOFNC  = OFF      // CLKO Output Signal Active on the OSCO Pin
Line 30... Line 31...
30
#pragma config BWP       = OFF      // Boot Flash Write Protect
31
#pragma config BWP       = OFF      // Boot Flash Write Protect
31
#pragma config PWP       = OFF      // Program Flash Write Protect
32
#pragma config PWP       = OFF      // Program Flash Write Protect
32
/* Debug Settings */
33
/* Debug Settings */
33
#pragma config ICESEL = ICS_PGx1    // ICE/ICD Comm Channel Select (on-board debugger)
34
#pragma config ICESEL = ICS_PGx1    // ICE/ICD Comm Channel Select (on-board debugger)
34
/* ------------------------------------------------------------ */
35
/* ------------------------------------------------------------ */
-
 
36
// </editor-fold>
35
 
37
 
36
#include <xc.h>
38
#include <xc.h>
37
#include <plib.h>
39
#include <plib.h>
-
 
40
#include <stdlib.h>
38
#include "defines.h"
41
#include "defines.h"
-
 
42
#include "SPI1.h"
-
 
43
#include "TIMER5.h"
-
 
44
#include "CUBE.h"
-
 
45
 
-
 
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
}
39
 
67
 
40
int main() {
68
int main() {
41
    /* Configure the target for maximum performance at 80 MHz. */
69
    /* Configure the target for maximum performance at 80 MHz. */
-
 
70
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
42
    SYSTEMConfigPerformance(80000000UL);
71
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
43
 
72
 
-
 
73
    // Configure the interrupts for multiple vectors
44
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
74
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
45
 
75
 
-
 
76
    // Set all analog I/O pins to digital
46
    LED1_TRIS = 0;
77
    AD1PCFGSET = 0xFFFF;
-
 
78
 
-
 
79
    SPI1_DATA spi_data;
-
 
80
    SPI1_Init(&spi_data);
-
 
81
 
47
    LED2_TRIS = 0;
82
    PWM2_Init();
48
    LED3_TRIS = 0;
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);
49
    LED4_TRIS = 0;
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
    }
-
 
103
}
-
 
104
 
-
 
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) {
50
    LED1_PORT = 1;
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);
51
    LED2_PORT = 1;
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);
52
    LED3_PORT = 1;
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);
53
    LED4_PORT = 1;
190
                }
-
 
191
            }
-
 
192
            Delay_MS(delay_ms);
-
 
193
        }
-
 
194
    }
-
 
195
}
54
 
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);
55
    while(1) {}
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
    }
56
}
233
}
57
 
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
}
58
402