STM32F103ZET6 uses timer cascade to output a specific number of PWM

Publisher:心灵清澈Latest update time:2017-02-19 Source: eefocusKeywords:STM32F103ZET6 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

There are 8 timers in STM32F103ZET6, including 6 advanced timers including TIM1-TIM5 and TIM8.

The timer cascade function needs to be used here. Pages P388 and P399 of ST's RM0008 REV12 explain how to select the cascade function for a specific timer. See Table 86.

The timer that outputs PWM here is TIM2, and the idle timer is TIM3. TIM2 is the master timer, and TIM3 is the slave timer to count the output pulses of TIM2.

From the table, we can see that TIM3 selects TIM2 as the trigger source for the slave timer, and needs to configure TS=001, that is, select ITR1.

To achieve the function of controlling the number of PWM outputs through the timer, there can be one of the following configuration methods:


void TIM2_Master__TIM3_Slave_Configuration(u32 PulseFrequency)

{

    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    TIM_OCInitTypeDef       TIM_OCInitStructure;


    u16 nPDTemp ;

    /* -----------------------------------------------------------------------

    TIMx Configuration: generate 4 PWM signals with 4 different duty cycles:

    TIMxCLK = 72 MHz, Prescaler = 0x0, TIMx counter clock = 72 MHz

    TIMx ARR Register = 0 => TIMx Frequency = TIMx counter clock/(ARR + 1)

    TIMx Frequency = 72MHz.

    ----------------------------------------------------------------------- */

    TIM_Cmd(TIM2, DISABLE);

    nPDTemp = 72000000UL/PulseFrequency;    


// Time base configuration: Configure PWM output timer - TIM2

    /* Time base configuration */

    TIM_TimeBaseStructure.TIM_Period          = nPDTemp-1;

    TIM_TimeBaseStructure.TIM_Prescaler       = 0;

    TIM_TimeBaseStructure.TIM_ClockDivision   = 0;

    TIM_TimeBaseStructure.TIM_CounterMode     = TIM_CounterMode_Up;

    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


    

// Output configuration: Configure PWM output timer - TIM2

    /* PWM1 Mode configuration: Channel1 */

    TIM_OCInitStructure.TIM_OCMode            = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OCPolarity        = TIM_OCPolarity_High;    

    TIM_OCInitStructure.TIM_OutputState       = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse             = nPDTemp>>1;//50%


    TIM_OC1Init(TIM2, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);

    

// Time base configuration: Configure pulse count register - TIM3

    TIM_TimeBaseStructure.TIM_Period        = 0xFFFF;

    TIM_TimeBaseStructure.TIM_Prescaler     = 1;

    TIM_TimeBaseStructure.TIM_ClockDivision = 0;

    TIM_TimeBaseStructure.TIM_CounterMode   = TIM_CounterMode_Up;

    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);    

    

    /* Output Compare Active Mode configuration: Channel1 */

    TIM_OCInitStructure.TIM_OCMode                        = TIM_OCMode_Inactive;

    TIM_OCInitStructure.TIM_OCPolarity                = TIM_OCPolarity_High;

    TIM_OCInitStructure.TIM_OutputState                = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 0xFFFF; // The configuration value here is not very meaningful

    

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);

    

//Configure TIM2 as the master timer

    /* Select the Master Slave Mode */

    TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);

    /* Master Mode selection */

    TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);

    

//Configure TIM3 as a slave timer

    /* Slave Mode selection: TIM3 */

    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);

    TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1);

    

    TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

   

    TIM_Cmd(TIM2, DISABLE);

    TIM_Cmd(TIM3, DISABLE);

}


The interrupt service routine is as follows:



u8  TIM2_Pulse_TIM3_Counter_OK = 0;

void TIM3_IRQHandler(void)

{

    if (TIM_GetITStatus(TIM3, TIM_IT_CC1) != RESET)

    {

        TIM_ClearITPendingBit(TIM3, TIM_IT_CC1); // Clear interrupt flag

        

        TIM_Cmd(TIM2, DISABLE); //Disable the timer

        TIM_Cmd(TIM3, DISABLE); //Disable the timer

        

        TIM2_Pulse_TIM3_Counter_OK = 1;

    }

}


The application is:


u16 pulsecnt = 10000;

void main(void)

