1 Blink: Normal Timer Mode

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

CUBEIDE

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

//you can do any activity depending on timer_val register


}

 INTERRUPT TIMER MODE


SYSTEM CORE-RCC-HSE->CRYSTAL/CERAMIC RESONETOR
SET PD12, PD13, PD14, PD15 AS GPIO_OUTPUT
TIM4-CLOCK SOURCE-INTERNAL CLOCK, NVIC-TIM4 GLOBAL INTERRUPT ENABLE
//Star timer
HAL_TIM_Base_Start_IT(&htim4);
While(1)
{
}
//write subrutin function when interrupt occur
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim==&htim4)//to check which timer interrupt occure
OR YOU CAN CHEK IT BY BELOW LINE
if (htim->Instance == htim4.Instance)
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_12);
}

Or you can write subrutin function at stm32f4xx_it.c


void TIM4_IRQHandler(void)
{
/* USER CODE BEGIN TIM4_IRQn 0 */

/* USER CODE END TIM4_IRQn 0 */


HAL_TIM_IRQHandler(&htim4);
/* USER CODE BEGIN TIM4_IRQn 1 */
HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_12);
/* USER CODE END TIM4_IRQn 1 */
}

 INPUT CAPTURE MODE


We will use the input capture to measure the Frequency and width of the input signal.

 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.

The TIMER1 is configures to give a PWM output signal.


In order to measure the Frequency of the input signal, we need to measure the time between the 2 rising edges, or
between 2 falling edges.

 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

SET TIM1 AS PWM GENERATOR


SELECT TIM1->CLOCK SOURCE-INTERNAL CLOCK
CHANNEL1- PWM GENERATION CH1
PSC- 100-1(100MHZ/100=1MHZ FREQUENCY)
ARR- 100-1(1MHZ/100=10KHZ FREQUENCY)

SET TIM2 AS INPUT CAPTURE COMPARE MODE


SELECT TIM2->CLOCK SOURCE-INTERNAL CLOCK
CHANNEL1- INPUT CAPTURE DIRECT MODE
PSC- 50-1(50MHZ/50=1MHZ FREQUENCY)
ARR- as it is(0xffffffff)
POLARITY SELECTION- RISING EDGE
NVIC-TIM2 GLOBAL INTERRUPT ENABLE

PROGRAMMING
#define TIMCLOCK 50000000
#define PSC 50
//Above two line are use calculation data,which use APB1 50MHz and TIM2 PSC 50

uint8_t capture_state=0; //USE FOR STATUS


uint32_t first_capture_time=0; /store TIM2->CNT vale when fist rising edge detected
uint32_t second_capture_time=0;/store TIM2->CNT vale when second rising edge detected
uint32_t tim_difference=0;store the difference between first and second capture value
float reference_clock=0;
float frequency=0;

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)
{

}
}

WRITE INTERRUPT FUNCTION


void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
if(capture_state==0)
{
//first_capture_time= HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
first_capture_time=TIM2->CCR1;//Can also be used library function
capture_state=1;
}
else
{
//second_capture_time=HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
second_capture_time=TIM2->CCR1;
TIM2->CNT=0;
if(first_capture_time<second_capture_time)
{
tim_difference=second_capture_time-first_capture_time;
}
else if(second_capture_time<first_capture_time)
{
tim_difference=(0xffffffff- first_capture_time)
+second_capture_time;
}
reference_clock=TIMCLOCK/PSC;
frequency=reference_clock/tim_difference;

capture_state=0;
}
}

 OUTPUT COMPARE MODE

You might also like