/*********************************************************************
* Function:         void InitSymbolTimer()
*
* PreCondition:     none
*
* Input:		    none
*
* Output:		    none
*
* Side Effects:	    TMR0 for PIC18 is configured for calculating
*                   the correct symbol times.  TMR2/3 for PIC24/dsPIC
*                   is configured for calculating the correct symbol
*                   times
*
* Overview:		    This function will configure the UART for use at 
*                   in 8 bits, 1 stop, no flowcontrol mode
*
* Note:			    The timer interrupt is enabled causing the timer
*                   roll over calculations.  Interrupts are required
*                   to be enabled in order to extend the timer to
*                   4 bytes in PIC18.  PIC24/dsPIC version do not 
*                   enable or require interrupts
********************************************************************/
void InitSymbolTimer()
{
#if defined(__18CXX)
    T0CON = 0b00000000 | CLOCK_DIVIDER_SETTING;
    INTCON2bits.TMR0IP = 1;
    INTCONbits.TMR0IF = 0;
    INTCONbits.TMR0IE = 1;
    T0CONbits.TMR0ON = 1;

    timerExtension1 = 0;
    timerExtension2 = 0;
#elif defined(__dsPIC30F__) || defined(__dsPIC33F__) || defined(__PIC24F__) || defined(__PIC24H__)
    T2CON = 0b0000000000001000 | CLOCK_DIVIDER_SETTING;
    T2CONbits.TON = 1;
#elif defined(__PIC32MX__)
    CloseTimer2();
    WriteTimer2(0x00);
    WriteTimer3(0x00);
    WritePeriod3(0xFFFF);
    OpenTimer2((T2_ON|T2_32BIT_MODE_ON|CLOCK_DIVIDER_SETTING),0xFFFFFFFF);     
#else
    #error "Symbol timer implementation required for stack usage."
#endif
}
Example #2
0
void main(void) {
    uint16_t v;
    uint8_t i;
    int16_t pad;
    uint32_t padsNow=0;
    uint32_t padsLast=0;
    int8_t  lastKey;
    uint8_t cnt=0;
    uint16_t lastEncValue;
    uint8_t encStep;
    int16_t senseDelta;
    uint8_t easteregg;

    SetupBoard();
    SetupCTMU();

    encStep=1;
    encValue=0;
    lastEncValue=0;
    octave=3;
    encMin=0;
    encMax=14;
    senseDelta=99;
    OpenTimer4(TIMER_INT_ON & T4_PS_1_1 & T4_POST_1_1); // Timer4 - Rotary Encoder polling
    WriteTimer4(0xFF);

    TMR4IF=0;
    OpenTimer2(TIMER_INT_ON & T2_PS_1_4 & T2_POST_1_8); // Timer2 = Display Refresh
    WriteTimer2(0xFF);
    TMR2IF=0;

    GIE=1;
    PEIE=1;
    ei();

    disp[0]=0x00;
    disp[1]=0x00;
    disp[2]=0x00;

    keys=0;
    easteregg=0;

    leds=0x7fff;
    __delay_ms(25);
    leds=0x0000;
    CalibratePads();
    leds=0x7fff;
    __delay_ms(25);
    leds=0x0000;


//
//    encMin=1;
//    encMax=24;
//    encValue=1;
//    for (;;) {
//        pad=ReadPad(encValue);
//        DispValue(pad);
//        leds=0;
//        if ((encValue>=1) && (encValue<=9))   {setbit(leds,encValue-1);   setbit(leds,10);}
//        if ((encValue>=10) && (encValue<=19)) {setbit(leds,encValue-10);setbit(leds,11);}
//        if ((encValue>=20) && (encValue<=29)) {setbit(leds,encValue-20);setbit(leds,12);}
//        __delay_ms(25);
//    }



    for (;;) {
        // Check for tones H+A+D
        if (padsLast==0b000000000000101000000100) EasterEgg(1);
        if (padsLast==0b101000000100000000000000) EasterEgg(2);

        padsNow=0;
        for (i=0; i<PADS; i++) {
            pad=ReadPad(i);
            if (GetPadBaseValue(i)-pad>senseDelta) {
                setbit(padsNow,i);
            }
        }
        for (i=0; i<PADS; i++) {
            if (testbit(padsNow,i) != testbit(padsLast,i)) {
                if (testbit(padsNow,i)) {
                    SendNoteOn(octave*12+i);
                } else {
                    SendNoteOff(octave*12+i);
                }
            }
        }
        padsLast=padsNow;

        for (i=0; i<KEYS; i++) {
            if (testbit(keys,i)) {
                disp[0]=charmap[butSettings[i].txt[0]-32];
                disp[1]=charmap[butSettings[i].txt[1]-32];
                disp[2]=charmap[butSettings[i].txt[2]-32];
                lastKey=i;
                encMin=butSettings[i].min;
                encMax=butSettings[i].max;
                encValue=butSettings[i].value;
                encStep=butSettings[i].stepSize;
                lastEncValue=encValue;
                break;
            }
        }

        if ((encValue!=lastEncValue) && (lastKey!=-1)) {
            lastEncValue=encValue;
            if (butSettings[lastKey].cc==127) {  // OCTAVE
                DispValue(encValue*encStep);
                octave=encValue;
                butSettings[lastKey].value=encValue;
                DispValue(encValue*encStep);
            } else if (butSettings[lastKey].cc==126) {  // SENSE DELTA
                senseDelta=encValue;
                butSettings[lastKey].value=senseDelta;
                DispValue(senseDelta);
                if (senseDelta&1) setbit(leds,0); else clrbit(leds,0);
            } else {
                SendCC(butSettings[lastKey].cc, encValue*encStep);
                DispValue(encValue*encStep);
            }
        }


        __delay_ms(10);

    }
}
Example #3
0
//*********************************************************************
//*	PWM output only works on the pins with hardware support. 
//*	These are defined in the appropriate pins_*.c file.
//*	For the rest of the pins, we default to digital output.
//*********************************************************************
void analogWrite(uint8_t pin, int val)
{

	// We need to make sure the PWM output is enabled for those pins
	// that support it, as we turn it off when digitally reading or
	// writing with them.  Also, make sure the pin is in output mode
	// for consistenty with Wiring, which doesn't require a pinMode
	// call for the analog output pins.
	pinMode(pin, OUTPUT);
	if (val == 0)
	{
		digitalWrite(pin, LOW);
	}
	else if (val == 255)
	{
		digitalWrite(pin, HIGH);
	}
	else
	{
		switch(digitalPinToTimer(pin))
		{
		#ifdef _OCMP1
			case TIMER_OC1:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC1( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256  );
				//Set duty cycle on fly
				SetDCOC1PWM((PWM_TIMER_PERIOD*val)/256);
				break;
		#endif

		#ifdef _OCMP2
			case TIMER_OC2:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC2( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256  );
				//Set duty cycle on fly
				SetDCOC2PWM((PWM_TIMER_PERIOD*val)/256);
				break;
		#endif

		#ifdef _OCMP3
			case TIMER_OC3:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC3( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256  );
				//Set duty cycle on fly
				SetDCOC3PWM((PWM_TIMER_PERIOD*val)/256);
				break;
		#endif

		#ifdef _OCMP4
			case TIMER_OC4:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC4( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256  );
				//Set duty cycle on fly
				SetDCOC4PWM((PWM_TIMER_PERIOD*val)/256);
				break;
		#endif

		#ifdef _OCMP5
			case TIMER_OC5:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC5( OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, (PWM_TIMER_PERIOD*val)/256, (PWM_TIMER_PERIOD*val)/256  );
				//Set duty cycle on fly
				SetDCOC5PWM((PWM_TIMER_PERIOD*val)/256);
				break;
		#endif

#if 0
//*	this is the original code, I want to keep it around for refernce for a bit longer
		#ifdef _OCMP1
			case TIMER_OC1:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC1( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) );

//			    SetDCOC1PWM((PWM_TIMER_PERIOD * val) / 256);
				break;
		#endif

		#ifdef _OCMP2
			case TIMER_OC2:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC2( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) );

//			    SetDCOC2PWM((PWM_TIMER_PERIOD * val) / 256);
				break;
		#endif

		#ifdef _OCMP3
			case TIMER_OC3:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC3( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) );

//			    SetDCOC3PWM((PWM_TIMER_PERIOD * val) / 256);
				break;
		#endif

		#ifdef _OCMP4
			case TIMER_OC4:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC4( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) );

//			    SetDCOC4PWM((PWM_TIMER_PERIOD * val) / 256);
				break;
		#endif

		#ifdef _OCMP5
			case TIMER_OC5:
				//* Open Timer2 with Period register value
				OpenTimer2(T2_ON | T2_PS_1_256, PWM_TIMER_PERIOD);
				OpenOC5( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH, 256, (256 - val) );

//			    SetDCOC5PWM((PWM_TIMER_PERIOD * val) / 256);
				break;
		#endif
