STM32f4 PWM output experimental code

Publisher:电子设计艺术家Latest update time:2018-10-05 Source: eefocusKeywords:STM32f4 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The pwm.c source file code is as follows:  

//TIM14 PWM initialization  

//PWM output initialization

//arr: automatic reload value psc: clock pre-division number

void TIM14_PWM_Init(u32 arr,u32 psc)

{                  

  GPIO_InitTypeDef GPIO_InitStructure;

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

  TIM_OCInitTypeDef  TIM_OCInitStructure;

  

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14,ENABLE); //TIM14 clock enable     

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); //Enable PORTF clock

  

  GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14); //GF9 is multiplexed as TIM14

  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;                //GPIOF9  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //Multiplexing function

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;  //速度 50MHz

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //Push-pull multiplexing output

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉

  GPIO_Init(GPIOF,&GPIO_InitStructure); //Initialize PF9

     

  TIM_TimeBaseStructure.TIM_Prescaler=psc; //Timer frequency division

  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //Upward counting mode

  TIM_TimeBaseStructure.TIM_Period=arr; //Automatically reload value

  TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;  

  TIM_TimeBaseInit(TIM14,&TIM_TimeBaseStructure); //Initialize timer 14

  

  //Initialize TIM14 Channel1 PWM mode    

  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM modulation mode 1

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Comparison output enable

  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Output polarity low

  TIM_OC1Init(TIM14, &TIM_OCInitStructure); //Initialize peripheral TIM1 4OC1

  TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable); //Enable preload register

     TIM_ARRPreloadConfig(TIM14,ENABLE);//ARPE enable  

  TIM_Cmd(TIM14, ENABLE); //Enable TIM14                     

}

This part of the code contains the first 5 steps of the PWM output settings introduced above. We will not talk about the settings of TIM14 here.  

Next, let's look at the main function in the main program as follows:

int main(void)

{  

  u16 led0pwmval=0;     

  u8 dir=1;

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set system interrupt priority group 2

  delay_init(168); //Initialize delay function

  uart_init(115200); //Initialize the serial port baud rate to 115200

    TIM14_PWM_Init(500-1,84-1); //The timer clock is 84M, the frequency division factor is 84, so the counting frequency

//84M/84=1Mhz, reload value 500, so PWM frequency is 1M/500=2Khz.      

     while(1)   

  {

      delay_ms(10);   

    if(dir)led0pwmval++; //dir==1 led0pwmval increments

    else led0pwmval--; //dir==0 led0pwmval decrement  

      if(led0pwmval>300)dir=0; //After led0pwmval reaches 300, the direction is decreasing

    if(led0pwmval==0)dir=1; //After led0pwmval decreases to 0, the direction changes to increasing

  

    TIM_SetCompare1(TIM14,led0pwmval); //Modify comparison value and duty cycle

  }

}

Here, we can see from the infinite loop function that we set the value of led0pwmval as the PWM comparison value, that is, we control the duty cycle of PWM through led0pwmval, and then control the value of led0pwmval from 0 to 300, and then from 300 to 0, and so on. Therefore, the brightness of DS0 will also change from dark to bright, and then from bright to dark as the duty cycle of the signal changes. As for the value here, why we take 300, because when the output duty cycle of PWM reaches this value, the brightness of our LED will not change much (although the maximum value can be set to 499), so it is unnecessary to design a value that is too large here. At this point, our software design is completed.


Keywords:STM32f4 Reference address:STM32f4 PWM output experimental code

Previous article:STM32f4 input capture experimental code
Next article:STM32f4 timer interrupt experimental code

Recommended ReadingLatest update time:2024-11-16 12:42

STM32F4 clock setting analysis
environment: Host: WIN7 Development environment: MDK4.72 MCU:STM32F407VGT6 The STM32F4 startup is different from the STM32F10X, the clock is already configured by default. 1. Startup code: File: startup_stm32f4xx.s span style="font-family:KaiTi_GB2312;font-size:18px;" ; Reset handler   Reset_Handler    PROC    
[Microcontroller]
STM32F4 clock setting analysis
STM32f4 learning path--clock
When I first started learning STM32, I found that its clock system is a bit complicated (compared to the 51 MCU I learned before). Why is this? –The 51 MCU has few I/O ports and simple peripherals, so the clock is generally fixed, which also leads to the limitations of the application scenarios of the 51 MCU. –The
[Microcontroller]
STM32f4 learning path--clock
How to use STM32F407 PC13-PC15 as GPIO
The problem is solved. PC13-PC15 can be used as GPIO and can be used as output. The manual says that only one can be used as output because the total output current of the three pins cannot exceed 4mA. If the output current is very small, all can be used as output. VBAT does not need to be connected to VDD. After VDD
[Microcontroller]
STM32F407 three ADC alternate sampling (operation register)
STM32F407 3 ADC alternate sampling (operation register) Three ADCs alternately sample In multi-ADC mode, ADC1 is the master and ADC2 or ADC3 is the slave, which are triggered alternately or simultaneously. The working mode depends on MULTI of the ADC_CCR register. In multi-ADC mode, the converted data can be read
[Microcontroller]
stm32F4 time base timer (2)
After searching a lot of information, I finally understood the time base timer. I did not use any library functions but operated the registers directly. The following introduces the systick in STM32. The Systick part belongs to the NVIC control part. There are 4 registers in total. The names and addresses are: STK_CS
[Microcontroller]
stm32F4 time base timer (2)
STM32F4——TFT-LCD principle and FSMC
1. Introduction:     TFT-LCD stands for Thin Film Transistor Liquid Crystal Display. There are many categories according to their size, resolution and driver chip. The following will introduce the 2.8-inch 320X240 resolution TFT-LCD driven by ILI9341 chip. 2. Interface:     The module uses 16-bit parallel connection w
[Microcontroller]
【stm32f407】Window watchdog wwdog
1. Window Watchdog The window watchdog (WWDG) is usually used to monitor software faults caused by external interference or unforeseen logic conditions when the application deviates from the normal operating sequence. Unless the value of the down counter is refreshed before the T6 bit (the sixth bit of WWDG- CR) bec
[Microcontroller]
【stm32f407】Window watchdog wwdog
How to set the system clock for STM32F4
STM32F4 system clock tree The system clock of STM32F4 is very important. It is related to the operation results of the entire system. No matter what operation is performed, a clock signal is required. The default system clock configuration of different types of microcontrollers is different. Here, two methods of
[Microcontroller]
How to set the system clock for STM32F4
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号