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 */
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"
206 Kevin 42
#include "TIMER4.h"
199 Kevin 43
#include "TIMER5.h"
44
#include "CUBE.h"
200 Kevin 45
#include "BTN.h"
226 Kevin 46
#include "ANIMATIONS.h"
193 Kevin 47
 
200 Kevin 48
void BTN1_Interrupt(void);
49
void BTN2_Interrupt(void);
206 Kevin 50
void BTN3_Interrupt(void);
199 Kevin 51
 
231 Kevin 52
void Delay_MS(uint32_t delay_ms) {
201 Kevin 53
    // Delays the CPU for the given amount of time.
54
    // Note: Watch out for integer overflow! (max delay_ms = 107374) ??
231 Kevin 55
    uint32_t delay = delay_ms * MS_TO_CT_TICKS;
56
    uint32_t startTime = ReadCoreTimer();
57
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 58
}
59
 
231 Kevin 60
void Delay_US(uint32_t delay_us) {
201 Kevin 61
    // Delays the CPU for the given amount of time.
62
    // Note: Watch out for integer overflow!
231 Kevin 63
    uint32_t delay = delay_us * US_TO_CT_TICKS;
64
    uint32_t startTime = ReadCoreTimer();
65
    while ((uint32_t)(ReadCoreTimer() - startTime) < delay) {};
199 Kevin 66
}
67
 
231 Kevin 68
int32_t main() {
226 Kevin 69
    // WARNING!! THIS BOARD WILL RESET EVERY 1048.576s DUE TO THE WDT!!
70
 
193 Kevin 71
    /* Configure the target for maximum performance at 80 MHz. */
199 Kevin 72
    // Note: This overrides the peripheral clock to 80Mhz regardless of config
73
    SYSTEMConfigPerformance(CPU_CLOCK_HZ);
193 Kevin 74
 
199 Kevin 75
    // Configure the interrupts for multiple vectors
193 Kevin 76
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
77
 
199 Kevin 78
    // Set all analog I/O pins to digital
79
    AD1PCFGSET = 0xFFFF;
193 Kevin 80
 
212 Kevin 81
    // Initialize the SPI1 module
226 Kevin 82
    SPI1_DATA spi_1_data;
83
    SPI1_Init(&spi_1_data, NULL);
199 Kevin 84
 
226 Kevin 85
    // Initialize the SPI4 module
86
    SPI4_DATA spi_4_data;
87
    SPI4_Init(&spi_4_data);
88
 
212 Kevin 89
    // Initialize the UART1 module
90
    UART1_DATA uart_data;
91
    UART1_Init(&uart_data, &Cube_Data_In);
92
 
93
    // Initializs the PWM2 output to 20MHz
199 Kevin 94
    PWM2_Init();
95
    PWM2_Start();
96
 
212 Kevin 97
    // Initialize the cube variables
199 Kevin 98
    CUBE_DATA cube_data;
206 Kevin 99
    Cube_Init(&cube_data, 0x40);
199 Kevin 100
 
207 Kevin 101
    // Start the cube update layer interrupt
199 Kevin 102
    // 2083 = 60Hz, 500 = 250Hz, 250 = 500Hz
206 Kevin 103
    TIMER5_DATA timer_5_data;
104
    TIMER5_Init(&timer_5_data, &Cube_Timer_Interrupt, 500);
199 Kevin 105
    TIMER5_Start();
106
 
207 Kevin 107
    // Start the overlay rotation interrupt
206 Kevin 108
    TIMER4_DATA timer_4_data;
226 Kevin 109
    TIMER4_Init(&timer_4_data, &Cube_Text_Interrupt, 90000);
206 Kevin 110
 
207 Kevin 111
    // Process button inputs
201 Kevin 112
    BTN_DATA btn_data;
208 Kevin 113
    BTN_Init(&btn_data, &BTN1_Interrupt, &BTN2_Interrupt, &BTN3_Interrupt);
205 Kevin 114
 
201 Kevin 115
    // Begin display
200 Kevin 116
 
208 Kevin 117
//    Cube_Set_All(RED);
226 Kevin 118
//    Delay_MS(2000);
208 Kevin 119
//    Cube_Set_All(GREEN);
226 Kevin 120
//    Delay_MS(2000);
208 Kevin 121
//    Cube_Set_All(BLUE);
226 Kevin 122
//    Delay_MS(2000);
123
//    Animation_Pseudo_Random_Colors(10,300);
207 Kevin 124
 
231 Kevin 125
//    int8_t start_text[] = "Cube Initialized\r\n";
215 Kevin 126
//    UART1_Write(start_text, 18);
212 Kevin 127
 
206 Kevin 128
    // Set the overlay text
231 Kevin 129
    uint8_t text_string[] = "Welcome to the AMP Lab     ";
226 Kevin 130
    Cube_Text_Init(text_string, 27, 0xFF, 0xFF, 0xFF);
216 Kevin 131
    TIMER4_Start();
206 Kevin 132
 
199 Kevin 133
    // Loop through some preset animations
134
    while(1) {
207 Kevin 135
//        Animation_Solid_Colors(2,300);
136
//        Animation_Layer_Alternate(2,300);
137
//        Animation_Pixel_Alternate(1,200);
138
//        Animation_Full_Color_Sweep(2,1000);
216 Kevin 139
        Animation_Row_Column_Sweep(2,40);
140
        Animation_Cube_In_Cube(4,300);
141
        Animation_Double_Rotation(2,40);
226 Kevin 142
//        Animation_Pseudo_Random_Colors(10,300);
207 Kevin 143
//        Animation_Random_Colors(10,300);
226 Kevin 144
 
145
//        ClearWDT(); // Clear the WDT if we dont want the board to reset
199 Kevin 146
    }
193 Kevin 147
}
148
 
