Ярлыки

_GetPixelIndex (1) _SetPixelIndex (1) 3-phase (1) 800x480 (1) АЦП (1) генератор (1) синхронный усилитель (2) структура (1) учебный курс (1) шаговый двигатель (1) ШИМ (2) accert (1) AD7608 (1) AD8429 (1) ADC (5) amplifer (1) arccos (1) arcsin (1) arctang (2) arctg (3) ARM (2) arm_sqrt_q15 (2) assembler (6) ASSERT (1) atan (2) bit (1) Bitband (1) boot (3) bootlloader (1) BUTTON (1) C (5) C# (1) CAN (2) CC2530 (5) CMSIS (4) command (1) Cordic (1) Core746I (1) CubeMX (4) DBGMCU (2) debug (2) debug.ini (1) delegate (1) Digital Potentiometers (1) DigitalPOT (1) Discovery (1) DMA (9) DMA2D (1) DSP (1) DSP library (1) DWT (1) EFM32 (5) EmWin (9) EXTI (1) FATFS (1) FMC (2) FreeRTOS (2) gl868-dual cmux (1) GPIO (4) GUI (2) GUIBuilder (1) GUIDRV_CompactColor_16 (1) HAL (3) HappyGecko (1) Hard Fault (2) heap (1) I2C (1) ID (1) ILI9320 (1) ILI9325 (1) Initialisation (1) InitLTDC (1) Instrumentithion (1) Interrupt (4) ITR (1) JTAG (1) Keil (5) LCDConf (2) lock-in (1) LTCD (1) LTDC (3) main (1) memory (1) MINI_STM32 Revision 01 (1) nBoot0 (1) NVIC (1) OnePulse (2) OSAL (4) pack (1) phase (1) printf (3) Pulse (1) PWM (12) RCC (2) RCR (1) Register (1) RESET (2) RS232 (3) RSS (1) RTC (3) RTOS-RTX (1) RTT (1) RTX-RTOS (1) SDCard (1) SDRAM (6) Segger (2) SPI (3) sqrt (3) SSD1298 (1) SSD1963 (1) Standart Peripherial Library (3) STANDBAY (1) startup (1) STemWin (8) stepper motor (1) STlink (2) STM32 (17) STM32429ZI (1) STM32Cube (1) STM32DBG.IN (1) STM32F (28) STM32F0 (4) STM32F1 (13) STM32F4 (10) STM32F4 Discovery (1) STM32F407ZG (1) STM32F429 (2) STM32F746 (1) STOP (1) string (1) struct (1) SWD (1) SWD JTAG (1) Synhronization (1) system_stm32f4xx.c (1) SystemInit (1) SysTick (1) task (4) telit (1) TIM (27) typedef (1) UART (1) USART (9) viewer (2) WM_PAINT (1) Z-stack (5) ZigBee (5)

пятница, 25 декабря 2015 г.

Timer Interrupt on STM32F4 using HAL & SPL

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


Комментариев нет:

Отправить комментарий