stm32 timer 2 outputs 10KHZ waveform

Publisher:sedsedqLatest update time:2015-09-21 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Continuing from the previous article

/*! @function
************************************************** **********************************

 
º¯ÊýÃû :RCC_Configuration
¹¦ÄÜ :ʱÖÓÅäÖÃ
--- -------------------------------------------------- -----------------------------
²ÎÊý :ÎÞ
·µ»ØÖµ :ÎÞ
------------ -------------------------------------------------- -------------------
ʹÓõÄÈ«¾Ö±äÁ¿£ºÎÞ
---------------------- -------------------------------------------------- ---------
×÷Õß:           
ʱ¼ä: 2012/10/17

************************** ****************************************************** *****/
static void RCC_Configuration(void)

 ErrorStatus HSEStartUpStatus; 

    RCC_ClocksTypeDef RccClocks;    
     
   RCC_DeInit();     
   RCC_HSEConfig(RCC_HSE_ON);  
   HSEStartUpStatus = RCC_WaitForHSEStartUp();  

   if(HSEStartUpStatus == SUCCESS)        
   {   
        /* Ñ¡ÔñHCLK£¨AHB£©Ê±ÖÓԴΪSYSCLK  1·ÖƵ */
     RCC_HCLKConfig(RCC_SYSCLK_Div1); 
           
     /* Ñ¡ÔñPCLK2ʱÖÓԴΪ HCLK£¨AHB£© 1·ÖƵ */
     RCC_PCLK2Config(RCC_HCLK_Div1); 
              
     /* Ñ¡ÔñPCLK1ʱÖÓԴΪ HCLK£¨AHB£© 2·ÖƵ */
     RCC_PCLK1Config(RCC_HCLK_Div2);     
     
        /* ÉèÖÃFLASHÑÓʱÖÜÆÚÊýΪ2 */
     FLASH_SetLatency(FLASH_Latency_2);     
     
        /* ʹÄÜFLASHԤȡ»º´æ */
     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);  
     
        /* Ñ¡ÔñËøÏà»·£¨PLL£©Ê±ÖÓԴΪHSE 1·ÖƵ£¬±¶ÆµÊýΪ9£¬ÔòPLLÊä³öƵÂÊΪ 8MHz * 9 = 72MHz */
     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);  
     
        /* ʹÄÜPLL */
     RCC_PLLCmd(ENABLE); 
              
     /* µÈ´ýPLLÊä³öÎȶ¨ */
     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); 
     /* Ñ¡ÔñSYSCLKʱÖÓԴΪPLL */
     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 

     /* µÈ´ýPLL³ÉΪSYSCLKʱÖÓÔ´ */
     while(RCC_GetSYSCLKSource() != 0x08);  
   }
    
    /* ÅäÖÃSysTick */ 
    RCC_GetClocksFreq(&RccClocks);  
    //SysTick_Config(RccClocks.HCLK_Frequency / OS_TICKS_PER_SEC);  
    
    /* ´ò¿ªGPIOʱÖÓ */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
                        RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD| RCC_APB2Periph_GPIOE,
                        ENABLE); 
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
}                                    

/*! @function
********************************************************************************


º¯ÊýÃû   :NVIC_Configuration
¹¦ÄÜ     :ÖжϿØÖÆÆ÷ÅäÖÃ
----------------------------------------------------------------------------------
²ÎÊý     :ÎÞ
·µ»ØÖµ   :ÎÞ
---------------------------------------------------------------------------------
ʹÓõÄÈ«¾Ö±äÁ¿£ºÎÞ
----------------------------------------------------------------------------------
×÷Õß     :           
ʱ¼ä     : 2012/10/17

*******************************************************************************/
static void NVIC_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
         
#ifdef  VECT_TAB_RAM  
 /* Set the Vector Table base location at 0x20000000 */ 
 NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); 
#else  /* VECT_TAB_FLASH  */
 /* Set the Vector Table base location at 0x08000000 */ 
 NVIC_SetVectorTable(NVIC_VectTab_FLASH  , 0);   
#endif
 /* Configure one bit for preemption priority */
 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); 
 
    /* Enable the USART1 Interrupt */ 
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);

 /* Enable the USART2 Interrupt */
 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
    
    /* Enable the USART3 Interrupt */
 NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);  
  /* Enable the TIMER3 Interrupt */
 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);
  /* Enable the TIMER3 Interrupt */
 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 NVIC_Init(&NVIC_InitStructure);      
}

