Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
197 Kevin 1
#include <xc.h>
202 Kevin 2
#include <string.h>
3
#include "main.h"
4
#include "TIMER.h"
5
#include "INTERRUPT.h"
6
#include "UART.h"
7
#include "I2C.h"
223 Kevin 8
#include "L3G.h"
9
#include "LSM303.h"
197 Kevin 10
 
202 Kevin 11
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
197 Kevin 12
// CONFIG1
13
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
14
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
15
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
16
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
17
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
18
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
19
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
20
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
21
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
22
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
23
 
24
// CONFIG2
25
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
202 Kevin 26
#pragma config PLLEN = ON       // PLL Enable (4x PLL disabled)
197 Kevin 27
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
28
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
202 Kevin 29
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
30
// </editor-fold>
197 Kevin 31
 
202 Kevin 32
UART_DATA uart_data;
33
I2C_DATA i2c_data;
34
 
222 Kevin 35
char LED_R_ON = 0;
36
char LED_G_ON = 0;
37
char LED_B_ON = 0;
223 Kevin 38
char timer_2_pwm = 0;
222 Kevin 39
 
202 Kevin 40
void Error(char ID) {
41
    while(1) {
42
        for (char i = 0; i < ID; i++) {
222 Kevin 43
            LED_R_ON = 1;
202 Kevin 44
            __delay_ms(120);
222 Kevin 45
            LED_R_ON = 0;
202 Kevin 46
            __delay_ms(150);
47
        }
48
        __delay_ms(1000);
49
    }
50
}
51
 
52
void Startup_Check(void) {
53
    char buffer[20];
54
    char result, length;
55
 
222 Kevin 56
//    BLE_RESET_LAT = 0;
57
//    __delay_ms(3000);
58
//    BLE_RESET_LAT = 1;
59
//    __delay_ms(200);
202 Kevin 60
 
222 Kevin 61
//    // Check BLE Module
62
//    length = UART_Read(buffer);
63
//    if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
64
//        Error(1);
65
//    }
66
//    UART_Write("AT\r", 3);
67
//    __delay_ms(10);
68
//    length = UART_Read(buffer);
69
//    if (memcmp(buffer, "\r\nOK\r\n", 6)) {
70
//        Error(1);
71
//    }
202 Kevin 72
 
73
    // Check Battery Gauge
74
    I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
75
    do {
76
        result = I2C_Get_Status();
77
    } while (!result);
78
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
79
        Error(2);
80
    }
81
    length = I2C_Read_Buffer(buffer);
82
    if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
83
        Error(2);
84
    }
222 Kevin 85
 
202 Kevin 86
    // Check Gyroscope
87
    I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
88
    do {
89
        result = I2C_Get_Status();
90
    } while (!result);
91
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
92
        Error(3);
93
    }
94
    length = I2C_Read_Buffer(buffer);
95
    if ((buffer[0] != 0xD4) || (length != 1)) {
96
        Error(3);
97
    }
98
 
99
    // Check Accelerometer
100
    I2C_Master_Restart(ADDRESS_ACCL, 0x20, 1);
101
    do {
102
        result = I2C_Get_Status();
103
    } while (!result);
104
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
105
        Error(4);
106
    }
107
    length = I2C_Read_Buffer(buffer);
108
    if ((buffer[0] != 0x07) || (length != 1)) {
109
        Error(4);
110
    }
111
 
112
    // Check Magnometer
113
    I2C_Master_Restart(ADDRESS_MAGN, 0x0A, 1);
114
    do {
115
        result = I2C_Get_Status();
116
    } while (!result);
117
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
118
        Error(4);
119
    }
120
    length = I2C_Read_Buffer(buffer);
121
    if ((buffer[0] != 0x48) || (length != 1)) {
122
        Error(4);
123
    }
124
}
125
 
