stm32 TIM3_CH1 PB4复用输出PWM 完整配置源码

stm32F103使用定时器3配置PB4引脚产生1KHz的PWM波

引脚配置

void TIM3_GPIO_Config(void) 
{
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
  GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE); 
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
  GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_4);
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
  GPIO_Init(GPIOB, &GPIO_InitStructure); 
  //GPIO_SetBits(GPIOB, GPIO_Pin_4);
}
功能参数配置

  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseStructure.TIM_Period = 1000;			    /*¼ÆÊýÖµ 1S*/  
	TIM_TimeBaseStructure.TIM_Prescaler = 72-1;    	    /*Ô¤·ÖƵ,´ËÖµ+1Ϊ·ÖƵµÄ³ýÊý*/
	TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;  	
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	TIM_ClearITPendingBit(TIM3,  TIM_IT_Update); 
	TIM_ITConfig(TIM3,  TIM_IT_Update, ENABLE);

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; 
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
	TIM_OCInitStructure.TIM_Pulse = 500-1;     
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OC1Init(TIM3, &TIM_OCInitStructure);  
	TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  

	TIM_CtrlPWMOutputs(TIM3,ENABLE);  
  TIM_ARRPreloadConfig(TIM3, ENABLE); 

	TIM_Cmd(TIM3, ENABLE);
这样就可以了,源码上传到我的账号了,可以下载使用,


你可能感兴趣的:(stm32)