| 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 */
|
| 226 |
Kevin |
23 |
#pragma config FWDTEN = ON // Watchdog Timer Enable
|
|
|
24 |
#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1048.576s)
|
| 193 |
Kevin |
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 "defines.h"
|
| 212 |
Kevin |
39 |
#include "UART1.h"
|
| 199 |
Kevin |
40 |
#include "SPI1.h"
|
| 226 |
Kevin |
41 |
#include "SPI4.h"
|
| 234 |
Kevin |
42 |
#include "I2C1.h"
|
| 206 |
Kevin |
43 |
#include "TIMER4.h"
|
| 199 |
Kevin |
44 |
#include "TIMER5.h"
|
|
|
45 |
#include "CUBE.h"
|
| 200 |
Kevin |
46 |
#include "BTN.h"
|
| 226 |
Kevin |
47 |
#include "ANIMATIONS.h"
|
| 193 |
Kevin |
48 |
|
| 200 |
Kevin |
49 |
void BTN1_Interrupt(void);
|
|
|
50 |
void BTN2_Interrupt(void);
|
| 206 |
Kevin |
51 |
void BTN3_Interrupt(void);
|
| 199 |
Kevin |
52 |
|
| 231 |
Kevin |
53 |
void Delay_MS(uint32_t delay_ms) {
|
| 201 |
Kevin |
54 |
// Delays the CPU for the given amount of time.
|
|
|
55 |
// Note: Watch out for integer overflow! (max delay_ms = 107374) ??
|
| 231 |
Kevin |
56 |
uint32_t delay = delay_ms * MS_TO_CT_TICKS;
|
|
|
57 |
uint32_t startTime = ReadCoreTimer();
|
|
|
58 |
while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
|
| 199 |
Kevin |
59 |
}
|
|
|
60 |
|
| 231 |
Kevin |
61 |
void Delay_US(uint32_t delay_us) {
|
| 201 |
Kevin |
62 |
// Delays the CPU for the given amount of time.
|
|
|
63 |
// Note: Watch out for integer overflow!
|
| 231 |
Kevin |
64 |
uint32_t delay = delay_us * US_TO_CT_TICKS;
|
|
|
65 |
uint32_t startTime = ReadCoreTimer();
|
|
|
66 |
while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
|
| 199 |
Kevin |
67 |
}
|
|
|
68 |
|
| 231 |
Kevin |
69 |
int32_t main() {
|
| 226 |
Kevin |
70 |
// WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
|
|
|
71 |
|
| 193 |
Kevin |
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 |
|
| 234 |
Kevin |
82 |
LED1_TRIS = 0;
|
|
|
83 |
LED2_TRIS = 0;
|
|
|
84 |
LED3_TRIS = 0;
|
|
|
85 |
LED4_TRIS = 0;
|
|
|
86 |
LED1_LAT = 0;
|
|
|
87 |
LED2_LAT = 0;
|
|
|
88 |
LED3_LAT = 0;
|
|
|
89 |
LED4_LAT = 0;
|
|
|
90 |
|
| 212 |
Kevin |
91 |
// Initialize the SPI1 module
|
| 226 |
Kevin |
92 |
SPI1_DATA spi_1_data;
|
|
|
93 |
SPI1_Init(&spi_1_data, NULL);
|
| 199 |
Kevin |
94 |
|
| 226 |
Kevin |
95 |
// Initialize the SPI4 module
|
|
|
96 |
SPI4_DATA spi_4_data;
|
|
|
97 |
SPI4_Init(&spi_4_data);
|
|
|
98 |
|
| 234 |
Kevin |
99 |
I2C1_DATA i2c_1_data;
|
|
|
100 |
I2C1_Init(&i2c_1_data, I2C1_400KHZ, 0x20);
|
|
|
101 |
|
| 212 |
Kevin |
102 |
// Initialize the UART1 module
|
|
|
103 |
UART1_DATA uart_data;
|
|
|
104 |
UART1_Init(&uart_data, &Cube_Data_In);
|
|
|
105 |
|
|
|
106 |
// Initializs the PWM2 output to 20MHz
|
| 199 |
Kevin |
107 |
PWM2_Init();
|
|
|
108 |
PWM2_Start();
|
|
|
109 |
|
| 212 |
Kevin |
110 |
// Initialize the cube variables
|
| 199 |
Kevin |
111 |
CUBE_DATA cube_data;
|
| 206 |
Kevin |
112 |
Cube_Init(&cube_data, 0x40);
|
| 199 |
Kevin |
113 |
|
| 207 |
Kevin |
114 |
// Start the cube update layer interrupt
|
| 199 |
Kevin |
115 |
// 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
|
| 206 |
Kevin |
116 |
TIMER5_DATA timer_5_data;
|
|
|
117 |
TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
|
| 199 |
Kevin |
118 |
TIMER5_Start();
|
|
|
119 |
|
| 207 |
Kevin |
120 |
// Start the overlay rotation interrupt
|
| 206 |
Kevin |
121 |
TIMER4_DATA timer_4_data;
|
| 226 |
Kevin |
122 |
TIMER4_Init(&timer_4_data, &Cube_Text_Interrupt, 90000);
|
| 206 |
Kevin |
123 |
|
| 207 |
Kevin |
124 |
// Process button inputs
|
| 201 |
Kevin |
125 |
BTN_DATA btn_data;
|
| 208 |
Kevin |
126 |
BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, &BTN3_Interrupt);
|
| 234 |
Kevin |
127 |
|
| 201 |
Kevin |
128 |
// Begin display
|
| 200 |
Kevin |
129 |
|
| 208 |
Kevin |
130 |
// Cube_Set_All(RED);
|
| 226 |
Kevin |
131 |
// Delay_MS(2000);
|
| 208 |
Kevin |
132 |
// Cube_Set_All(GREEN);
|
| 226 |
Kevin |
133 |
// Delay_MS(2000);
|
| 208 |
Kevin |
134 |
// Cube_Set_All(BLUE);
|
| 226 |
Kevin |
135 |
// Delay_MS(2000);
|
|
|
136 |
// Animation_Pseudo_Random_Colors(10,300);
|
| 207 |
Kevin |
137 |
|
| 231 |
Kevin |
138 |
// int8_t start_text[] = "Cube Initialized\r\n";
|
| 215 |
Kevin |
139 |
// UART1_Write(start_text, 18);
|
| 212 |
Kevin |
140 |
|
| 206 |
Kevin |
141 |
// Set the overlay text
|
| 231 |
Kevin |
142 |
uint8_t text_string[] = "Welcome to the AMP Lab ";
|
| 226 |
Kevin |
143 |
Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
|
| 216 |
Kevin |
144 |
TIMER4_Start();
|
| 206 |
Kevin |
145 |
|
| 199 |
Kevin |
146 |
// Loop through some preset animations
|
| 234 |
Kevin |
147 |
uint8_t buffer1[2];
|
|
|
148 |
uint8_t buffer2[2];
|
|
|
149 |
uint8_t result, length;
|
|
|
150 |
|
| 199 |
Kevin |
151 |
while(1) {
|
| 234 |
Kevin |
152 |
// I2C1_Master_Restart(0x24, 0xA, 1);
|
|
|
153 |
// do {
|
|
|
154 |
// result = I2C1_Get_Status();
|
|
|
155 |
// } while (!result);
|
|
|
156 |
// length = I2C1_Read_Buffer(buffer1);
|
|
|
157 |
// buffer1[0] = ~buffer1[0];
|
|
|
158 |
//
|
|
|
159 |
//
|
|
|
160 |
// buffer1[1] = buffer1[0];
|
|
|
161 |
// buffer1[0] = 0xB;
|
|
|
162 |
// I2C1_Master_Send(0x24, buffer1, 2);
|
|
|
163 |
// do {
|
|
|
164 |
// result = I2C1_Get_Status();
|
|
|
165 |
// } while (!result);
|
|
|
166 |
|
|
|
167 |
I2C1_Master_Restart(0x25, 0xA, 1);
|
|
|
168 |
do {
|
|
|
169 |
result = I2C1_Get_Status();
|
|
|
170 |
} while (!result);
|
|
|
171 |
length = I2C1_Read_Buffer(buffer2);
|
|
|
172 |
buffer2[0] = ~buffer2[0];
|
|
|
173 |
|
|
|
174 |
buffer2[1] = buffer2[0];
|
|
|
175 |
buffer2[0] = 0xB;
|
|
|
176 |
I2C1_Master_Send(0x25, buffer2, 2);
|
|
|
177 |
|
|
|
178 |
Delay_MS(1);
|
|
|
179 |
|
|
|
180 |
// do {
|
|
|
181 |
// result = I2C1_Get_Status();
|
|
|
182 |
// } while (!result);
|
|
|
183 |
// length = I2C1_Read_Buffer(buffer);
|
|
|
184 |
|
| 207 |
Kevin |
185 |
// Animation_Solid_Colors(2,300);
|
|
|
186 |
// Animation_Layer_Alternate(2,300);
|
|
|
187 |
// Animation_Pixel_Alternate(1,200);
|
|
|
188 |
// Animation_Full_Color_Sweep(2,1000);
|
| 234 |
Kevin |
189 |
// Animation_Row_Column_Sweep(2,40);
|
|
|
190 |
// Animation_Cube_In_Cube(4,300);
|
|
|
191 |
// Animation_Double_Rotation(2,40);
|
| 226 |
Kevin |
192 |
// Animation_Pseudo_Random_Colors(10,300);
|
| 207 |
Kevin |
193 |
// Animation_Random_Colors(10,300);
|
| 226 |
Kevin |
194 |
|
|
|
195 |
// ClearWDT(); // Clear the WDT if we dont want the board to reset
|
| 199 |
Kevin |
196 |
}
|
| 193 |
Kevin |
197 |
}
|
|
|
198 |
|
| 200 |
Kevin |
199 |
// Function call on button 1 press to change refresh rate
|
|
|
200 |
void BTN1_Interrupt(void) {
|
| 231 |
Kevin |
201 |
static uint8_t state;
|
| 206 |
Kevin |
202 |
state = (state == 4) ? 0 : state + 1;
|
| 200 |
Kevin |
203 |
TIMER5_Stop();
|
|
|
204 |
switch (state) {
|
|
|
205 |
case 0:
|
| 207 |
Kevin |
206 |
TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
|
| 200 |
Kevin |
207 |
break;
|
|
|
208 |
case 1:
|
| 207 |
Kevin |
209 |
TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
|
| 200 |
Kevin |
210 |
break;
|
|
|
211 |
case 2:
|
| 207 |
Kevin |
212 |
TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
|
| 200 |
Kevin |
213 |
break;
|
|
|
214 |
case 3:
|
| 207 |
Kevin |
215 |
TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
|
| 200 |
Kevin |
216 |
break;
|
| 206 |
Kevin |
217 |
case 4:
|
| 207 |
Kevin |
218 |
TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
|
| 206 |
Kevin |
219 |
break;
|
| 200 |
Kevin |
220 |
}
|
|
|
221 |
TIMER5_Start();
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
// Function call on button 2 press to change brightness
|
|
|
225 |
void BTN2_Interrupt(void) {
|
| 231 |
Kevin |
226 |
static uint8_t state;
|
| 205 |
Kevin |
227 |
state = (state == 6) ? 0 : state + 1;
|
| 200 |
Kevin |
228 |
TIMER5_Stop();
|
|
|
229 |
Delay_MS(1); // Need to wait for all SPI writes to complete
|
| 231 |
Kevin |
230 |
uint8_t BC;
|
| 200 |
Kevin |
231 |
switch (state) {
|
|
|
232 |
case 0:
|
|
|
233 |
BC = 0x01;
|
|
|
234 |
break;
|
|
|
235 |
case 1:
|
|
|
236 |
BC = 0x08;
|
|
|
237 |
break;
|
|
|
238 |
case 2:
|
|
|
239 |
BC = 0x10;
|
|
|
240 |
break;
|
|
|
241 |
case 3:
|
|
|
242 |
BC = 0x20;
|
|
|
243 |
break;
|
|
|
244 |
case 4:
|
|
|
245 |
BC = 0x40;
|
|
|
246 |
break;
|
|
|
247 |
case 5:
|
| 205 |
Kevin |
248 |
BC = 0x80;
|
| 200 |
Kevin |
249 |
break;
|
| 205 |
Kevin |
250 |
case 6:
|
|
|
251 |
BC = 0xFF;
|
|
|
252 |
break;
|
| 200 |
Kevin |
253 |
}
|
|
|
254 |
Cube_Write_DCS(BC);
|
|
|
255 |
TIMER5_Start();
|
|
|
256 |
}
|
|
|
257 |
|
| 206 |
Kevin |
258 |
// Function call on button 3 press to change text scroll speed
|
|
|
259 |
void BTN3_Interrupt(void) {
|
| 231 |
Kevin |
260 |
static uint8_t state;
|
| 206 |
Kevin |
261 |
state = (state == 4) ? 0 : state + 1;
|
|
|
262 |
TIMER4_Stop();
|
|
|
263 |
switch (state) {
|
|
|
264 |
case 0:
|
|
|
265 |
TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
|
|
|
266 |
break;
|
|
|
267 |
case 1:
|
|
|
268 |
TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
|
|
|
269 |
break;
|
|
|
270 |
case 2:
|
|
|
271 |
TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
|
|
|
272 |
break;
|
|
|
273 |
case 3:
|
| 207 |
Kevin |
274 |
TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
|
| 206 |
Kevin |
275 |
break;
|
|
|
276 |
case 4:
|
| 207 |
Kevin |
277 |
TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
|
| 206 |
Kevin |
278 |
break;
|
|
|
279 |
}
|
|
|
280 |
TIMER4_Start();
|
|
|
281 |
}
|