- UID
- 1029342
- 性别
- 男
|
#include "stm32f10x_lib.h"TIM_TimeBaseInitTypeDef TIM1_TimeBaseInitStructure; //根据 TIM_TimeBaseInitStruct 中指定的参数初始化 TIMx 的时间基数单位
TIM_OCInitTypeDef TIM1_OCInitStructure; //根据 TIM_OCInitStruct 中指定的参数初始化外设 TIMx
TIM_BDTRInitTypeDef TIM1_BDTRInitStructure; //TIM1_BDTRInitStruct:指向结构 TIM1_BDTRInitTypeDef的指针,包含了TIM1的BDTR寄存器的配置信息
u16 capture = 0;
u16 CCR1_Val = 0x800; //这里原来是设置TIM1通道输出占空比的。原书中作者设置的三个值为0x7FFF,0x3FFF,0x1FFF,彻底错了
u16 CCR2_Val = 0x400;
u16 CCR3_Val = 0x200;
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void); //复位和时钟配置;
void GPIO_Configuration(void);//通用IO端口配置;
void NVIC_Configuration(void);//中断向量嵌套配置;
int main(void)
{
#ifdef DEBUG
debug();
#endif
RCC_Configuration(); //配置系统时钟;
GPIO_Configuration(); //配置NVIC;
NVIC_Configuration(); //配置GPIO;
TIM_DeInit(TIM1); //将外设 TIM1 寄存器重设为缺省值;
TIM1_TimeBaseInitStructure.TIM_Prescaler = 0x0;//TIM1_Prescaler设置了用来作为 TIM1时钟频率除数的预分频值。它的取值必须在 0x0000 和0xFFFF 之间。
TIM1_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//TIM1_CounterMode 选择了计数器模式——向上计数;
TIM1_TimeBaseInitStructure.TIM_Period = 0x1000;//TIM1_Period设置了在下一个更新事件装入活动的自动重装载寄存器周期的值——0xFFFF;
//原书中值为0xFFFF,胡扯嘛,f=TIM1CLK/(TIM1_Period+1),如果TIM1的时钟频率为72MHz,则TIM1_Period应为4096左右,即0x1000。
TIM1_TimeBaseInitStructure.TIM_ClockDivision = 0x0;//TIM1_ClockDivision 设置了时钟分割;
TIM1_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseInitStructure);//根据 TIM1_TIM1BaseInitStruct 中指定的参数初始化 TIM1 的时间基数单位
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//TIM1_OCMode 选择定时器模式
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TIM1_OutputState选择输出比较状态
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //TIM1_OutputNState选择互补输出比较状态
TIM1_OCInitStructure.TIM_Pulse = CCR1_Val; //TIM1_Pulse设置了待装入捕获比较寄存器的脉冲值——占空比为50%。
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM1_OCPolarity输出极性高;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCPolarity_High; //TIM1互补输出极性为高
//原书中OCP和OCNP均设置为Low,看不出互补特性
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //TIM1_OCIdleState选择空闲状态下的非工作状态(MOE=0时设置TIM1输出比较空闲状态)
TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //MOE = 0时重置互补输出的输出比较空闲状态
TIM_OC1Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR2_Val; //设置通道2输出占空比为25%
TIM_OC2Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR3_Val; //设置通道3输出占空比为12.5%
TIM_OC3Init(TIM1,&TIM1_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //TIM_OSSRState 设置在运行模式下非工作状态选项
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //TIM_OSSIState 设置在运行模式下非工作状态选项
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //TIM_LOCKLevel 设置了锁电平参数——锁电平1
TIM1_BDTRInitStructure.TIM_DeadTime = 0x75; //死区时间1.625μs
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //TIM1 刹车输入使能
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; //TIM1 刹车输入管脚极性
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; //TIM1_AutomaticOutput 自动输出使能
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure); //设置刹车特性,死区时间,锁电平,OSSI,OSSR 状态和 AOE(自动输出使能)
TIM_Cmd(TIM1,ENABLE); //TIM1 使能
TIM_CtrlPWMOutputs(TIM1,ENABLE); //使能外设 TIM1 的主输出
while(1)
{
}
} |
|