【Experiment 6】Timer interrupt experiment

Publisher:nu23Latest update time:2017-09-26 Source: eefocusKeywords:Timer Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Experimental Purpose

1) Familiar with STM32 general timer;

2) Use the timer interrupt to flip the LED light.


2. Introduction to STM32 general timer

The general purpose timer is a 16-bit auto-reload counter driven by a programmable prescaler. It is suitable for many applications, including measuring the pulse length of input signals (input capture) or generating output waveforms (output compare and

PWM). Using the timer prescaler and the RCC clock controller prescaler, the pulse length and waveform period can be adjusted from a few microseconds to a few milliseconds. Each timer is completely independent and does not share any resources with each other. They

Can be operated synchronously together.


The STM32F103 has four general-purpose TIMx (TIM2, TIM3, TIM4 and TIM5). Their functions include:


● 16-bit up, down, up/down auto-load counter

● 16-bit programmable (can be modified in real time) prescaler, the counter clock frequency division factor is any value between 1 and 65536

● 4 independent channels:

─ Input capture

─ Output comparison

─ PWM generation (edge ​​or center aligned mode)

─ Single pulse mode output

● Synchronous circuits that use external signals to control timers and interconnect timers

● Interrupt/DMA is generated when the following events occur:

─ Update: counter overflow/underflow, counter initialization (by software or internal/external trigger)

─ Trigger event (counter starts, stops, initializes, or counts by internal/external trigger)

─ Input capture

─ Output comparison

● Supports incremental (quadrature) encoder and Hall sensor circuits for positioning


● Trigger input as external clock or cycle-by-cycle current management


The functions of the STM32 general-purpose timer are very complex. The ones we generally use more are input capture, output comparison, PWM generation and corresponding interrupts.


Main registers: control register 1 (TIMx_CR1), control register 2 (TIMx_CR2), slave mode control register (TIMx_SMCR), DMA/interrupt enable register (TIMx_DIER), status register (TIMx_SR), event generation register (TIMx_EGR), capture/compare mode register 1 (TIMx_CCMR1), capture/compare mode register 2 (TIMx_CCMR2), capture/compare enable register (TIMx_CCER), capture/compare register 1 (TIMx_CCR1), capture/compare register 2 (TIMx_CCR2), capture/compare register 3 (TIMx_CCR3), capture/compare register 4 (TIMx_CCR4), DMA control register (TIMx_DCR), DMA address of continuous mode (TIMx_DMAR), counter register (TIMx_CNT), prescaler register (TIMx_PSC), automatic load register (TIMx_ARR).

The time base unit contains:

● Counter register (TIMx_CNT)

● Prescaler register (TIMx_PSC)

● Automatic reload register (TIMx_ARR)


Configuration steps:
1) Turn on TIM3 clock and multiplexed function clock, and configure PB5 as multiplexed output.
2) Set TIM3_CH2 to remap to PB5.
3) Initialize TIM3, set ARR and PSC of TIM3.
4) Set PWM mode of TIM3_CH2, and enable CH2 output of TIM3.

5) Enable TIM3.

6) Modify TIM3_CCR2 to control the duty cycle.


3. Hardware Design

Indicator lamp, TIM3


4. Software Design


  1. void TIM3_Int_Init(u16 arr,u16 psc)  

  2. {  

  3.      TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  

  4.     NVIC_InitTypeDef NVIC_InitStructure;  

  5.   

  6.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Clock enable  

  7.       

  8.     //Timer TIM3 initialization  

  9.     TIM_TimeBaseStructure.TIM_Period = arr; //Set the value of the auto-reload register period to load the activity at the next update event     

  10.     TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value used as the TIMx clock frequency divisor  

  11.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Set clock division: TDTS = Tck_tim  

  12.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode  

  13.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //Initialize the time base unit of TIMx according to the specified parameters  

  14.    

  15.     TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //Enable the specified TIM3 interrupt and allow update interrupt  

  16.   

  17.     //Interrupt priority NVIC setting  

  18.     NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 interrupt  

  19.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //Preempt priority level 0  

  20.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //From priority level 3  

  21.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ channel is enabled  

  22.     NVIC_Init(&NVIC_InitStructure); //Initialize NVIC registers  

  23.   

  24.   

  25.     TIM_Cmd(TIM3, ENABLE); //Enable TIMx                       

  26. }  

  27. //Timer 3 interrupt service routine  

  28. void TIM3_IRQHandler(void) //TIM3 interrupt  

  29. {  

  30.     if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //Check whether TIM3 update interrupt occurs  

  31.         {  

  32.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //Clear TIMx update interrupt flag   

  33.         LED1=!LED1;  

  34.         }  

  35. }  




V. Experimental Results

success!


Keywords:Timer Reference address:【Experiment 6】Timer interrupt experiment

Previous article:STM32F103ZET6 general timer single pulse mode experiment
Next article:[Experiment 5] Window watchdog experiment

Recommended ReadingLatest update time:2024-11-17 00:50

PIC16F877A microcontroller (interrupt and timer Timer1)
1 Basic principles In the figure above, if an external crystal oscillator is connected to the left side of RC0, then T1OSCEN must be set to 1. The frequency of this external crystal oscillator is generally low. The lower the crystal oscillator frequency, the lower the power consumption. Why is an external cry
[Microcontroller]
PIC16F877A microcontroller (interrupt and timer Timer1)
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号