Subversion Repositories Code-Repo

Rev

Rev 202 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 202 Rev 222
Line 28... Line 28...
28
// </editor-fold>
28
// </editor-fold>
29
 
29
 
30
UART_DATA uart_data;
30
UART_DATA uart_data;
31
I2C_DATA i2c_data;
31
I2C_DATA i2c_data;
32
 
32
 
-
 
33
char LED_R_ON = 0;
-
 
34
char LED_G_ON = 0;
-
 
35
char LED_B_ON = 0;
-
 
36
 
33
void Error(char ID) {
37
void Error(char ID) {
34
    while(1) {
38
    while(1) {
35
        for (char i = 0; i < ID; i++) {
39
        for (char i = 0; i < ID; i++) {
36
            LED_LAT = 1;
40
            LED_R_ON = 1;
37
            __delay_ms(120);
41
            __delay_ms(120);
38
            LED_LAT = 0;
42
            LED_R_ON = 0;
39
            __delay_ms(150);
43
            __delay_ms(150);
40
        }
44
        }
41
        __delay_ms(1000);
45
        __delay_ms(1000);
42
    }
46
    }
43
}
47
}
44
 
48
 
45
void Startup_Check(void) {
49
void Startup_Check(void) {
46
    char buffer[20];
50
    char buffer[20];
47
    char result, length;
51
    char result, length;
48
 
52
 
49
    BLE_RESET_LAT = 0;
53
//    BLE_RESET_LAT = 0;
50
    __delay_ms(3000);
54
//    __delay_ms(3000);
51
    BLE_RESET_LAT = 1;
55
//    BLE_RESET_LAT = 1;
52
    __delay_ms(200);
56
//    __delay_ms(200);
53
 
57
 
54
    // Check BLE Module
58
//    // Check BLE Module
55
    length = UART_Read(buffer);
59
//    length = UART_Read(buffer);
56
    if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
60
//    if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {
57
        Error(1);
61
//        Error(1);
58
    }
62
//    }
59
    UART_Write("AT\r", 3);
63
//    UART_Write("AT\r", 3);
60
    __delay_ms(10);
64
//    __delay_ms(10);
61
    length = UART_Read(buffer);
65
//    length = UART_Read(buffer);
62
    if (memcmp(buffer, "\r\nOK\r\n", 6)) {
66
//    if (memcmp(buffer, "\r\nOK\r\n", 6)) {
63
        Error(1);
67
//        Error(1);
64
    }
68
//    }
65
 
69
 
66
    // Check Battery Gauge
70
    // Check Battery Gauge
67
    I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
71
    I2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);
68
    do {
72
    do {
69
        result = I2C_Get_Status();
73
        result = I2C_Get_Status();
Line 73... Line 77...
73
    }
77
    }
74
    length = I2C_Read_Buffer(buffer);
78
    length = I2C_Read_Buffer(buffer);
75
    if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
79
    if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {
76
        Error(2);
80
        Error(2);
77
    }
81
    }
78
    
82
 
79
    // Check Gyroscope
83
    // Check Gyroscope
80
    I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
84
    I2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);
81
    do {
85
    do {
82
        result = I2C_Get_Status();
86
        result = I2C_Get_Status();
83
    } while (!result);
87
    } while (!result);
Line 116... Line 120...
116
    }
120
    }
117
}
121
}
118
 
122
 
119
int main() {
123
int main() {
120
 
124
 
121
    OSCCON = 0x70; // PLL disabled, 8MHz
125
    OSCCON = 0xF0; // Software PLL enabled, 32MHz
122
    ANSELA = 0;
126
    ANSELA = 0;
123
    ANSELB = 0;
127
    ANSELB = 0;
124
    ANSELC = 0;
128
    ANSELC = 0;
125
 
129
 
126
    LED_TRIS = 0;
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;
127
    LED_LAT = 0;
135
    LED_B_LAT = 1;
128
 
136
 
129
    BLE_RESET_TRIS = 0;
137
    BLE_RESET_TRIS = 0;
130
    BLE_RESET_LAT = 0;
138
    BLE_RESET_LAT = 1;
131
 
-
 
132
    BLE_SLEEP_TRIS = 0;
-
 
133
    BLE_SLEEP_LAT = 0;
-
 
134
 
-
 
135
    BLE_MODE_TRIS = 0;
-
 
136
    BLE_MODE_LAT = 0;
-
 
137
 
139
 
-
 
140
    UART_CTS_TRIS = 1;
-
 
141
    UART_RTS_TRIS = 0;
-
 
142
    UART_RTS_LAT = 0;
-
 
143
 
-
 
144
    // Initialize all peripherals
-
 
145
//    TIMER_1_Init();
138
    TIMER_1_Init();
146
    TIMER_2_Init();
139
    UART_Init(&uart_data);
147
    UART_Init(&uart_data);
140
    I2C_Init(&i2c_data);
148
    I2C_Init(&i2c_data);
141
    INTERRUPT_Init();
149
    INTERRUPT_Init();
142
 
150
 
143
    I2C_Configure_Master(I2C_100KHZ);
151
    I2C_Configure_Master(I2C_100KHZ);
144
    INTERRUPT_Enable();
152
    INTERRUPT_Enable();
145
    
153
//    TIMER_1_Start();
146
    Startup_Check();
154
    TIMER_2_Start();
147
 
155
 
-
 
156
    // A small delay is needed for the sensors to start up
148
    TIMER_1_Start();
157
    __delay_ms(10);
149
 
158
 
150
    while(1);
159
    Startup_Check();
151
}
160
 
-
 
161
//    LED_R_ON = 1;
-
 
162
//    LED_G_ON = 1;
-
 
163
//    LED_B_ON = 1;
152
 
164
 
-
 
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
}
153
178