Exemple #1
0
int main()
{
    uint16_t PrescalerValue;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_ICInitTypeDef TIM_ICInitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    LED_Init();
    LED_R_ON();
    LED_G_ON();

    LLIO_Init(115200);



    /* TIM2, GPIOA clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);


    /* Enable the TIM2 global Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0f;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);


    // PA0 핀을 TIM2_CH1 input으로 설정
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM2);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);


    /* Compute the prescaler value */
    SystemCoreClockUpdate();
    PrescalerValue = (uint16_t) (SystemCoreClock / 2 / TIMER_PRESCALER_FREQ) - 1;           // timer base counter에 1MHz 입력

    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = TIMER_PERIOD;
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
    TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
    TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
    TIM_ICInitStructure.TIM_ICFilter = 0;

    TIM_ICInit(TIM2, &TIM_ICInitStructure);

    /* TIM2 enable counter */
    TIM_Cmd(TIM2, ENABLE);

    TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_CC1, ENABLE);



    printf("\r\nTimer Capture example\r\n");
    while(1)
    {
        __WFI();

        if (capture_flag)
        {
            capture_flag = 0;

            printf("time diff = %d[us]\r\n", diff);

            LED_G_TOGGLE();
        }

    }
}
/***************************************************
 * Function:        void timer0_isr(void)
 *
 * OverView:		If low interrupt actived, called this function.
 *					Control LED
 *
 * Note:			reference BootLoader.h
 ***************************************************/
void timer0_isr(void)
{
	if(INTCONbits.TMR0IF)
	{
		INTCONbits.TMR0IF = 0;
		TMR0L = tmr0l_temp;

		if(!wg_ledCtrl)
		{
			if(wg_ledPwmDuty <= 0)
			{
				TMR0L = wg_ledPwmDuty;
				LED_WG_OFF();
			}

			else if(TMR0L == wg_ledPwmDuty)
			{
				TMR0L = 255 - wg_ledPwmDuty;
				LED_WG_ON();
			}
			else
			{
				TMR0L = wg_ledPwmDuty;
				LED_WG_OFF();
			}
		}
		else if(!r_ledCtrl)
		{
			if(r_ledPwmDuty <= 0)
			{
				TMR0L = r_ledPwmDuty;
				LED_R_OFF();
			}

			else if(TMR0L == r_ledPwmDuty)
			{
				TMR0L = 255 - r_ledPwmDuty;
				LED_R_ON();
			}
			else
			{
				TMR0L = r_ledPwmDuty;
				LED_R_OFF();
			}
		}
		else if(!g_ledCtrl)
		{
			if(g_ledPwmDuty <= 0)
			{
				TMR0L = g_ledPwmDuty;
				LED_G_OFF();
			}

			else if(TMR0L == g_ledPwmDuty)
			{
				TMR0L = 255 - g_ledPwmDuty;
				LED_G_ON();
			}
			else
			{
				TMR0L = g_ledPwmDuty;
				LED_G_OFF();
			}
		}
		else if(!b_ledCtrl)
		{
			if(b_ledPwmDuty <= 0)
			{
				TMR0L = b_ledPwmDuty;
				LED_B_OFF();
			}

			else if(TMR0L == b_ledPwmDuty)
			{
				TMR0L = 255 - b_ledPwmDuty;
				LED_B_ON();
			}
			else
			{
				TMR0L = b_ledPwmDuty;
				LED_B_OFF();
			}
		}
		else
		{
			TMR0L = tmr0l_temp;
			LED_WG_OFF();
			LED_R_OFF();
			LED_G_OFF();
			LED_B_OFF();
		}
		tmr0l_temp = TMR0L;
	}
}