void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
// Set the Vector Table base location at 0x20000000 /
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else // VECT_TAB_FLASH /
// Set the Vector Table base location at 0x08000000 /
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
////////////////////////////////2017.1.2添加TIME1中断使能///////////////////////
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn; // TIME1中断使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
////////////////////////////////2017.1.2添加TIME1中断使能///////////////////////
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; // TIME3中断使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // UART2中断配置使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; // TIME2中断使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* TIM2 clock enable */
//PCLK1经过2倍频后作为TIM1的时钟源等于72MHz
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能TIM2时钟
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); //使能重映射后的GPIO口的时钟和重映射时钟
/*GPIOA Configuration: TIM1 channel 1 and 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 ; // PA8/9----PWM输出 //PA.8/9口设置为TIM1的CH1N/CH2N输出口
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //进行重映射
}
/********************************************************************************
* 函数名:TIM1_Mode_Config
* 描述 :配置TIM1输出的PWM信号的模式,如周期、极性、占空比
* 输入 :无
* 输出 :无
* 调用 :内部调用
********************************************************************************/
void TIM1_Mode_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 8000; //当定时器从0计数到999,即为1000次,为一个定时周期
TIM_TimeBaseStructure.TIM_Prescaler = 0; //设置预分频:不预分频,即为72MHz 原0
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV4 ; //设置时钟分频系数:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//循环计数次数(只是TIM1,8有)
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM_ARRPreloadConfig(TIM1, ENABLE); //启用ARR的影子寄存器(直到产生更新事件才更改设置) 2016.5.18添加
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //配置为PWM模式2
//选择定时器模式:TIM脉冲宽度调制模式2(极性控制)
// TIM3->CCMR1|=3<<5;//110:PWM模式1- 在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为有效电平,否则为无效电平;
// TIM3->CCMR1|=7<<12;//111:PWM模式2- 在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道2为无效电平,否则为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 120; //设置通道1的电平跳变值,输出另外一个占空比的PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; // 2016.5.18添加
/* PWM1 Mode configuration: Channel1 */
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/*PWM1 Mode configuration: Channel2*/ //设置通道2的电平跳变值,输出另外一个占空比的PWM
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道2
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
/*PWM1 Mode configuration: Channel3*/ //设置通道3的电平跳变值,输出另外一个占空比的PWM
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道3
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* Automatic Output enable, Break, dead time and lock configuration*/
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 = 5;
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE); // 使能TIM1重载寄存器ARR
//TIM1_OC通道输出PWM(一定要加)
TIM_CtrlPWMOutputs(TIM1, ENABLE); // 2016.5.18添加
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE); //使能TIMx外设
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>