<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出

<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出

一 基础工程

本例基础工程以正点原子战舰V3开发板配套 库函数 开发例程《实验9 PWM输出实验》;
在此例程基础上进行 定时器1互补PWM输出。

二 代码修改

基于例程,只需修改main.c即可,修改后内容如下:

#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
 
void GPIO_TIM1_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

    /* GPIOA and GPIOB clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);

    /* GPIOA Configuration: Channel 1 and 2 as alternate function push-pull */
    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(GPIOA, &GPIO_InitStructure);

    /* GPIOB Configuration: Channel 1N and 2N as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* TIM1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 , ENABLE);

    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 0;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 0;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;

    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
		TIM_OC2Init(TIM1, &TIM_OCInitStructure);

    /* Automatic Output enable, Break, dead time and lock configuration */
    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    TIM_BDTRInitStructure.TIM_DeadTime = 0;
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;

    TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);

    TIM_Cmd(TIM1, ENABLE);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

void Adjust_TIM1(uint16_t freq, uint16_t deadTime, uint16_t dutyCycle)
{
    uint16_t period = (SystemCoreClock / freq) - 1;
    uint16_t pulse = (period + 1) * dutyCycle / 100;

    TIM1->ARR = period;
    TIM1->CCR1 = pulse;
    TIM1->CCR2 = pulse;
    TIM1->BDTR &= ~TIM_BDTR_DTG;
    TIM1->BDTR |= deadTime;
}

	
 int main(void)
 {		
 	u16 led0pwmval=0;
	u8 dir=1;	
	delay_init();	    	 //延时函数初始化	  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 	 //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
	uart_init(115200);	 //串口初始化为115200
 	LED_Init();			     //LED端口初始化
 	TIM3_PWM_Init(8990,0);	 //不分频。PWM频率=72000000/900=80Khz PB5
	 GPIO_TIM1_Init(); //pa8  pb7  如何 使用 keil5 logic analyzer 查看GPIOA8的信号波形
	 Adjust_TIM1(7200,20,60);
   	while(1)
	{
 		delay_ms(10);	 
		if(dir)led0pwmval++;
		else led0pwmval--;

 		if(led0pwmval>300)dir=0;
		if(led0pwmval==0)dir=1;										 
		TIM_SetCompare2(TIM3,led0pwmval);		   
	}	 
 }


其中函数 void GPIO_TIM1_Init(void) 和void Adjust_TIM1(uint16_t freq, uint16_t deadTime, uint16_t dutyCycle) 是新增 内容。

GPIO_TIM1_Init():用以初始化定时器 互补输出;
Adjust_TIM1():用以修改定时器1的输出频率、占空比 以及死区时间。

三 查看波形

3.1 配置

<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出_第1张图片

3.2 debug

<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出_第2张图片
<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出_第3张图片
<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出_第4张图片

3.3 运行

<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出_第5张图片

四 结论

配置完成TIM1的PWM输出后 可看到两路PWM为互补输出波形。

五 拓展

在上述代码中其实以及配置了 第二组的PWM互补输出。分别是GPIOA9 和 GPIOB14,以及 使用 TIM_OC2Init(TIM1, &TIM_OCInitStructure);初始化;并通过TIM1->CCR2 = pulse;配置占空比。
拓展查看波形如下:
<STM32>STM32F103ZET6-可调参数定时器1互补PWM输出_第6张图片

你可能感兴趣的:(stm32,单片机,嵌入式硬件)