200 Kevin 149
// Function call on button 1 press to change refresh rate
150
void BTN1_Interrupt(void) {
231 Kevin 151
    static uint8_t state;
206 Kevin 152
    state = (state == 4) ? 0 : state + 1;
200 Kevin 153
    TIMER5_Stop();
154
    switch (state) {
155
        case 0:
207 Kevin 156
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 500); // 250Hz
200 Kevin 157
            break;
158
        case 1:
207 Kevin 159
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 2083); // 60Hz
200 Kevin 160
            break;
161
        case 2:
207 Kevin 162
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 4166); // 30Hz
200 Kevin 163
            break;
164
        case 3:
207 Kevin 165
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 12498); // 10Hz
200 Kevin 166
            break;
206 Kevin 167
        case 4:
207 Kevin 168
            TIMER5_Init(NULL, &Cube_Timer_Interrupt, 24996); // 5Hz
206 Kevin 169
            break;
200 Kevin 170
    }
171
    TIMER5_Start();
172
}
173
 
174
// Function call on button 2 press to change brightness
175
void BTN2_Interrupt(void) {
231 Kevin 176
    static uint8_t state;
205 Kevin 177
    state = (state == 6) ? 0 : state + 1;
200 Kevin 178
    TIMER5_Stop();
179
    Delay_MS(1); // Need to wait for all SPI writes to complete
231 Kevin 180
    uint8_t BC;
200 Kevin 181
    switch (state) {
182
        case 0:
183
            BC = 0x01;
184
            break;
185
        case 1:
186
            BC = 0x08;
187
            break;
188
        case 2:
189
            BC = 0x10;
190
            break;
191
        case 3:
192
            BC = 0x20;
193
            break;
194
        case 4:
195
            BC = 0x40;
196
            break;
197
        case 5:
205 Kevin 198
            BC = 0x80;
200 Kevin 199
            break;
205 Kevin 200
        case 6:
201
            BC = 0xFF;
202
            break;
200 Kevin 203
    }
204
    Cube_Write_DCS(BC);
205
    TIMER5_Start();
206
}
207
 
206 Kevin 208
// Function call on button 3 press to change text scroll speed
209
void BTN3_Interrupt(void)  {
231 Kevin 210
    static uint8_t state;
206 Kevin 211
    state = (state == 4) ? 0 : state + 1;
212
    TIMER4_Stop();
213
    switch (state) {
214
        case 0:
215
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 209712);
216
            break;
217
        case 1:
218
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 180000);
219
            break;
220
        case 2:
221
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 150000);
222
            break;
223
        case 3:
207 Kevin 224
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 120000);
206 Kevin 225
            break;
226
        case 4:
207 Kevin 227
            TIMER4_Init(NULL, &Cube_Text_Interrupt, 90000);
206 Kevin 228
            break;
229
    }
230
    TIMER4_Start();
231
}