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
 
33
void Error(char ID) {
34
    while(1) {
35
        for (char i = 0; i < ID; i++) {
36
            LED_LAT = 1;
37
            __delay_ms(120);
38
            LED_LAT = 0;
39
            __delay_ms(150);
40
        }
41
        __delay_ms(1000);
42
    }
43
}
44
 
45
void Startup_Check(void) {
46
    char buffer[20];
47
    char result, length;
48
 
49
    BLE_RESET_LAT = 0;
50
    __delay_ms(3000);
51
    BLE_RESET_LAT = 1;
52
    __delay_ms(200);
53
 
54
    // Check BLE Module
55
    length = UART_Read(buffer);
56
    if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
57
        Error(1);
58
    }
59
    UART_Write("AT\r", 3);
60
    __delay_ms(10);
61
    length = UART_Read(buffer);
62
    if (memcmp(buffer, "\r\nOK\r\n", 6)) {
63
        Error(1);
64
    }
65
 
66
    // Check Battery Gauge
67
    I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
68
    do {
69
        result = I2C_Get_Status();
70
    } while (!result);
71
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
72
        Error(2);
73
    }
74
    length = I2C_Read_Buffer(buffer);
75
    if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
76
        Error(2);
77
    }
78
 
79
    // Check Gyroscope
80
    I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
81
    do {
82
        result = I2C_Get_Status();
83
    } while (!result);
84
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
85
        Error(3);
86
    }
87
    length = I2C_Read_Buffer(buffer);
88
    if ((buffer[0] != 0xD4) || (length != 1)) {
89
        Error(3);
90
    }
91
 
92
    // Check Accelerometer
93
    I2C_Master_Restart(ADDRESS_ACCL, 0x20, 1);
94
    do {
95
        result = I2C_Get_Status();
96
    } while (!result);
97
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
98
        Error(4);
99
    }
100
    length = I2C_Read_Buffer(buffer);
101
    if ((buffer[0] != 0x07) || (length != 1)) {
102
        Error(4);
103
    }
104
 
105
    // Check Magnometer
106
    I2C_Master_Restart(ADDRESS_MAGN, 0x0A, 1);
107
    do {
108
        result = I2C_Get_Status();
109
    } while (!result);
110
    if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {
111
        Error(4);
112
    }
113
    length = I2C_Read_Buffer(buffer);
114
    if ((buffer[0] != 0x48) || (length != 1)) {
115
        Error(4);
116
    }
117
}
118
 
197 Kevin 119
int main() {
202 Kevin 120
 
121
    OSCCON = 0x70; // PLL disabled, 8MHz
122
    ANSELA = 0;
123
    ANSELB = 0;
124
    ANSELC = 0;
125
 
126
    LED_TRIS = 0;
127
    LED_LAT = 0;
128
 
129
    BLE_RESET_TRIS = 0;
130
    BLE_RESET_LAT = 0;
131
 
132
    BLE_SLEEP_TRIS = 0;
133
    BLE_SLEEP_LAT = 0;
134
 
135
    BLE_MODE_TRIS = 0;
136
    BLE_MODE_LAT = 0;
137
 
138
    TIMER_1_Init();
139
    UART_Init(&uart_data);
140
    I2C_Init(&i2c_data);
141
    INTERRUPT_Init();
142
 
143
    I2C_Configure_Master(I2C_100KHZ);
144
    INTERRUPT_Enable();
145
 
146
    Startup_Check();
147
 
148
    TIMER_1_Start();
149
 
197 Kevin 150
    while(1);
151
}
152