|
/* ADC1, ADC2, DMA, GPIO, TIM1 clocks enabling -----------------------------*/
/* Enable TIM1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* TIM1 Peripheral Configuration -------------------------------------------*/
/* TIM1 Registers reset */
TIM_DeInit(TIM1); //重置TIMx 外设寄存器为默认的复位值
TIM_TimeBaseStructInit(&TIM1_TimeBaseStructure); //使用缺省值填充TIM_TimeBaseInitStruct 的每个成员
/* Time Base configuration */
TIM1_TimeBaseStructure.TIM_Prescaler = PWM_PRSC; //配置TIMx 的预分频数
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;//指定要使用的TIMx 计数器模式
TIM1_TimeBaseStructure.TIM_Period = PWM_PERIOD; //配置周期值
TIM1_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV2; //配置时钟分割(TDTS = 2Tck_tim)
TIM1_TimeBaseStructure.TIM_RepetitionCounter = REP_RATE; //配置循环计数器的值
TIM_TimeBaseInit(TIM1, &TIM1_TimeBaseStructure); //根据TIM_TimeBaseInitStruct 中的特定参数初始化TIMx的时间基单元
TIM_OCStructInit(&TIM1_OCInitStructure); //使用缺省值填充TIM_ OCInitStruct 的每个成员
/* Channel 1, 2,3 and 4 Configuration in PWM mode */
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择计时器模式(TIM 脉冲宽度调制模式1)
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //选择TIM1 输出比较状态(TIM1 输出比较状态使能)
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //选择TIM1 补充输出比较状态(TIM1 输出N 比较状态使能)
TIM1_OCInitStructure.TIM_Pulse = 0x505; //dummy value //配置将被加载在捕获比较寄存器中的脉冲值
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //配置输出极性(TIM 输出比较极为低)
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //配置补充输出极性(输出比较N 极性低)
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //为空闲状态选择关闭状态(当MOE=0 重置TIM1 输出OC空闲状态复位)
TIM1_OCInitStructure.TIM_OCNIdleState = LOW_SIDE_POLARITY; //为空闲状态选择关闭状态(当MOE=0 重置TIM1 输出OC空闲状态复位)
TIM_OC1Init(TIM1, &TIM1_OCInitStructure); //根据TIM1_OCInitStruct 中指定的参数初始化TIM1 信道1
TIM1_OCInitStructure.TIM_Pulse = 0x505; //dummy value
TIM_OC2Init(TIM1, &TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = 0x505; //dummy value
TIM_OC3Init(TIM1, &TIM1_OCInitStructure);
/*Timer1 alternate function full remapping*/
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1,ENABLE);//(改变定时器引脚的映射)
GPIO_StructInit(&GPIO_InitStructure);//为GPIO_InitStruct 各成员赋默认值
/* GPIOE Configuration: Channel 1, 1N, 2, 2N, 3 and 3N Output */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出备用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //PWM输出IO驱动时钟
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Lock GPIOE Pin9 to Pin 13 */
GPIO_PinLockConfig(GPIOE, GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13);//锁定GPIO 引脚配置寄存器
GPIO_StructInit(&GPIO_InitStructure); //为GPIO_InitStruct 各成员赋默认值
/* GPIOE Configuration: BKIN pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Automatic Output enable, Break, dead time and lock configuration*/
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; //配置被用于运行模式的关闭状态选择(TIM1 OSSR 状态是使能的)
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; //选择被用于空闲状态的关闭状态(TIM1 OSSI 状态是使能的)
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; //配置锁级别参数(使用锁级别1)
TIM1_BDTRInitStructure.TIM_DeadTime = DEADTIME; //死区时间
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Enable; //使能或禁止TIM1 Break 输出(TIM1 间断输入被使能)
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low; //配置TIM1_Break 输入的高低(TIM1 间断输入引脚极性为低)
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;//使能或禁用自动输出特征(TIM1自动输出禁用)
TIM_BDTRConfig(TIM1, &TIM1_BDTRInitStructure); //配置:间断特征,空载时间,锁级别,OSSI,OSSR 状态和AOE(自动输出使能)
TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update); //选择TIM1 触发器输出模式
TIM_ClearITPendingBit(TIM1, TIM_IT_Break); //清除某个TIM 的中断挂起位
TIM_ITConfig(TIM1, TIM_IT_Break, ENABLE); //使能或禁用特定TIM1 中断
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE); //使能或禁用特定的TIM1 外围设备(使能)
/* Main Output Enable */
TIM_CtrlPWMOutputs(TIM1,ENABLE);
/* Configure one bit for preemption priority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置优先级组:优先级和字优先级
NVIC_StructInit(&NVIC_InitStructure); //给每一个NVIC_InitStruct 成员填上默认值
/* Enable the TIM1 BRK Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BRK_PRE_EMPTION_PRIORITY;//这个成员配置了在NVIC_IRQChannel 成员中指定的IRQ 通道的优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = BRK_SUB_PRIORITY; //这个成员配置了在NVIC_IRQChannel 成员中指定的IRQ 通道的子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //这个参数说明了在NVIC_IRQChannel 成员定义的IRQ 通道是使能还是无效的
NVIC_Init(&NVIC_InitStructure); //写入设置//通过NVIC_InitStruct 中的给定参数来初始化外围设备 |
|