初中生
最后登录1970-1-1
在线时间 小时
注册时间2017-5-2
|
#ifndef PWM_H
#define PWM_H
#include "stm32f10x.h"
//用的是TIM8的PC6,PA7引脚来实现PWM互补输出
//PA6来实现刹车死驱输出
/*高级定时器8的参数宏定义*/
//PWM时钟配置
#define PWM_TIM TIM8
#define PWM_TIM_APBxClock_FUN RCC_APB2PeriphClockCmd
#define PWM_TIM_APBxCLK RCC_APB2Periph_TIM8
//PWM信号的频率配置 f=72M/(PSC+1)(ARR+1)
#define PWM_TIM_Period (200-1)
#define PWM_TIM_PSC (36-1)
#define PWM_TIM_PULSE 100
#define PWM_TIM_IRQ TIM8_UP_IRQn
#define PWM_TIM_IRQHandler TIM8_UP_IRQHandler
//TIM8输出比较通道
#define PWM_TIM_CH1_GPIO_CLK RCC_APB2Periph_GPIOC
#define PWM_TIM_CH1_GPIO_PORT GPIOC
#define PWM_TIM_CH1_GPIO_PIN GPIO_Pin_6
//TIM8互补输出比较通道
#define PWM_TIM_CH1N_GPIO_CLK RCC_APB2Periph_GPIOA
#define PWM_TIM_CH1N_GPIO_PORT GPIOA
#define PWM_TIM_CH1N_GPIO_PIN GPIO_Pin_7
//TIM8刹车死驱通道
#define PWM_TIM_CH1_BKIN_CLK RCC_APB2Periph_GPIOA
#define PWM_TIM_CH1_BKIN_PORT GPIOA
#define PWM_TIM_CH1_BKIN_PIN GPIO_Pin_6
void PWM_Init(void);
#endif /*PWM_H*/
C文件
#include "PWM.h"
static void PWM_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//输出比较通道初始化
RCC_APB2PeriphClockCmd(PWM_TIM_CH1_GPIO_CLK,ENABLE);
GPIO_InitStructure.GPIO_Pin = PWM_TIM_CH1_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_TIM_CH1_GPIO_PORT, &GPIO_InitStructure);
//输出互补比较通道初始化
RCC_APB2PeriphClockCmd(PWM_TIM_CH1N_GPIO_CLK,ENABLE);
GPIO_InitStructure.GPIO_Pin=PWM_TIM_CH1N_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;// 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_TIM_CH1N_GPIO_PORT, &GPIO_InitStructure);
//刹车死驱时间通道初始化
RCC_APB2PeriphClockCmd(PWM_TIM_CH1_BKIN_CLK,ENABLE);
GPIO_InitStructure.GPIO_Pin = PWM_TIM_CH1_BKIN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;// 推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PWM_TIM_CH1_BKIN_PORT, &GPIO_InitStructure);
GPIO_ResetBits(PWM_TIM_CH1_BKIN_PORT,PWM_TIM_CH1_BKIN_PIN);
}
static void PWM_GPIO_Mode_Config(void)
{//打开时钟
PWM_TIM_APBxClock_FUN(PWM_TIM_APBxCLK,ENABLE);
//时基结构体初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//配置ARR的值
TIM_TimeBaseStructure.TIM_Period=PWM_TIM_Period;
//配置PSC的值
TIM_TimeBaseStructure.TIM_Prescaler=PWM_TIM_PSC;
//计数模式
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
//时钟分频
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1 ;
//重复计数器
TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(PWM_TIM,&TIM_TimeBaseStructure);
//输出比较结构体初始化
TIM_OCInitTypeDef TIM_OCInitStructure;
//
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=PWM_TIM_PULSE ;
//
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_OCNIdleState_Reset;
//
TIM_OC1Init(PWM_TIM,&TIM_OCInitStructure);
//
TIM_OC1PreloadConfig(PWM_TIM,TIM_OCPreload_Enable);
//刹车结构体初始化
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
//
TIM_BDTRInitStructure.TIM_OSSRState=TIM_OSSRState_Enable;
//
TIM_BDTRInitStructure.TIM_OSSIState=TIM_OSSIState_Enable;
//
TIM_BDTRInitStructure.TIM_LOCKLevel=TIM_LOCKLevel_1;
//
TIM_BDTRInitStructure.TIM_DeadTime=11;
//
TIM_BDTRInitStructure.TIM_Break=TIM_Break_Enable;
//
TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_High ;
//
TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable ;
//
TIM_BDTRConfig(PWM_TIM,&TIM_BDTRInitStructure);
//
TIM_Cmd(PWM_TIM, ENABLE);
//
TIM_CtrlPWMOutputs(PWM_TIM,ENABLE);
}
void PWM_Init(void)
{
PWM_GPIO_Config();
PWM_GPIO_Mode_Config();
}
主函数
int main(void)
{
PWM_Init();
while(1)
{
}
}
|
|