Exemplo n.º 1
0
void delayus(unsigned t) {
    OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);
    while (t--) { // t x 1ms loop
        WriteTimer1(0);
        while (ReadTimer1() < SYS_FREQ / 256 / 1000000);
    }
    CloseTimer1();
}// Delayus
Exemplo n.º 2
0
void DelayMs(int cms)
{
	int		ims;

	for (ims=0; ims<cms; ims++) {
		WriteTimer1(0);
		while (ReadTimer1() < cntMsDelay);
	}

}		
Exemplo n.º 3
0
void delayms(unsigned t)
// This uses Timer 1, can be changed to another timer.
{
    OpenTimer1(T1_ON | T1_PS_1_256, 0xFFFF);
    while (t--) { // t x 1ms loop
        WriteTimer1(0);
        while (ReadTimer1() < SYS_FREQ / 256 / 1000);
    }
    CloseTimer1();
} // Delayms
// A function called by the interrupt handler
// This one does the action I wanted for this program on a timer1 interrupt
void timer1_int_handler()
{
	unsigned int result;

	// read the timer and then send an empty message to main()
	result = ReadTimer1();
	ToMainLow_sendmsg(0,MSGT_TIMER1,(void *) 0);
	
	// reset the timer
	WriteTimer1(0);
}
Exemplo n.º 5
0
void timer1_int_handler() {
    unsigned int result;

    // read the timer and then send an empty message to main()
#ifdef __USE18F2680
    LATBbits.LATB1 = !LATBbits.LATB1;
#endif

    result = ReadTimer1();
    ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0);

    // reset the timer
    WriteTimer1(0);
}
// ADC interrupt hdlr
// This interrupt is drivin high every time the ADC conversion is complete
void adc_int_handler()	{
	unsigned char val[3];

	// Read ADC, then store the values into message buffer
	val[0] = ADRESH;
	val[1] = ADRESL;
	
	// Read the timer to determine when the ADC was read
	val[2] = ReadTimer1();

	ToMainHigh_sendmsg(3, MSGT_ADC, (void *) val);	// Send ADC value to ToMainHigh MSGQ
	PIR1bits.ADIF = 0;								// Reset the ADC interrupt

}
Exemplo n.º 7
0
/* Specify interupt priority 1, but stop the interupt once detected */
void __ISR(_EXTERNAL_2_VECTOR, IPL1) _Int2Handler(void) {

    uint state = getState();
    if(STATE_BEGINING == state)
    {
        /* STEP 1. configure the Timer1*/
        mT1ClearIntFlag();
        OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, 64000);
        /* STEP 2. set the timer interrupt to prioirty level 2 */
        ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
        writeBit(1);
        writeBit(1);
        setState(STATE_FIRST_EDGE);
    }
    else if( STATE_FIRST_EDGE == state)
    {
          ConfigIntTimer1(T1_INT_OFF);
          setEtu(ReadTimer1()/3);
          OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, getEtu()/5);
          /* STEP 2. set the timer interrupt to prioirty level 2 */
          ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
          /*Stopping the timer 1 and reading the value*/
          DisableINT2;
          
          /*the two first bits of ts are sets*/
          setState(STATE_TS);
          bitIndex = 10 ;
    }
    else
    {
        /* state == normal processing or no atr but an etu value is set*/
          DisableINT2;
          OpenTimer1(T1_ON | T1_SOURCE_INT | T1_PS_1_1, getEtu());
          ConfigIntTimer1(T1_INT_ON | T1_INT_PRIOR_2);
    }
     /* The flag is cleared at the end to avoid stack overflow
    * PORTDbits.RD0 is the EXTERNAL_0_VECTOR*/

    mINT2ClearIntFlag();
}
Exemplo n.º 8
0
void timer1_int_handler() {
    unsigned int result;

    #ifdef __USE18F26J50
        #ifdef DEBUG
    LATAbits.LA0 ^= 0x1;//Test to see if the handler works.
        #endif
    #endif

    ADCON0bits.GO_NOT_DONE = 1;////////////////////////////////////Needed to activate the A/D converter interupt

    

    // read the timer and then send an empty message to main()
#ifdef __USE18F2680
    LATBbits.LATB1 = !LATBbits.LATB1;
#endif

    result = ReadTimer1();
    ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0);

    // reset the timer
    WriteTimer1(0);
}
void updateStatusOfSensor(Sensor sensor)
{
    UINT8 byte3;
    UINT8 i;
    SensorStatus res;

    if(!isSensorReadReady)
    {
        prepareForSensorRead();
    }

    res.sensor = sensor;

    CLOCK_PORT = 1;

    SPI_TIME_OFFSET

    readTime = ReadTimer1();

    selectSensor(sensor);

    // Waiting 500 ns for sensor to be ready (Period = 83 ns => about 7 cycles, waiting 20)

    SPI_TIME_OFFSET
    

    res.position = 0;

    for(i=0 ; i<13 ; i++)
    {
        res.position <<= 1;

        CLOCK_PORT = 1;

        SPI_TIME_OFFSET_UP

        CLOCK_PORT = 0;

        SPI_TIME_OFFSET

        res.position += DATA_IN;
    }

    byte3 = 0;

    for(i=0 ; i<6 ; i++)
    {
        byte3 <<= 1;

        CLOCK_PORT = 1;

        SPI_TIME_OFFSET_UP

        CLOCK_PORT = 0;

        SPI_TIME_OFFSET

        byte3 += DATA_IN;


    }
    
    SPI_TIME_OFFSET

    LATA |= 0x3F; // Return to waiting state

    SPI_TIME_OFFSET

    CLOCK_PORT = 1;

    res.error = 0;

    if(OCF)
    {
        res.error += SENSOR_ERROR_OCF_NOT_FINISHED;
    }
    if(COF)
    {
        res.error += SENSOR_ERROR_OVERFLOW;
    }
    if(LIN)
    {
        res.error += SENSOR_ERROR_LINEARITY;
    }
    if(MAGDEC)
    {
        res.error += SENSOR_ERROR_MAG_DEC;
    }
    if(MAGINC)
    {
        res.error += SENSOR_ERROR_MAG_INC;
    }

    if(!checkParity(res.position,byte3))
    {
        res.error += SENSOR_ERROR_PARITY;
    }

    if(res.error == 0 || res.error == 48)
    {
        updateSensorStatus(res,sensor);
    }
}
Exemplo n.º 10
0
static unsigned char CheckButton(unsigned char ButtonIdx)
{	
	DWORD TimerHighByte;
	DWORD CurrentTime;		

	BYTE ButtonValue;
	BYTE EventBase;
	static DWORD lastCurrentTime = 0;
	
//    if(LEFT_BUTTON == 1)
//        LOG((ROMCHAR)"!! %d, %d, %d, %d, %d\r\n", UP_BUTTON, DN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, OK_BUTTON);  
    
//LOG((ROMCHAR)"!! %d, %d, %d, %d, %d\r\n", UP_BUTTON, DN_BUTTON, LEFT_BUTTON, RIGHT_BUTTON, OK_BUTTON);  

	// read the IO bit attached to the selected button 
	switch(ButtonIdx)
	{	
		case UP_BUTTON_IDX:
			ButtonValue = UP_BUTTON;
			EventBase = EVENT_UP_BUTTON_BASE;
			break;
		
		case DN_BUTTON_IDX:
			ButtonValue = DN_BUTTON;
			EventBase = EVENT_DN_BUTTON_BASE;
			break;
			
		case LEFT_BUTTON_IDX:
			ButtonValue = LEFT_BUTTON;
			EventBase = EVENT_LEFT_BUTTON_BASE;
			break;
            
		case RIGHT_BUTTON_IDX:
			ButtonValue = RIGHT_BUTTON;
			EventBase = EVENT_RIGHT_BUTTON_BASE;
			break;
            
		case OK_BUTTON_IDX:
			ButtonValue = OK_BUTTON;
			EventBase = EVENT_OK_BUTTON_BASE;
			break;            
            
		case MENU_BUTTON_IDX:
			ButtonValue = MENU_BUTTON;
			EventBase = EVENT_MENU_BUTTON_BASE;
			break;            
	}

	// check if nothing is going on (waiting for button to go down and it's not pressed)
	if ((ButtonState[ButtonIdx].State == WAITING_FOR_BUTTON_DOWN) && (ButtonValue == 0))
    {
		return(EVENT_NOTHING);	// return no new event
    }

	// something is going on, read the time
    TimerHighByte = ReadTimer1();
	CurrentTime   = TimerHighByte;		

//LOG((ROMCHAR)"algo pressionado %d, %d, %d, %ld\r\n", ButtonValue, ButtonState[ButtonIdx].State, ButtonIdx, TimerHighByte);  
    
	// FIX: se o timer rodar preciso corrigir o start senao nao dispara nunca
	if(lastCurrentTime != 0 && lastCurrentTime > CurrentTime)
		ButtonState[ButtonIdx].EventStartTime -= (lastCurrentTime - CurrentTime);
		
	lastCurrentTime = CurrentTime;
	
	// check the state that the button was in last
	switch(ButtonState[ButtonIdx].State)
	{	
		// waiting for button to go down
		case WAITING_FOR_BUTTON_DOWN:
			// check if button is now down
			if (ButtonValue == 1)				
			{	
				// button down, start timer
				ButtonState[ButtonIdx].EventStartTime = TimerHighByte;
				ButtonState[ButtonIdx].State = DEBOUNCE_AFTER_BUTTON_DOWN;
                
				// return button "pressed" event
				return(EventBase + BUTTON_PUSHED); 
			}
			break;
			
		// waiting for timer after button has gone down	
		case DEBOUNCE_AFTER_BUTTON_DOWN:		
			// check if it has been up long enough
			if (CurrentTime >= (ButtonState[ButtonIdx].EventStartTime + BUTTON_DEBOUNCE_PERIOD))
			{	
				// debouncing period over, start auto repeat timer
				ButtonState[ButtonIdx].EventStartTime = TimerHighByte;
				ButtonState[ButtonIdx].State = WAITING_FOR_BUTTON_UP;
			}
			break;
				
		// waiting for button to go back up
		case WAITING_FOR_BUTTON_UP:
			// check if button is now up
			if (ButtonValue == 0)
			{									
				// button up, start debounce timer
				ButtonState[ButtonIdx].EventStartTime = TimerHighByte;
				ButtonState[ButtonIdx].State = DEBOUNCE_AFTER_BUTTON_UP;
			
				// return button "released" event
				return(EventBase + BUTTON_RELEASED); 
			}
			break;
		
		// waiting for timer after button has gone up
		case DEBOUNCE_AFTER_BUTTON_UP:			
			if (CurrentTime >= (ButtonState[ButtonIdx].EventStartTime + BUTTON_DEBOUNCE_PERIOD))
				ButtonState[ButtonIdx].State = WAITING_FOR_BUTTON_DOWN;
          
			break;
	}

	// return no new event
	return(EVENT_NOTHING);
}
Exemplo n.º 11
0
void timer1_int_handler() {
    unsigned int result;

    // read the timer and then send an empty message to main()
    //LATBbits.LATB7 = !LATBbits.LATB7;
    result = ReadTimer1();
    //ToMainLow_sendmsg(0, MSGT_TIMER1, (void *) 0);
    

#if defined (MOTOR_PIC)
    // reset the timer
    WriteTimer1(TIMER1_START);
    ticks_left++;
    
    if ( executingEncode && ticks_left_C > 0)
        ticks_left_C--;
    if ( executingEncodeLong && ticks_left_C_Long > 0)
        ticks_left_C_Long--;

    ticks_left_total++;

    if ( ticks_left_C  <= 1 && ticks_right_C <= 1 && executingEncode ) {
        if ( motor_state == RoverMsgMotorForward)
            RoverForward();
        
        else if ( motor_state == RoverMsgMotorLeft2 || motor_state == RoverMsgMotorRight2 ) {
            motor_encode_lthread(RoverMsgMotorForwardCMDelim+10);
        }
        else
            RoverStop();

         executingEncode = 0;
    }
    else if ( ticks_right_C_Long  <= 1 && ticks_left_C_Long <= 1 && executingEncodeLong ) {
        if ( motor_state == RoverMsgMotorLeft2 ) {
            motor_encode_lthread(RoverMsgMotorRight2);
        }
        else if ( motor_state == RoverMsgMotorRight2 )
        {
            motor_encode_lthread(RoverMsgMotorLeft2);
        }
        else {
         RoverStop();
        }

         motor_state = RoverMsgMotorStop;
         executingEncodeLong = 0;
    }
    
#elif defined(MAIN_PIC)
    if ( !i2c_master_busy() )
        ToMainHigh_sendmsg(10, MSGT_GET_SENSOR_DATA, (void *) 0);
    
    if ( timer1_extender > 100 && tempWallCorrection == 0 ) {
        tempWallCorrection = 1;
    }
    
    timer1_extender++;

    WriteTimer1(0);
#else
    WriteTimer1(0);
#endif

}
Exemplo n.º 12
0
INT32U  OSProbe_TmrRd (void)
{
    return  (ReadTimer1());
}