Use a single ordinary timer of STM32 to generate 4 square waves of different frequencies

Publisher:TranquilJourneyLatest update time:2015-04-14 Source: eechinaKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The ordinary timer of STM32 has four outputs: TIMx_CH1, TIMx_CH2, TIMx_CH3 and TIMx_CH4. The output comparison method can be used to generate square wave outputs of different frequencies. The simple method is:
 
1) Set the counter to up-counting mode and set the auto-reload register to 0xFFFF; the counter will then count in a loop.
 
2) Set each timer channel to output compare mode, and set the corresponding output pin to flip output when compare matches.
 
3) Calculate a value called Half_Cyc according to the half-wave period of the output waveform. For example: if the clock frequency of the timer is 72MHz and a 3456Hz square wave needs to be generated, then Half_Cyc = 72M/(3456*2) = 41667; if a 200kHz square wave needs to be generated, then Half_Cyc = 72M/(200k*2) = 180.
 
4) Set each channel to generate an interrupt when the output compare matches. In the interrupt, read the value of the compare register and add the value of Half_Cyc. If the calculated value exceeds 16 bits, discard the excess part and write the new value back to the corresponding compare register. In this way, the next successful comparison will occur exactly after a half-wave cycle, and the corresponding pin will be flipped.
 
The above method is very effective when the required frequency is not high, but if the frequency is high, frequent interrupts will occur. In this case, DMA can be used to improve it.
 
The basis of the above method is to generate pin flip output by constantly changing the match point of the output comparison. We can calculate these comparison match points in advance and update the contents of the comparison register one by one through DMA at each match:
 
Method 1: Use two DMA buffers. When the DMA controller operates one buffer, the program calculates the data of the other buffer, and then switches the buffer for DMA operation in the interrupt processing when the DMA transfer ends.
 
Method 2: Use a large DMA buffer, calculate the contents of half of the buffer first, start DMA in loop mode and set it to generate interrupts when the DMA transfer is halfway and completed; after starting DMA, continue to calculate the contents of the other half of the buffer. When a DMA interrupt occurs, it means that half of the buffer has become empty. At this time, calculate this half of the buffer in the interrupt processing.
 
As long as the DMA buffer is large enough, method 2 can ensure that the CPU has sufficient time to process data and ensure continuous waveform output.
 
Disclaimer: The above explanation has not been verified in practice, it is just a principle explanation, and there may be some things that are not fully considered.
Keywords:STM32 Reference address:Use a single ordinary timer of STM32 to generate 4 square waves of different frequencies

Previous article:Description of Cortex-M3 registers used in STM32
Next article:Points to note about clock settings when IO is used for multiplexing functions

Recommended ReadingLatest update time:2024-11-16 13:58

32-bit MCU development and design solution based on STM32F100VBT6
The STM32F100VBT6 uses the ARM Cortex™-M3 32-bit RISC core, operates at a frequency of 24MHz, integrates high-speed embedded memory (up to 128kB flash memory, up to 8kB SRAM) and various enhanced peripherals and I/O connected to two APB buses. All devices provide two I2C, two SPI, one HDMI CEC and up to 3 USART Peug
[Microcontroller]
STM32 Learning IIC
Like other peripherals, the STM32 standard library provides an I2C initialization structure and initialization function to configure the I2C peripheral. The initialization structure and function are defined in the library files "stm32f4xx_i2c.h" and "stm32f4xx_i2c.c".   I2C_ClockSpeed;  I2C_Mode;  I2C_DutyCycle;  I2C
[Microcontroller]
STM32_External interrupt button control to light up the LED
/*  Name: STM32_External interrupt button control to light up the LED  Note: For STM32, there are many types of interrupts. (In addition to external interrupts, there are also internal exceptions.) Each pin of its GPIO port can be used as an interrupt source for external interrupts. Its settings are also much more com
[Microcontroller]
STM32 CAN --- Analysis of working/testing modes
1 bxCAN working mode     bxCAN has three main working modes: initialization mode, normal mode and sleep mode.     After hardware reset, bxCAN works in sleep mode to save power, and the internal pull-up resistor of CANTX pin is activated. Software can request bxCAN to enter initialization or sleep mode by setting INRQ
[Microcontroller]
STM32 Series Chapter 32--NRF24L01 Wireless Communication
Introduction: NRF24L01 is a wireless communication chip produced by NORDIC. It uses FSK modulation and integrates NORDIC's own Enhanced Short Burst protocol. It can realize point-to-point or 1 to 6 wireless communication. The wireless communication speed can reach up to 2Mbps. NRF24L01 uses SPI communication and can b
[Microcontroller]
STM32 Series Chapter 32--NRF24L01 Wireless Communication
STM32 Getting Started with GPIO (STM32F030F4P6 based on CooCox IDE) (Part 1)
1. The latest version of CooCox IDE is V2. However, most of the examples I have seen online are based on the old version. I have also tried the new version and found that some operations are not familiar to me. Here I will still introduce the old version 1.7.8. http://www.coocox.org/software/coide.php   2. Run CoID
[Microcontroller]
STM32 system clock monitoring and switching
In the company's project some time ago, it was required to use the STM32 processor to automatically switch to the internal crystal oscillator when the external crystal oscillator is abnormal. After searching for a lot of information on the Internet, I finally found an official seminar PPT on the Internet that briefly
[Microcontroller]
STM32 system clock monitoring and switching
STM32 capture/compare channels
Each capture/compare channel is built around a capture/compare register (including shadow registers), including the capture input section (digital filtering, multiplexing and prescaler), and the output section (comparator and output control). Input: Digital filtering, multiplexing and prescaler Output: Comparator and
[Microcontroller]
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
Guess you like

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号