Esempio n. 1
0
/******************************************************************************
 * Function:        void UserInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This routine should take care of all of the demo code
 *                  initialization that is required.
 *
 * Note:            
 *
 *****************************************************************************/
void UserInit(void)
{
    //Make sure that the SD-card is not selected
    TRISCbits.TRISC6 = 0;
    LATCbits.LATC6 = 1;
    PORTCbits.RC6 = 1;

	/* Initialize the mTouch library */
	mTouchInit();

	/* Call the mTouch callibration function */
	mTouchCalibrate();

	/* Initialize the accelerometer */
	InitBma150(); 

    //make sure that the accelerometer is not selected
    LATCbits.LATC7 = 1;
    PORTCbits.RC7 = 1;

	Fill(0x00);
	g_level = 0;
	loadLevel(g_level);
	InitPhysics();
}//end UserInit
Esempio n. 2
0
/******************************************************************************
 * Function:        void UserInit(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        This routine should take care of all of the demo code
 *                  initialization that is required.
 *
 * Note:
 *
 *****************************************************************************/
void UserInit(void)
{
    /* Initialize the mTouch library */
    mTouchInit();

    /* Call the mTouch callibration function */
    mTouchCalibrate();

    /* Initialize the accelerometer */
    InitBma150();

    //initialize the variable holding the handle for the last
    // transmission
    USBOutHandle = 0;
    USBInHandle = 0;
}//end UserInit
unsigned char init(void)
{
	//init CPU clock
	//OSCCON = 0b01111010; //PLL off, 16MHz HF, system clocked by HFINTOSC block
	OSCCON = 0b11110000; //PLL on, 8MHz HF, system clocked by HFINTOSC block
	while(!OSCSTATbits.PLLR); //wait for PLL to be ready
	
	//init RTC
	T1CON = 0b10001100; //T1OSC selected, 1:1 prescale, T1OSC on, timer1 off
	while(!OSCSTATbits.T1OSCR); //wait for timer1 oscillator to be ready
	TMR1 = 0;
	TMR1IF = 0; //clear interrupt flag
	TMR1IE = 1;	//enable interrupt on TMR0 overflow
	__delay_ms(1); //wait for timer1 oscillator to stabilize
	T1CONbits.TMR1ON = 1; //timer1 on
	PEIE = 1; //enable peripheral interrupts
	
	hours = START_H;
	minutes = START_M;
	seconds = START_S;
	ampm = START_AP;
	
	newSecond = tickCounter = 0;
	
	//init display
	////gpio
	ANSELA = ANSELE = ANSELF = ANSELG = 0; //analog gpio as digital inputs
	
	MATRIX_LAT_COLOR_RED = ~0x00; //gpio high (columns off) by default (these are RED column current sinks)
	MATRIX_LAT_COLOR_GRN = ~0x00; //gpio high (columns off) by default (these are GREEN column current sinks)
	MATRIX_LAT_COLOR_BLU = ~0x00; //gpio high (columns off) by default (these are BLUE column current sinks)
	MATRIX_TRIS_COLOR_RED = 0x00; //gpio as output
	MATRIX_TRIS_COLOR_GRN = 0x00; //gpio as output
	MATRIX_TRIS_COLOR_BLU = 0x00; //gpio as output
	
	MATRIX_LAT_ROW_1 = 0; //gpio low (rows off) by default (this is row current driver 1)
	MATRIX_LAT_ROW_2 = 0; //gpio low (rows off) by default (this is row current driver 2)
	MATRIX_LAT_ROW_3 = 0; //gpio low (rows off) by default (this is row current driver 3)
	MATRIX_LAT_ROW_4 = 0; //gpio low (rows off) by default (this is row current driver 4)
	MATRIX_LAT_ROW_5 = 0; //gpio low (rows off) by default (this is row current driver 5)
	MATRIX_LAT_ROW_6 = 0; //gpio low (rows off) by default (this is row current driver 6)
	MATRIX_LAT_ROW_7 = 0; //gpio low (rows off) by default (this is row current driver 7)
	MATRIX_LAT_ROW_8 = 0; //gpio low (rows off) by default (this is row current driver 8)
	MATRIX_TRIS_ROW_1 = 0; //gpio as output
	MATRIX_TRIS_ROW_2 = 0; //gpio as output
	MATRIX_TRIS_ROW_3 = 0; //gpio as output
	MATRIX_TRIS_ROW_4 = 0; //gpio as output
	MATRIX_TRIS_ROW_5 = 0; //gpio as output
	MATRIX_TRIS_ROW_6 = 0; //gpio as output
	MATRIX_TRIS_ROW_7 = 0; //gpio as output
	MATRIX_TRIS_ROW_8 = 0; //gpio as output

	////matrix scan variables
	currentRefreshingColor = currentRefreshingRow = \
	currentRefreshingRowIndex = currentRefreshingPwmSweep = 0;
	
	bitmapClear();
	updateDisplayRequest = 0;
	
	////matrix scan timer
	//TODO: consider using timer2/4/6 with auto-clear and period register
	OPTION_REG = 0b00001010; //WPU individual, falling edge, fosc/4, 1:8 prescale
	TMR0 = MATRIX_SCAN_TIMER_PRELOAD;
	TMR0IF = 0; //clear interrupt flag
	TMR0IE = 1;	//enable interrupt on TMR0 overflow
	
	//init accelerometer
	InitBma150();
	
	//init serial flash
	
	//init battery charging
	BATT_STAT1_TRIS = 1; //battery status line as input
	WPUB |= 0b00000010; //battery status line pulled up (BATT_STAT1 is open collector on charger)
	IOCBP |= 0b00000010; //RB1 enabled as positive-edge interrupt-on-change pin
	IOCBN |= 0b00000010; //RB1 enabled as negative-edge interrupt-on-change pin
	
	//init battery level sensing
	BATT_VSENSE_TRIS = 1; //battery voltage sense as input
	BATT_VSENSE_GND_TRIS = 1; //battery voltage divider bottom as Hi-Z
	BATT_VSENSE_GND = 0; //preload battery voltage divider bottom to sink current
	
	FVRCON = 0b00000010; //disable FVR, comparator/DAC not fed, ADC fed with 2.048v.
	ADCON1 = 0b00000011; //left justified, FOSC/2, Vref- = Vss, Vref+ = FVR.
	ADCON0 = 0b00110000; //AN12, ADC OFF
	
	//init user interface
	//WPUB = 0b00011100; //enable internal pullups for all IOC pins (pulled up by resistors on Rev A board)
	SW1_TRIS = 1;
	SW2_TRIS = 1;
	SW3_TRIS = 1;
	IOCBN = 0b00011100; //RB4-RB2 enabled as negative-edge interrupt-on-change pins
	IOCBF = 0; //clear all pin-change interrupt flags
	IOCIF = 0; //clear the interrupt flag
	IOCIE = 1; //enable interrupt-on-change
	
	////generic 1ms tick timer
	T2CON = 0b00000011; //1:1, OFF, 1:64
	PR2 = 125; //makes for interval of 1ms
	TMR2IF = 0; //clear interrupt flag
	TMR2IE = 1; //enable timer2 interrupt
	
	//init state machine
	currentState = state_setTime;
	
	return 0;
}