Rev 202 | Blame | Last modification | View Log | Download | RSS feed
#include <xc.h>#include <string.h>#include "main.h"#include "TIMER.h"#include "INTERRUPT.h"#include "UART.h"#include "I2C.h"// <editor-fold defaultstate="collapsed" desc="Configuration Bits">// CONFIG1#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)// CONFIG2#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)#pragma config PLLEN = ON // PLL Enable (4x PLL disabled)#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)// </editor-fold>UART_DATA uart_data;I2C_DATA i2c_data;char LED_R_ON = 0;char LED_G_ON = 0;char LED_B_ON = 0;void Error(char ID) {while(1) {for (char i = 0; i < ID; i++) {LED_R_ON = 1;__delay_ms(120);LED_R_ON = 0;__delay_ms(150);}__delay_ms(1000);}}void Startup_Check(void) {char buffer[20];char result, length;// BLE_RESET_LAT = 0;// __delay_ms(3000);// BLE_RESET_LAT = 1;// __delay_ms(200);// // Check BLE Module// length = UART_Read(buffer);// if (memcmp(buffer, "\r\nBR-LE4.0-S2\r\n", 15)) {// Error(1);// }// UART_Write("AT\r", 3);// __delay_ms(10);// length = UART_Read(buffer);// if (memcmp(buffer, "\r\nOK\r\n", 6)) {// Error(1);// }// Check Battery GaugeI2C_Master_Restart(ADDRESS_LIPO, 0x0C, 2);do {result = I2C_Get_Status();} while (!result);if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {Error(2);}length = I2C_Read_Buffer(buffer);if ((buffer[0] != 0x97) || (buffer[1] != 0x00) || (length != 2)) {Error(2);}// Check GyroscopeI2C_Master_Restart(ADDRESS_GYRO, 0x0F, 1);do {result = I2C_Get_Status();} while (!result);if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {Error(3);}length = I2C_Read_Buffer(buffer);if ((buffer[0] != 0xD4) || (length != 1)) {Error(3);}// Check AccelerometerI2C_Master_Restart(ADDRESS_ACCL, 0x20, 1);do {result = I2C_Get_Status();} while (!result);if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {Error(4);}length = I2C_Read_Buffer(buffer);if ((buffer[0] != 0x07) || (length != 1)) {Error(4);}// Check MagnometerI2C_Master_Restart(ADDRESS_MAGN, 0x0A, 1);do {result = I2C_Get_Status();} while (!result);if ((result != I2C_SEND_OK) && (result != I2C_RECV_OK)) {Error(4);}length = I2C_Read_Buffer(buffer);if ((buffer[0] != 0x48) || (length != 1)) {Error(4);}}int main() {OSCCON = 0xF0; // Software PLL enabled, 32MHzANSELA = 0;ANSELB = 0;ANSELC = 0;LED_R_TRIS = 0;LED_R_LAT = 1;LED_G_TRIS = 0;LED_G_LAT = 1;LED_B_TRIS = 0;LED_B_LAT = 1;BLE_RESET_TRIS = 0;BLE_RESET_LAT = 1;UART_CTS_TRIS = 1;UART_RTS_TRIS = 0;UART_RTS_LAT = 0;// Initialize all peripherals// TIMER_1_Init();TIMER_2_Init();UART_Init(&uart_data);I2C_Init(&i2c_data);INTERRUPT_Init();I2C_Configure_Master(I2C_100KHZ);INTERRUPT_Enable();// TIMER_1_Start();TIMER_2_Start();// A small delay is needed for the sensors to start up__delay_ms(10);Startup_Check();// LED_R_ON = 1;// LED_G_ON = 1;// LED_B_ON = 1;while(1) {UART_Write("Hello World \r\n", 14);LED_B_ON = 0;LED_R_ON = 1;__delay_ms(250);LED_R_ON = 0;LED_G_ON = 1;__delay_ms(250);LED_G_ON = 0;LED_B_ON = 1;__delay_ms(250);}}