예제 #1
0
// **************************************************************
//		PWM0P0 interrupt is for IR Reciver
// **************************************************************
void PWM0P2_IRQHandler(void)
{
#ifdef SONAR
	// ============================================================
	//			Caculate distance between object and sonar
	//			distance(cm) = time(us)/58.773
	//			distance(cm) = clock(10us)/5.8773
	// ============================================================
	if(PWM_GetCaptureIntFlag(PWM0, 4))
	{
		//Clear interrupt
		PWM_ClearCaptureIntFlag(PWM0, 4, PWM_CAPTURE_INT_FALLING_LATCH);
	
		Sonar_caprure_timer = IrSonar_CalPeriodTime(PWM0, 4);
		// Caculate distance between Sonar and object
		Sonar_Distance = (float)Sonar_caprure_timer/(float)SONARDISTANCE_SCALE;
		Sonar_Distance_OUT = Sonar_Distance;
		//Enable next time distence detect
		SonarExecuteFLAG=0;
	}
#endif
#ifdef IR
	// ============================================================
	//			Get IR data
	// ============================================================
	if(PWM_GetCaptureIntFlag(PWM0, 5))
	{
		//Clear interrupt
		PWM_ClearCaptureIntFlag(PWM0, 5, PWM_CAPTURE_INT_FALLING_LATCH);
	
		PWM_caprure_timer = IrSonar_CalPeriodTime(PWM0, 5);
	
		//start receive IR raw data
		if(IR_RxExecute_Flag==0)
		{
			if(PWM_caprure_timer < IRRX_FIRST_BIT_MAX && PWM_caprure_timer > IRRX_FIRST_BIT_MIN)  // start if > 3ms 
			{
				IR_RxExecute_Flag=1;
				IR_init_counter=getTickCount();
			}
		}
		//store IR raw data
		else if(IR_capture_count<32 && IR_RxExecute_Flag==1)
		{
			IR_rawDATA[IR_capture_count] = PWM_caprure_timer;
			IR_capture_count++;
			if(IR_capture_count==32)
			{
				IR_RxComplete_Flag=1;
			}
		}
		// Detect stop bit
		if(PWM_caprure_timer>1500)
		{
			IR_RxExecute_Flag = 0;
			IR_capture_count = 0;
		}
	}
#endif
}
예제 #2
0
/*--------------------------------------------------------------------------------------*/
void CalPeriodTime(PWM_T *PWM, uint32_t u32Ch)
{
    uint16_t u32Count[4];
    uint32_t u32i;
    uint16_t u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid;

    /* Clear Capture Falling Indicator (Time A) */
    PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_FALLING_LATCH);

    /* Wait for Capture Falling Indicator  */
    while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

    /* Clear Capture Falling Indicator (Time B)*/
    PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_FALLING_LATCH);

    u32i = 0;

    while(u32i < 4)
    {
        /* Wait for Capture Falling Indicator */
        while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

        /* Clear Capture Falling and Rising Indicator */
        PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_FALLING_LATCH | PWM_CAPTURE_INT_RISING_LATCH);

        /* Get Capture Falling Latch Counter Data */
        u32Count[u32i++] = PWM_GET_CAPTURE_FALLING_DATA(PWM, u32Ch);

        /* Wait for Capture Rising Indicator */
        while(PWM_GetCaptureIntFlag(PWM, u32Ch) < 2);

        /* Clear Capture Rising Indicator */
        PWM_ClearCaptureIntFlag(PWM, u32Ch, PWM_CAPTURE_INT_RISING_LATCH);

        /* Get Capture Rising Latch Counter Data */
        u32Count[u32i++] = PWM_GET_CAPTURE_RISING_DATA(PWM, u32Ch);
    }

    u16RisingTime = u32Count[1];

    u16FallingTime = u32Count[0];

    u16HighPeroid = u32Count[1] - u32Count[2];

    u16LowPeroid = 0x10000 - u32Count[1];

    u16TotalPeroid = 0x10000 - u32Count[2];

    printf("\nPWM generate: \nHigh Period=7199 ~ 7201, Low Period=16799 ~ 16801, Total Period=23999 ~ 24001\n");
    printf("\nCapture Result: Rising Time = %d, Falling Time = %d \nHigh Period = %d, Low Period = %d, Total Period = %d.\n\n",
           u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid);
    if((u16HighPeroid < 7199) || (u16HighPeroid > 7201) || (u16LowPeroid < 16799) || (u16LowPeroid > 16801) || (u16TotalPeroid < 23999) || (u16TotalPeroid > 24001))
        printf("Capture Test Fail!!\n");
    else
        printf("Capture Test Pass!!\n");
}
예제 #3
0
/**
 * @brief       PWM0 IRQ Handler
 *
 * @param       None
 *
 * @return      None
 *
 * @details     ISR to handle PWM0 interrupt event
 */
void PWM0_IRQHandler(void)
{
    if(PWM_GetCaptureIntFlag(PWM0, 0) > 1)
    {
        PWM_ClearCaptureIntFlag(PWM0, 0, PWM_CAPTURE_INT_FALLING_LATCH);
    }
}