Professional Documents
Culture Documents
1 Blink: Normal Timer Mode
1 Blink: Normal Timer Mode
1 Blink: Normal Timer Mode
1> BLINK
SYSTEM CORE-RCC-HSE->CRYSTAL/CERAMIC RESONETOR
SET PD12, PD13, PD14, PD15 AS GPIO_OUTPUT
PROGRAMMING
Under while(1)
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_14);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
HAL_Delay(1000);
2> TIMER
NORMAL TIMER MODE
SYSTEM CORE-RCC-HSE->CRYSTAL/CERAMIC RESONETOR
SET PD12, PD13, PD14, PD15 AS GPIO_OUTPUT
TIM4-CLOCK SOURCE-INTERNAL CLOCK
//Star timer
HAL_TIM_Base_Start(&htim4);
While(1)
{
timer_val=__HAL_TIM_GET_COUNTER(&htim4);//To chek the value of cnt register
timer_val=TIM4->CNT;//OR you can chek cnt register directly
Here I am going to use two timers, i.e Timer 1 for the PWM output and Timer 2 for the Input Capture
Timer 1 is connected to the APB2 clock, which is running at 100 MHz
Timer 2 is connected to the APB1 clock, which is running at 50 MHz
This part is very important. The minimum frequency, which the device can read will
depend on it. Below is the configuration for the TIMER 2
I have enable input capture direct mode for channel 1
The PSC set to 50, which would devide the APB2 clock by 50 making
The timer2 clock =1MHz
I am leaving ARR to 0Xffffffff(Max for 32 bit timer)
The minimum frequency that the timer can read is equal to
(TIMx CLOCK/ARR). In our case it will be (1MHz/0xffffffff)Hz.
When the first Rising edge occurs, the counter value is recorded.
Another counter value is recorded after the second rising edge occurs.
Now the difference between these 2 counter values is calculated.
The Difference in the counter values will give us the frequency.
This entire process is shown below
SYSTEM CORE-RCC-HSE->CRYSTAL/CERAMIC RESONETOR
CLOCK CONFIGURATIO ->SET APB2 AS 100MHz and SETAPB1 AS 50MHz
PROGRAMMING
#define TIMCLOCK 50000000
#define PSC 50
//Above two line are use calculation data,which use APB1 50MHz and TIM2 PSC 50
int main()
{
TIM1->CCR1=60;//to get 60% duty cycle of pwm signal
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);// to generate pwm
HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);/start input capture in interrupt mode
Whil(1)
{
}
}
capture_state=0;
}
}