[STM32 Cotex-M3 processor series programming] Timer output PWM wave

Publisher:灵感火花Latest update time:2015-09-22 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//Use the 3-channel CH3 of timer TIM4 to output a PWM wave with a duty cycle of 25%
#include "stm32f10x.h"
 
int main(void)
//  SystemInit();
  
  // Placement IO port
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE); //IO port enable setting
  GPIO_InitTypeDef GPIO_InitStructure;    //Define the structure
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;    //Connect CH3 of TIM3 to PB8 pin
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplexed push-pull output
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
  
   //Timer 2
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //Configure clock
  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_TimeBaseStructure.TIM_Period=10000;    //1s
  TIM_TimeBaseStructure.TIM_Prescaler=7199;   //720 division
  TIM_TimeBaseStructure.TIM_ClockDivision=0;
  TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //Count up
  TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
 
  
  TIM_OCInitTypeDef TIM_OCInitStructure;
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1 mode 1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Comparison output enable
  TIM_OCInitStructure.TIM_Pulse = 2500;   //Set duty cycle
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;   //Output comparison polarity high
  TIM_OC3Init(TIM4, & TIM_OCInitStructure); //Initialize CH3 channel of TIM4
  TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable); //Enable TIM4 preload register on CH3 channel CCR3
  TIM_ARRPreloadConfig(TIM4, ENABLE); //Enable TIM4 preload register on CH3 channel ARR3
  TIM_Cmd(TIM4,ENABLE); //Enable timer 4
  
while(1);
}
 
 
//The following is the error reporting function
#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line)
{
  while (1)
  {
  }
}
#endif
Keywords:STM32 Reference address:[STM32 Cotex-M3 processor series programming] Timer output PWM wave

Previous article:[STM32 Cotex-M3 processor series programming] time base timer
Next article:[STM32 Cotex-M3 processor series programming] serial port debugging

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

[STM32 Cotex-M3 processor series programming] serial port debugging
#include "stm32f10x.h" //#include "stm32f10x_lib.h"   void Delay(unsigned int x); void UART_Init(void);   int main(void) {               while (1)    {       Delay(300000);       UART_Init();    //Initialize the serial port       USART_SendData(USART1,0x1A);    //Send data from the serial p
[Microcontroller]
Analyzing the startup process of STM32
In the current embedded application development process, C language has become the best choice for most occasions. In this way, the main function seems to be the starting point for granted - because C programs often start execution from the main function. But a question that is often overlooked is: how does the microco
[Microcontroller]
DMA demo for STM32, USART
#include "stm32f10x_lib.h" #include "stdio.h" #define USART1_DR_Base 0x40013804 #define SENDBUFF_SIZE 10240 vu8 SendBuff ; vu8 RecvBuff ; vu8 recv_ptr; void RCC_Configuration(void); void GPIO_Confi guration(void); void NVIC_Configuration(void); void DMA_Configuration(void); void USART1_Configuration(void); int fput
[Microcontroller]
Analysis of STM32 Systick timer
There is a Systick timer in the ARM Cortex-M3 core, which is a 24-bit countdown timer. When the count reaches 0, it will automatically reload the initial value of the timer from the Load register. As long as the ENABLE in the CTRL register is not cleared to 0, it will never stop. The understanding of the tick timer is
[Microcontroller]
How to code the STM32 serial port to receive messages more efficiently
Abstract This article introduces the design of a circular queue data structure to achieve more stable serial port message reception and effectively prevent packet loss. I have been studying multi-rotor aircraft and other things during this period. I haven't updated my blog. If I don't stick to it, I'm afraid it will
[Microcontroller]
Stm32 peripheral module programming initialization steps
1. External interruption 1) Initialize the IO port as input. In this step, set the state of the IO port you want to use as the external interrupt input. It can be set to pull-up/pull-down input or floating input, but when floating, it must be pulled up or pulled down externally. Otherwise, the interrupt may be trigge
[Microcontroller]
How to set up the STM32 input pull-up and pull-down registers
In output mode: ODR is the data output register, but in input mode, it is also used to configure the pull-up and pull-down settings. In the key input experiment in the Alientek source code, there is the following initialization code: void KEY_Init(void){       RCC- APB2ENR|=1 2; //Enable PORTA clock     GPIOA- CRL&=0
[Microcontroller]
How to set up the STM32 input pull-up and pull-down registers
STM32 Learning Notes-TIM3 Overflow Timing
TIM3 is a general-purpose timer. The program uses the APB1 clock (PCLK1), 72MHz. In the program, TIM3 overflows, that is, overflows when 0-ARR. The frequency of TIM3 in the above program is (PCLK1*2)/(36000-1+1)=2KHz, where PCLK1 is 36MHz, so counting 2000 times is 1s. Steps to use timer timing: 1
[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

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号