Cortex_m3_stm32 Embedded Learning Note (eight): Timer interrupt Experiment

Source: Internet
Author: User

STM32 timer function is very strong , there are advanced timers such as TIME1 and TIME8, there are general timers such as Time2~time5, as well as TIME6 and TIME7 and other basic timers.

This section learns the Universal timer TIM3

The STM32 universal timer can be used to measure the pulse length of the input signal ( input capture ) or to produce an output waveform ( output comparison and PWM).

The STM3 Universal timx (TIM2, TIM3, TIM4, and TIM5) timer features include:
1) 16-bit up, down, up/down Auto load counter (timx_cnt).
2) 16-bit programmable (can be modified in real time) Prescaler (TIMX_PSC), counter clock frequency divider factor of 1~
65535.
3) 4 Independent channels (TIMX_CH1~4) that can be used as:
a. The input captures the
b. Output comparison
c. PWM generation (edge or middle alignment mode)
d. Single-pulse mode output
4) can use an external signal (TIMX_ETR) to control the timer and timer interconnect (can be controlled with 1 timers additional
a timer) of the synchronization circuit.
5) produces interrupt /DMA when the following event occurs:
a. Update: Counter overflow/down overflow, counter initialized (via software or internal/external trigger)
b. Trigger events (counters start, stop, initialize, or internal/external trigger count)
c. The input captures the
d. Output comparison
e. Supports incremental (quadrature) encoders for positioning and Hall sensor circuits
f. Trigger input as an external clock or as a cycle current management


This experiment is an update interrupt for TIM3.

TIM3 Related configuration Files timer.c

#include "led.h" #include "timer.h"//arr: Automatically reload values. PSC: Clock Prescaler frequency void Tim3_int_init (U16 arr,u16 PSC) {tim_timebaseinittypedef tim_ist; Nvic_inittypedef nvic_ist; Rcc_apb1periphclockcmd (RCC_APB1PERIPH_TIM3, ENABLE); ① clock TIM3 enable//timer TIM3 to initialize tim_ist. Tim_period = arr; Sets the value of the auto-reload register period tim_ist. Tim_prescaler =PSC; Sets the Prescaler value for the divisor of the clock frequency tim_ist. Tim_clockdivision = TIM_CKD_DIV1; Sets the clock split tim_ist. Tim_countermode = tim_countermode_up;  TIM counts up Tim_timebaseinit (TIM3, &tim_ist);  ② initialization of Tim3tim_itconfig (tim3,tim_it_update,enable); ③ allow update of interrupt nvic_ist. NVIC_IRQCHANNEL=TIM3_IRQN; Nvic_ist. nvic_irqchannelpreemptionpriority=0; Nvic_ist. nvic_irqchannelsubpriority=3; Nvic_ist. nvic_irqchannelcmd=enable; Nvic_init (&nvic_ist);  Tim_cmd (TIM3, ENABLE); ⑤ Enable tim3}//Interrupt service program void Tim3_irqhandler (void) {if (Tim_getitstatus (TIM3, tim_it_update)! = RESET) {tim_ Clearitpendingbit (TIM3, tim_it_update); Clear the TIM3 update interrupt flag, (<span style= "color: #ff6600;" > Very important </span>) led1=! LED1;}}

The timer's overflow time calculation formula is as follows:

Tout (overflow time) = ((arr+1) * (psc+1))/tclk;

the input clock frequency (in Mhz) (72Mhz) of the TCLK is TIM3. if we pass in a parameter of arr=4999 psc=7199 to get the overflow time of 500ms means that every 500ms change will trigger an update interrupt (LED1 flashing) when the TIM3 overflow will reload 0, The update interrupt is then counted up (the up-counting mode is selected in the initialization function) until the overflow occurs.
Timer.h
#ifndef _timer_#define _timer_#include "sys.h" void Tim3_int_init (U16 arr,u16 PSC); void Tim3_irqhandler (void); #endif

Main function
#include "led.h" #include "sys.h" #include "timer.h" #include "delay.h" #include "usart.h" void init (void) {delay_init (); Led_init (); Nvic_configuration (); Uart_init (9600); Tim3_int_init (4999,7199);//overflow time is 500ms}int main (void) {Init (); while (1) {led0=! Led0;delay_ms (250);}}



you will eventually see LED0 and LED1 flashing, but the LED0 blinks at a higher frequency than LED1 (one times)




Cortex_m3_stm32 Embedded Learning Note (eight): Timer interrupt Experiment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.