Dear friends, how can i use interrupts instead of delay? (stm32f4xx) - Page 1
Forum section: electronics / general

author: babe
author_rank: Contributor
timestamp: October 18, 2013, 05:02:49 pm
content: I am using stm32f4 discovery board, and keil program. i blink led by using Delay() .. but this time i need to define startblinking that uses timer interrupt ...how can i continue writing code?Code:void EnableTimerInterrupt(){NVIC_InitTypeDef nvicStructure;nvicStructure.NVIC_IRQChannel = TIM2_IRQn;nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;nvicStructure.NVIC_IRQChannelSubPriority = 1;nvicStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&nvicStructure);}

author: Crazy Ape
author_rank: Regular Contributor
timestamp: October 18, 2013, 05:53:29 pm
content: Let's hope I'm not way off on this, I need to play more with various MCU's.Generally, setting up a timer interrupt on any CPU or MCU requires three things.1.) Set up the interrupt vector (looks like thats what your code does).2.) You need to write a block of code that should run when an interrupt occurs (Interrupt Service Routine (ISR)).3.) You need to configure the timer properly to actually trigger the interrupts.There should be plenty of helpful stm32f4 examples floating about the internet.

author: Volta500
author_rank: Contributor
timestamp: October 18, 2013, 07:00:36 pm
content: I have never heard of a stm32f4 before, but on all micro's I have programmed it always boiled down to what Crazy Ape said.To find out how to do that, or any of the micro's functionality for that matter, check the datasheet! If the controller supports it, be careful of which interrupt vector is set to which interrupts and ISRs. Trust me, it will give pretty funny results when you start processing a rotary encoder turn as a received RF packetSometimes it helps to look at the disassembly of your interrupt code, sometimes C obfuscates a very simple problem that is immediately obvious in the assembly.

author: Harvs
author_rank: Super Contributor
timestamp: October 19, 2013, 01:00:55 am
content: If you haven't done so yet, go an download the standard peripheral drivers from ST, it should look like: STM32F4xx_DSP_StdPeriph_Lib_V1.1.0Don't just get the board package. e.g. if you've got the DISCO board, you'll have something like STM32F4-Discovery_FW_V1.1.0Reason being, the peripheral library package contains at least double the number of examples in it.Then you'll find an example there called "TIM_TimeBase example"Code:Example DescriptionThis example shows how to configure the TIM peripheral in Output Compare Timingmode with the corresponding Interrupt requests for each channel in order to generate4 different time bases.That's pretty much exactly what you want to do. Just they're doing it 4 times over.

author: westfw
author_rank: Super Contributor
timestamp: October 19, 2013, 01:12:34 am
content: Cortex ARM chips also usually have a "sysTick timer" designed for exactly the sort of thing that Arduino's timer0 interrupt is used for: provide a periodic interrupt at a "moderate" rate. For example, you'd set the systick timer to interrupt every millisecond (approximately), allowing you to keep a "elapsed milliseconds since boot" time value, which you can then use to time "long" events like blinking LEDs, without using delay() functions.See all of the "Blink without delay" examples for Arduino. (For all of its detractors, Arduino offers a "typical" embedded microcontroller platform with source available and extensive tutorials/documentation.)"sysTick" might be preferred over the other timers available because:1) It's considered part of the cortex core, so it should be available and "similar" on a chip from any vendor.2) Similarly, it probably has CMSIS (standard) APIs for setting it up.3) It's likely much simpler and easier to understand than the "timer peripherals" that vendors like to throw on their chips. One function (periodic interrupt) instead of a dozen modes and options, not associated with external pins, etc.

author: Harvs
author_rank: Super Contributor
timestamp: October 19, 2013, 01:23:05 am
content: Yes, the systick timer is the much simpler way of doing this. To set it up is just:Code:if (SysTick_Config(SystemCoreClock / 1000)){//Capture errorwhile (1);}Again just grab the examples...