STM32 PWM duty cycle variable function

Publisher:静默思考Latest update time:2018-09-20 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

When the frequency is 10K, the duty cycle can achieve a change accuracy of 1%, while when it is 100K, the change accuracy can only be 10%.




#define PWM_CLK1M_ARR_10K 100-1

 

void IzADCPWMInit(uint8_t duty)//10%~90%

{

 

  if(duty>90)

  {

 duty = 90;

  }

  if(duty<10)

  {

 duty = 10;

  } 

//reset 

  /* TIM4 enable counter */

  TIM_Cmd(TIM4, DISABLE);

 

   /* TIM4 Main Output Enable */

  TIM_CtrlPWMOutputs(TIM4, DISABLE); 

//end of reset

 

    /* System Clocks Configuration */

  RCCPWMADCConfiguration();

 

  /* GPIO Configuration */

  GPIOPWMADCConfiguration();

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

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

    The TIM3CLK frequency is set to SystemCoreClock (Hz), to get TIM3 counter

    clock at 24 MHz the Prescaler is computed as following:

     - Prescaler = (TIM3CLK / TIM3 counter clock) - 1

    SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density

    and Connectivity line devices and to 24 MHz for Low-Density Value line and

    Medium-Density Value line devices

    The TIM3 is running at 36 KHz: TIM3 Frequency = TIM3 counter clock/(ARR + 1)

                                                  = 24 MHz / 666 = 36 KHz

    TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR)* 100 = 50%

    TIM3 Channel2 duty cycle = (TIM3_CCR2/ TIM3_ARR)* 100 = 37.5%

    TIM3 Channel3 duty cycle = (TIM3_CCR3/ TIM3_ARR)* 100 = 25%

    TIM3 Channel4 duty cycle = (TIM3_CCR4/ TIM3_ARR)* 100 = 12.5%

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

  /* Compute the prescaler value */

  //PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;//24M

  //TIM3 Frequency=1MHz/(9999+1) =100Hz

  PrescalerValue = (uint16_t) (SystemCoreClock / 1000000) - 1; //TIM3 counter clock: 1MHz

  /* Time base configuration */

  //TIM_TimeBaseStructure.TIM_Period = 665;//(ARR)

  //TIM3 Frequency=1MHz/(9999+1) =100Hz

  //TIM_TimeBaseStructure.TIM_Period = 10000-1; //TIM3 counter clock/TIM3 Frequency

  //TIM3 Frequency=1MHz/(99+1) =10 000Hz

  TIM_TimeBaseStructure.TIM_Period = PWM_CLK1M_ARR_10K;//PWM_CLK1M_ARR_100; //TIM3 counter clock/TIM3 Frequency 

  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

 

  /* PWM1 Mode configuration: Channel1 */

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  //CCR1_Val =TIM3_ARR*duty/100;

 //TIM_OCInitStructure.TIM_Pulse = CCR1_Val;

 // TIM_OCInitStructure.TIM_Pulse = (uint16_t)(10000-1)*duty/100; 

  TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100;   

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

 

// TIM_OC1Init(TIM3, &TIM_OCInitStructure);

 

// TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

//PB6: TM4 Channel1  

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

 

  TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable); 

//PB8: TM4 Channel3

 

  /* PWM1 Mode configuration: Channel3 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100;   

 

 

  TIM_OC3Init(TIM4, &TIM_OCInitStructure);

 

  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);

 

//PB7: TM4 Channel2

  /* PWM1 Mode configuration: Channel2 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = (uint16_t)(PWM_CLK1M_ARR_10K)*duty/100; ;

 

  TIM_OC2Init(TIM4, &TIM_OCInitStructure);

 

  TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);

#if 0

  /* PWM1 Mode configuration: Channel4 */

  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;

 

  TIM_OC4Init(TIM3, &TIM_OCInitStructure);

 

 

  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);

#endif

 

  TIM_ARRPreloadConfig(TIM4, ENABLE);

 

  /* TIM4 enable counter */

  TIM_Cmd(TIM4, ENABLE);

 

   /* TIM4 Main Output Enable */

  TIM_CtrlPWMOutputs(TIM4, ENABLE);

 

}


Keywords:STM32 Reference address:STM32 PWM duty cycle variable function

Previous article:Summary of STM32's CAN1 module
Next article:The difference between RO segment, RW segment and ZI segment in ARM

Recommended ReadingLatest update time:2024-11-16 15:48

STM32 PWM Final Edition - Clearly Annotated and Easy to Understand
#include "stm32f10x.h"   void GPIO_TimPWM(void) {   GPIO_InitTypeDef GPIO_InitStructure;   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA   , ENABLE);//   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;             //TIM_CH2   GPIOA_Pin_7 output   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   //Multiplex push-p
[Microcontroller]
stm32 download method serial port ISP\swd (JLink)
1. First look at the stm32 startup mode  The boot mode of stm32 is determined by the boot0 and boot1 pins of the 32 chip. It is divided into embedded flash boot mode (normal boot mode), memory boot mode, and rom boot mode. The corresponding relationship between the boot mode and the pin high and low levels is as follo
[Microcontroller]
stm32 download method serial port ISP\swd (JLink)
Internal FLASH principle of STM32
Different models of STM32 have different FLASH capacities, the smallest is only 16K bytes, and the largest is 1024K bytes. The chip used in the STM32F1 development board on the market is the STM32F103 series, and its FLASH capacity is generally 512K bytes, which is a large-capacity chip. The Flash module organization
[Microcontroller]
Internal FLASH principle of STM32
Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep (STOP) mode
By using the sleep mode of the stm32 microcontroller, the microcontroller can be put into sleep intermittently to achieve low power consumption. It often fails to wake up after entering sleep mode, resulting in the next program not being able to be burned in. The usual solution is: the general development board or mic
[Microcontroller]
Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep (STOP) mode
stm32 storage structure & memory map
1 STM32 system architecture     To deeply understand the memory of STM32, you need to first know the system structure of STM32. Figure 1 is a block diagram of the STM32 system structure. According to the description in the STM32 Reference manual (RM0008), as shown in the figure:     You can learn about the com
[Microcontroller]
stm32 storage structure & memory map
STM32 RTC clock configuration
1. RTC Instructions    "RTC" is the abbreviation of Real Time Clock, which means real-time clock. STM32 provides a second interrupt source and an alarm interrupt source. Modifying the value of the counter can reset the current time and date of the system.    The reason why the RTC module has the real-time clock funct
[Microcontroller]
STM32 timer for external pulse counting
Because stm32f103c8 is used as the main controller to control the car, the speed of the car is input by two photoelectric encoders (one on the left and one on the right). Therefore, the external clock trigger mode (TIM-ETRClockMode2Config) is considered.         After trying for a long time, I found that TIM1 could
[Microcontroller]
Research on a New Single-Phase PWM Algorithm Based on DSP
Pulse-Width Modulation (PWM) technology is widely used in the field of power electronics. The PWM mode is the basis for determining the output voltage characteristics of the inverter. A PWM mode with superior performance can make the inverter have good output characteristics. From Fourier analysis, it can be seen th
[Embedded]
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号