对于普通定时器如TIM2、TIM3、TIM4、TIM5,如下配置:
//TIM3 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM3_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射 TIM3_CH2->PB5
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); //使能指定的TIM3中断,允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIM3
}
//定时器3中断服务程序
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
LED1 = !LED1;
}
}
int main(void)
{
u16 led0pwmval = 0;
u8 dir = 1;
u8 keyval;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
KEY_Init(); //初始化按键
TIM3_PWM_Init(1000 - 1, 72 - 1); //不分频。PWM频率=72000000/900=80Khz
while(1)
{
delay_ms(10);
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval > 300)dir = 0;
if(led0pwmval == 0)dir = 1;
TIM_SetCompare2(TIM3, led0pwmval);
keyval = KEY_Scan(0);
if(keyval == KEY1_PRES)
{
TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Disable);
}
else if(keyval == KEY0_PRES)
{
TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Enable);
}
}
}
通过TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Enable);启动PWM通道输出,TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Disable);停止PWM通道输出。
对于高级定时器如TIM1、TIM8则参考原子的步进电机驱动程序:
void TIM8_OPM_RCR_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //TIM8时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); //使能GPIOC外设时钟使能
//设置该引脚为复用输出功能,输出TIM8 CH2的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //TIM8_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ClearITPendingBit(TIM8,TIM_IT_Update);
TIM_UpdateRequestConfig(TIM8,TIM_UpdateSource_Regular); /********* 设置只有计数溢出作为更新中断 ********/
TIM_SelectOnePulseMode(TIM8,TIM_OPMode_Single);/******* 单脉冲模式 **********/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出2使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; /****** 比较输出2N失能 *******/
TIM_OCInitStructure.TIM_Pulse = arr>>1; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //CH2预装载使能
TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_ITConfig(TIM8, TIM_IT_Update ,ENABLE); //TIM8 使能或者失能指定的TIM中断
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; //TIM8中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级1级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ClearITPendingBit(TIM8, TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源
TIM_Cmd(TIM8, ENABLE); //使能TIM8
}
void TIM8_UP_IRQHandler(void)
{
if(TIM_GetITStatus(TIM8, TIM_FLAG_Update) != RESET) //更新中断
{
TIM_ClearITPendingBit(TIM8, TIM_FLAG_Update); //清除更新中断标志位
if(is_rcr_finish == 0) //重复计数器未设置完成
{
if(rcr_integer != 0) //整数部分脉冲还未发送完成
{
TIM8->RCR = RCR_VAL; //设置重复计数值
rcr_integer--;//减少RCR_VAL+1个脉冲
} else if(rcr_remainder != 0) //余数部分脉冲 不位0
{
TIM8->RCR = rcr_remainder - 1; //设置余数部分
rcr_remainder = 0; //清零
is_rcr_finish = 1; //重复计数器设置完成
} else goto out; //rcr_remainder=0,直接退出
TIM_GenerateEvent(TIM8, TIM_EventSource_Update); //产生一个更新事件 重新初始化计数器
TIM_CtrlPWMOutputs(TIM8, ENABLE); //MOE 主输出使能
TIM_Cmd(TIM8, ENABLE); //使能TIM8
if(motor_dir == CW) //如果方向为顺时针
current_pos += (TIM8->RCR + 1); //加上重复计数值
else //否则方向为逆时针
current_pos -= (TIM8->RCR + 1); //减去重复计数值
} else
{
out:
is_rcr_finish = 1; //重复计数器设置完成
TIM_CtrlPWMOutputs(TIM8, DISABLE); //MOE 主输出关闭
TIM_Cmd(TIM8, DISABLE); //关闭TIM8
printf("当前位置=%ldrn", current_pos); //打印输出
}
}
}
int main(void)
{
u8 i;
u8 keyval;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
usmart_dev.init(72); //初始化USMART
LED_Init(); //LED端口初始化
KEY_Init(); //初始化按键
Driver_Init(); //驱动器初始化
TIM8_OPM_RCR_Init(999, 72 - 1); //1MHz计数频率 单脉冲+重复计数模式
while(1)
{
keyval = KEY_Scan(0);
if(keyval == WKUP_PRES)
{
Locate_Abs(0, 500); //按下WKUP,回零点
} else if(keyval == KEY0_PRES)
{
Locate_Rle(200, 500, CW); //按下KEY0,以500Hz的频率 顺时针发200脉冲
} else if(keyval == KEY1_PRES)
{
Locate_Rle(40000, 500, CCW); //按下KEY1,以500Hz的频率 逆时针发400脉冲
}
delay_ms(10);
i++;
if(i == 50)
{
i = 0;
LED1 = !LED1;
}
}
}
对于普通定时器如TIM2、TIM3、TIM4、TIM5,如下配置:
//TIM3 PWM部分初始化
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM3_PWM_Init(u16 arr, u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射 TIM3_CH2->PB5
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE ); //使能指定的TIM3中断,允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
//初始化TIM3 Channel2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIM3在CCR2上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIM3
}
//定时器3中断服务程序
void TIM3_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
LED1 = !LED1;
}
}
int main(void)
{
u16 led0pwmval = 0;
u8 dir = 1;
u8 keyval;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LED_Init(); //LED端口初始化
KEY_Init(); //初始化按键
TIM3_PWM_Init(1000 - 1, 72 - 1); //不分频。PWM频率=72000000/900=80Khz
while(1)
{
delay_ms(10);
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval > 300)dir = 0;
if(led0pwmval == 0)dir = 1;
TIM_SetCompare2(TIM3, led0pwmval);
keyval = KEY_Scan(0);
if(keyval == KEY1_PRES)
{
TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Disable);
}
else if(keyval == KEY0_PRES)
{
TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Enable);
}
}
}
通过TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Enable);启动PWM通道输出,TIM_CCxCmd(TIM3, TIM_Channel_2, TIM_CCx_Disable);停止PWM通道输出。
对于高级定时器如TIM1、TIM8则参考原子的步进电机驱动程序:
void TIM8_OPM_RCR_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); //TIM8时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC , ENABLE); //使能GPIOC外设时钟使能
//设置该引脚为复用输出功能,输出TIM8 CH2的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //TIM8_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_ClearITPendingBit(TIM8,TIM_IT_Update);
TIM_UpdateRequestConfig(TIM8,TIM_UpdateSource_Regular); /********* 设置只有计数溢出作为更新中断 ********/
TIM_SelectOnePulseMode(TIM8,TIM_OPMode_Single);/******* 单脉冲模式 **********/
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出2使能
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; /****** 比较输出2N失能 *******/
TIM_OCInitStructure.TIM_Pulse = arr>>1; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM8, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable); //CH2预装载使能
TIM_ARRPreloadConfig(TIM8, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_ITConfig(TIM8, TIM_IT_Update ,ENABLE); //TIM8 使能或者失能指定的TIM中断
NVIC_InitStructure.NVIC_IRQChannel = TIM8_UP_IRQn; //TIM8中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级1级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级1级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ClearITPendingBit(TIM8, TIM_IT_Update); //清除TIMx的中断待处理位:TIM 中断源
TIM_Cmd(TIM8, ENABLE); //使能TIM8
}
void TIM8_UP_IRQHandler(void)
{
if(TIM_GetITStatus(TIM8, TIM_FLAG_Update) != RESET) //更新中断
{
TIM_ClearITPendingBit(TIM8, TIM_FLAG_Update); //清除更新中断标志位
if(is_rcr_finish == 0) //重复计数器未设置完成
{
if(rcr_integer != 0) //整数部分脉冲还未发送完成
{
TIM8->RCR = RCR_VAL; //设置重复计数值
rcr_integer--;//减少RCR_VAL+1个脉冲
} else if(rcr_remainder != 0) //余数部分脉冲 不位0
{
TIM8->RCR = rcr_remainder - 1; //设置余数部分
rcr_remainder = 0; //清零
is_rcr_finish = 1; //重复计数器设置完成
} else goto out; //rcr_remainder=0,直接退出
TIM_GenerateEvent(TIM8, TIM_EventSource_Update); //产生一个更新事件 重新初始化计数器
TIM_CtrlPWMOutputs(TIM8, ENABLE); //MOE 主输出使能
TIM_Cmd(TIM8, ENABLE); //使能TIM8
if(motor_dir == CW) //如果方向为顺时针
current_pos += (TIM8->RCR + 1); //加上重复计数值
else //否则方向为逆时针
current_pos -= (TIM8->RCR + 1); //减去重复计数值
} else
{
out:
is_rcr_finish = 1; //重复计数器设置完成
TIM_CtrlPWMOutputs(TIM8, DISABLE); //MOE 主输出关闭
TIM_Cmd(TIM8, DISABLE); //关闭TIM8
printf("当前位置=%ldrn", current_pos); //打印输出
}
}
}
int main(void)
{
u8 i;
u8 keyval;
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
usmart_dev.init(72); //初始化USMART
LED_Init(); //LED端口初始化
KEY_Init(); //初始化按键
Driver_Init(); //驱动器初始化
TIM8_OPM_RCR_Init(999, 72 - 1); //1MHz计数频率 单脉冲+重复计数模式
while(1)
{
keyval = KEY_Scan(0);
if(keyval == WKUP_PRES)
{
Locate_Abs(0, 500); //按下WKUP,回零点
} else if(keyval == KEY0_PRES)
{
Locate_Rle(200, 500, CW); //按下KEY0,以500Hz的频率 顺时针发200脉冲
} else if(keyval == KEY1_PRES)
{
Locate_Rle(40000, 500, CCW); //按下KEY1,以500Hz的频率 逆时针发400脉冲
}
delay_ms(10);
i++;
if(i == 50)
{
i = 0;
LED1 = !LED1;
}
}
}
举报