Subversion Repositories Code-Repo

Rev

Rev 119 | Blame | Last modification | View Log | RSS feed

#include "maindefs.h"
#include "interrupts.h"
#include "uart.h"
#include "i2c.h"
//#include "adc.h"
//#include "timers.h"
//#include "xbee.h"
//#include "led_driver.h"
//#include "pwm.h"
//#include "delays.h"
//#include "pin_interrupts.h"
#include <usart.h>
#include <delays.h>

#pragma config WDTEN = OFF          // Turn off watchdog timer
#pragma config XINST = OFF          // Turn off extended instruction set
#pragma config OSC = HSPLL          // Use external oscillator (101)
#pragma config PLLDIV = 3           // Set PPL prescaler to 3 (to get 4MHz)
#pragma config CFGPLLEN = ON        // Enable PLL on startup
#pragma config PLLSEL = PLL96       // Use 96MHz PLL 4MHz -> 96MHz / 2 = 48MHz
//#pragma config SOSCSEL = HIGH       // High Power T1OSC/SOSC circuit selected
//#pragma config ADCSEL = BIT12       // 12-bit ADrC
//#pragma config IOL1WAY = OFF        // IOLOCK bit can be set and cleared as needed

/* ----------- IO Pins -----------
 * RA0 - 
 * RA1 - 
 * RA2 - 
 * RA3 - 
 * RA4 - [CANNOT BE USED (VDDCORE/VCAP)]
 * RA5 - 
 * RA6 - Oscillator
 * RA7 - Oscillator
 * 
 * RC0 - 
 * RC1 - 
 * RC2 - 
 * RC3 - 
 * RC4 - 
 * RC5 - 
 * RC6 - UART Debug Output
 * RC7 - UART Debug Input
 *
 * RB0 - 
 * RB1 - 
 * RB2 - 
 * RB3 - 
 * RB4 - 
 * RB5 - 
 * RB6 - 
 * RB7 - 
 * ---------------------------- */

//static I2C_DATA i2c_data;
//static XBEE_DATA xbee_data;

void main(void) {
    unsigned char i = 0;
    unsigned char length = 0;
    unsigned char result = 0;
    unsigned char test[8] = "ASDF123";
    unsigned char buffer[100];
    
    /* --------------------- Oscillator Configuration --------------------- */
//    OSCTUNEbits.PLLEN = 1;          // Enable 4x PLL
    OSCCONbits.IRCF = 0b111;        // Set INTOSC postscaler to 8MHz
    OSCCONbits.SCS = 0b00; // Use 96MHz PLL as primary clock source
    /* -------------------------------------------------------------------- */

    // Set all ports as digial I/O
    ANCON0 = 0xFF;
    ANCON1 = 0x1F;

    UART1_Init(); // Initialize the UART handler code
    I2C_Init(); // Initialize the I2C handler code
//    xbee_init(&xbee_data); // Initialize the XBee handler code
//    adc_init();                 // Initialize the ADC
//    timers_init(); // Initialize timers
//    port_b_int_init(); // Initialze Port B interrupt handler
//    pwm_init(); // Initialize the PWM output driver
    
    interrupt_enable(); // Enable high-priority interrupts and low-priority interrupts
    interrupt_init(); // Initialize the interrupt priorities

    DBG_PRINT_MAIN("\r\nBegin Program\r\n");
    
    // Loop and process recieved messages from interrupts;

//    I2C_Configure_Master();
    I2C_Configure_Slave(0x30);

    TRISCbits.TRISC0 = 0;
    TRISCbits.TRISC1 = 0;
    TRISCbits.TRISC2 = 0;
    LATCbits.LATC0 = 0;
    LATCbits.LATC1 = 0;
    LATCbits.LATC2 = 0;
    
    while (1) {
//
//        Delay10KTCYx(255);
//        Delay10KTCYx(255);
        
/* UART Test */
//        length = UART1_Read((char *)buffer);
//        if (length != 0) {
//            UART1_WriteB((char *)buffer, length) ;
//        }
/* UART Test*/

/* I2C Master Test */
//        buffer[0] = 0xBB;
//        I2C_Master_Send(0x30, 1, buffer);
//
//        result = I2C_Get_Status();
//        while (!result) {
//            result = I2C_Get_Status();
//        }
//        DBG_PRINT_MAIN("%x ", result);
//
//        I2C_Master_Recv(0x30, 2);
//
//        result = I2C_Get_Status();
//        while (!result) {
//            result = I2C_Get_Status();
//        }
//        DBG_PRINT_MAIN("%x ", result);
//
//        I2C_Master_Restart(0x30, 0xBB, 2);
//
//        result = I2C_Get_Status();
//        while (!result) {
//            result = I2C_Get_Status();
//        }
//        DBG_PRINT_MAIN("%x\r\n", result);
/* I2C Master Test */

/* I2C Slave Receive Test */
        result = I2C_Get_Status();
        while (!result) {
            result = I2C_Get_Status();
        }
        DBG_PRINT_MAIN("%X ", result);
        length = I2C_Read_Buffer(buffer);
        DBG_PRINT_MAIN("%d ", length);
        for (i = 0; i < length; i++) {
            DBG_PRINT_MAIN("%X ", buffer[i]);
        }
        DBG_PRINT_MAIN("\r\n");
/* I2C Slave Receive Test */
    }
}