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"
197 Kevin 8
 
202 Kevin 9
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
197 Kevin 10
// CONFIG1
11
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
12
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
13
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
14
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
15
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
16
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
17
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
18
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
19
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
20
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
21
 
22
// CONFIG2
23
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
202 Kevin 24
#pragma config PLLEN = ON       // PLL Enable (4x PLL disabled)
197 Kevin 25
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
26
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
202 Kevin 27
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
28
// </editor-fold>
197 Kevin 29
 
202 Kevin 30
UART_DATA uart_data;
31
I2C_DATA i2c_data;
32
 
222 Kevin 33
char LED_R_ON = 0;
34
char LED_G_ON = 0;
35
char LED_B_ON = 0;
36
 
202 Kevin 37
void Error(char ID) {
38
    while(1) {
39
        for (char i = 0; i < ID; i++) {
222 Kevin 40
            LED_R_ON = 1;
202 Kevin 41
            __delay_ms(120);
222 Kevin 42
            LED_R_ON = 0;
202 Kevin 43
            __delay_ms(150);
44
        }
45
        __delay_ms(1000);
46
    }
47
}
48
 
49
void Startup_Check(void) {
50
    char buffer[20];
51
    char result, length;
52
 
222 Kevin 53
//    BLE_RESET_LAT = 0;
54
//    __delay_ms(3000);
55
//    BLE_RESET_LAT = 1;
56
//    __delay_ms(200);
202 Kevin 57
 
222 Kevin 58
//    // Check BLE Module
59
//    length = UART_Read(buffer);
60
//    if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
61
//        Error(1);
62
//    }
63
//    UART_Write("AT\r", 3);
64
//    __delay_ms(10);
65
//    length = UART_Read(buffer);
66
//    if (memcmp(buffer, "\r\nOK\r\n", 6)) {
67
//        Error(1);
68
//    }
202 Kevin 69
 
70
    // Check Battery Gauge
71
    I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
72
    do {
73
        result = I2C_Get_Status();
74
    } while (!result);
75
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
76
        Error(2);
77
    }
78
    length = I2C_Read_Buffer(buffer);
79
    if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
80
        Error(2);
81
    }
222 Kevin 82
 
202 Kevin 83
    // Check Gyroscope
84
    I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
85
    do {
86
        result = I2C_Get_Status();
87
    } while (!result);
88
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
89
        Error(3);
90
    }
91
    length = I2C_Read_Buffer(buffer);
92
    if ((buffer[0] != 0xD4) || (length != 1)) {
93
        Error(3);
94
    }
95
 
96
    // Check Accelerometer
97
    I2C_Master_Restart(ADDRESS_ACCL, 0x20, 1);
98
    do {
99
        result = I2C_Get_Status();
100
    } while (!result);
101
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
102
        Error(4);
103
    }
104
    length = I2C_Read_Buffer(buffer);
105
    if ((buffer[0] != 0x07) || (length != 1)) {
106
        Error(4);
107
    }
108
 
109
    // Check Magnometer
110
    I2C_Master_Restart(ADDRESS_MAGN, 0x0A, 1);
111
    do {
112
        result = I2C_Get_Status();
113
    } while (!result);
114
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
115
        Error(4);
116
    }
117
    length = I2C_Read_Buffer(buffer);
118
    if ((buffer[0] != 0x48) || (length != 1)) {
119
        Error(4);
120
    }
121
}
122
 
197 Kevin 123
int main() {
202 Kevin 124
 
222 Kevin 125
    OSCCON = 0xF0; // Software PLL enabled, 32MHz
202 Kevin 126
    ANSELA = 0;
127
    ANSELB = 0;
128
    ANSELC = 0;
129
 
222 Kevin 130
    LED_R_TRIS = 0;
131
    LED_R_LAT = 1;
132
    LED_G_TRIS = 0;
133
    LED_G_LAT = 1;
134
    LED_B_TRIS = 0;
135
    LED_B_LAT = 1;
202 Kevin 136
 
137
    BLE_RESET_TRIS = 0;
222 Kevin 138
    BLE_RESET_LAT = 1;
202 Kevin 139
 
222 Kevin 140
    UART_CTS_TRIS = 1;
141
    UART_RTS_TRIS = 0;
142
    UART_RTS_LAT = 0;
202 Kevin 143
 
222 Kevin 144
    // Initialize all peripherals
145
//    TIMER_1_Init();
146
    TIMER_2_Init();
202 Kevin 147
    UART_Init(&uart_data);
148
    I2C_Init(&i2c_data);
149
    INTERRUPT_Init();
150
 
151
    I2C_Configure_Master(I2C_100KHZ);
152
    INTERRUPT_Enable();
222 Kevin 153
//    TIMER_1_Start();
154
    TIMER_2_Start();
155
 
156
    // A small delay is needed for the sensors to start up
157
    __delay_ms(10);
158
 
202 Kevin 159
    Startup_Check();
160
 
222 Kevin 161
//    LED_R_ON = 1;
162
//    LED_G_ON = 1;
163
//    LED_B_ON = 1;
202 Kevin 164
 
222 Kevin 165
    while(1) {
166
        UART_Write("Hello World \r\n", 14);
167
        LED_B_ON = 0;
168
        LED_R_ON = 1;
169
        __delay_ms(250);
170
        LED_R_ON = 0;
171
        LED_G_ON = 1;
172
        __delay_ms(250);
173
        LED_G_ON = 0;
174
        LED_B_ON = 1;
175
        __delay_ms(250);
176
    }
177
}