STM32 Universal timer for LED flashing

Source: Internet
Author: User

Just looked at the STM32 General Timer tutorial, in fact, and 51 of the timer use almost. Just because the 32 clock is more complex and can manipulate more registers, it can be more complex to write.

General steps to use general timer interrupts:
1. Enable Timer clock

This needs to look at the clock tree, so that the corresponding clock will be good. I'm using TIM3 here, so I can APB1 the clock.

2. Set the frequency divider and reload value of the timer

Timer Interrupt Time: Tout = (arr+1)/[tclk/(psc+1)]

In fact, this timer interrupt time is still to be specific analysis, here is so, other places may not be, specific or refer to the "STM32 reference manual".

3. Enable Timer interrupt

4. Start Timer

5, timer interrupt initialization, set its interrupt priority, grouping and so on

6. Write Interrupt Service function

Here is the corresponding code

//Initialize TimervoidTimer_init (U16 psc,u16 arr) {RCC->apb1enr =1<<1;//Enable timer clockTIM3-&GT;PSC = PSC;//setting the Prescaler factorTim3->arr = ARR;//Setting the Reload valueTim3->dier |=1<<0;//Enable timer interruptTIM3-&GT;CR1 |=1<<0;//Start TimerMy_nvic_init (1,3, TIM3_IRQN,2);//Timer Interrupt Initialization}//Interrupt Service FunctionvoidTim3_irqhandler () {if(tim3->sr&0x0001) LED0 = ~LED0; TIM3-&GT;SR &= ~ (1<<0);//Clear Interrupt Flag}

STM32 Universal timer for LED flashing

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.