{

    SystemSetup(); // Initialize the kernel and peripherals

    TIM2_Master__TIM3_Slave_Configuration(10000); //Configure TIM2 pulse output to 10k

    while(1)

    {

        TIM_ITConfig(TIM3, TIM_IT_CC1, DISABLE);        /* TIM enable counter */

        TIM_Cmd(TIM3, ENABLE);

        TIM3->CCR1  = pulsecnt;

        TIM3->CNT   = 0;

        TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE);

        TIM_Cmd(TIM2, ENABLE);    /* TIM enable counter */

        

        while(TIM2_Pulse_TIM3_Counter_OK  == 0);

    }

}


In this configuration, the TIM3 comparison interrupt is used. I haven't tried other methods yet, but I think they should work, such as using the timer update interrupt...

Keywords:STM32F103ZET6 Reference address:STM32F103ZET6 uses timer cascade to output a specific number of PWM

Previous article:STM32 FSMC configuration SRAM
Next article:STM32|4-20mA output circuit

Recommended ReadingLatest update time:2024-11-23 10:58

Use the PWM output function of the STM32 timer to directly obtain the PWM waveform
This experiment shows you how to output a PWM waveform with a fixed duty cycle. 1. Establishment of the project: 2. Main function code: 3.pwm_output.c code: 4.output.h code: 5. Results: 6. If the result cannot be displayed, please refer to the articles in the previous sections, which have been resolved.
[Microcontroller]
51 MCU Getting Started Experience Sharing 5-Use of STC15W1K16PWM Internal EEPROM
In actual development, we often encounter some occasions where the power needs to be turned off and then powered on again for memory. This requires a power-off storage chip. The most commonly used EEPROM chip is AT24C02, which has almost become the standard configuration of every development board. However, sometimes,
[Microcontroller]
51 MCU Getting Started Experience Sharing 5-Use of STC15W1K16PWM Internal EEPROM
STM32 TIM2 channel remapping PWM no output waveform
Use TIM2 to output PWM to drive the servo, and use TIM2CH3 (PB11 pin) to output the drive waveform. From the data sheet, we know that the default multiplexing function of PB10 is USART3_TX can be used as TIM2_CH3 by remapping. TIM2_CH3 is configured as PWM output mode according to the following steps: 1. Configure
[Microcontroller]
ATmega128 (PWM—time 0)
//Atmega128 timer to achieve fast PWM output example program, timer 0 overflow interrupt method //Output PWM waveform to drive the light-emitting diode, the brightness changes from dark to off, and it is in a flashing state. //The sample value adopts the sine wave formula. The example is as follows. //Sine wave samp
[Microcontroller]
How to use the timer of s3c2410
2410 has 5 timers in total, timer4 has no pin output, and the rest can be used as PWM. 0 and 1 share a prescaler 2, 3, 4 share a prescaler TCFG0 correspond to these two pre-dividers respectively. Don’t forget to add +1 to the division value, because the division value cannot be 0. TCFG1 corresponds to the fr
[Microcontroller]
51 single chip temperature + pwm controls the fan speed, and the temperature is displayed on LCD1602
The schematic diagram is as follows:   The source code is as follows: #include reg52.h #include intrins.h #include"define.h" #include"delay.h" #include"LCD1602.h" #include"DS18B20.h" #include"HL_alarm.h" sbit KEY3 = P3^5; //define start/stop   void zhuan();   unsigned char timer1; //********************************
[Microcontroller]
51 single chip temperature + pwm controls the fan speed, and the temperature is displayed on LCD1602
PIC microcontroller - using the overflow interrupt of Timer2 to realize dynamic scanning of digital tubes
Write a program to make the display sequence of the digital tube be: 0123,1230,2301,3012. Digital tube display is divided into static scanning and dynamic scanning. Dynamic scanning display is generally divided into two ways 1. Select a digital tube position, write a broken code display, delay for a certain period
[Microcontroller]
High-performance PWM modulated power management IC - ME8263
Nanjing Micro One Electronics Inc. recently launched a high-performance PWM modulated power management IC-ME8263, which is an AC/DC switching power supply controller specially designed for cost-effective small and medium power power adapters. ME8263 has lower standby power consumption, lower EMI (with frequency
[Power Management]
High-performance PWM modulated power management IC - ME8263
Latest Microcontroller Articles
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号