223 Kevin 126
void Timer_2_Callback(void) {
127
    // Here we manually 'PWM' the LEDs
128
    // Note: this is terribly inefficient but we need to do this
129
    //  otherwise we will blow out the blue LED (max 10mA)
130
    if (timer_2_pwm == 0) {
131
        LED_R_LAT = (LED_R_ON) ? 0 : 1;
132
        LED_G_LAT = (LED_G_ON) ? 0 : 1;
133
        LED_B_LAT = (LED_B_ON) ? 0 : 1;
134
    }
135
    if (timer_2_pwm == LED_R_MAX_BRIGHTNESS) {
136
        LED_R_LAT = 1;
137
    }
138
    if (timer_2_pwm == LED_G_MAX_BRIGHTNESS) {
139
        LED_G_LAT = 1;
140
    }
141
    if (timer_2_pwm == LED_B_MAX_BRIGHTNESS) {
142
        LED_B_LAT = 1;
143
    }
144
    timer_2_pwm++;
145
}
146
 
147
void Timer_1_Callback(void) {
148
//    int A_X,A_Y,A_Z;
149
//    int G_X,G_Y,G_Z;
150
//    int M_X,M_Y,M_Z;
151
 
152
//    LSM303_Read_Accl(&A_X, &A_Y, &A_Z);
153
//    L3G_Read_Gyro(&G_X, &G_Y, &G_Z);
154
//    LSM303_Read_Magn(&output[6], &output[7], &output[8]);
155
 
156
//    UART_Write("Hello", 5);
157
}
158
 
197 Kevin 159
int main() {
202 Kevin 160
 
222 Kevin 161
    OSCCON = 0xF0; // Software PLL enabled, 32MHz
202 Kevin 162
    ANSELA = 0;
163
    ANSELB = 0;
164
    ANSELC = 0;
165
 
222 Kevin 166
    LED_R_TRIS = 0;
167
    LED_R_LAT = 1;
168
    LED_G_TRIS = 0;
169
    LED_G_LAT = 1;
170
    LED_B_TRIS = 0;
171
    LED_B_LAT = 1;
202 Kevin 172
 
173
    BLE_RESET_TRIS = 0;
222 Kevin 174
    BLE_RESET_LAT = 1;
202 Kevin 175
 
222 Kevin 176
    UART_CTS_TRIS = 1;
177
    UART_RTS_TRIS = 0;
178
    UART_RTS_LAT = 0;
202 Kevin 179
 
222 Kevin 180
    // Initialize all peripherals
223 Kevin 181
    TIMER_1_Init(&Timer_1_Callback);
182
    TIMER_2_Init(&Timer_2_Callback);
202 Kevin 183
    UART_Init(&uart_data);
184
    I2C_Init(&i2c_data);
185
    INTERRUPT_Init();
186
 
223 Kevin 187
    I2C_Configure_Master(I2C_400KHZ);
202 Kevin 188
    INTERRUPT_Enable();
222 Kevin 189
//    TIMER_1_Start();
190
    TIMER_2_Start();
191
 
192
    // A small delay is needed for the sensors to start up
193
    __delay_ms(10);
194
 
223 Kevin 195
    // Run a check to ensure that all sensors are properly connected
202 Kevin 196
    Startup_Check();
197
 
223 Kevin 198
    // Initialze the sensors
199
    L3G_Init();
200
    LSM303_Init();
202 Kevin 201
 
223 Kevin 202
    char output[19] = {0};
203
    output[18] = '\n';
204
 
205
    LED_B_ON = 1;
206
 
207
    __delay_ms(3000);
208
 
222 Kevin 209
    while(1) {
223 Kevin 210
        LSM303_Read_Accl(&output[0], &output[1], &output[2], &output[3], &output[4], &output[5]);
211
        L3G_Read_Gyro(&output[6], &output[7], &output[8], &output[9], &output[10], &output[11]);
212
        LSM303_Read_Magn(&output[12], &output[13], &output[14], &output[15], &output[16], &output[17]);
213
 
214
        UART_Write((char *)&output[0], 19);
215
 
216
        __delay_ms(10);
217
 
218
//        LED_B_ON = 0;
219
//        LED_R_ON = 1;
220
//        __delay_ms(250);
221
//        LED_R_ON = 0;
222
//        LED_G_ON = 1;
223
//        __delay_ms(250);
224
//        LED_G_ON = 0;
225
//        LED_B_ON = 1;
226
//        __delay_ms(250);
222 Kevin 227
    }
228
}