• STM32 ~ STM32 TIM重映射


    复用功能 没有重映射 部分重映射 完全重映射
    TIM3_CH1 PA6 PB4 PC6
    CH2 PA7 PB5 PC7
    CH3 PB0 PB0 PC8
    CH4 PB1 PB1 PC9
    /**重映射 tim3 使得PC7 PC8输出Pwm波    
      ******************************************************************************
      * @file    Project/Template/main.c
      * @author  MCD Application Team
      * @version V3.0.0
      * @date    04/06/2009
      * @brief   Main program body
      ******************************************************************************
      * @copy
      *
      * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
      * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
      * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
      * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
      * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
      * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
      *
      * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
      */
    /* Includes ------------------------------------------------------------------*/
    #include "stm32f10x.h"
    #include "stm32f10x_tim.h"
    #include "misc.h"
    void GPIO_AFIODeInit(void);//这个函数不能落下,否则不能成功
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;//配置PWM需要使用的结构体声名
    TIM_OCInitTypeDef          TIM_OCInitStructure;
    u16 CCR3_Val =250;
    u16 CCR4_Val =250;
                   
    void GPIO_Config(void);
    void Timer_Config(void);
    /**
      * @brief  Delay program.
      * @param  None
      * @retval : None
      */
    void  Delay (u32 nCount)
    {
      for(; nCount != 0; nCount--);
    }
    /**
      * @brief  Main program.
      * @param  None
      * @retval : None
      */
    int main(void)
    {
    /* Setup STM32 system (clock, PLL and Flash configuration) */
    SystemInit();
    GPIO_Config();
    Timer_Config();
    /* Infinite loop */
    while (1) {
                   }
    }
    /**
      * @brief  GPIO_Config program.
      * @param  None
      * @retval : None
      */
    void GPIO_Config(void)
    {
      GPIO_InitTypeDef GPIO_InitStructure;
      
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |            RCC_APB2Periph_GPIOC |RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
    
       GPIO_AFIODeInit();  //刚开始没有这个语句,所以一直没有重映射成功
       GPIO_PinRemapConfig(GPIO_FullRemap_TIM3 , ENABLE); //这个就是重映射功能函数
    /*GPIOA Configuration: TIM3 channel 3 and 4 as alternate function push-pull */
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //这里也要注意
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
      GPIO_Init(GPIOB, &GPIO_InitStructure); //这四句是为了校验究竟PB0,PB1是否还有PWM输出  
      GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8 | GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
      GPIO_Init(GPIOC, &GPIO_InitStructure);
      
      
    }
    void Timer_Config(void)
    {
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    
      TIM_DeInit(TIM3);
      TIM_TimeBaseStructure.TIM_Period = 999;  //在下一个更新事件装入活动的自动重装载寄存器周期的值
      TIM_TimeBaseStructure.TIM_Prescaler = 0; //  设置了用来作为 TIMx 时钟频率除数的预分频值
      TIM_TimeBaseStructure.TIM_ClockDivision = 0;   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  
      TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
      /* PWM1 Mode configuration: Channel3 */
      TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;  //TIM_OCMode
      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
      TIM_OCInitStructure.TIM_Pulse = CCR3_Val;      //设置了待装入捕获比较寄存器的值
      TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //TIM_OCPolarity :TIM 输出比较极性高
      TIM_OC3Init(TIM3, &TIM_OCInitStructure);
      TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能或者失能 TIM3 在 CCR3 上的预装载寄存器
      /* PWM1 Mode configuration: Channel4 */
      TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;  
      TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
      TIM_OC4Init(TIM3, &TIM_OCInitStructure);
      TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
      /* TIM3 enable counter */
      TIM_Cmd(TIM3, ENABLE);          //开启时钟,别落下
    }
    /**
      * @brief  EXTI_Config Program.
      * @param  None
      * @retval : None
      */
    
    /**
      * @brief  Configures the nested vectored interrupt controller.
      * @param  None
      * @retval : None
      */
    
    #ifdef  USE_FULL_ASSERT
    /**
      * @brief  Reports the name of the source file and the source line number
      *   where the assert_param error has occurred.
      * @param file: pointer to the source file name
      * @param line: assert_param error line source number
      * @retval : None
      */
    void assert_failed(uint8_t* file, uint32_t line)
    {
      /* User can add his own implementation to report the file name and line number,
         ex: printf("Wrong parameters value: file %s on line %d
    ", file, line) */
      /* Infinite loop */
      while (1)
      {
      }
    }
    #endif
    /**
      * @}
      */
    
    /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
    
  • 相关阅读:
    20172322 《程序设计与数据结构》第六周学习总结
    20172322 2018-2019-1 10月19日课上测试报告
    20172322 《程序设计与数据结构》第五周学习总结
    20172322 《程序设计与数据结构》第四周学习总结
    172322 2018-2019-1 《Java软件结构与数据结构》实验一报告
    20172322 《程序设计与数据结构》第三周学习总结
    20172322 《程序设计与数据结构》第二周学习总结
    20172303 20172322 2017-2018-2 暑假作业 结对编程项目-舒尔特方格(增补:计时器的加入与页面优化)
    20172322 《程序设计与数据结构》第一周学习总结
    20172322 2017-2018-2《程序设计与数据结构》(上)课程总结
  • 原文地址:https://www.cnblogs.com/einstein-2014731/p/4617707.html
Copyright © 2020-2023  润新知