PWM output based on STM32

Publisher:快乐时刻Latest update time:2016-10-05 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
c) Initialization function definition:
void TIM_Configuration(void); //Define TIM initialization function
d) Initialization function call:
TIM_Configuration(); //TIM initialization function call
e) Initialization function, different from the previous module, TIM initialization is divided into two parts - basic initialization and channel initialization:
void TIM_Configuration(void) //TIM initialization function

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //Timer initialization structure
  TIM_OCInitTypeDef TIM_OCInitStructure; //Channel output initialization structure

//TIM3 initialization
  TIM_TimeBaseStructure.TIM_Period = 0xFFFF; //Period 0~FFFF
  TIM_TimeBaseStructure.TIM_Prescaler = 5; //Clock division
  TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Clock division
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ModeTIM_TimeBaseInit
  (TIM3, &TIM_TimeBaseStructure); //Basic initializationTIM_ITConfig
  (TIM3, TIM_IT_CC4, ENABLE); //Turn on interrupt, interrupt needs this line of code
  
//TIM3 channel initializationTIM_OCStructInit
  (& TIM_OCInitStructure); //Default
  parametersTIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Working stateTIM_OCInitStructure.TIM_OutputState
  = TIM_OutputState_Enable; //Set to output, this line of code is required for PWM outputTIM_OCInitStructure.TIM_Pulse
  = 0x2000; //Duty lengthTIM_OCInitStructure.TIM_OCPolarity
  = TIM_OCPolarity_High; //High levelTIM_OC4Init
  (TIM3, &TIM_OCInitStructure); //Channel initialization

  TIM_Cmd(TIM3, ENABLE); //Start TIM3
}

f) Add TIM clock start in RCC initialization function:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM3, ENABLE);
g) Set the input and output pin modes in GPIO. Signal: AF_PP, 50MHz.
h) If you use interrupts, add the following code to NVIC:

//Open TIM2 interrupt
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; //Channel
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //Preemption level
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //Response level
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Start
  NVIC_Init(&NVIC_InitStructure); //Initialization

Interrupt code:
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2, TIM_IT_CC4) != RESET) //Judge the interrupt source
  {
    TIM_ClearITPendingBit(TIM2, TIM_IT_CC4); //Clear interrupt flag
    GPIO_WriteBit(GPIOB, GPIO_Pin_11, (BitAction)(1-GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_11)));//Change LED color
    IC4value = TIM_GetCapture4(TIM2); //Get captured value
  }  
}

i) Simple application:
//Change duty cycle
TIM_SetCompare4(TIM3, variable);

j) Note:
The IO output mode of the pin is determined according to the application. For example, if PWM output is used to drive the LED, the corresponding pin should be set to AF_PP, otherwise the microcontroller will have no output.
Keywords:STM32 Reference address:PWM output based on STM32

Previous article:STM32 learning three
Next article:STM32 PB2 (BOOT1) usage precautions

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

Detailed explanation of the magic of STM32 buttons
Assume that there are three ports PB6, 7, and 4: If pressed, it is low level (0); otherwise, it is high level (1). void ReadKey(void) { //1. Get the GPIB- IDR register value (get the key value)          u16 ReadData = (GPIO_InputData( GPIOB ) & 0xD0) ^ 0xD0;                     //2. Determine which bit h
[Microcontroller]
STM32 hardware IIC and 51 analog IIC communication
IIC Introduction   The IIC protocol stipulates that the data transmitted on SDA must remain stable during the high level of SCL, and the data on SDA can only change during the low level of SCL. During IIC, data is placed on SDA at the rising edge of the pulse, and data is read from SAD at the falling edge of the pul
[Microcontroller]
STM32 timer timing calculation formula
Tout = ((arr+1)*(psc+1))/Tclk; in: Tclk: Timer input clock frequency (unit: MHZ) Tout: Timer overflow time (unit: us)   .TIM_Period = arr; eg; 4999   .TIM_Prescaler = psc; eg: 7199   Tout = ((4999+1) × (7199+1))/72 = 500000us = 500ms.
[Microcontroller]
First look at I2C with STM32
1. Communication protocol.   I2C is a two-wire serial bus developed by PHILIPS and is a synchronous half-duplex bus. Data is valid When transmitting data, the SDA line must remain stable during the high level period of the clock. The high or low level state of SDA can only change when the clock signal of the SCL line
[Microcontroller]
First look at I2C with STM32
STM32 IO configuration lighting
1. Specific code of led.c: /*----------------------------------------------------------*/ #include "led.h"   /* ------------------------------------------------------------------------- File name: led.c Description: Configure the LED port according to the hardware connection and open the corresponding register ----
[Microcontroller]
STM32F4 study notes 5——The highest bit error of the data sent by the stm32 serial port
        Recently, I encountered a problem with the data transmission of the stm32 serial port when doing ModBus protocol communication based on the stm32f401 serial port. I spent a whole day looking for the problem, from the ModBus protocol format, scheduling algorithm to the serial port configuration, and finally sol
[Microcontroller]
How to use TXE and TC flags when STM32 USART sends data
There are two registers at the transmitting end of USART, one is the USART_DR register (the shaded TDR in the figure below) that can be seen by the program, and the other is the shift register (the shaded Transmit Shift Register in the figure below) that cannot be seen by the program. There are two flags correspondi
[Microcontroller]
How to use TXE and TC flags when STM32 USART sends data
Implementation of STM32+IAP solution under IAR environment
1. What is IAP and why do we need IAP?       IAP stands for In Application Programming. Generally, the devices with STM32F10x series chips as the main controller have already used J-Link emulator to burn the application code before leaving the factory. If the application code needs to be replaced or upgraded during th
[Microcontroller]
Implementation of STM32+IAP solution under IAR environment
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号