小学生
最后登录1970-1-1
在线时间 小时
注册时间2019-5-10
|
[mw_shl_code=c,true]#include "bsp_PwmTim.h"
static void ADVANCE_TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// 输出比较通道 GPIO 初始化
RCC_APB2PeriphClockCmd(ADVANCE_TIM3_CH1_GPIO_CLK, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM3_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADVANCE_TIM3_CH1_PORT, &GPIO_InitStructure);
}
static void ADVANCE_TIM2_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
// 输出比较通道 GPIO 初始化
RCC_APB2PeriphClockCmd(ADVANCE_TIM2_CH1_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADVANCE_TIM2_CH1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADVANCE_TIM2_CH1_PORT, &GPIO_InitStructure);
}
static void ADVANCE_TIM3_Mode_Config(void)
{
// 开启定时器时钟,即内部时钟CK_INT=72M
ADVANCE_TIM3_APBxClock_FUN(ADVANCE_TIM3_CLK,ENABLE);
/*--------------------时基结构体初始化-------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// 自动重装载寄存器的值,累计TIM_Period+1个频率后产生一个更新或者中断
TIM_TimeBaseStructure.TIM_Period=ADVANCE_TIM3_PERIOD;
// 驱动CNT计数器的时钟 = Fck_int/(psc+1)
TIM_TimeBaseStructure.TIM_Prescaler= ADVANCE_TIM3_PSC;
// 时钟分频因子 ,配置死区时间时需要用到
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// 计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
// 初始化定时器
TIM_TimeBaseInit(ADVANCE_TIM3, &TIM_TimeBaseStructure);
/*--------------------输出比较结构体初始化-------------------*/
TIM_OCInitTypeDef TIM_OCInitStructure;
// 配置为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// 输出使能
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// 设置占空比大小
TIM_OCInitStructure.TIM_Pulse = ADVANCE_TIM3_PULSE;
// 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
// 输出通道空闲电平极性配置
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC2Init(ADVANCE_TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(ADVANCE_TIM3,TIM_OCPreload_Enable);
// 使能计数器
TIM_Cmd(ADVANCE_TIM3,ENABLE);
}
static void ADVANCE_TIM2_Mode_Config(void)
{
// 开启定时器时钟,即内部时钟CK_INT=72M
ADVANCE_TIM2_APBxClock_FUN(ADVANCE_TIM2_CLK,ENABLE);
/*--------------------时基结构体初始化-------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
// 自动重装载寄存器的值,累计TIM_Period+1个频率后产生一个更新或者中断
TIM_TimeBaseStructure.TIM_Period=ADVANCE_TIM2_PERIOD;
// 驱动CNT计数器的时钟 = Fck_int/(psc+1)
TIM_TimeBaseStructure.TIM_Prescaler= ADVANCE_TIM2_PSC;
// 时钟分频因子 ,配置死区时间时需要用到
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
// 计数器计数模式,设置为向上计数
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
// 重复计数器的值,没用到不用管
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
// 初始化定时器
TIM_TimeBaseInit(ADVANCE_TIM2, &TIM_TimeBaseStructure);
/*--------------------输出比较结构体初始化-------------------*/
TIM_OCInitTypeDef TIM_OCInitStructure;
// 配置为PWM模式1
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
// 输出使能
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
// 设置占空比大小
TIM_OCInitStructure.TIM_Pulse = ADVANCE_TIM2_PULSE;
// 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// 输出通道空闲电平极性配置
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OC2Init(ADVANCE_TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(ADVANCE_TIM2,TIM_OCPreload_Enable);
// 使能计数器
TIM_Cmd(ADVANCE_TIM2,ENABLE);
}
void ADVANCE_TIM3_Init(void)
{
ADVANCE_TIM3_GPIO_Config();
ADVANCE_TIM3_Mode_Config();
}
void ADVANCE_TIM2_Init(void)
{
ADVANCE_TIM2_GPIO_Config();
ADVANCE_TIM2_Mode_Config();
}
[/mw_shl_code]
[mw_shl_code=c,true]#ifndef __BSP_PWMTIM_H
#define __BSP_PWMTIM_H
#include "stm32f10x.h"
#define ADVANCE_TIM3 TIM3
#define ADVANCE_TIM3_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADVANCE_TIM3_CLK RCC_APB1Periph_TIM3
// PWM 信号的频率 F = TIM_CLK/{(ARR+1)*(PSC+1)}
#define ADVANCE_TIM3_PERIOD (800-1)
#define ADVANCE_TIM3_PSC (9-1)
#define ADVANCE_TIM3_PULSE 40
#define ADVANCE_TIM3_IRQ TIM3_UP_IRQn
#define ADVANCE_TIM3_IRQHandler TIM3_UP_IRQHandler
// TIM3 输出比较通道
#define ADVANCE_TIM3_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADVANCE_TIM3_CH1_PORT GPIOA
#define ADVANCE_TIM3_CH1_PIN GPIO_Pin_6
#define ADVANCE_TIM2 TIM2
#define ADVANCE_TIM2_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADVANCE_TIM2_CLK RCC_APB1Periph_TIM2
// PWM 信号的频率 F = TIM_CLK/{(ARR+1)*(PSC+1)}
#define ADVANCE_TIM2_PERIOD (80-1)
#define ADVANCE_TIM2_PSC (900-1)
#define ADVANCE_TIM2_PULSE 50
#define ADVANCE_TIM2_IRQ TIM2_UP_IRQn
#define ADVANCE_TIM2_IRQHandler TIM2_UP_IRQHandler
// TIM2 输出比较通道
#define ADVANCE_TIM2_CH1_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADVANCE_TIM2_CH1_PORT GPIOA
#define ADVANCE_TIM2_CH1_PIN GPIO_Pin_0
void ADVANCE_TIM2_Init(void);
void ADVANCE_TIM3_Init(void);
#endif /*__BSP_PWMTIM_H */
[/mw_shl_code]
以上是我编写的程序,找了很久没发现哪的问题导致没有输出
|
|