STM32 PA15复用为PWM波输出

N32G45X是类似STM32的一种国产方案,我想让PA15输出一组PWM波,
可以选用TIM2通道1(方案1),也可以选用TIM8的反向通道1(方案2)

以输出占空比为50%的方波波形为例:
方案1**
方案1代码 方法如下:

void GPIO_Config(void)
{
  	GPIO_InitType GPIO_InitStructure;
	RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2,ENABLE);
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
	GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_SW_ENABLE,ENABLE);	//GPIO_RMP_SW_JTAG_SW_ENABLE  GPIO_RMP_SW_JTAG_DISABLE 
	GPIO_ConfigPinRemap(GPIO_PartialRemap1_TIM2,ENABLE);		
	GPIO_InitStructure.Pin  =  GPIO_PIN_15;			//PWM-IO
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //GPIO_Mode_AF_PP  GPIO_Mode_Out_PP
	GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);	
	GPIO_SetBits(GPIOA,GPIO_PIN_15);
}
void TIM2_Init(u16 psc, u16 arr)
{ 
    TIM_TimeBaseInitType  TIM_TimeBaseStructure;
    OCInitType  TIM_OCInitStructure;
    
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2, ENABLE);
	                                                       	
    /*Set auto-load register value*/    
    TIM_TimeBaseStructure.Period = arr;
    /*Set the prescaler value*/    
    TIM_TimeBaseStructure.Prescaler = psc; 
    /*Set the clock segmentation*/    
    TIM_TimeBaseStructure.ClkDiv=0;
		TIM_TimeBaseStructure.RepetCnt = 0;
    /*Set the counting mode*/    
    TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
    /*Initialize the TIMx register*/    
    TIM_InitTimeBase(TIM2, &TIM_TimeBaseStructure);                             
#if 1                                                                                
    TIM_InitOcStruct(&TIM_OCInitStructure);     
    /*Configure PWM mode*/    
    TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1; 
    /*Output enabled*/    
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;  
    /*Initial pulse width is set 20,is the capture compare value*/    
    TIM_OCInitStructure.Pulse = 365;  //   arr/2 , output square wave
    /*Less than CCR1 value is low*/    
    TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_LOW;
    /*Initialize TIM8_OC1 register*/    
    TIM_InitOc1(TIM2, &TIM_OCInitStructure);                                    
        
    /*Preload enable*/          
    TIM_ConfigOc1Preload(TIM2, TIM_OC_PRE_LOAD_ENABLE);                           
#endif              
    /*Enable TIM2*/                                                                           
    TIM_Enable(TIM2, ENABLE);                                                      
}

然后再main中调用之即可。
例如:
int main(void)
{
GPIO_Config();
TIM2_Init(8,799);
while(1)
{}
}
示波器读取PA15对地输出波形如下:

请添加图片描述

*******一个问题
有的初学者工程师会问:单独输出一路反向通道PWM波是否可以??答案是可以,查看方案2.

*******方案2
方案2代码如下:

void GPIO_Config(void)
{
  	GPIO_InitType GPIO_InitStructure;
	RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_GPIOB |RCC_APB2_PERIPH_GPIOC|
	RCC_APB2_PERIPH_GPIOD|RCC_APB2_PERIPH_AFIO|RCC_APB2_PERIPH_TIM8, ENABLE);//RCC_APB2_PERIPH_AFIO  
	GPIO_ConfigPinRemap(GPIO_RMP_SW_JTAG_SW_ENABLE,ENABLE);	//GPIO_RMP_SW_JTAG_SW_ENABLE  GPIO_RMP_SW_JTAG_DISABLE 
	GPIO_ConfigPinRemap(GPIO_RMP3_TIM8,ENABLE);			
	GPIO_InitStructure.Pin  =  GPIO_PIN_15;			//output PWM-IO
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //GPIO_Mode_AF_PP  GPIO_Mode_Out_PP
	GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);	
	GPIO_SetBits(GPIOA,GPIO_PIN_15);
}
void TIM8_Init(u16 psc, u16 arr)
{ 
    TIM_TimeBaseInitType  TIM_TimeBaseStructure;
    OCInitType  TIM_OCInitStructure;    
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_TIM8, ENABLE);	                                                       	
    /*Set auto-load register value*/    
    TIM_TimeBaseStructure.Period = arr;
    /*Set the prescaler value*/    
    TIM_TimeBaseStructure.Prescaler = psc; 
    /*Set the clock segmentation*/    
    TIM_TimeBaseStructure.ClkDiv=0;
		TIM_TimeBaseStructure.RepetCnt = 0;
    /*Set the counting mode*/    
    TIM_TimeBaseStructure.CntMode = TIM_CNT_MODE_UP;
    /*Initialize the TIMx register*/    
    TIM_InitTimeBase(TIM8, &TIM_TimeBaseStructure);                             
#if 1                                                                                
    TIM_InitOcStruct(&TIM_OCInitStructure);     
    /*Configure PWM mode*/    
    TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1; 
    /*Output enabled*/    
    TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;  		
    /*Initial pulse width is set 20,is the capture compare value*/    
    TIM_OCInitStructure.Pulse = 365;  
    /*Less than CCR1 value is low*/    
    TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_LOW;	
	TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;
    /*Initialize TIM8_OC1 register*/    
    TIM_InitOc1(TIM8, &TIM_OCInitStructure);                                    
        
    /*Preload enable*/          
    TIM_ConfigOc1Preload(TIM8, TIM_OC_PRE_LOAD_ENABLE);                           
#endif          		
    /*Enable TIM8*/                                                                           
    TIM_Enable(TIM8, ENABLE);  
	TIM_EnableCtrlPwmOutputs(TIM8,ENABLE);		
}

然后再main中调用之即可。
例如:
int main(void)
{
GPIO_Config();
TIM8_Init(8,799);
while(1)
{}
}
示波器读取PA15对地输出波形如下:
请添加图片描述
拍的有点反光,另外我要说明一点,我设置的就是让它输出占空比为50%的方波。
各位亲们可以修改捕获值和ARR值来配置不同的PWM波。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值