//72M timer2 
//ÖжÏʱ¼ä£º£¨720*10£©/72000000=0.1Ms=10K
void Timer2_config() //ucosiiʱÖÓ
 {
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Prescaler = 360-1; //36M/360=100K HZ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //ÏòÉϼÆÊý
TIM_TimeBaseStructure.TIM_Period = 10;       // ;   //×°ÔØֵѡÔñ 10K
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  TIM_ITConfig(TIM2,TIM_IT_Update|TIM_IT_Trigger,ENABLE);
  TIM_Cmd(TIM2, ENABLE);
 }

 

void TIM2_IRQHandler(void)
{

if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì²éÖ¸¶¨µÄTIMÖжϷ¢ÉúÓë·ñ:TIM ÖжÏÔ´ 
 {
 
   if(Ledflg==0)
  {
    GPIO_SetBits(GPIOC, GPIO_Pin_6);
   GPIO_SetBits(GPIOC, GPIO_Pin_7);
    Ledflg=1;
  }
  else
  {
  GPIO_ResetBits(GPIOC, GPIO_Pin_6);
   GPIO_ResetBits(GPIOC, GPIO_Pin_7);
   Ledflg=0;
  }
  Ledcnt=0;

 } 
TIM_ClearITPendingBit(TIM2, TIM_IT_Update  );  //Çå³ýTIMxµÄÖжϴý´¦Àíλ:TIM ÖжÏÔ´ 
                        
}

Keywords:stm32 Reference address:stm32 timer 2 outputs 10KHZ waveform

Previous article:About the application configuration of stm32 timer
Next article:MCU drawing curve

Recommended ReadingLatest update time:2024-11-23 15:38

Detailed explanation of STM32 clock RCC (Part 3)
Analysis of RCC related library functions 1. Structures involved in the library typedef struct {   uint32_t SYSCLK_Frequency;     uint32_t HCLK_Frequency;       uint32_t PCLK1_Frequency;      uint32_t PCLK2_Frequency;      uint32_t ADCCLK_Frequency; }RCC_ClocksTypeDef; 2. Library function analysis void RCC_DeInit(void
[Microcontroller]
STMicroelectronics adds support for deep quantized neural networks to STM32Cube.AI development tool
STMicroelectronics (ST) has released STM32Cube.AI version 7.2.0, the microcontroller vendor’s first artificial intelligence (AI) development tool supporting ultra-efficient deep quantized neural networks.  STM32Cube.AI converts pre-trained neural networks into C language code that can be run by STM32 microcontro
[Internet of Things]
STMicroelectronics adds support for deep quantized neural networks to STM32Cube.AI development tool
How to send multiple packets using the USB non-control endpoint of STM32
The following are questions raised by netizens and my explanation of this issue.   SMT32F103, modified according to the routine Custom_HID, using EP1 to send packets in EP_INTERRUPT mode. The original routine sends 2 bytes each time. Now after the modification, the sending is normal when the length of the packet doe
[Microcontroller]
STM32 clock understanding
1. Hardware connection issues   If you use the internal RC oscillator instead of an external crystal oscillator, please proceed as follows: 1) For products with 100 or 144 pins, OSC_IN should be grounded and OSC_OUT should be left floating. 2) For products with less than 100 pins, there are two ways to connect
[Microcontroller]
STM32 learning notes timer input capture experiment
Purpose: Print the time the key is pressed on the serial debugging assistant Experimental steps: Experimental Procedure: /******************************timer.c****************** *******************/   #include "sys.h"   #include "stm32f4xx.h"         extern u8 TIM5CHA1_CAPTURE_STA;   extern u16 TIM5CHA
[Microcontroller]
STM32 Getting Started Tutorial System Clock SysTick
1. Background In traditional embedded system software, the Delay(N) function is usually implemented as follows: for(i = 0; i = x; i ++);                 x --- corresponds to the loop value corresponding to N milliseconds For the STM32 series microprocessors, it only takes tens of nanoseconds to execute an instru
[Microcontroller]
STM32 stack size details and variable storage location
Stack growth and big-endian/little-endian issues are two issues related to the CPU. 1. Let's first look at the problem of the stack (STACK). The local variables of a function are all stored in the "stack". The English name of the stack is: STACK. The size of the STACK can be set in the startup file of stm32. Taking
[Microcontroller]
STM32 stack size details and variable storage location
Design and implementation of digitally controlled sinusoidal wave inverter power supply based on STM32 series microcontroller
    Inverter power supplies are widely used, especially precision instruments have higher performance requirements for inverter power supplies. High-performance inverter power supplies not only require stable operation, high inverter efficiency, good output waveform characteristics, and complete protection functions, b
[Microcontroller]
Design and implementation of digitally controlled sinusoidal wave inverter power supply based on STM32 series microcontroller
Latest Microcontroller Articles
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号