本帖最后由 huangcunxiake 于 2016-3-25 11:31 编辑
#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)
{
}
}
void RCC_Configuration(void)
{
RCC_DeInit(); //将外设 RCC寄存器重设为缺省值
RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振(HSE)
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待 HSE 起振,该函数将等待直到 HSE 就绪,或者在超时的情况下退出
if(HSEStartUpStatus == SUCCESS) //如果 HSE 就绪,则开始设置
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); //设置 AHB 增强型高速总线的时钟(HCLK)
RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速 AHB 时钟(PCLK2)
RCC_PCLK1Config(RCC_HCLK_Div2); //设置高速 AHB 时钟(PCLK1)
FLASH_SetLatency(FLASH_Latency_2); //设置代码延时值——两个延时周期
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //使能预先取指令缓存
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //设置 PLL 时钟源及倍频系数,外部HSE默认8MHz,倍频系数为9,则PLL时钟为72MHz
RCC_PLLCmd(ENABLE); //使能PLL
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //如果检查指定的 RCC 标志位设置——PLL未就绪,则循环等待
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //设置PLL时钟为系统时钟
while(RCC_GetSYSCLKSource()!= 0x08) //如果用作系统的时钟源不是PLL,则循环等待。
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);//使能GPIOA GPIOB TIM1 外设时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11; //选中引脚8、9、10、11
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置选中引脚的最高输出速率为50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置选中管脚的工作状态为复用推挽输出。
GPIO_Init(GPIOA,&GPIO_InitStructure); //根据 GPIO_InitStruct中指定的参数初始化外设 GPIOA 寄存器
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; // GPIO_InitStruct中指定的参数未变,所以只改变需要改变的参数
GPIO_Init(GPIOB,&GPIO_InitStructure); //根据 GPIO_InitStruct中指定的参数初始化外设 GPIOB 寄存器
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置选中管脚的工作状态为输入浮空
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
//NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
#endif
}
#ifdef DEBUG
void assert_failed(u8 *file,u32 line) //返回出错时的行列信息
{
}
#endif
|