Subversion Repositories Code-Repo

Rev

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

Rev 222 Rev 223
Line 3... Line 3...
3
#include "main.h"
3
#include "main.h"
4
#include "TIMER.h"
4
#include "TIMER.h"
5
#include "INTERRUPT.h"
5
#include "INTERRUPT.h"
6
#include "UART.h"
6
#include "UART.h"
7
#include "I2C.h"
7
#include "I2C.h"
-
 
8
#include "L3G.h"
-
 
9
#include "LSM303.h"
8
 
10
 
9
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
11
// <editor-fold defaultstate="collapsed" desc="Configuration Bits">
10
// CONFIG1
12
// CONFIG1
11
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
13
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
12
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
14
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
Line 31... Line 33...
31
I2C_DATA i2c_data;
33
I2C_DATA i2c_data;
32
 
34
 
33
char LED_R_ON = 0;
35
char LED_R_ON = 0;
34
char LED_G_ON = 0;
36
char LED_G_ON = 0;
35
char LED_B_ON = 0;
37
char LED_B_ON = 0;
-
 
38
char timer_2_pwm = 0;
36
 
39
 
37
void Error(char ID) {
40
void Error(char ID) {
38
    while(1) {
41
    while(1) {
39
        for (char i = 0; i < ID; i++) {
42
        for (char i = 0; i < ID; i++) {
40
            LED_R_ON = 1;
43
            LED_R_ON = 1;
Line 118... Line 121...
118
    if ((buffer[0] != 0x48) || (length != 1)) {
121
    if ((buffer[0] != 0x48) || (length != 1)) {
119
        Error(4);
122
        Error(4);
120
    }
123
    }
121
}
124
}
122
 
125
 
-
 
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
 
123
int main() {
159
int main() {
124
 
160
 
125
    OSCCON = 0xF0; // Software PLL enabled, 32MHz
161
    OSCCON = 0xF0; // Software PLL enabled, 32MHz
126
    ANSELA = 0;
162
    ANSELA = 0;
127
    ANSELB = 0;
163
    ANSELB = 0;
Line 140... Line 176...
140
    UART_CTS_TRIS = 1;
176
    UART_CTS_TRIS = 1;
141
    UART_RTS_TRIS = 0;
177
    UART_RTS_TRIS = 0;
142
    UART_RTS_LAT = 0;
178
    UART_RTS_LAT = 0;
143
 
179
 
144
    // Initialize all peripherals
180
    // Initialize all peripherals
145
//    TIMER_1_Init();
181
    TIMER_1_Init(&Timer_1_Callback);
146
    TIMER_2_Init();
182
    TIMER_2_Init(&Timer_2_Callback);
147
    UART_Init(&uart_data);
183
    UART_Init(&uart_data);
148
    I2C_Init(&i2c_data);
184
    I2C_Init(&i2c_data);
149
    INTERRUPT_Init();
185
    INTERRUPT_Init();
150
 
186
 
151
    I2C_Configure_Master(I2C_100KHZ);
187
    I2C_Configure_Master(I2C_400KHZ);
152
    INTERRUPT_Enable();
188
    INTERRUPT_Enable();
153
//    TIMER_1_Start();
189
//    TIMER_1_Start();
154
    TIMER_2_Start();
190
    TIMER_2_Start();
155
 
191
 
156
    // A small delay is needed for the sensors to start up
192
    // A small delay is needed for the sensors to start up
157
    __delay_ms(10);
193
    __delay_ms(10);
158
 
194
 
-
 
195
    // Run a check to ensure that all sensors are properly connected
159
    Startup_Check();
196
    Startup_Check();
160
 
197
 
-
 
198
    // Initialze the sensors
161
//    LED_R_ON = 1;
199
    L3G_Init();
162
//    LED_G_ON = 1;
200
    LSM303_Init();
-
 
201
 
-
 
202
    char output[19] = {0};
-
 
203
    output[18] = '\n';
-
 
204
    
163
//    LED_B_ON = 1;
205
    LED_B_ON = 1;
-
 
206
 
-
 
207
    __delay_ms(3000);
164
 
208
 
165
    while(1) {
209
    while(1) {
-
 
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
 
166
        UART_Write("Hello World \r\n", 14);
214
        UART_Write((char *)&output[0], 19);
-
 
215
 
-
 
216
        __delay_ms(10);
-
 
217
 
167
        LED_B_ON = 0;
218
//        LED_B_ON = 0;
168
        LED_R_ON = 1;
219
//        LED_R_ON = 1;
169
        __delay_ms(250);
220
//        __delay_ms(250);
170
        LED_R_ON = 0;
221
//        LED_R_ON = 0;
171
        LED_G_ON = 1;
222
//        LED_G_ON = 1;
172
        __delay_ms(250);
223
//        __delay_ms(250);
173
        LED_G_ON = 0;
224
//        LED_G_ON = 0;
174
        LED_B_ON = 1;
225
//        LED_B_ON = 1;
175
        __delay_ms(250);
226
//        __delay_ms(250);
176
    }
227
    }
177
}
228
}
178
229