#endif

			case NOT_ON_TIMER:
			default:
				if (val < 128)
				{
					digitalWrite(pin, LOW);
				}
				else
				{
					digitalWrite(pin, HIGH);
				}
		}
	}
}
Example #4
0
void ResetDevice(void)
{

    DMACONbits.SUSPEND =1;    //Suspend ALL DMA transfers

    BMXCONbits.BMXARB = 0x02; //Faster Refresh Rate
    BMXCONbits.BMXCHEDMA = 1;

    LCD_DC_TRIS =0;
    HSYNC_TRIS =0;
    LCD_CS_TRIS =0;
    VSYNC_TRIS =0;
    LCD_RESET_TRIS =0;

    BACKLIGHT_TRIS=0;
    DATA_ENABLE_TRIS=0;

    SRAM_TRIS =0;

    ADDR15_TRIS=0;
    ADDR16_TRIS=0;
    ADDR17_TRIS =0;
    ADDR18_TRIS =0;

    LCD_RESET =1;  
    LCD_CS    =1;      
    LCD_DC    =1;       

    SRAM_CS   =0;
    ADDR17    =0;     
    ADDR18    =0;

    PIXELCLOCK_TRIS =0;

    #ifdef DISP_INV_LSHIFT
    PIXELCLOCK =1;
    #else
    PIXELCLOCK =0;
    #endif

    #if defined(USE_TCON_MODULE)
    GfxTconInit();
    #endif

    // setup the PMP
    mPMPOpen(PMP_CONTROL, PMP_MODE, PMP_ADDRESS_LINES, PMP_INT_ON);
    PMADDR = 0x0000;

	// Open the desired DMA channel.
	DmaChnOpen(1, 0, DMA_OPEN_DEFAULT);

    // set the transfer event control: what event is to start the DMA transfer
     DmaChnSetEventControl(1, DMA_EV_START_IRQ(_TIMER_2_IRQ)); 

   	// set the transfer parameters: source & destination address, source & destination size, number of bytes per event
    #ifdef LCC_INTERNAL_MEMORY
    BACKLIGHT =0;     //Turn Backlight on

    #ifdef USE_PALETTE
    DmaChnSetTxfer(1, &GraphicsFrame[0], (void*)&PMDIN, HBackPorch, 2, 2); 
    #else
    DmaChnSetTxfer(1, &GraphicsFrame[0], (void*)&PMDIN, HBackPorch, 1, 2); 
    #endif
    #else    
    #if defined(GFX_USE_DISPLAY_PANEL_TFT_G240320LTSW_118W_E)
    BACKLIGHT =0;     //Turn Backlight on
    DmaChnSetTxfer(1, (void*)&PMDIN ,&GraphicsFrame[0] , 1, HBackPorch, 2);  
    #else
    BACKLIGHT =1;
    DmaChnSetTxfer(1, (void*)&PMDIN ,&GraphicsFrame[0] , 1, HBackPorch, 16);  
    #endif 
    #endif
    
    INTSetVectorPriority(INT_VECTOR_DMA(1), INT_PRIORITY_LEVEL_6);		        // set INT controller priority
    DmaChnSetEvEnableFlags(1, DMA_EV_BLOCK_DONE);	// enable the transfer done interrupt, when all buffer transferred
    INTEnable(INT_SOURCE_DMA(1), INT_ENABLED);		// enable the chn interrupt in the INT controller

    DCH1CONbits.CHPRI = 0b11;  //DMA channel has highest priority  

    // once we configured the DMA channel we can enable it
	DmaChnEnable(1);
    
   #ifdef LCC_INTERNAL_MEMORY
        #ifdef USE_PALETTE 
        OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 70); //Start Timer
        #else
   OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 27); //Start Timer
   #endif
   #else  //External Memory
   OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 2); //Start Timer
   #endif

   DMACONbits.SUSPEND = 0;

    #ifdef USE_DOUBLE_BUFFERING
    // initialize double buffering feature
    blInvalidateAll = 1;            
    blDisplayUpdatePending = 0;
    NoOfInvalidatedRectangleAreas = 0;
    _drawbuffer = GFX_BUFFER1;
    SetActivePage(_drawbuffer);
    SwitchOnDoubleBuffering();
    #endif //USE_DOUBLE_BUFFERING

}
Example #5
0
int main(void) {
	timesec=0;
	// set PIC32 to max computing power
	SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);
	INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
	INTEnableSystemMultiVectoredInt();   
    initLEDs(); 	
    LED0 = 1;
    LED1 = 1;
    initSerialNU32v2();
	setup_counters();

  	CloseADC10();
  	#define PARAM1  ADC_MODULE_ON | ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
  	#define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_ON | ADC_SAMPLES_PER_INT_16 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
  	#define PARAM3  ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_31
  	#define PARAM4  ENABLE_AN0_ANA | ENABLE_AN1_ANA | ENABLE_AN2_ANA | ENABLE_AN3_ANA | ENABLE_AN5_ANA | ENABLE_AN15_ANA
  	OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4,0);
  	EnableADC10();


    // Setup and turn off electromagnets
    EMAG1 = 0; EMAG2 = 0; 
    TRISEbits.TRISE7 = 0; TRISCbits.TRISC1 = 0;
	//Direction Output
	DIR = 1;
  	TRISAbits.TRISA9 = 0;
	//g-select Outputs
	GSEL1 = 0; GSEL2 = 0;
  	TRISEbits.TRISE2 = 0; TRISCbits.TRISC13= 0;
  	//0g Inputs
  	TRISAbits.TRISA0 = 1;
  	TRISAbits.TRISA4 = 1;

  	// 20kHz PWM signal, duty from 0-1000, pin D3
  	OpenTimer2(T2_ON | T2_PS_1_4, 1000);
  	OpenOC4(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0);
  	HBridgeDuty = 0;
  	SetDCOC4PWM(HBridgeDuty);

    // 20Hz ISR
    OpenTimer3(T3_ON | T3_PS_1_256, 15625);
    mT3SetIntPriority(1);
    mT3ClearIntFlag();
    mT3IntEnable(1);


	while(1) {
		if(start){
			EMAG2=0;
			SetDCOC4PWM(100);
			delaysec(delay1);
			SetDCOC4PWM(1000);		
			delaysec(delay2);
			SetDCOC4PWM(500);
			delaysec(delay3);
			EMAG2=1;
			SetDCOC4PWM(0);	

//			EMAG1=0;
//			SetDCOC4PWM(900);
//			DIR = 0;
//			delaysec(delay1);
//			SetDCOC4PWM(0);
//			delaysec(delay2);
//			SetDCOC4PWM(700);
//			delaysec(delay1);
//			SetDCOC4PWM(1000);
//			EMAG1=1;

			start=0;
		}
	}
}
int main(void)
{
    BOARD_Init();
    SYSTEMConfig(SYS_FREQ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
    SERIAL_Init();
    SPI_Init(SPI_SS1_CLKRATE, 0, 0);
    TIMERS_Init();
    printf("program begins...\n");
    uint16_t i, j;
    /* timer test: sucess
    SET_OUTPUT_PIN(D, 10);
    TIMERS_Init();
    while (1) {
        WRITE_HIGH(D, 10);
        InitTimer(1, 100);
        while (IsTimerActive(1));
        WRITE_LOW(D, 10);
        InitTimer(1, 100);
        while (IsTimerActive(1)) ;
    }
    //*/

    /* SPI read test 1: need to get 0x70 to ensure SPI reading is good
    printf("IMU: SPI read test 1\n");
    IMU_Init();
    uint8_t id;
    while (1) {
        id = MPU6500_readOneByte( MPU6500_RA_WHO_AM_I );
        printf( "id should be 0x70: 0x%x\n", id );
        InitTimer(1, 1000);
        while (IsTimerActive(1)) ;
    }
    //*/

    /* SPI read test 2: need to get 0x70 to ensure SPI reading is good
    printf("IMU: SPI read test 2\n");
    IMU_Init();
    uint8_t id;
    while (1) {
        MPU6500_readBytes(SPI_SS2_ID, MPU6500_RA_WHO_AM_I, 1, &id);
        printf( "id should be 0x70: 0x%x\n", id );
        InitTimer(1, 1000);
        while (IsTimerActive(1)) ;
    }
    //*/

    /* inv_mpu test: porting invense IMU library
    printf("inv_mpu test: read test\n");
    uint8_t buf[3] = {0};
    uint8_t flag;
    while (1) {
        flag = mpu_read_reg(MPU6500_RA_WHO_AM_I, buf);
        printf("flag: %d\n", flag);
        printf( "id should be 0x70: 0x%x\n", buf[0]);
        InitTimer(2, 1000);
        while (IsTimerActive(2)) {
            //printf("timer is still active\n");
        }
    }
    //*/

    /* porting inv_mpu test: init and read gyro
    printf("inv_mpu test: read test\n");
    uint16_t buf[3] = {0};
    uint8_t flag = mpu_init(0);
    mpu_reset_fifo();
    mpu_set_sensors(INV_XYZ_GYRO|INV_XYZ_ACCEL);
    mpu_configure_fifo(INV_XYZ_GYRO|INV_XYZ_ACCEL);
    printf("flag: %d\n", flag);

    uint16_t gyro[3], accel[3];

   // flag = mpu_run_6500_self_test(gyro, accel, 0);
    printf("flag=%d, gyro: % 5d, % 5d, % 5d", flag, gyro[0], gyro[1], gyro[2]);
    printf(", accel: % 5d, % 5d, % 5d\n", accel[0], accel[1], accel[2]);
    while (1) {
        flag = mpu_get_gyro_reg(buf, 0);
        printf("flag=%d, gyro: % 5d, % 5d, % 5d", flag, buf[0], buf[1], buf[2]);
        mpu_get_accel_reg(buf, 0);
        printf(", accel: % 5d, % 5d, % 5d\n", buf[0], buf[1], buf[2]);
        InitTimer(2, 1000);
        while (IsTimerActive(2));
    }
    //*/

    /* test: reading multiple times from the same register: the internal pointer +1 when it read once
    int vals;
    uint16_t gyro[3], accel[3];
    vals = mympu_open(200);
    printf("MPU Init: %d\n", vals);
    struct s_mympu mympu;
    uint8_t flag;
    while(1) {
        mpu_get_accel_reg(accel, 0);
        printf("all  accel: % 5d, % 5d, % 5d\n", accel[0], accel[1], accel[2]);
        uint8_t tmp = MPU6500_readOneByte(MPU6500_RA_ACCEL_XOUT_H);
        accel[0] = tmp << 8;
        tmp = MPU6500_readOneByte(MPU6500_RA_ACCEL_XOUT_L);
        accel[0] |= tmp;
        tmp = MPU6500_readOneByte(MPU6500_RA_ACCEL_YOUT_H);
        accel[1] = tmp << 8;
        tmp = MPU6500_readOneByte(MPU6500_RA_ACCEL_YOUT_L);
        accel[1] |= tmp;
        tmp = MPU6500_readOneByte(MPU6500_RA_ACCEL_ZOUT_H);
        accel[2] = tmp << 8;
        tmp = MPU6500_readOneByte(MPU6500_RA_ACCEL_ZOUT_L);
        accel[2] |= tmp;
        printf("byte accel: % 5d, % 5d, % 5d\n", accel[0], accel[1], accel[2]);

        InitTimer(2, 100);
        while (IsTimerActive(2));
    }
    //*/

    /* test: reading multiple times from the same register: the internal pointer +1 when it read once
    int vals;
    uint8_t buf[20] = {0};
    uint16_t gyro[3] = {0}, accel[3] = {0};
    vals = mympu_open(200);
    printf("MPU Init: %d\n", vals);
    //IMU_Init();
    uint8_t id = MPU6500_readOneByte( MPU6500_RA_WHO_AM_I );
    printf( "id should be 0x70: 0x%x\n", id);
    struct s_mympu mympu = {0};
    int flag;
    //MPU6500_writeOneByte( MPU6500_RA_USER_CTRL, 0x40 | 0x10 );
    while(1) {
        flag = mympu_update(&mympu);
        //printf("yaw=[%d, %d], pitch=[%d,%d], roll=[%d,%d]\n", mympu.ypr[0], mympu.gyro[0], mympu.ypr[1], mympu.gyro[1], mympu.ypr[2], mympu.gyro[2]);
        //printf("flag=%d, yaw=% 11f, pitch=% 11f, roll=% 11f\n", flag, mympu.ypr[0], mympu.ypr[1], mympu.ypr[2]);
        printf("yaw=[%.3f, %.3f], pitch=[%.3f,%.3f], roll=[%.3f,%.3f]\n",
        mympu.ypr[0], mympu.gyro[0], mympu.ypr[1], mympu.gyro[1], mympu.ypr[2], mympu.gyro[2]);
        //flag = dmp_read_fifo(gyro, accel, buf, NULL,&vals,&vals);

        //buf[0] = MPU6500_readOneByte(MPU6500_RA_FIFO_COUNTH);
        //buf[1] = MPU6500_readOneByte(MPU6500_RA_FIFO_COUNTL);
        //uint16_t count = (buf[0] << 8) | buf[1];
        //printf("fifo count: %d\n", count);

        //flag = mpu_read_fifo(gyro, accel, &vals, &vals, &vals);
        //flag = mpu_read_fifo_stream(10, buf, &vals);
        //read_fifo(buf, gyro, accel);
        //printf("flag=%d, gyro: % 5d, % 5d, % 5d", flag, gyro[0], gyro[1], gyro[2]);
       // printf(", accel: % 5d, % 5d, % 5", accel[0], accel[1], accel[2]);
        //printf(", quat: %d, %d, %d, %d\n", buf[0], buf[1], buf[2], buf[3]);

        InitTimer(2, 50);
        while (IsTimerActive(2));
    }
    //*/

    /* test: calibration by adding offset
    int vals = mympu_open(200);
    printf("MPU Init: %d\n", vals);
    uint8_t id = MPU6500_readOneByte( MPU6500_RA_WHO_AM_I );
    printf( "id should be 0x70: 0x%x\n", id);

    struct s_mympu mympu = {0};
    int flag;
    printf("waiting for initial drift\n");
    InitTimer(2, 20000); // wait for 20s to pass initial drift
    while (IsTimerActive(2));
    printf("get average offset\n");
    float offsetX = 0, offsetY = 0, offsetZ = 0;
    float offsetPrev[3] = {0};
    float alpha = 0;
    uint8_t k;
    for (k=1; k <= 200; k++) {
        flag = mympu_update(&mympu);
        //printf("yaw=% 3.3f, pitch=% 3.3f, roll=% 3.3f\n",
        //mympu.ypr[0], mympu.ypr[1], mympu.ypr[2]);
        alpha = ((float) k - 1.) / k;
        offsetX = alpha*offsetPrev[0] + (1. - alpha) * mympu.ypr[0];
        offsetY = alpha*offsetPrev[1] + (1. - alpha) * mympu.ypr[1];
        offsetZ = alpha*offsetPrev[2] + (1. - alpha) * mympu.ypr[2];
        offsetPrev[0] = offsetX;
        offsetPrev[1] = offsetY;
        offsetPrev[2] = offsetZ;
        InitTimer(2, 50);
        while (IsTimerActive(2));
    }
    printf("average: x offset= %3.3f, y offset= %3.3f, z offset= %3.3f\n",
        offsetX, offsetY, offsetZ);
    //offsetX = abs(offsetX);
    offsetY = abs(offsetY);
    //offsetZ = abs(offsetZ);

    while(1) {
        flag = mympu_update(&mympu);
        //printf("yaw=% 3.0f, pitch=% 3.0f, roll=% 3.0f\n",
        //printf("%4.1f, %4.1f, %4.1f\n", mympu.ypr[0] - offsetX, mympu.ypr[1] - offsetY, mympu.ypr[2] - offsetZ);

        printf("yaw=[%.3f, %.3f], pitch=[%.3f,%.3f], roll=[%.3f,%.3f]\n", mympu.ypr[0] - offsetX, mympu.gyro[0],        mympu.ypr[1] - offsetY, mympu.gyro[1],        mympu.ypr[2] - offsetZ, mympu.gyro[2]);

        InitTimer(2, 50);
        while (IsTimerActive(2));
    }
    //*/


    //* test: two SPI devices, OLED and IMU
    int vals = mympu_open(10);
    OLED_Init();
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 0x2710);
    ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);
    printf("MPU Init: %d\n", vals);
    uint8_t id = MPU6500_readOneByte( MPU6500_RA_WHO_AM_I );
    printf( "id should be 0x70: 0x%x\n", id);

    UG_SetForecolor(0x0F);
    UG_SetBackcolor(0x00);
    UG_FontSelect(FONT_6X8);
    char str[50] = "";
    while(1) {
        //mympu_update(&mympu);
        sprintf(str, "%.3f, %.3f, %.3f\n", mympu.ypr[0], mympu.ypr[1], mympu.ypr[2]);
        printf("%s\n",str);
        flag_writingScreen = 1;
        UG_PutString(0, 0, str);
        flag_writingScreen = 0;
        InitTimer(1, 200);
        while (IsTimerActive(1));
    }
    //*/

    /*
    flag = mpu_run_6500_self_test(gyro, accel, 0);
    printf("\nflag=%d, gyro: % 5d, % 5d, % 5d", flag, gyro[0], gyro[1], gyro[2]);
    printf(", accel: % 5d, % 5d, % 5d\n", accel[0], accel[1], accel[2]);

    MPU6500_writeOneByte(MPU6500_RA_XA_OFFS_H, (accel[0] & 0x7F80) >> 8);
    MPU6500_writeOneByte(MPU6500_RA_XA_OFFS_L_TC, accel[0] & 0x7F);
    MPU6500_writeOneByte(MPU6500_RA_YA_OFFS_H, (accel[1] & 0x7F80) >> 8);
    MPU6500_writeOneByte(MPU6500_RA_YA_OFFS_L_TC, accel[1] & 0x7F);
    MPU6500_writeOneByte(MPU6500_RA_ZA_OFFS_H, (accel[2] & 0x7F80) >> 8);
    MPU6500_writeOneByte(MPU6500_RA_ZA_OFFS_L_TC, accel[2] & 0x7F);

    MPU6500_writeOneByte(MPU6500_RA_XG_OFFS_USRH, gyro[0] >> 8);
    MPU6500_writeOneByte(MPU6500_RA_XG_OFFS_USRL, gyro[0] & 0xFF);
    MPU6500_writeOneByte(MPU6500_RA_YG_OFFS_USRH, gyro[1] >> 8);
    MPU6500_writeOneByte(MPU6500_RA_YG_OFFS_USRL, gyro[1] & 0xFF);
    MPU6500_writeOneByte(MPU6500_RA_ZG_OFFS_USRH, gyro[2] >> 8);
    MPU6500_writeOneByte(MPU6500_RA_ZG_OFFS_USRL, gyro[2] & 0xFF);
    //*/
    printf("program ended...\n");
    return 0;
}
/*****************************************************************
* Function:			setTime2PWM
* Input Variables:	none
* Output Return:	none
* Overview:			Initializes the timer 2 for pusle width modulation
******************************************************************/
void setTimer2PWM()
{
	OpenTimer2(TIMER_INT_OFF & T2_PS_1_1);	
}	
void main (void)
{
	/*
		Define Variables ---------------------------------------------------------------------
	*/
	// I2C/MSG Q variables
	char c;		// Is this used?
	signed char	length;
	unsigned char	msgtype;
	unsigned char last_reg_recvd;
	i2c_comm ic;
	//unsigned char msgbuffer[MSGLEN+1];
	unsigned char msgbuffer[12];
	unsigned char i;
	int I2C_buffer[];
	int index = 0;
	int ITR = 0;
	int I2C_RX_MSG_COUNT = 0;
	int I2C_RX_MSG_PRECOUNT = 0;
	int I2C_TX_MSG_COUNT = 1;

	// Timer variables
	timer1_thread_struct t1thread_data; 	// info for timer1_lthread
	timer0_thread_struct t0thread_data; 	// info for timer0_lthread
	int timer_on = 1;
	int timer2Count0 = 0, timer2Count1 = 0;

	// UART variables
	uart_comm uc;
	//uart_thread_struct	uthread_data; 		// info for uart_lthread


	// ADC variables
	int ADCVALUE = 0;
	int adc_counter = 0;
	int adc_chan_num = 0;
	int adcValue = 0;
	int count = 0;

	// MIDI variable
	char notePlayed;

	/*
		Initialization ------------------------------------------------------------------------
	*/
	

	// Clock initialization
	OSCCON = 0x7C; // 16 MHz	// Use for internal oscillator	
	OSCTUNEbits.PLLEN = 1; 		// 4x the clock speed in the previous line
	
	
	// UART initialization
	init_uart_recv(&uc);		// initialize my uart recv handling code
	// configure the hardware USART device
  	Open2USART( USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE & USART_EIGHT_BIT   & 
		USART_CONT_RX & USART_BRGH_LOW, 31);
	Open1USART( USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT   & 
		USART_CONT_RX & USART_BRGH_LOW, 51);

	//RCSTA1bits.CREN = 1;
	//RCSTA1bits.SPEN = 1;
	//TXSTA1bits.SYNC = 0;
	//PIE1bits.RC1IE = 1;
	IPR1bits.RC1IP = 0;
	
	// I2C/MSG Q initialization
	init_i2c(&ic);				// initialize the i2c code
	init_queues();				// initialize message queues before enabling any interrupts
	i2c_configure_slave(0x9E);	// configure the hardware i2c device as a slave

	// Timer initialization
	init_timer1_lthread(&t1thread_data);	// init the timer1 lthread
	OpenTimer0( TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_8);
	OpenTimer2( TIMER_INT_ON & T2_PS_1_16 /*& T2_8BIT_RW & T2_SOURCE_INT & T2_OSC1EN_OFF & T2_SYNC_EXT_OFF*/); // Turn Off
// ADC initialization
	// set up PORTA for input
	PORTA = 0x0;	// clear the port
	LATA = 0x0;		// clear the output latch
	TRISA = 0xFF;	// set RA3-RA0 to inputs
	ANSELA = 0xFF;	
	initADC();

	// Interrupt initialization
	// Peripheral interrupts can have their priority set to high or low
	// enable high-priority interrupts and low-priority interrupts
	enable_interrupts();
	// Decide on the priority of the enabled peripheral interrupts, 0 is low 1 is high
	IPR1bits.TMR1IP = 0;		// Timer1 interrupt
	//IPR1bits.RCIP = 0;			// USART RX interrupt
	IPR1bits.SSP1IP = 1;			// I2C interrupt
	PIE1bits.SSP1IE = 1;			// must specifically enable the I2C interrupts
	IPR1bits.ADIP = 1;			// ADC interrupt WE ADDED THIS

	
	// set direction for PORTB to output
	TRISB = 0x0;
	TRISD = 0xFF;
	LATB = 0x0;
	ANSELC = 0x00;


	/*
		Hand off messages to subroutines -----------------------------------------------------------
	*/
	// This loop is responsible for "handing off" messages to the subroutines
	// that should get them.  Although the subroutines are not threads, but
	// they can be equated with the tasks in your task diagram if you 
	// structure them properly.
  	while (1) {
		// Call a routine that blocks until either on the incoming
		// messages queues has a message (this may put the processor into
		// an idle mode
		block_on_To_msgqueues();
		/*
			High Priority MSGQ ----------------------------------------------------------------------
		*/
		
		// At this point, one or both of the queues has a message.  It 
		// makes sense to check the high-priority messages first -- in fact,
		// you may only want to check the low-priority messages when there
		// is not a high priority message.  That is a design decision and
		// I haven't done it here.
		length = ToMainHigh_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer);
		if (length < 0) {
			// no message, check the error code to see if it is concern
			if (length != MSGQUEUE_EMPTY) {
				//printf("Error: Bad high priority receive, code = %x\r\n", length);
			}
		} else {
			switch (msgtype) {
				case MSGT_ADC:	{				
					// Format I2C msg
					msgbuffer[6] = (timer2Count0 & 0x00FF);
					msgbuffer[5] = (timer2Count0 & 0xFF00) >> 8;
					msgbuffer[4] = (timer2Count1 & 0x00FF);
					msgbuffer[3] = (timer2Count1 & 0xFF00) >> 8;

					msgbuffer[8] = 0x00;
					msgbuffer[10] = adc_chan_num;
					msgbuffer[11] = 0xaa;			// ADC MSG opcode
					
					// Send I2C msg
					FromMainHigh_sendmsg(12, msgtype, msgbuffer);	// Send ADC msg to FromMainHigh MQ, which I2C
																	// int hdlr later Reads

					
					// Increment I2C message count from 1 to 100
					if(I2C_TX_MSG_COUNT < 100)	{
						I2C_TX_MSG_COUNT = I2C_TX_MSG_COUNT + 1;
					}
					else	{
						I2C_TX_MSG_COUNT = 1;
					}

					// Increment the channel number
					if(adc_chan_num <= 4)	adc_chan_num++;
					else	adc_chan_num = 0;
					// Set ADC channel based off of channel number
					if(adc_chan_num == 0)	SetChanADC(ADC_CH0);
					else if(adc_chan_num == 1)	SetChanADC(ADC_CH1);
					else if(adc_chan_num == 2)	SetChanADC(ADC_CH2);
					else if(adc_chan_num == 3)	SetChanADC(ADC_CH3);
					else if(adc_chan_num == 4)	SetChanADC(ADC_CH4);
					else	SetChanADC(ADC_CH5);
				};
				case MSGT_TIMER0: {
					timer0_lthread(&t0thread_data,msgtype,length,msgbuffer);

					break;
				};
				case MSGT_TIMER2:	{
					timer2Count0++;
					if(timer2Count0 >= 0xFFFF)	{	
						timer2Count1++;
						timer2Count0 = 0;
					}

					break;
				}

				case MSGT_I2C_DATA: { //this data still needs to be put in a buffer
;

						if(msgbuffer[0] == 0xaf)	{
						//FromMainLow_sendmsg(5, msgtype, msgbuffer);
						// The code below checks message 'counts' to see if any I2C messages were dropped
						//I2C_RX_MSG_COUNT = msgbuffer[4];
						
							FromMainLow_sendmsg(9, msgtype, msgbuffer);						
							TXSTA2bits.TXEN = 1;
	/*
							// Send note data to the MIDI device
							//while(Busy2USART());
							putc2USART(msgbuffer[1]);
							//while(Busy2USART());
							Delay1KTCYx(8);
							putc2USART(msgbuffer[2]);
							//while(Busy2USART());
							Delay1KTCYx(8);		
							putc2USART(msgbuffer[3]);
	*/
	
							if(I2C_RX_MSG_COUNT - I2C_RX_MSG_PRECOUNT == 1)	{
								if(I2C_RX_MSG_PRECOUNT < 99)	{
									I2C_RX_MSG_PRECOUNT++;
								}
								else	{
									I2C_RX_MSG_PRECOUNT = 0;
								}
							}
							else	{
								I2C_RX_MSG_PRECOUNT = I2C_RX_MSG_COUNT;
							}
						}
				};
				
	`			
				case MSGT_I2C_DBG: {
					//printf("I2C Interrupt received %x: ",msgtype);
					for (i=0;i<length;i++) {
						//printf(" %x",msgbuffer[i]);
					}
					//printf("\r\n");
					// keep track of the first byte received for later use
					last_reg_recvd = msgbuffer[0];
					break;
				};
				case MSGT_I2C_RQST: {
					//printf("I2C Slave Req\r\n");
					// The last byte received is the "register" that is trying to be read
					// The response is dependent on the register.
					switch (last_reg_recvd) {
						case 0xaa: {
							break;
						}
						/*
						case 0xa8: {
							length = 1;
							msgbuffer[0] = 0x3A;
							break;
						}					
						case 0xa9: {
							length = 1;
							msgbuffer[0] = 0xA3;
							break;
						}*/
					};
					//start_i2c_slave_reply(length,msgbuffer);
					break;
				};
				default: {
					//printf("Error: Unexpected msg in queue, type = %x\r\n", msgtype);
					break;
				};
			};
		}

		/*
			Low Priority MSGQ -----------------------------------------------------------------------
		*/
		
		length = ToMainLow_recvmsg(MSGLEN,&msgtype,(void *) msgbuffer);
		if (length < 0) {
			// no message, check the error code to see if it is concern
			if (length != MSGQUEUE_EMPTY) {
			
			}
		} else {
			switch (msgtype) {
				
				case MSGT_TIMER1: {
					timer1_lthread(&t1thread_data,msgtype,length,msgbuffer);
					break;
				};
				case MSGT_OVERRUN:
				case MSGT_UART_DATA: 
				{
					LATB = 0xFF;
					msgbuffer[11] = 0xBB;
					FromMainHigh_sendmsg(12, msgtype, msgbuffer);
					
					break;
				};
				default: {
					
					break;
				};
			};
		}
 	 }
Example #9
0
void InitApp(void)
{

    // PORT A is all f****d because of JTAG. Input only on A0 and A1 it seems...
    // Disable JTAG so we can use A4 and A5 for gpio (and maybe A0-A3 too?)
    DDPCONbits.JTAGEN = 0;


    // Set GPIO pin functionality:
    // ===========================

    // Note: how to set pins:
    // TRIS*CLR -> clears a pin/bit in the * port
    // TRIS*SET -> sets a pin/bit in the * port
    // TRIS*INV -> inverts a pin/bit in the * port

    // Note: UBW32 pin meaning:
    // Set the LED's on the UBW32 as outputs:
    // RE0 -> yellow
    // RE1 -> red
    // RE2 -> white
    // RE3 -> green
    //
    // RE6 -> PRG button
    // RE7 -> USER button

    // Cleared pins (set to zero) are configured as outputs.
    // This is the hard way to clear pins:
//    TRISECLR = 0x0F;
    
    // And this is the easy way:
    _TRISE0 = 0; 
    _TRISE1 = 0;
    _TRISE2 = 0;
    _TRISE3 = 0;



    // Set the PRG and USER buttons on the UBW32 as an inputs:
    // This is the hard way:
//    TRISESET = 0x00C0; // This sets pin RE7 and RE6: 1100 0000 = C0
    
    // This is the easy way:
    _TRISE6 = 1; // Set pins, set to one, are configured as inputs
    _TRISE7 = 1;
    
    // Set RE9 as an output to test the 44.1kHz interupt timing:
    _TRISE9 = 0;

    // Set pin RD8 as an output, could be written as TRISD = 0xFEFF;
    // but that takes more clock cycles to perform:
//    TRISDCLR = 0x0100;



    

    
    // PWM Intialization:
    // ==================
    // configure RD0 as output for PWM
    // PWM mode, Single output, Active High
    
//    TRISDCLR = 0x00000001;
//    OC1R 	 = 0;
//    OC1CON   = 0x0000;
//    OC1RS    = 0;
//    OC1CON   = 0x0006;
//    OC1CONSET = 0x8000;



    // Set up some interupts:
    // ======================
    OpenCoreTimer( 0xFFFFFFFF ); // This is needed for the delay functions

    // The next line: turn on timer2 | have it use an internal clock source | have it
    // use a prescaler of 1:256, and use a period of 0xFFFF or 2^16 cycles
    //
    // This fires an interrupt at a frequency of (80MHZ/256/65535), or 4.77
    // times a second.
//    OpenTimer2( T2_ON | T2_SOURCE_INT | T2_PS_1_256, 0xFFFF);

    // Set Timer2 to fire at 44.1kHz:
    // (80MHz/1/44100) = 1814.059 = 1814 = 0x0716 with a prescaler of 1:1
    OpenTimer2( T2_ON | T2_SOURCE_INT | T2_PS_1_1, 0x0716);

    /*Configure Multivector Interrupt Mode.  Using Single Vector Mode
    is expensive from a timing perspective, so most applications
    should probably not use a Single Vector Mode*/
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);

    // The next line seems to provide the exact same functionality...
    INTEnableSystemMultiVectoredInt();// Use multi-vectored interrupts:


    // This statement configured the timer to produce an interrupt with a priority of 2
    ConfigIntTimer2( T2_INT_ON | T2_INT_PRIOR_2);
}
Example #10
0
static void lcd_setup(void)
{
    //mPORTDSetPinsDigitalOut(BIT_3);
    //mPORTDClearBits(BIT_3);

    mPORTDSetPinsDigitalOut( BIT_2 );

    //Set backlight to open drain
    mPORTDOpenDrainOpen( BIT_2 );

    //Enable contrast control
    OpenTimer2(T2_ON, LCD_PWMPERIOD);
    OpenOC4( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x0458 );

    //Start the timer for contrast/brightness PWM
    //OpenTimer3(T3_ON | T3_PS_1_1 | T3_SOURCE_INT, LCD_PWMPERIOD);
    //PR3 = LCD_PWMPERIOD - 1;
    //OC3R = 0;
    //mT3SetIntPriority(4);
    //mT3ClearIntFlag();
    //mT3IntEnable(1);
    //SetDCOC3PWM(LCD_PWMPERIOD >> 2);
    //SetDCOC3PWM(1);

    //Enable brightness control
    OpenOC3( OC_ON | OC_TIMER_MODE16 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , LCD_PWMPERIOD, 0x500 );

    // Open ENABLE line as output
    LCD_EN_CLR();
    PORT_DIR_OUT(LCD_EN_P, LCD_EN);
    LCD_EN_CLR();

    // Open RW and RS lines as output
    LCD_RS_CLR();
    PORT_CLR(LCD_RW_P, LCD_RW);

    PORT_DIR_OUT(LCD_RS_P, LCD_RS);
    PORT_DIR_OUT(LCD_RW_P, LCD_RW);

    LCD_RS_CLR();
    PORT_CLR(LCD_RW_P, LCD_RW);

    // Set data lines as output
    PORT_CLR(LCD_DATA_P, LCD_DATA_MASK);
    PORT_DIR_OUT(LCD_DATA_P, LCD_DATA_MASK);
    PORT_CLR(LCD_DATA_P, LCD_DATA_MASK);

    // Wait for proper power up
    task_delay(LCD_LONG_DELAY);
    task_delay(LCD_LONG_DELAY);
    task_delay(LCD_LONG_DELAY);
    task_delay(LCD_LONG_DELAY);

    // Wait for the LCD to power up correctly
    lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS);
    task_delay(LCD_SHORT_DELAY);
    lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS);
    task_delay(LCD_VERY_SHORT_DELAY);
    lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS);
    task_delay(LCD_VERY_SHORT_DELAY);

    // Set up the LCD function
    lcd_command(LCD_FUNCTION_SET_CMD | LCD_FUNCTION_SET_8_BITS | LCD_FUNCTION_SET_2_LINES);
    task_delay(LCD_VERY_SHORT_DELAY);

    // Turn the display on
    lcd_command(LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_CURSOR_ON | LCD_DISPLAY_CTRL_BLINK_ON);
    task_delay(LCD_VERY_SHORT_DELAY);

    // Clear the display
    lcd_command(LCD_CLEAR_DISPLAY_CMD);
    task_delay(LCD_SHORT_DELAY);

    // Increase the cursor
    lcd_command(LCD_ENTRY_MODE_CMD | LCD_ENTRY_MODE_INCREASE);
    task_delay(LCD_SHORT_DELAY);

    lcd_command(LCD_DISPLAY_CTRL_CMD | LCD_DISPLAY_CTRL_DISPLAY_ON);
    task_delay(LCD_SHORT_DELAY);
}
Example #11
0
int setupTimer(int timer, int frequency, int priority)
{
    int tX_on;
    int tX_source_int;
    int tX_int_on;
    int tX_ps_1_X;

    long countTo = SYS_FREQ / frequency;

    switch (timer)
    {
        case 1:
            tX_on = T1_ON;
            tX_source_int = T1_SOURCE_INT;
            tX_int_on = T1_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 8;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 8;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 8;
                            if (countTo >= 65536)
                            {
                                return 0;
                            }
                            tX_ps_1_X = T1_PS_1_256;
                            break;
                        }
                        tX_ps_1_X = T1_PS_1_64;
                        break;
                    }
                    tX_ps_1_X = T1_PS_1_8;
                    break;
                }
                else
                {
                    tX_ps_1_X = T1_PS_1_1;
                    break;
                }
            }
            OpenTimer1(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer1(tX_int_on | priority);
            break;

        case 2:
            tX_on = T2_ON;
            tX_source_int = T2_SOURCE_INT;
            tX_int_on = T2_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 2;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 2;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 2;
                            if (countTo >= 65536)
                            {
                                countTo = countTo / 2;
                                if (countTo >= 65536)
                                {
                                    countTo = countTo / 2;
                                    if (countTo >= 65536)
                                    {
                                        countTo = countTo / 2;
                                        if (countTo >= 65536)
                                        {
                                            countTo = countTo / 2;
                                            if (countTo >= 65536)
                                            {
                                                return 0;
                                            }
                                            tX_ps_1_X = T2_PS_1_256;
                                            break;
                                        }
                                        tX_ps_1_X = T2_PS_1_64;
                                        break;
                                    }
                                    tX_ps_1_X = T2_PS_1_32;
                                    break;
                                }
                                tX_ps_1_X = T2_PS_1_16;
                                break;
                            }
                            tX_ps_1_X = T2_PS_1_8;
                            break;
                        }
                        tX_ps_1_X = T2_PS_1_4;
                        break;
                    }
                    tX_ps_1_X = T2_PS_1_2;
                    break;
                }
                else
                {
                    tX_ps_1_X = T2_PS_1_1;
                    break;
                }
            }
            OpenTimer2(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer2(tX_int_on | priority);
            break;

        case 3:
            tX_on = T3_ON;
            tX_source_int = T3_SOURCE_INT;
            tX_int_on = T3_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 2;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 2;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 2;
                            if (countTo >= 65536)
                            {
                                countTo = countTo / 2;
                                if (countTo >= 65536)
                                {
                                    countTo = countTo / 2;
                                    if (countTo >= 65536)
                                    {
                                        countTo = countTo / 2;
                                        if (countTo >= 65536)
                                        {
                                            countTo = countTo / 2;
                                            if (countTo >= 65536)
                                            {
                                                return 0;
                                            }
                                            tX_ps_1_X = T3_PS_1_256;
                                            break;
                                        }
                                        tX_ps_1_X = T3_PS_1_64;
                                        break;
                                    }
                                    tX_ps_1_X = T3_PS_1_32;
                                    break;
                                }
                                tX_ps_1_X = T3_PS_1_16;
                                break;
                            }
                            tX_ps_1_X = T3_PS_1_8;
                            break;
                        }
                        tX_ps_1_X = T3_PS_1_4;
                        break;
                    }
                    tX_ps_1_X = T3_PS_1_2;
                    break;
                }
                else
                {
                    tX_ps_1_X = T3_PS_1_1;
                    break;
                }
            }
            OpenTimer3(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer3(tX_int_on | priority);
            break;

        case 4:
            tX_on = T4_ON;
            tX_source_int = T4_SOURCE_INT;
            tX_int_on = T4_INT_ON;

            while (1)
            {
                if (countTo >= 65536)
                {
                    countTo = countTo / 2;
                    if (countTo >= 65536)
                    {
                        countTo = countTo / 2;
                        if (countTo >= 65536)
                        {
                            countTo = countTo / 2;
                            if (countTo >= 65536)
                            {
                                countTo = countTo / 2;
                                if (countTo >= 65536)
                                {
                                    countTo = countTo / 2;
                                    if (countTo >= 65536)
                                    {
                                        countTo = countTo / 2;
                                        if (countTo >= 65536)
                                        {
                                            countTo = countTo / 2;
                                            if (countTo >= 65536)
                                            {
                                                return 0;
                                            }
                                            tX_ps_1_X = T4_PS_1_256;
                                            break;
                                        }
                                        tX_ps_1_X = T4_PS_1_64;
                                        break;
                                    }
                                    tX_ps_1_X = T4_PS_1_32;
                                    break;
                                }
                                tX_ps_1_X = T4_PS_1_16;
                                break;
                            }
                            tX_ps_1_X = T4_PS_1_8;
                            break;
                        }
                        tX_ps_1_X = T4_PS_1_4;
                        break;
                    }
                    tX_ps_1_X = T4_PS_1_2;
                    break;
                }
                else
                {
                    tX_ps_1_X = T4_PS_1_1;
                    break;
                }
            }
            OpenTimer4(tX_on | tX_source_int | tX_ps_1_X, countTo);
            ConfigIntTimer4(tX_int_on | priority);
            break;
    }
    return 1;
}
Example #12
0
int main(void)
{
    int  value;
    int junk;
    millisec = 0;
    value = SYSTEMConfigWaitStatesAndPB( GetSystemClock() );

    // Enable the cache for the best performance
    CheKseg0CacheOn();

    //Setupt input for inteface button JF8 (RA01) (0x02)
    TRISASET = 0x02;
    //RED LED - JF9 (RA04)  (0x10)
    TRISACLR = 0x10;
    ODCACLR = 0x10;
    LATASET = 0x10;
    //Green LED -JF7 (RE9)  (0x200)
    TRISECLR = 0x200;
    ODCECLR = 0x200;
    LATESET = 0x200;
    //Setupt Input for DataFlag Button - JF10 - RA5 0x20
    TRISASET = 0x20;
    //Setup Output for Clutch Hold (Launch) JE1 RD14 0x4000
    //This function is active low, driving the FET on the PDU
    TRISDCLR = 0x4000;
    ODCDCLR = 0x4000;
    LATDSET = 0x4000; //Default state is high (off)

    CAN1Init();//CAN1 ACCL 500kbs
    CAN2Init();//Motec 1mbs
    DelayInit();

    initUART2(); // GPS UART
    prevButton1 = 0;
    prevButton2 = 0;
    millisec = 0;

   // Configure Timer 2 to request a real-time interrupt once per millisecond.
   // The period of Timer 2 is (16 * 5000)/(80 MHz) = 1 ms.
   OpenTimer2(T2_ON | T2_IDLE_CON | T2_SOURCE_INT | T2_PS_1_16 | T2_GATE_OFF, 5000);

   // Configure the CPU to respond to Timer 2's interrupt requests.
   INTEnableSystemMultiVectoredInt();
   INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_2);
   INTClearFlag(INT_T2);
   INTEnable(INT_T2, INT_ENABLED);

   //UART GPS Interrupts
   INTSetVectorPriority(INT_UART_2_VECTOR ,INT_PRIORITY_LEVEL_1); //Make sure UART interrupt is top priority
   INTClearFlag(INT_U2RX);
   INTEnable(INT_U2RX, INT_ENABLED);


    value = OSCCON;
    while (!(value & 0x00000020))
    {
        value = OSCCON;    // Wait for PLL lock to stabilize
    }

    deviceAttached = FALSE;

    //Initialize the stack
    USBInitialize(0);

    shouldLog = FALSE;
    shouldStop = FALSE;
    //count = 0;
    angularRateInfoRec = FALSE;
    accelerationSensorRec = FALSE;
    HRaccelerationSensorRec = FALSE;

       //init tim er 3 to convert adc at 100hz
    OpenTimer3(T3_ON|T3_PS_1_256|T3_SOURCE_INT, 1562);

    //initialize i2c for the psoc
    initI2CPSoC();
    
    state = wait;
    logNum = 0;

    initI2CEEPROM();
    short addy = 0x0000;
    BYTE num = 0x00;
    logNum = readEEPROM(addy);
    if(logNum >= 0xEF)  //Address stored in EEPROM  if greater than 0xEF reset to zero, limited to a single byte with current code configuration
    {
        writeEEPROM(addy, 0x00);
    }
    char GroupString[550];//Group Names (Line1)
    char UnitString[550];//Units (line2)
    char ParamString[650];//Paramater Names (line3)
    sprintf(GroupString,"Time,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Accelerometer,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Engine,Drivetrain,Drivetrain,Electrical,Drivetrain,Drivetrain,Drivetrain,Drivetrain,Engine,Engine,Engine,Engine,Electrical,Electrical,Electrical,Electrical,Electrical,Electrical,Suspension,Suspension,Suspension,Suspension,Suspension,Drivetrain,Driver\n");
    sprintf(UnitString,"ms,deg/s,deg/s,deg/s,m/s^2,m/s^2,m/s^2,m/s^2,m/s^2,m/s^2,rpm,%,kpa,degF,degF,lambda,psi,degF,na,na,psi,psi,V,mph,mph,mph,mph,s,gal,degF,degBTDC,mV,mV,mV,mV,mV,mV,mV,mV,mV,mV,mV,mV,\n");
    sprintf(ParamString, "Millisec,pitch(deg/sec),roll(deg/sec),yaw(deg/sec),lat(m/s^2),long(m/s^2),vert(m/s^2),latHR(m/s^2),longHR(m/s^2),vertHR(m/s^2),rpm,tps(percent),MAP(kpa),AT(degF),ect(degF),lambda,fuel pres,egt(degF),launch,neutral,brake pres,brake pres filtered,BattVolt(V),ld speed(mph), lg speed(mph),rd speed(mph),rg speed(mph),run time(s),fuel used,Oil Temp (deg F), Ignition Adv (degBTDC),Overall Consumption(mV),Overall Production(mV),Fuel Pump(mV),Fuel Injector(mV),Ignition(mV),Vref(mV),Back Left(mV),Back Right(mV),Front Left(mV),Front Right(mV),Steering Angle(mV),Brake Temp(mV),Data Flag,GPRMC,Time,Valid,Lat,N/S,Long,E/W,Speed,Course,Date,Variation,E/W\n");

    LATACLR = 0x10; //Turn on Red LED
   // LATECLR = 0x200;

    UARTSendString(UART2,PMTK_HOT_RESTART);
    int i = 0;
    while(!UARTTransmissionHasCompleted(UART2)){
        i++;
    }

    while(1)
    {
        GPSDataRead();
        GPSSentenceParse();
        ClutchHold(); //This function handles the venting direction of the clutch actuator
        DataFlagFunc(); //This function handles the updates of the data flag variable
        //USB stack process function
        USBTasks();

        switch(state){
            case wait:
                USBTasks();
                millisec = 0;
                if(CheckLogStateChange() == 1){ //start the transition from wait to log
                    state = startLog;
                }
                break;
            case startLog:
                //if thumbdrive is plugged in
                if(USBHostMSDSCSIMediaDetect())
                {
                    deviceAttached = TRUE;
                    //now a device is attached
                    //See if the device is attached and in the right format
                    if(FSInit())
                    {
                        //Opening a file in mode "w" will create the file if it doesn't
                        //  exist.  If the file does exist it will delete the old file
                        //  and create a new one that is blank.
                        logNum = readEEPROM(addy);
                        sprintf(nameString, "test%d.csv", logNum);
                        myFile = FSfopen(nameString,"w");
                        FSfwrite(GroupString,1,strlen(GroupString),myFile);
                        FSfwrite(UnitString,1,strlen(UnitString),myFile);
                        FSfwrite(ParamString,1, strlen(ParamString),myFile);
                        millisec = 0;
                        //LATDSET = 0x4000; //Send sync pulse (aeroprobe)
                       // while(millisec < 1000){} //Wait 1s then move to log, the aeroprobe ADC waits 1s.
                            state = log;
                        LATECLR = 0x200; //Turn on Green
                        LATASET = 0x10; //Turn off Red
                    }
                }
                break;
            case log:
                //This uses MOTEC as the master timer.  Data is only written to the USB after all the motec Data is received
                if(motec0Read && motec1Read && motec2Read && motec3Read && motec4Read && motec5Read){
                    WriteToUSB();
                }
                else{}//Wait for motec data to write the next row
                if(CheckLogStateChange() == 2){ //Start the transition from log to wait
                    state = stopLog;
                }
                if(millisec > 2000){
                    LATDCLR = 0x4000; //After 2 seconds pass no need to keep output high
                }
                //Add a function to check for a flag button and set a variable
                break;
            case stopLog:
                //Always make sure to close the file so that the data gets written to the drive.
                FSfwrite("endFile", 1, 7, myFile);
                FSfclose(myFile);
                state = wait;
                logNum++;
                writeEEPROM(addy, logNum);
                LATACLR = 0x10; //Turn on Red
                LATESET = 0x200; //Turn off Green
                break;
            default:
                state = wait;
                break;
        }


        //CAN Handlers
        CANRxMessageBuffer* CAN1RxMessage = CAN1RxMsgProcess();
        if(CAN1RxMessage){
            WriteAccelData(CAN1RxMessage); //Accel is on CAN 1
        }
        CANRxMessageBuffer* CAN2RxMessage = CAN2RxMsgProcess();
        if(CAN2RxMessage){
            writeCan2Msg(CAN2RxMessage); //Motec is on CAN 2
        }
    }
    return 0;
}
Example #13
0
void init(void)
{
unsigned char msgdata[ 8 ];

     vscp18f_init( TRUE );

	// Initialize the uP

	// PortA
	// RA0 -  SENS  - Input
	// RA1 -        - Output
	// RA2 -        - Output
	// RA3 -        - Output
	// RA4 -        - Output
	// RA5 -        - Output
	// RA6 - OSC
	TRISA = 0b00000001;
    PORTA = 0b00000000;

	// PortB
	// RB0 - RFID_RX- Input
	// RB1 -        - Output
	// RB2 - CAN TX - Output
	// RB3 - CAN RX - input
	// RB4 -  		- input
	// RB5 - 		- input
	// RB6 - 		- input
	// RB7 - 		- input 
	TRISB = 0b11111001;
    PORTB = 0b00000000;
	
	// RC0 -           - Output
	// RC1 - Button    - Input
    // RC2 - PWM       - Output
	// RC3 -           - Output
	// RC4 -           - Output
	// RC5 -           - Output
	// RC6 -           - Output
 	// RC7 -           - Output
	TRISC = 0b00000010;
    PORTC = 0b00000000;    

    // TIMERS    
    OpenTimer0(TIMER_INT_OFF  &               
               T0_16BIT       &
               T0_SOURCE_INT  &
               T0_PS_1_1
              );

    OpenTimer1(TIMER_INT_ON   &
               T1_16BIT_RW    &
               T1_SOURCE_INT  &
               T1_PS_1_1      &
               T1_OSC1EN_OFF
              );

    OpenTimer2(TIMER_INT_OFF  & 
               T2_PS_1_1      &
               T2_POST_1_1
              );

    // ADC

    OpenADC(ADC_FOSC_64       &
            ADC_RIGHT_JUST    &
            ADC_20_TAD        ,
            ADC_CH0           &
            ADC_INT_OFF       &
            ADC_REF_VDD_VREFMINUS /*ADC_VREFPLUS_VDD*/  &
            ADC_REF_VREFPLUS_VSS  /*ADC_VREFMINUS_VSS*/ ,
            0x0E);            
     
    // PWM 125 kHz
    PWM_Calibr(&PWM_f,&PWM_adc);
    OpenPWM1(PWM_f);// 79
    SetDCPWM1((PWM_f*2)-6);  // 156  
    PWM_enable = ON;

    RCONbits.IPEN = 1; // enable HI and LOW priorities

    // INT priority
    IPR1bits.TMR1IP = 0;      // Timer1 LOW priority

    OpenRB0INT( PORTB_CHANGE_INT_ON & RISING_EDGE_INT & PORTB_PULLUPS_OFF );

    // Global INT ebable
	INTCONbits.GIEH = 1;		
    INTCONbits.GIEL = 1;    

	return;
}
Example #14
0
int main()
{
    // Configure the device for maximum performance but do not change the PBDIV
    // Given the options, this function will change the flash wait states, RAM
    // wait state and enable prefetch cache but will not change the PBDIV.
    // The PBDIV value is already set via the pragma FPBDIV option above..
    SYSTEMConfig(F_SYS, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // Auto-configure the PIC32 for optimum performance at the specified operating frequency.
    SYSTEMConfigPerformance(F_SYS);

    // osc source, PLL multipler value, PLL postscaler , RC divisor
    OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_20, OSC_PLL_POST_1, OSC_FRC_POST_1);

    // Configure the PB bus to run at 1/4th the CPU frequency, so 20MHz.
    OSCSetPBDIV(OSC_PB_DIV_4);

    // Enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();
    INTEnableInterrupts();

    // Configure Timer 2 using PBCLK as input. We configure it using a 1:16 prescalar, so each timer
    // tick is actually at F_PB / 16 Hz, so setting PR2 to F_PB / 16 / 100 yields a .01s timer.
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_16, F_PB / 16 / 100);

    // Set up the timer interrupt with a medium priority of 4.
    INTClearFlag(INT_T2);
    INTSetVectorPriority(INT_TIMER_2_VECTOR, INT_PRIORITY_LEVEL_4);
    INTSetVectorSubPriority(INT_TIMER_2_VECTOR, INT_SUB_PRIORITY_LEVEL_0);
    INTEnable(INT_T2, INT_ENABLED);

/******************************** Your custom code goes below here ********************************/
    int check;
    OledInit();
    AdcInit();
    LEDS_INIT();
    check = GameInit();

    if(check == STANDARD_ERROR) {
        FATAL_ERROR();
    }
    float currPage;
    float binSize;
    float titleSize;
    float descSize;
    float numPages;
    uint8_t roomExit;
    uint16_t adcValue = 0;

    while(1) {
        roomExit = GameGetCurrentRoomExits();
        LEDS_SET(roomExit);
        while(buttonEvents == 0) {
            descSize = GameGetCurrentRoomDescription(roomData.description);
            titleSize = GameGetCurrentRoomTitle(roomData.title);

            numPages = ((titleSize + descSize) / MAX_OLED_PIXELS);
            binSize = (ADC_MAX_VALUE / numPages);

            if(AdcChanged()) {
                adcValue = AdcRead();
            }

            currPage = (adcValue / binSize);
            if(currPage < 1) {
                char titleArray[TITLE_OLED_SPACE] = {0};
                char descriptionBuffer[FIRST_PG_DESCRIPTION_OLED_SPACE] = {0};

                strncpy(descriptionBuffer, roomData.description, DESCRIPTION_COPY);
                sprintf(titleArray, "%s\n%s", roomData.title, descriptionBuffer);

                OledClear(OLED_COLOR_BLACK);
                OledDrawString(titleArray);
            } else {
                char buffer[MAX_OLED_PIXELS] = {0};
                int buffIndex;
                buffIndex = (int)currPage * MAX_OLED_PIXELS;
                strncpy(buffer, (roomData.description + buffIndex - OFFSET), MAX_OLED_PIXELS);

                OledClear(OLED_COLOR_BLACK);
                OledDrawString(buffer);
            }
            OledUpdate();
        }

        if((buttonEvents & BUTTON_EVENT_4UP) && (roomExit & GAME_ROOM_EXIT_NORTH_EXISTS)) {
            GameGoNorth();
        } else if((buttonEvents & BUTTON_EVENT_3UP) && (roomExit & GAME_ROOM_EXIT_EAST_EXISTS)) {
            GameGoEast();
        } else if((buttonEvents & BUTTON_EVENT_2UP) && (roomExit & GAME_ROOM_EXIT_SOUTH_EXISTS)) {
            GameGoSouth();
        } else if((buttonEvents & BUTTON_EVENT_1UP) && (roomExit & GAME_ROOM_EXIT_WEST_EXISTS)) {
            GameGoWest();
        }
        buttonEvents = BUTTON_EVENT_NONE;
    }



/**************************************************************************************************/
    while (1);
}
Example #15
0
int main(int argc, char** argv) {

    /*Configuring POSC with PLL, with goal FOSC = 80 MHZ */
    // Configure PLL prescaler, PLL postscaler, PLL divisor
    // Fin = 8 Mhz, 8  * (40/2/2) = 80
    PLLFBD = 18; // M=40          // change to 38 for POSC 80 Mhz - this worked only on a single MCU for uknown reason
    CLKDIVbits.PLLPOST = 0; // N2=2
    CLKDIVbits.PLLPRE = 0; // N1=2

    // Initiate Clock Switch to Primary Oscillator with PLL (NOSC=0b011)
    //__builtin_write_OSCCONH(0x03);

    // tune FRC
    OSCTUN = 23;  // 23 * 0.375 = 8.625 % -> 7.37 Mhz * 1.08625 = 8.005Mhz
    // Initiate Clock Switch to external oscillator NOSC=0b011 (alternative use FRC with PLL (NOSC=0b01)
    __builtin_write_OSCCONH(0b011);
    __builtin_write_OSCCONL(OSCCON | 0x01);

    // Wait for Clock switch to occur
    while (OSCCONbits.COSC!= 0b011);
    // Wait for PLL to lock
    while (OSCCONbits.LOCK!= 1);

     // local variables in main function
    int status = 0;
    int i = 0;
    int ax = 0, ay = 0, az = 0;
    int statusProxi[8];
    int slowLoopControl = 0;
    UINT16 timerVal = 0;
    float timeElapsed = 0.0;
    //extern UINT8 pwmMotor;
    extern UINT16 speakerAmp_ref;
    extern UINT16 speakerFreq_ref;
    extern UINT8 proxyStandby;
    UINT16 dummy = 0x0000;

    setUpPorts();
    delay_t1(50);

    PWMInit();
    delay_t1(50);

    ctlPeltier = 0;
    PeltierVoltageSet(ctlPeltier);
    FanCooler(0);
    diagLED_r[0] = 100;
    diagLED_r[1] = 0;
    diagLED_r[2] = 0;
    LedUser(diagLED_r[0], diagLED_r[1],diagLED_r[2]);

    // Speaker initialization - set to 0,1
    spi1Init(2, 0);
    speakerAmp_ref = 0;
    speakerAmp_ref_old = 10;
    speakerFreq_ref = 1;
    speakerFreq_ref_old = 10;
    int count = 0;
    UINT16 inBuff[2] = {0};
    UINT16 outBuff[2] = {0};

    while (speakerAmp_ref != speakerAmp_ref_old) {
        if (count > 5 ) {
            // Error !
            //LedUser(100, 0, 0);
            break;
        }

        inBuff[0] = (speakerAmp_ref & 0x0FFF) | 0x1000;

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], outBuff);
        chipDeselect(slaveVib);

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], &speakerAmp_ref_old);
        chipDeselect(slaveVib);

        count++;
    }

    count = 0;

    while (speakerFreq_ref != speakerFreq_ref_old) {
        if (count > 5 ) {
            // Error !
            //LedUser(0, 100, 0);
            break;
        }

        inBuff[0] = (speakerFreq_ref & 0x0FFF) | 0x2000;

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], outBuff);
        chipDeselect(slaveVib);

        chipSelect(slaveVib);
        status = spi1TransferWord(inBuff[0], &speakerFreq_ref_old);
        chipDeselect(slaveVib);

        count++;
    }

    accPin = aSlaveR;
    accPeriod = 1.0 / ACC_RATE * 1000000.0;  // in us; for ACC_RATE = 3200 Hz it should equal 312.5 us
    status = adxl345Init(accPin);
    ax = status;
    delay_t1(5);

    /* Init FFT coefficients */
    TwidFactorInit(LOG2_FFT_BUFF, &Twiddles_array[0],0);
    delta_freq = (float)ACC_RATE / FFT_BUFF;

    // read 100 values to calculate bias
    int m;
    int n = 0;
    for (m = 0; m < 100; m++) {

        status = readAccXYZ(accPin, &ax, &ay, &az);
        if (status <= 0) {
            //
        }
        else {
            ax_b_l += ax;
            ay_b_l += ay;
            az_b_l += az;
            n++;
        }
        delay_t1(1);
    }

    ax_b_l /= n;
    ay_b_l /= n;
    az_b_l /= n;

    _SI2C2IE = 0;
    _SI2C2IF = 0;

    // Proximity sensors initalization
    I2C1MasterInit();
    status = VCNL4000Init();

    // Cooler temperature sensors initalization
    status = adt7420Init(0, ADT74_I2C_ADD_mainBoard);
    delay_t1(1);
    muxCh = I2C1ChSelect(1, 6);
    status = adt7420Init(0, ADT74_I2C_ADD_flexPCB);

    // Temperature sensors initialization
    statusTemp[0] = adt7320Init(tSlaveF, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);
    statusTemp[1] = adt7320Init(tSlaveR, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);
    statusTemp[2] = adt7320Init(tSlaveB, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);
    statusTemp[3] = adt7320Init(tSlaveL, ADT_CONT_MODE | ADT_16_BIT);
    delay_t1(5);

    // Temperature estimation initialization
    for (i = 0; i < 50; i++) {
        adt7320ReadTemp(tSlaveF, &temp_f);
        delay_t1(1);
        adt7320ReadTemp(tSlaveL, &temp_l);
        delay_t1(1);
        adt7320ReadTemp(tSlaveB, &temp_b);
        delay_t1(1);
        adt7320ReadTemp(tSlaveR, &temp_r);
        delay_t1(1);
    }

    tempBridge[0] = temp_f;
    tempBridge[1] = temp_r;
    tempBridge[2] = temp_b;
    tempBridge[3] = temp_l;

    if (statusTemp[0] != 1)
        temp_f = -1;
    if (statusTemp[1] != 1)
        temp_r = -1;
    if (statusTemp[2] != 1)
        temp_b = -1;
    if (statusTemp[3] != 1)
        temp_l = -1;

    // CASU ring average temperature
    temp_casu = 0;
    tempNum = 0;
    tempSensors = 0;

    for (i = 0; i < 4; i++) {
        if (statusTemp[i] == 1 && tempBridge[i] > 20 && tempBridge[i] < 60) {
            tempNum++;
            temp_casu += tempBridge[i];
            tempSensors++;
        }
    }

    if (tempNum > 0)
        temp_casu /= tempNum;
    else
        temp_casu = -1;

    temp_casu1 = temp_casu;
    temp_wax = temp_casu;
    temp_wax1 = temp_casu;
    temp_model = temp_wax;

    temp_old[0] = temp_f;
    temp_old[1] = temp_r;
    temp_old[2] = temp_b;
    temp_old[3] = temp_l;
    temp_old[4] = temp_flexPCB;
    temp_old[5] = temp_pcb;
    temp_old[6] = temp_casu;
    temp_old[7] = temp_wax;

    for (i = 0; i < 4; i++) {
        uref_m[i] = temp_wax;
    }

    // Configure i2c2 as a slave device and interrupt priority 5
    I2C2SlaveInit(I2C2_CASU_ADD, BB_I2C_INT_PRIORITY);

    // delay for 2 sec
    for(i = 0; i < 4; i ++) {
        delay_t1(500);
        ClrWdt();
    }

    while (i2cStarted == 0) {
        delay_t1(200);
        ClrWdt();
    }

    dma0Init();
    dma1Init();

    CloseTimer4();
    ConfigIntTimer4(T4_INT_ON | TEMP_LOOP_PRIORITY);
    OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(2000, 256));

    CloseTimer5();
    ConfigIntTimer5(T5_INT_ON | FFT_LOOP_PRIORITY);
    OpenTimer5(T5_ON | T5_PS_1_256, ticks_from_ms(1000, 256));

    diagLED_r[0] = 0;
    diagLED_r[1] = 0;
    diagLED_r[2] = 0;
    LedUser(diagLED_r[0], diagLED_r[1],diagLED_r[2]);

    start_acc_acquisition();

    while(1) {

        ConfigIntTimer2(T2_INT_OFF);    // Disable timer interrupt
        IFS0bits.T2IF = 0;              // Clear interrupt flag
        OpenTimer2(T2_ON | T2_PS_1_256, 65535); // Configure timer

        if (!proxyStandby) {
            statusProxi[0] = I2C1ChSelect(1, 2);            // Front
            proxy_f = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[1] = I2C1ChSelect(1, 4);            // Back right
            proxy_br = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[2] = I2C1ChSelect(1, 3);            // Front right
            proxy_fr = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[3] = I2C1ChSelect(1, 5);            // Back
            proxy_b = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[4] = I2C1ChSelect(1, 0);            // Back left
            proxy_bl = VCNL4000ReadProxi();
            delay_t1(1);
            statusProxi[5] = I2C1ChSelect(1, 1);            // Front left
            proxy_fl = VCNL4000ReadProxi();
            delay_t1(1);
        }
        else {
            proxy_f = 0;            // Front
            proxy_br = 0;            // Back right
            proxy_fr = 0;            // Front right
            proxy_b = 0;            // Back
            proxy_bl = 0;            // Back left
            proxy_fl = 0;            // Front left
        }

        if (timer4_flag == 1) {
            // every 2 seconds
            CloseTimer4();
            ConfigIntTimer4(T4_INT_ON | TEMP_LOOP_PRIORITY);
            timer4_flag = 0;

            if (dma_spi2_started == 0) {
                OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(2000, 256));
                skip_temp_filter++;
                tempLoop();
            }
            else {
                OpenTimer4(T4_ON | T4_PS_1_256, ticks_from_ms(50, 256));
            }
        }

        if (dma_spi2_done == 1) {
            fftLoop();
            dma_spi2_done = 0;
        }
        if ((timer5_flag == 1) || (new_vibration_reference == 1)) {
            // every 1 seconds
            CloseTimer5();
            ConfigIntTimer5(T5_INT_ON | FFT_LOOP_PRIORITY);
            OpenTimer5(T5_ON | T5_PS_1_256, ticks_from_ms(1000, 256));

            timer5_flag = 0;
            if (new_vibration_reference == 1) {
            //if(1){
                CloseTimer3();
                dma0Stop();
                dma1Stop();
                spi2Init(2, 0);
                dma0Init();
                dma1Init();
                chipDeselect(aSlaveR);
                IFS0bits.DMA0IF = 0;
                delay_t1(30); // transient response
            }
            new_vibration_reference = 0;

            start_acc_acquisition();
        }

        // Cooler fan control
        if (fanCtlOn == 1) {
            if (temp_pcb >= 25 && fanCooler == FAN_COOLER_OFF)
                fanCooler = FAN_COOLER_ON;
            else if (temp_pcb <= 24 && fanCooler == FAN_COOLER_ON)
                fanCooler = FAN_COOLER_OFF;
            // In case of I2C1 fail turn on the fan
            if ((proxy_f == 0xFFFF) && (proxy_fr == 0xFFFF) && (proxy_br == 0xFFFF) && (proxy_b == 0xFFFF) && (proxy_bl == 0xFFFF) && (proxy_fl == 0xFFFF))
                fanCooler = FAN_COOLER_ON;
        }
        else if (fanCtlOn == 2)
            fanCooler = FAN_COOLER_ON;
        else
            fanCooler = FAN_COOLER_OFF;

        //TEST
//        temp_f = temp_model;
//        if (temp_ref < 30) {
//            temp_r = smc_parameters[0] * 10;
//        }
//        else {
//            temp_r = smc_parameters[0] / 2.0 * 10.0;
//        }
//        temp_r = alpha*10;
//        temp_b = sigma_m * 10;
//        temp_l = sigma * 10;
        //temp_flexPCB = temp_ref_ramp;
/*
        proxy_f = dma_spi2_started;
        proxy_fl = dma_spi2_done;
        proxy_bl = new_vibration_reference;
        proxy_b = timer5_flag;
        proxy_br = timer4_flag;
*/
        int dummy_filt = 0;
        for (i = 0; i < 8; i++) {
            if (index_filter[i] > 0){
                dummy_filt++;
            }
        }

        if (dummy_filt > 0) {
            filtered_glitch = dummy_filt;
            //for (i = 0; i< 8; index_filter[i++] = 0);
        }
        else {
            filtered_glitch = 0;
        }

        updateMeasurements();

        timerVal = ReadTimer2();
        CloseTimer2();
        timeElapsed = ms_from_ticks(timerVal, 256);
        //if (timeElapsed < MAIN_LOOP_DUR)
        //    delay_t1(MAIN_LOOP_DUR - timeElapsed);

        ClrWdt(); //Clear watchdog timer

    } // end while(1)
    return (EXIT_SUCCESS);
}
Example #16
0
// === Main  ======================================================
void main(void) {
    //SYSTEMConfigPerformance(PBCLK);
    ANSELA = 0; ANSELB = 0; 
    // === config threads ==========
    // turns OFF UART support and debugger pin, unless defines are set
    PT_setup();
    // === setup system wide interrupts  ========
    INTEnableSystemMultiVectoredInt();
    CloseADC10();	// ensure the ADC is off before setting the configuration
    #define PARAM1  ADC_FORMAT_INTG16 | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_OFF //
    // define setup parameters for OpenADC10
    // ADC ref external  | disable offset test | disable scan mode | do 1 sample | use single buf | alternate mode off
    #define PARAM2  ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_1 | ADC_ALT_BUF_OFF | ADC_ALT_INPUT_OFF
    // Define setup parameters for OpenADC10
    // use peripherial bus clock | set sample time | set ADC clock divider
    // ADC_CONV_CLK_Tcy2 means divide CLK_PB by 2 (max speed)
    // ADC_SAMPLE_TIME_5 seems to work with a source resistance < 1kohm
    #define PARAM3 ADC_CONV_CLK_PB | ADC_SAMPLE_TIME_5 | ADC_CONV_CLK_Tcy2 //ADC_SAMPLE_TIME_15| ADC_CONV_CLK_Tcy2
    // define setup parameters for OpenADC10
    // set AN11 and  as analog inputs
    #define PARAM4	ENABLE_AN11_ANA // pin 24
    // define setup parameters for OpenADC10
    // do not assign channels to scan
    #define PARAM5	SKIP_SCAN_ALL
    // use ground as neg ref for A | use AN11 for input A     
    // configure to sample AN11 
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN11 ); // configure to sample AN4 
    OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using the parameters defined above
    EnableADC10(); // Enable the ADC      
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, 400); 
    mPORTAClearBits(BIT_0 );		//Clear bits to ensure light is off.
    mPORTASetPinsDigitalOut(BIT_0 );    //Set port as output
    int CVRCON_setup;
    // set up the Vref pin and use as a DAC
    // enable module| eanble output | use low range output | use internal reference | desired step
    CVREFOpen( CVREF_ENABLE | CVREF_OUTPUT_ENABLE | CVREF_RANGE_LOW | CVREF_SOURCE_AVDD | CVREF_STEP_0 );
    // And read back setup from CVRCON for speed later
    // 0x8060 is enabled with output enabled, Vdd ref, and 0-0.6(Vdd) range
    CVRCON_setup = CVRCON; //CVRCON = 0x8060
	// Open the desired DMA channel.
	// We enable the AUTO option, we'll keep repeating the same transfer over and over.
	DmaChnOpen(dmaChn, 0, DMA_OPEN_AUTO);
	// set the transfer parameters: source & destination address, source & destination size, number of bytes per event
    // Setting the last parameter to one makes the DMA output one byte/interrut
    //DmaChnSetTxfer(dmaChn, sine_table, (void*) &CVRCON, sizeof(sine_table), 1, 1);
	// set the transfer event control: what event is to start the DMA transfer
    // In this case, timer2 
	DmaChnSetEventControl(dmaChn,  DMA_EV_START_IRQ(_TIMER_2_IRQ));
	// once we configured the DMA channel we can enable it
	// now it's ready and waiting for an event to occur...
	//DmaChnEnable(dmaChn);
  
    int i;
    for(i=0; i <256; i++){
        ball_scored[i] = 0x60|((unsigned char)((float)255/2*(sin(6.2832*(((float)i)/(float)256))+1))) ;
        //ball_lost[i]
       // game_over
    }

        
    // init the threads
    PT_INIT(&pt_timer);
    PT_INIT(&pt_adc);
    PT_INIT(&pt_launch);
    PT_INIT(&pt_anim);
    // init the display
    tft_init_hw();
    tft_begin();
    tft_fillScreen(ILI9340_BLACK);
    tft_setRotation(1); // Use tft_setRotation(1) for 320x240
    while (1){
        PT_SCHEDULE(protothread_timer(&pt_timer));
        PT_SCHEDULE(protothread_adc(&pt_adc));
        PT_SCHEDULE(protothread_launch_balls(&pt_launch));
        PT_SCHEDULE(protothread_anim_balls(&pt_anim));
    }
  } // main
Example #17
0
void configureTimeout(int secondes) {
    numberOfMillis = secondes * 1000;
    mT2ClearIntFlag();
    OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_64, 625);
    ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_2);
}
Example #18
0
File: PWM.c Project: gpeal/Sherman
void setupPWMTimer()
{
    OpenTimer2(T2_ON | T2_PS_1_4, 1000); // 20kHz PWM: 80000000Hz / 4ps / 1000 = 20 kHz
}
Example #19
0
void InitApp()
{
    _TRISA0 = 0; //led en sortie
    _TRISA1 = 0;
    led = 0;
    led2 = 0;

    //Sortie enable
    _TRISB9 = 0;
    _TRISB11 = 0;
    _ODCB9 = 1; //Open drain RB9 (dir 1)
    _ODCB11 = 1; //Open drain RB11 (dir 2)
    _ODCB10 = 1; //open drain RB10 PWM1H3

    //Le microswicth sur la pin RC5 (par exemple), on la met en entrée
    _TRISC5 = 1; //bumper bas de pince
    //Et on active la pullup qui va bien (registres CNPU1 et CNPU2)
    _CN26PUE = 1;

    _ODCC9 = 1; // Open drain sur la pin RC9 (pour les AX12)

    _TRISA4 = 1;
    _TRISA8 = 1;
    _TRISA9 = 1;
    _TRISB2 = 1; //RTS ?
    _TRISB3 = 1; //FLush ?
    _TRISA2 = 1; //CLK12 ?

    _TRISB4 = 1; // Arret d'urgence
    _CN1PUE = 1; // avec pullup
    _TRISB12 = 1; // switch 1
    _CN14PUE = 1; // avec pullup
    _TRISB13 = 1; // switch 2
    _CN13PUE = 1; // avec pullup
    _TRISB14 = 1; // switch 3
    _CN12PUE = 1; // avec pullup
    _TRISB15 = 1; // Laisse
    _CN11PUE = 1; // avec pullup

    OpenUART2(UART_EN & UART_IDLE_CON & UART_IrDA_DISABLE & UART_MODE_FLOW
              & UART_UEN_00 & UART_DIS_WAKE & UART_DIS_LOOPBACK
              & UART_DIS_ABAUD & UART_UXRX_IDLE_ONE & UART_BRGH_SIXTEEN
              & UART_NO_PAR_8BIT & UART_1STOPBIT,
              UART_INT_TX_BUF_EMPTY & UART_IrDA_POL_INV_ZERO
              & UART_SYNC_BREAK_DISABLED & UART_TX_ENABLE & UART_TX_BUF_NOT_FUL & UART_INT_RX_CHAR
              & UART_ADR_DETECT_DIS & UART_RX_OVERRUN_CLEAR,
              BRGVALAX12);

    ConfigIntUART2(UART_RX_INT_PR4 & UART_RX_INT_EN
                   & UART_TX_INT_PR4 & UART_TX_INT_DIS);

    OpenTimer2(T2_ON & T2_GATE_OFF & T2_PS_1_256 & T2_32BIT_MODE_OFF & T2_SOURCE_INT, 1500);
    ConfigIntTimer2(T2_INT_PRIOR_3 & T2_INT_ON); //Interruption ON et priorite 3

    OpenTimer5(T5_OFF & T5_GATE_OFF & T5_PS_1_1 & T5_SOURCE_INT, 40000);
    ConfigIntTimer5(T5_INT_PRIOR_2 & T5_INT_ON);

    OpenQEI1(QEI_DIR_SEL_QEB & QEI_INT_CLK & QEI_INDEX_RESET_DISABLE & QEI_CLK_PRESCALE_1
             & QEI_NORMAL_IO & QEI_MODE_x4_MATCH & QEI_UP_COUNT,0);
//    ConfigIntQEI1(QEI_INT_DISABLE);
//    WriteQEI1(65535);        //Valeur pour declencher l'interruption du module QEI

    _QEA1R = 5;     //Module QEI 1 phase A sur RB5
    _QEB1R = 6;     //Module QEI 1 phase B sur RB6
    POS1CNT = 0; //valeur QEI

    IFS2bits.SPI2IF = 0; // Flag SPI2 Event Interrupt Priority
    IPC8bits.SPI2IP = 2; // Priority SPI2 Event Interrupt Priority
    IEC2bits.SPI2IE = 1; //Enable SPI2 Event Interrupt Priority

    // activation de la priorité des interruptions
    _NSTDIS = 0;
}
Example #20
0
// main() ---------------------------------------------------------------------
//
int main(void)
{
    int pbClk;         // Peripheral bus clock

 	// Configure the device for maximum performance, but do not change
	// the PBDIV clock divisor.  Given the options, this function will
	// change the program Flash wait states, RAM wait state and enable
	// prefetch cache, but will not change the PBDIV.  The PBDIV value
	// is already set via the pragma FPBDIV option above.
   	pbClk = SYSTEMConfig(CPU_HZ, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

    // The pic32 has 2 programming i/f's: ICD/ICSP and JTAG. The
    // starter kit uses JTAG, whose pins are muxed w/ RA0,1,4 & 5.
    // If we wanted to disable JTAG to use those pins, we'd need:
#if defined M4557_DUINOMITE
    DDPCONbits.JTAGEN = 0;  // Disable the JTAG port.
#endif

    // Pins that share ANx functions (analog inputs) will default to
    // analog mode (AD1PCFG = 0x0000) on reset.  To enable digital I/O
    // Set all ones in the ADC Port Config register. (I think it's always
    // PORTB that is shared with the ADC inputs). PORT B is NOT 5v tolerant!
    // Also, RB6 & 7 are the ICD/ICSP lines.
    AD1PCFG = 0xffff;  // Port B as digital i/o.

 	//Initialize the DB_UTILS IO channel
	DBINIT();
	
#if defined ST7565_M4557_PROTOTYPE_STARTERKIT
    // Enable pullup resistors for Starter Kit's 3 switches.
    //CNPUE = (CN15_PULLUP_ENABLE | CN16_PULLUP_ENABLE | CN19_PULLUP_ENABLE);
    mCNOpen(CN_ON, CN15_ENABLE | CN16_ENABLE,
            CN15_PULLUP_ENABLE | CN16_PULLUP_ENABLE | CN19_PULLUP_ENABLE);
    // Read the port to clear any mismatch on change notice pins
    dummy = mPORTDRead();
    // Clear change notice interrupt flag
    ConfigIntCN(CHANGE_INT_ON | CHANGE_INT_PRI_2);
    // Ok now to enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();

    // Display the introduction
    DBPUTS("SW1:Set contrast; SW2:Halt display\n");
    //DBPRINTF("DBPRINTF: The build date and time is (" __DATE__ "," __TIME__ ")\n");

    // Init ports for ST7565 parallel prototype setup (using pic32 starter kit
    // and i/o expansion board): TODO: This belongs somewhere else!!
    //
    //    PIC32       NHD Display
    //    RB10        /CS1, /RES, A0, /WR, /RD
    //    RE0..7      D0..7
    LATBSET  = 0x7C00;   // Set the control bits high (inactive)
    TRISBCLR = 0x7C00;   // Set the control bits as outputs.

    // Init ESI touch-screen controller's (TSC2046) CS line as output
    LATFSET  = BIT_5;     // Set CSn inactive...
    TRISFCLR = BIT_5;     //    and set as output

    // Data is on port E.  The LCD controller (ST7565) uses its parallel
    // mode on port E, RE0..7. The touch screen controller shares port
    // shares a serial interface on some of these bits.
    TRISECLR = 0x00ff;   // Set the cmd/data bits as outputs.

#elif defined M4557_DUINOMITE

    // Init ports for the M4557.
    //
    // Control bits are on port B:
    //
    //    bit    M4557 function
    //    ----   --------------
    //      3     /CS1   - LCD Chip sel (ST7565 controller)
    //      4     /RES   - Reset
    //      6     A0     - 
    //      7     /WR    - Write
    //      9     /RD    - Read
    //      10    /TPCS  - Touch panel Chip Sel (TSC2046)
    //
    // Set the control bits low, and enable as outputs.
    LATBSET  = (BIT_3|BIT_4|BIT_6|BIT_7|BIT_9|BIT_10);
    TRISBCLR = (BIT_3|BIT_4|BIT_6|BIT_7|BIT_9|BIT_10);

    // Data is on port E.  The LCD controller (ST7565) uses its parallel
    // mode on port E, RE0..7. The touch screen controller shares port
    // shares a serial interface on some of these bits.
    TRISECLR = 0x00ff;   // Set the cmd/data bits as outputs.

#else
    #error Need product defined
#endif

    Nop();
    lcdInit(5,35);        // Init lcd controller
	Nop();

    // Output Compare (PWM) pins
    //
    //   OCn  64pin  100pin/port  SKII-J11-       Duinomite
    //   ---  -----  -----------  --------------  ----------
    //   OC1   46     72/RD0       19 (LED1,Red)   SOUND
    //   OC2   49     76/RD1       20 (LED2,Yel)   D13,SD_CLK
    //   OC3   50     77/RD2       17 (LED3,Grn)   D12,SD_MISO
    //   OC4   51     78/RD3       18              D11,SD_MOSI
    //   OC5   52     81           15              vga_hsync
    //

	// Init Timer 2 for use by the OC (PWM) module(s).
    // This will set the PWM frequency, f = pbClk/reloadValue.
    // Examples (pcClk = 40MHz):  
    //     f = pbClk/100000 = 400Hz
    //     f = pbClk/10000  = 4kHz
    //     f = pbClk/2500 = 20kHz
    // 
    //OpenTimer2(T2_ON | T2_32BIT_MODE_ON, 100000);  // f = pbClk/100000 = 400Hz
    //OpenTimer2(T2_ON | T2_32BIT_MODE_ON, 10000);  // f = pbClk/10000 = 4kHz
    OpenTimer2(T2_ON | T2_32BIT_MODE_ON, 2500);  // f = pbClk/2500 = 16kHz


	// I tried the uChip example using "OpenOC2", and as is ofter the
	// case, I can't get it to work, the documentation is lacking, and
	// it's easier to just read the datasheet and program the bloody
	// OCxCON register directly.
    //OpenOC2( OC_ON | OC_TIMER_MODE32 | OC_TIMER2_SRC | OC_CONTINUE_PULSE | OC_LOW_HIGH , 40000, 30000 );
    SetDCOC2PWM(0);
    OC2CON = 0x8026;  // OC on; 32bit-mode; PWM mode.

    SetDCOC3PWM(0);
    OC3CON = 0x8026;  // OC on; 32bit-mode; PWM mode.

/*
    int testOnly = 1;
    uint32_t loadVal = 0;
	int i;
    while(testOnly)
    {
		// Try the dimmer's 16 levels (0..15)
		for(i=0; i<16; i++)
		{
			loadVal = pwmTable1[i];
        	SetDCOC2PWM(loadVal);
        	delay_ms(300);
		}
    }
    CloseOC2();
*/

    // Do ESI M4557 demo application (dimmer-control type demo w/ touchscreen)
    esi_M4557();
    
    return 0;
}
Example #21
0
File: pu-test.c Project: gke/UAVP
void main(void)
{
	int8	i;

	DisableInterrupts;

	InitPorts();
	InitADC();

	OpenUSART(USART_TX_INT_OFF&USART_RX_INT_OFF&USART_ASYNCH_MODE&
			USART_EIGHT_BIT&USART_CONT_RX&USART_BRGH_HIGH, _B38400);
	
	OpenTimer0(TIMER_INT_OFF&T0_8BIT&T0_SOURCE_INT&T0_PS_1_16);
	OpenTimer1(T1_8BIT_RW&TIMER_INT_OFF&T1_PS_1_8&T1_SYNC_EXT_ON&T1_SOURCE_CCP&T1_SOURCE_INT);

	OpenCapture1(CAPTURE_INT_ON & C1_EVERY_FALL_EDGE); 	// capture mode every falling edge
	CCP1CONbits.CCP1M0 = NegativePPM;

	OpenTimer2(TIMER_INT_ON&T2_PS_1_16&T2_POST_1_16);		
	PR2 = TMR2_5MS;		// set compare reg to 9ms

	INTCONbits.TMR0IE = false;

	// setup flags register
	for ( i = 0; i<16; i++ )
		Flags[i] = false;

	_NoSignal = true;
	InitArrays();
	ReadParametersEE();
	ConfigParam = 0;

    ALL_LEDS_OFF;
	Beeper_OFF;

	LedBlue_ON;

	INTCONbits.PEIE = true;		// enable peripheral interrupts
	EnableInterrupts;

	LedRed_ON;		// red LED on

	Delay1mS(100);
	IsLISLactive();
#ifdef ICD2_DEBUG
	_UseLISL = 1;	// because debugger uses RB7 (=LISL-CS) :-(
#endif

	NewK1 = NewK2 = NewK3 = NewK4 =NewK5 = NewK6 = NewK7 = 0xFFFF;

	PauseTime = 0;

	InitBarometer();

	InitDirection();
	Delay1mS(COMPASS_TIME);

	// send hello text to serial COM
	Delay1mS(100);
	ShowSetup(true);
	Delay1mS(BARO_PRESS_TIME);
	while(1)
	{
		// turn red LED on of signal missing or invalid, green if OK
		// Yellow led to indicate linear sensor functioning.
		if( _NoSignal || !Switch )
		{
			LedRed_ON;
			LedGreen_OFF;
			if ( _UseLISL  )
				LedYellow_ON;
		}
		else
		{
			LedGreen_ON;
			LedRed_OFF;
			LedYellow_OFF;
		}

		ProcessComCommand();

	}
} // main
Example #22
0
void configTimers (void)
{
    //TIMER0
    OpenTimer0 (    TIMER_INT_ON &
            T0_8BIT &
            T0_EDGE_FALL &
            T0_PS_1_256 &
            T0_SOURCE_INT
            );              // Clock Interno, contador de 8 bits e Prescaler 1/256
    WriteTimer0 ( 0x3D );   // Gera um intervalo de 0,1 segundo a 4 mhz

    //TMR0ON=1;               // Liga o Timer0
    //extern volatile __bit TMR0ON              @ (((unsigned) &T0CON)*8) + 7;
    //#define               TMR0ON_bit          BANKMASK(T0CON), 7

    TMR0IF=0;               // Zera o Overflow do Timer0
    TMR0IE=1;               // Habilita a Interrupcao para o Overflow do Timer0

    //////////////////////////////////////////////////////////////////////

    // TIMER1
    OpenTimer1(TIMER_INT_ON &
               T1_SOURCE_EXT &
               T1_SYNC_EXT_OFF &
               T1_PS_1_1 &
               T1_OSC1EN_ON &
               T1_16BIT_RW
               );
    //WriteTimer1( 0xBE5  );   // equivalemnte a 1/2 segundo em 4,00 mhz
    WriteTimer1( 0x8000 );   // equivalente a 1 segundo em 32,768 khz

    //TMR1ON = 1;   //  Liga o Timer1 -> Igual ao TIMER_INT_ON
    TMR1IF = 0;     // Zera o Overflow para o Timer1
    TMR1IE=1;       // Habilita Interrupcao para o Overflow do Timer1
    //////////////////////////////////////////////////////////////////////

     // PWM TIMER2
     OpenPWM1(0x7C);    // Inicializa o PWM com intervalo de 2khz e PS_1/16
                        // num clock de 4 mhz
     SetDCPWM1(0x000F);  // Configura o Duty Cycle Inicial

     OpenTimer2(
             TIMER_INT_OFF &
             T2_PS_1_16
             );             // Para o PWM funcionar corretamente, a Interrupcao
                            // TIMER_INT_OFF (ou TMR2IE) deve ser desabilitada

     // obs: como o PWM funciona como um "temporizador", nao precisa executar
     // interrupcao ou acao; a propria porta de saida PWM1 ja executa

     //TMR2=0;    // Clear Timer2 -> Nao necessario para este exemplo
     //T2CKPS1=1; // Pre-Scaler 16 (ja setado no T2_PS1_16), redundante

     SetOutputPWM1( SINGLE_OUT , PWM_MODE_1 );    // Configura o CCP1CON
     // apenas o PWM1: P1A modulated; P1B, P1C, P1D assigned as port pins
     // no modo PxA,PxC active high, PxB,PxD active high */

     //CCP1IE=1;  // Habilita Interrupcao no modulo CCP1, Nao Necessario
     //TMR2IF=0;  // Limpando o flag de overflow do Timer2, Nao Necessario
     TMR2ON=1;  // Ligando o Timer2
     //TMR2IE=0;    // -> TIMER_INT_OFF , redundante

     // Pisca Verde/Vermelho 2 vezes para sinalizar inicio do PWM
    __delay_ms(100);LED_VERD=1;__delay_ms(100);LED_VERD=0;
    __delay_ms(100);LED_VERM=1;__delay_ms(100);LED_VERM=0;
    __delay_ms(100);LED_VERD=1;__delay_ms(100);LED_VERD=0;
    __delay_ms(100);LED_VERM=1;__delay_ms(100);LED_VERM=0;

    PEIE=1; //  Habilita As Interrupcoes dos Perifericos
    GIE=1;  //  Habilita as Interrupcoes Globais
}
Example #23
0
/**********************************************************************
 *	初期化.
 **********************************************************************
	t2config:
			bit ---- --xx = prescale
			bit -yyy y--- = postscale
			bit 7--- ---- = interrupt enable

	period: n (1〜255)
			1/n
 **********************************************************************
	prescale xx:
			 00=  1/1
			 01=  1/4
			 1x=  1/16
    postscale yyyy:
    		  0000 = 1/1
    		  0001 = 1/2
			  0010 = 1/3
			   ・・・
			  1111 = 1/16
 **********************************************************************
	分周比	TMR2IF = 12MHz * prescale * period * postscale ;
 */
void timer2_init(uchar t2config,uchar period)
{
    OpenTimer2(t2config);
	PR2 = period;
}
int main(void)
{
//LOCALS
	unsigned int temp;
	unsigned int channel1, channel2;
	M1_stepPeriod = M2_stepPeriod = M3_stepPeriod = M4_stepPeriod = 50; // in tens of u-seconds
	unsigned char M1_state = 0, M2_state = 0, M3_state = 0, M4_state = 0;

	SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

/* TIMER1 - now configured to interrupt at 10 khz (every 100us) */
	OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, T1_TICK);
	ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
/* TIMER2 - 100 khz interrupt for distance measure*/
	OpenTimer2(T2_ON | T2_SOURCE_INT | T2_PS_1_1, T2_TICK);
	ConfigIntTimer2(T2_INT_ON | T2_INT_PRIOR_3); //It is off until trigger

/* PORTA b2 and b3 for servo-PWM */
	mPORTAClearBits(BIT_2 | BIT_3);
	mPORTASetPinsDigitalOut(BIT_2 | BIT_3);

/* ULTRASONICS: some bits of PORTB for ultrasonic sensors */
	PORTResetPins(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11 );	
	PORTSetPinsDigitalOut(IOPORT_B, BIT_8 | BIT_9| BIT_10 | BIT_11); //trigger
/* Input Capture pins for echo signals */
	//interrupt on every risging/falling edge starting with a rising edge
	PORTSetPinsDigitalIn(IOPORT_D, BIT_8| BIT_9| BIT_10| BIT_11); //INC1, INC2, INC3, INC4 Pin
	mIC1ClearIntFlag();
	OpenCapture1(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//front
	ConfigIntCapture1(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);
	OpenCapture2(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//back
	ConfigIntCapture2(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);
	OpenCapture3(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//left
	ConfigIntCapture3(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);
	OpenCapture4(  IC_EVERY_EDGE | IC_INT_1CAPTURE | IC_TIMER2_SRC | IC_ON );//right
	ConfigIntCapture4(IC_INT_ON | IC_INT_PRIOR_4 | IC_INT_SUB_PRIOR_3);

/* PINS used for the START (RD13) BUTTON */
    PORTSetPinsDigitalIn(IOPORT_D, BIT_13);
	#define CONFIG          (CN_ON | CN_IDLE_CON)
	#define INTERRUPT       (CHANGE_INT_ON | CHANGE_INT_PRI_2)
	mCNOpen(CONFIG, CN19_ENABLE, CN19_PULLUP_ENABLE);
	temp = mPORTDRead();

/* PORT D and E for motors */
	//motor 1
	mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7); 		// Turn on PORTD on startup.
	mPORTDSetPinsDigitalOut(BIT_4 | BIT_5 | BIT_6 | BIT_7);	// Make PORTD output.
	//motor 2
	mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4); 		// Turn on PORTC on startup.
	mPORTCSetPinsDigitalOut(BIT_1 | BIT_2 | BIT_3 | BIT_4);	// Make PORTC output.
	//motor 3 and 4
	mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7); 		// Turn on PORTE on startup.
	mPORTESetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7);	// Make PORTE output.

// UART2 to connect to the PC.
	// This initialization assumes 36MHz Fpb clock. If it changes,
	// you will have to modify baud rate initializer.
    UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
    UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
    UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
    UARTSetDataRate(UART2, GetPeripheralClock(), BAUD);
    UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
	// Configure UART2 RX Interrupt
	INTEnable(INT_SOURCE_UART_RX(UART2), INT_ENABLED);
    INTSetVectorPriority(INT_VECTOR_UART(UART2), INT_PRIORITY_LEVEL_2);
    INTSetVectorSubPriority(INT_VECTOR_UART(UART2), INT_SUB_PRIORITY_LEVEL_0);


/* PORTD for LEDs - DEBUGGING */
	mPORTDClearBits(BIT_0 | BIT_1 | BIT_2);
	mPORTDSetPinsDigitalOut(BIT_0 | BIT_1 | BIT_2);

	

// Congifure Change/Notice Interrupt Flag
	ConfigIntCN(INTERRUPT);
// configure for multi-vectored mode
    INTConfigureSystem(INT_SYSTEM_CONFIG_MULT_VECTOR);
// enable interrupts
    INTEnableInterrupts();


	counterDistanceMeasure=600; //measure ULTRASONICS distance each 60 ms

	while (1) {
	
/***************** Robot MAIN state machine *****************/
		unsigned char ret = 0;
		switch (Robo_State) {
			case 0:
				MotorsON = 0;
				Robo_State = 0;

				InvInitialOrientation(RESET);
				TestDog(RESET);
				GoToRoom4short(RESET);
				BackToStart(RESET);
				InitialOrientation(RESET);
				GoToCenter(RESET);
				GoToRoom4long(RESET);
				break;
			case 1:
				ret = InvInitialOrientation(GO);
				if (ret == 1) {
					Robo_State = 2;
				}
				break;
			case 2:
				ret = TestDog(GO);
				if (ret == 1) {
					Robo_State = 3;		//DOG not found
				} else if (ret == 2) {
					Robo_State = 4;		//DOG found
				}
				break;
			case 3:
				ret = GoToRoom4short(GO);
				if (ret == 1) {
					Robo_State = 0;
				}
				break;
			case 4:
				ret = BackToStart(GO);
				if (ret == 1) {
					Robo_State = 5;
				}
				break;
			case 5:
				ret = GoToCenter(GO);
				if (ret == 1) {
					Robo_State = 6;
				}
				break;
			case 6:
				ret = GoToRoom4long(GO);
				if (ret == 1) {
					Robo_State = 0;
				}
				break;
		}

		if (frontDistance < 30 || backDistance < 30 || leftDistance < 30 || rightDistance < 30)
			mPORTDSetBits(BIT_0);
		else 
			mPORTDClearBits(BIT_0);
/***************************************************************/


/***************** Motors State Machine ************************/

		if (MotorsON) {
			/****************************
			MOTOR MAP
				M1 O-------------O M2   ON EVEN MOTORS, STEPS MUST BE INVERTED
					|	 /\		|			i.e. FORWARD IS BACKWARD
					|	/  \	|
					|	 || 	|
					|	 ||		|
				M3 O-------------O M4
			*****************************/
			if (M1_counter == 0) {
				switch (M1_state) {
					case 0: // set 0011
						step (0x3 , 1);
						if (M1forward)
							M1_state = 1;
						else
							M1_state = 3;
						break;
					case 1: // set 1001
						step (0x9 , 1);
						if (M1forward)
							M1_state = 2;
						else
							M1_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 1);
						if (M1forward)
							M1_state = 3;
						else
							M1_state = 1;
						break;
					case 3: // set 0110
					default:
						step (0x6 , 1);
						if (M1forward)
							M1_state = 0;
						else
							M1_state = 2;
						break;	
				}
				M1_counter = M1_stepPeriod;
				step_counter[0]--;
				if (directionNow == countingDirection)
					step_counter[1]--;
			}
			
			if (M2_counter == 0) {
				switch (M2_state) {
					case 0: // set 0011
						step (0x3 , 2);
						if (M2forward)
							M2_state = 1;
						else
							M2_state = 3;
						break;
					case 1: // set 0110
						step (0x6 , 2);
						if (M2forward)
							M2_state = 2;
						else
							M2_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 2);
						if (M2forward)
							M2_state = 3;
						else
							M2_state = 1;
						break;
					case 3: // set 1001
					default:
						step (0x9 , 2);
						if (M2forward)
							M2_state = 0;
						else
							M2_state = 2;
						break;	
				}
				M2_counter = M2_stepPeriod;
			}

			if (M3_counter == 0) {
				switch (M3_state) {
					case 0: // set 0011
						step (0x3 , 3);
						if (M3forward)
							M3_state = 1;
						else
							M3_state = 3;
						break;
					case 1: // set 1001
						step (0x9 , 3);
						if (M3forward)
							M3_state = 2;
						else
							M3_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 3);
						if (M3forward)
							M3_state = 3;
						else
							M3_state = 1;
						break;
					case 3: // set 0110
					default:
						step (0x6 , 3);
						if (M3forward)
							M3_state = 0;
						else
							M3_state = 2;
						break;	
				}
				M3_counter = M3_stepPeriod;
			}
			
			if (M4_counter == 0) {
				switch (M4_state) {
					case 0: // set 0011
						step (0x3 , 4);
						if (M4forward)
							M4_state = 1;
						else
							M4_state = 3;
						break;
					case 1: // set 0110
						step (0x6 , 4);
						if (M4forward)
							M4_state = 2;
						else
							M4_state = 0;
						break;
					case 2: // set 1100
						step (0xC , 4);
						if (M4forward)
							M4_state = 3;
						else
							M4_state = 1;
						break;
					case 3: // set 1001
					default:
						step (0x9 , 4);
						if (M4forward)
							M4_state = 0;
						else
							M4_state = 2;
						break;	
				}
				M4_counter = M4_stepPeriod;
			}
		} else {
			//motors off
			mPORTDSetBits(BIT_4 | BIT_5 | BIT_6 | BIT_7);
			mPORTCSetBits(BIT_1 | BIT_2 | BIT_3 | BIT_4);
			mPORTESetBits(BIT_0 | BIT_1 | BIT_2 | BIT_3 |
					BIT_4 | BIT_5 | BIT_6 | BIT_7);
		}
/************************************************************/
		

/******* TEST CODE, toggles the servos (from 90 deg. to -90 deg.) every 1 s. ********/
/*		if (auxcounter == 0) {
			
			servo1_angle = 0;

			if (servo2_angle == 90)
				servo2_angle = -90;
			else
				servo2_angle = 90;

			auxcounter = 20000;		// toggle angle every 2 s.
		}
*/

		servo1_angle = 0;
		servo2_angle = -90;
	/*
		if (frontDistance > 13 && frontDistance < 17) {
			servo2_angle = 90;
		}
		else
			servo2_angle = -90;
	*/
/*******************************************************************/


/****************** SERVO CONTROL ******************/
		/*
			Changing the global servoX_angle at any point in the code will 
			move the servo to the desired angle.
		*/
		servo1_counter = (servo1_angle + 90)*(18)/180 + 6; // between 600 and 2400 us
		if (servo1_period == 0) {
			mPORTASetBits(BIT_2);
			servo1_period = SERVOMAXPERIOD; 		/* 200 * 100us = 20000us period  */
		}

		servo2_counter = (servo2_angle + 90)*(18)/180 + 6; // between 600 and 2400 us
		if (servo2_period == 0) {
			mPORTASetBits(BIT_3);
			servo2_period = SERVOMAXPERIOD; 		/* 200 * 100us = 20000us period  */
		}
/*****************************************************/
	
	} /* end of while(1)  */
		
	return 0;
}
Example #25
0
/******************************************************************************
****                                                                       ****
**                                                                           **
init()

**                                                                           **
****                                                                       ****
******************************************************************************/
void init(void) {

  // Force WDT off for now ... having it on tends to confuse novice
  //   users.
  csk_wdt_off();
  

  // Keep interrupts off for now ...
  __disable_interrupt();

  
  // All CSK control signals are active LOW.
  #if   defined(__PIC24FJ256GA110__) // PSPM D

  // Minimal set of I/O ... only necessary control signals are configured as outputs.
  TRISA = 0xFFFF;
  TRISB = ~( BIT5); // TX3
  TRISC = ~( BIT1); // -OE_USB
  TRISD = ~( BIT8+BIT5+BIT2+BIT1); // HS3, HS4 & HS5, TX2
  TRISE = ~( BIT8+BIT4+BIT3+BIT2); // IO.30, -ON_SD, -ON_MHX & -OE_MHX
  TRISF = ~( BIT5+BIT3); // TX1 & TX0

  PORTA = 0x0000;
  PORTB = 0x0000+BIT5;              // TX3 initially HIGH
  PORTC = 0x0000+BIT1;              // -OE_USB is OFF
  PORTD = 0x0000+BIT8;              // TX2 initially HIGH
  PORTE = 0x0000+BIT4+BIT3+BIT2;   	// -ON_SD, -ON_MHX, -OE_MHX are OFF
  PORTF = 0x0000+BIT3+BIT5;			// TX0 & TX1 initially HIGH.
  PORTG = 0x0000;

  AD1PCFGL = 0xFFFF;

  #elif defined(__PIC24FJ256GB110__) // PSPM E Rev A

  // Minimal set of I/O ... only necessary control signals are configured as outputs.
  TRISA = 0xFFFF;
  TRISB = ~(                                                             BIT5                    ); // TX3
  TRISC = ~(                                                                            BIT1     ); // -OE_USB
  TRISD = ~(                                    BIT9+BIT8+BIT7+          BIT5                    ); // TX0, TX2, HS3, HS5
  TRISE = ~(                                         BIT8+               BIT4+BIT3+BIT2          ); // IO.30, -ON_SD, -ON_MHX & -OE_MHX
  TRISF = ~(                                                        BIT5                         ); // TX1
  TRISG = ~(BIT15                                                                                ); // HS4

  PORTA = 0x0000;
  PORTB = 0x0000+BIT5;           // TX3 initially HIGH
  PORTC = 0x0000+BIT1;           // -OE_USB is OFF
  PORTD = 0x0000+BIT9+BIT8;      // TX0, TX2 initially high
  PORTE = 0x0000+BIT4+BIT3+BIT2; // -ON_SD, -ON_MHX, -OE_MHX are OFF
  PORTF = 0x0000+BIT5;	         // TX1 initially high.
  PORTG = 0x0000;

  #elif defined(__PIC24FJ256GB210__) // PSPM E Rev B
  
  // Minimal set of I/O ... only necessary control signals are configured as outputs.
  TRISA = 0xFFFF;
  TRISB = ~(                                                             BIT5                    ); // TX3
  TRISC = ~(                                                                            BIT1     ); // -OE_USB
  TRISD = ~(                                    BIT9+BIT8+BIT7+          BIT5                    ); // TX0, TX2, HS3, HS5
  TRISE = ~(                                         BIT8+               BIT4+BIT3+BIT2          ); // IO.30, -ON_SD, -ON_MHX & -OE_MHX
  TRISF = ~(                                                        BIT5                         ); // TX1
  TRISG = ~(BIT15                                                                                ); // HS4

  PORTA = 0x0000;
  PORTB = 0x0000+BIT5;           // TX3 initially HIGH
  PORTC = 0x0000+BIT1;           // -OE_USB is OFF
  PORTD = 0x0000+BIT9+BIT8;      // TX0, TX2 initially high
  PORTE = 0x0000+BIT4+BIT3+BIT2; // -ON_SD, -ON_MHX, -OE_MHX are OFF
  PORTF = 0x0000+BIT5;	         // TX1 initially high.
  PORTG = 0x0000;

  ANSA = 0x0000;
  ANSB = 0x0000;
  ANSC = 0x0000;
  ANSD = 0x0000;
  ANSE = 0x0000;
  ANSF = 0x0000;
  ANSG = 0x0000;
  #else
  #error PIC24F device not supported by CubeSat Kit
  #endif 

  // High-level inits (works at any clock speed).
  csk_mhx_close();
  csk_mhx_pwr_off();
  csk_usb_close();
  csk_led_status_close();
  

  // Set up to run with primary oscillator.
  // See _CONFIG2 above. A configuration-word-centric setup of the
  //  oscillator(s) was chosen because of its relative simplicity.
  //  Note e.g. that PwrMgnt_OscSel() returns FALSE if clock switching
  //  (FCKSM) is disabled ...


  // Set up Timer2 to run at system tick rate                
  ConfigIntTimer2(T2_INT_ON & T2_INT_PRIOR_1);   // Timer is configured for 10 msec (100Hz), with interrupts
  OpenTimer2(T2_ON & T2_IDLE_CON & T2_GATE_OFF & T2_PS_1_1 & T2_32BIT_MODE_OFF & T2_SOURCE_INT,
             (MAIN_XTAL_FREQ/(2*100)));        // A prescalar is not required because 8E6/200 < 16 bits.


  #if   defined(__PIC24FJ256GA110__) // PSPM D
  // Configure I/O pins for UARTs via PIC24's PPS system.
  // RP inputs must be configured as inputs!
  // CSK UART0 is used as the terminal, via USB, IO.6(RP17) & IO.7(RP10)
  // CSK UART0 (PIC24 UART1) TX/RX = IO.6/IO.7
  iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP10);
  iPPSOutput(OUT_PIN_PPS_RP17,OUT_FN_PPS_U1TX);
  
  // CSK UART1 can talk to GPSRM 1 via IO.4(RP16) & IO.5(RP30)
  // CSK UART1 (PIC24 UART2) TX/RX = IO.4/IO.5
  iPPSInput(IN_FN_PPS_U2RX,IN_PIN_PPS_RP30);
  iPPSOutput(OUT_PIN_PPS_RP16,OUT_FN_PPS_U2TX);

  // CSK UART2 can talk to GPSRM 1 via IO.16(RP2) & IO.17(RP22)
  // CSK UART2 (PIC24 UART3) TX/RX = IO.16/IO.17
  iPPSInput(IN_FN_PPS_U3RX,IN_PIN_PPS_RP22);
  iPPSOutput(OUT_PIN_PPS_RP2,OUT_FN_PPS_U3TX);

  // CSK UART3 can talk to GPSRM 1 via IO.32(RP18) & IO.33(RP28)
  // CSK UART3 (PIC24 UART4) TX/RX = IO.4/IO.5
  iPPSInput(IN_FN_PPS_U4RX,IN_PIN_PPS_RP28);
  iPPSOutput(OUT_PIN_PPS_RP18,OUT_FN_PPS_U4TX);
  #elif defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GB210__)
  iPPSInput(IN_FN_PPS_U1RX,IN_PIN_PPS_RP10); 	// RF4
  iPPSOutput(OUT_PIN_PPS_RP17,OUT_FN_PPS_U1TX);	// RF5
  iPPSInput(IN_FN_PPS_U2RX,IN_PIN_PPS_RP30);	// RF2
  iPPSOutput(OUT_PIN_PPS_RP4,OUT_FN_PPS_U2TX);	// RD9 
  iPPSInput(IN_FN_PPS_U3RX,IN_PIN_PPS_RP22);	// RD3
  iPPSOutput(OUT_PIN_PPS_RP2,OUT_FN_PPS_U3TX);	// RD8
  iPPSInput(IN_FN_PPS_U4RX,IN_PIN_PPS_RP28);	// RB4
  iPPSOutput(OUT_PIN_PPS_RP18,OUT_FN_PPS_U4TX);	// RB5
  #else
  #error PIC24F device not supported by CubeSat Kit
  #endif 


  // Init UARTs to 9600,N,8,1  
  // UARTs won't transmit until interrupts are enabled ...
  csk_uart0_open(CSK_UART_9600_N81);
  csk_uart1_open(CSK_UART_9600_N81);
  csk_uart2_open(CSK_UART_9600_N81);
  csk_uart3_open(CSK_UART_9600_N81);

  csk_usb_open();
  csk_uart0_puts(STR_CRLF STR_CRLF);
  csk_uart0_puts("Pumpkin " STR_CSK_TARGET " " STR_APP_NAME "." STR_CRLF);
  csk_uart0_puts(STR_VERSION "." STR_CRLF);
  csk_uart0_puts(STR_WARNING "." STR_CRLF);

  i2c1_open();
    
} /* init() */