TIM_HandleTypeDef TIM_Handle;int main(void){ __TIM4_CLK_ENABLE(); TIM_Handle.Init.Prescaler = 671; TIM_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; TIM_Handle.Init.Period = 62499; TIM_Handle.Instance = TIM4; //Same timer whose clocks we enabled HAL_TIM_Base_Init(&TIM_Handle); // Init timer HAL_TIM_Base_Start_IT(&TIM_Handle); // start timer interrupts HAL_NVIC_SetPriority(TIM4_IRQn, 0, 1); HAL_NVIC_EnableIRQ(TIM4_IRQn); while(1) { }}void TIM4_IRQHandler(void){ if (__HAL_TIM_GET_FLAG(&TIM_Handle, TIM_FLAG_UPDATE) != RESET) //In case other interrupts are also running { if (__HAL_TIM_GET_ITSTATUS(&TIM_Handle, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_FLAG(&TIM_Handle, TIM_FLAG_UPDATE); /*put your code here */ } }}
=========================================================================================main.c
#include "stm32f4xx_it.h"#include "stm32f4xx_tim.h"#include "stm324xg_eval.h"TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;void TIM_Config(void);int main(void){ TIM_Config(); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 2000 - 1; TIM_TimeBaseStructure.TIM_Prescaler =42000 - 1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM Interrupts enable */ TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); /* TIM3 enable counter */ TIM_Cmd(TIM3, ENABLE);while (1);}void TIM_Config(void){ NVIC_InitTypeDef NVIC_InitStructure;/* TIM3 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);/* Enable the TIM3 gloabal Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);/* Initialize Leds mounted on STM324xG-EVAL board */ STM_EVAL_LEDInit(LED1); /* Turn on LED1 */ STM_EVAL_LEDOn(LED1);}
stm32f4xx_it.c
void TIM3_IRQHandler(void){ if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM3, TIM_IT_Update); STM_EVAL_LEDToggle(LED1); }}
======================================================================================
#include "stm32f4xx.h"int main () {
RCC->AHB1ENR |= 0x0010; // Enable clock for GPIOE & GPIOA
RCC->APB1ENR |= 0x00000008; // Enable Clock for Timer 5
GPIOE->MODER |= 0x00010000; // Output pin for time mark at port E
GPIOA->MODER |= 0x00001000; // output pin for LED D390
NVIC_EnableIRQ(TIM5_IRQn); // Enable IRQ for TIM5 in NVIC
TIM5->ARR = 84000; // Auto Reload Register value => 1ms
TIM5->DIER |= 0x0001; // DMA/IRQ Enable Register - enable IRQ on update
TIM5->CR1 |= 0x0001; // Enable Counting
while (1) {
if (GPIOE->IDR & 0x01) GPIOA->ODR |= 0x0040; // press S370 to turn on D390
else GPIOA->ODR &= ~0x0040;
};
}
void TIM5_IRQHandler(void)
{
TIM5->SR &= ~TIM_SR_UIF; // clear IRQ flag in TIM5
GPIOE->ODR |= 0x0100; // PE08 up
GPIOE->ODR &= ~0x0100; // PE08 down
GPIOE->ODR |= 0x0100; // PE08 up
GPIOE->ODR &= ~0x0100; // PE08 down
}
=======================================================================================
int main(void){ INTTIM_Config(); /* LED Configuration */ LED_Config(); while (1) { if (TIM_GetFlagStatus(TIM2, TIM_FLAG_Update) != RESET) { TIM_ClearFlag(TIM2, TIM_IT_Update); GPIO_ToggleBits(GPIOD, GPIO_Pin_13); } }}
void INTTIM_Config(void)
{
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 100 - 1; // 1 MHz down to 10 KHz (0.1 ms)
TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1; // Down to 1 MHz (adjust per your clock)
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
========================================================================================
void INTTIM_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the TIM2 gloabal Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* TIM2 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 MHz down to 1 KHz (1 ms)
TIM_TimeBaseStructure.TIM_Prescaler = 84 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
/* TIM IT enable */
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
/* TIM2 enable counter */
TIM_Cmd(TIM2, ENABLE);
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
GPIO_ToggleBits(GPIOD, GPIO_Pin_13);
}
}
=========================================================================================
#include "stm32f4xx_tim.h"
#include "TimerExample.h"
//-----------------------------------------------------------------------------
void TimerExample_Init(uint16_t frequency)
{
// initializing timer to run TimerExample in the separate "thread"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_ClocksTypeDef RCC_Clocks;
uint16_t APB1_PrescalerValue;
uint32_t TIM_PeriodValue;
RCC_GetClocksFreq(&RCC_Clocks);
/* timer clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// setting up the timer clock frequency to 1000000 Hz
APB1_PrescalerValue = (RCC_Clocks.PCLK1_Frequency/1000000) - 1;
// setting up the frequency of timer interrupts (according to the APB1 clock scheme (AN3988))
TIM_PeriodValue = frequency * ((APB1_PrescalerValue == 1) ? 1 : 2);
TIM_TimeBaseStructure.TIM_Prescaler = APB1_PrescalerValue;
TIM_TimeBaseStructure.TIM_Period = TIM_PeriodValue;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* enable timer gloabal interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 10;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* clear interrupt pending bit (mark interrupt as processed) */
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
return;
}
//-----------------------------------------------------------------------------
void TimerExample_Start(void)
{
/* TIM Interrupts enable */
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
/* start timer */
TIM_Cmd(TIM4, ENABLE);
}
//-----------------------------------------------------------------------------
void TimerExample_Stop(void)
{
/* timer interrupts disable */
TIM_ITConfig(TIM4, TIM_IT_Update, DISABLE);
/* stops the timer */
TIM_Cmd(TIM4, DISABLE);
}
//-----------------------------------------------------------------------------
void TimerExample_Reset(void)
{
/* set default value to the timer counter */
TIM_SetCounter(TIM4, 0);
/* start timer */
TIM_Cmd(TIM4, ENABLE);
}
//-----------------------------------------------------------------------------
void TIM4_IRQHandler(void)
{
if (RESET != TIM_GetITStatus(TIM4, TIM_IT_Update))
{
/* clear interrupt pending bit (mark interrupt as processed) */
TIM_ClearITPendingBit(TIM4, TIM_IT_Update);
// do something on timer interrupt...
}
}
//-----------------------------------------------------------------------------
int main(void)
{
RCC_ClocksTypeDef RCC_Clocks;
/* SysTick end of count event each 1us */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000000);
TimerExample_Init(100);
TimerExample_Start();
while (1)
{
}
}
Комментариев нет:
Отправить комментарий