STM32F103PWM+重映射 小程序

来源:互联网 发布:sql一句话木马 编辑:程序博客网 时间:2024/05/08 15:43

原文地址:STM32F103PWM+重映射 小程序

我的开发板是学习板,很多管脚没有引出。弄到定时器产生PWM这一块,想用下管脚重映射功能,毕竟库函数没有像操作寄存器那样容易理解。所以一开始着实糊涂了一会,最后还是给调出来了。结合很多别人的例程,但是发现都不够详细,所以就共享下,如果有同样问题的同学,希望能给你帮助。下面就共享下main.c。声明:以下是本人开发板的管道对应引脚

复用功能没有重映射部分重映射完全重映射TIM3_CH1PA6PB4PC6CH2PA7PB5PC7CH3PB0PB0PC8CH4PB1PB1PC9
/**重映射 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>&copy; 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\r\n", file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */

/******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
0 0
原创粉丝点击