STM32 MCU PWM output test

Publisher:ShuangfeiLatest update time:2016-12-19 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

environment:

Host: XP

Development environment: MDK4.23

MCU:STM32F103CBT6

 

illustrate:

Use the internal 8M crystal oscillator, multiply it to 64M to supply the TIM3 timer, and generate 640K, 50% square wave on PA6 (channel 1)


source code:

Initialize the clock:


  1. //Initialize RCC clock  

  2. void init_rcc(void)  

  3. {  

  4.     //Reset the peripheral RCC register to the default value  

  5.     RCC_DeInit();  

  6.     //Internal crystal oscillator enabled  

  7.     RCC_HSICmd(ENABLE);  

  8.     //Wait for the job to stabilize  

  9.     while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);  

  10.   

  11.     //LSI startup, providing watchdog clock  

  12.     RCC_LSICmd(ENABLE); //Enable LSI  

  13.     while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY)==RESET); //Wait until LSI is stable  

  14.    

  15.     if(1)  

  16.     {  

  17.         // Enable prefetch cache  

  18.         FLASH_PrefetchBufferCmd( FLASH_PrefetchBuffer_Enable );  

  19.         //Set the number of FLASH memory delay clock cycles. (Set to delay 2 clock cycles)  

  20.         FLASH_SetLatency( FLASH_Latency_2 );  

  21.         //Set the AHB clock (HCLK) (set to = system clock at this time)  

  22.         RCC_HCLKConfig( RCC_SYSCLK_Div1 );  

  23.         //Set the AHB clock (HCLK2) of SDIO (set to = HCLK at this time), 64M  

  24.         RCC_PCLK2Config( RCC_HCLK_Div1 );  

  25.         //Set APB1 peripheral clock (HCLK1) (set to = HCLK/2 at this time), 32M  

  26.         RCC_PCLK1Config( RCC_HCLK_Div2 );  

  27.         //ADCCLK = PCLK2/8,1M  

  28.         RCC_ADCCLKConfig( RCC_PCLK2_Div8 );  

  29.   

  30.         //PLL clock source and frequency multiplier, here set to 64MHz  

  31.         RCC_PLLConfig(RCC_PLLSource_HSI_Div2,RCC_PLLMul_16);  

  32.   

  33.         // Enable PLL  

  34.         RCC_PLLCmd(ENABLE);  

  35.         //Check if PLL is ready  

  36.         while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ) ;  

  37.         //Set PLL as system clock  

  38.         RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK ) ;  

  39.         //Check if the system clock source is PLL  

  40.         while( RCC_GetSYSCLKSource() != 0x08 ) ;  

  41.     }  

  42. }  


Initialize IO ports and timers:


  1. //Set IO port  

  2. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);  

  3. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;  

  4. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  

  5. GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;     

  6. GPIO_Init(GPIOA, &GPIO_InitStructure);  

  7.   

  8. //Set timer 3  

  9. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);  

  10. //Reset the Timer to its default value  

  11. TIM_DeInit(TIM3);  

  12. //Use the internal clock to provide the clock source for TIM3  

  13. TIM_InternalClockConfig(TIM3);  

  14.   

  15. timInitStruct.TIM_ClockDivision = TIM_CKD_DIV2;               

  16. timInitStruct.TIM_Prescaler = 0; //Counting frequency is 64M  

  17. timInitStruct.TIM_CounterMode = TIM_CounterMode_Up; //Count up  

  18. timInitStruct.TIM_RepetitionCounter = 0;  

  19. timInitStruct.TIM_Period = 100; //This value is actually TIMX->ARR, and can be reset when the delay starts  

  20.   

  21. TIM_TimeBaseInit(TIM3, &timInitStruct);  

  22.   

  23. //Set PWM output  

  24. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  

  25. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  

  26. TIM_OCInitStructure.TIM_Pulse = 50;  

  27. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  

  28. TIM_OC1Init(TIM3, &TIM_OCInitStructure);  

  29. TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  

  30.   

  31. //ARR preload buffer enable  

  32. TIM_ARRPreloadConfig(TIM3, ENABLE);  

  33. //Start the timer  

  34. TIM_Cmd(TIM3, ENABLE);                                        


Output waveform:


Keywords:STM32 Reference address:STM32 MCU PWM output test

Previous article:STM32 collects multi-channel AD through DMA
Next article:STM32 standby mode wake-up test and independent watchdog test

Recommended ReadingLatest update time:2024-11-15 15:57

Real-time temperature acquisition and analysis system based on AVR microcontroller and DS18B20
Introduction: The real-time temperature acquisition and analysis system described in this article is a real-time temperature acquisition and analysis system based on the combination of AVR series microcontroller ATmega16 and temperature sensor DS18B20. The real-time temperature acquisition and analysis system descri
[Microcontroller]
Real-time temperature acquisition and analysis system based on AVR microcontroller and DS18B20
Application Design of PWM of AVR Microcontroller Timer/Counter
1. Key points of timer/counter PWM design According to the characteristics of PWM, the following points should be noted when using the ATmega128 timer/counter to design output PWM: 1. First, the PWM frequency range to be output should be determined according to the actual situation. This frequency is related to the
[Microcontroller]
Definition of standard registers and bits of MSP430 MCU
1. Standard bit definition (STANDARD BITS)  :   WORD: word 15                  14                                                                                                          7                                                                        1                            0    
[Microcontroller]
MCU Novice Learning Series 15 Summary of Failed MCU Program Download
The whole process of lighting up the first LED was introduced above. Many people may be lucky enough to light up the LED, but it is also very likely that many people are very unlucky and fail to download the program. Beginners often encounter situations where the program cannot be downloaded. Many people are frustrated
[Microcontroller]
STM32 debugging-serial port printing function and usage
1. In the usart.h file, add the following code: #if 1 #pragma import(__use_no_semihosting)              //Support functions required by the standard library                  struct __FILE  {  int handle;  };   FILE __stdout;        // define _sys_exit() to avoid using semihost mode     _sys_exit(int x)  {  x = x;  } 
[Microcontroller]
Simple digital filtering algorithm for single chip microcomputer
1) Limiting filter algorithm During this operation, two adjacent samples are subtracted to find their increment, and then the absolute value of the increment is compared with the maximum difference A allowed between the two samples. The size of A depends on the specific situation of the object being measured. If it is
[Microcontroller]
51 MCU IO port expansion - 74HC165 chip parallel to serial experiment
We all know that there are two major types of communication: serial and parallel. The biggest advantage of serial is that it occupies less bus, but the transmission rate is low; parallel is just the opposite, occupying more bus and high transmission rate. There are many such chips on the market, some are serial input
[Microcontroller]
51 MCU IO port expansion - 74HC165 chip parallel to serial experiment
Small problems encountered by PIC microcontroller when continuously receiving serial port data
Regarding the serial port data reception of the PIC microcontroller, if the data interface can always detect data, then even if RCIE is turned off, data will actually be received. When the data continues to come in, when you enable RCIE, you can actually only receive the two data after it is turned off, and you will n
[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号