专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
怎样让定时器输出一定数量的脉冲
2019-08-14 19:28
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
3453
15
1357
如题,如何让定时器输出一定数量的PWM呢,我要用它来做工作前监测。
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
15条回答
laoguren1122
2019-08-16 11:20
TIM2,TIM1进行主从配置
void TIM_PWM_Number_Init(u16 master_arr, u16 master_psc, u16 slave_arr, u16 slave_psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能定时器2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//使能定时器3的时钟
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8|GPIO_Pin_9; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO
//TIM2工作在单脉冲下
TIM_TimeBaseStructure.TIM_Period = master_arr;//TIM2per;//重装值
TIM_TimeBaseStructure.TIM_Prescaler = master_psc;//7200;//预分频值,每100us计数一次
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0;//TIM_CKD_DIV1;//没有时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);//设置TIM2在单脉冲模式,且是单一的脉冲,在下一个更新事件停止
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能定时器2的通道1预装载寄存器
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref);
TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE); // 中断源, TIM 触发中断源 使能
NVIC_InitStructure.NVIC_IRQChannel = TIM2_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寄存器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = 1;//比较捕获1的预装载值
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,DISABLE);//先不使能能TIM2
//TIM1工作在从模式的门控模式下的PWM输出模式
TIM_TimeBaseStructure.TIM_Period = slave_arr; //TIM3per;设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = slave_psc; //720;设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0; //TIM_CKD_DIV1;/设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; //TIM向上计数模式
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 10;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);//TIM3为门控模式
// TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);//使能TIM3的主从模式
TIM_SelectInputTrigger(TIM1,TIM_TS_ITR1);//内部触发,从TIM2触发
//初始化TIM3 Channel2 
WM模式
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_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OCNPolarity_Low; //输出极性:TIM输出比较极性高
// TIM_OCInitStructure.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);//使能TIM3
}
void TIM2_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
// LED1=!LED1;
TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
}
}
main()
{
//初始化TIM1,2使其固定数量脉冲输出
TIM_PWM_Number_Init(3999,719,999,71);
TIM_SetCompare1(TIM1,arr/2+400);
TIM_SetCompare2(TIM1,arr/2-400);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_SetCompare1(TIM2,10);
TIM_Cmd(TIM2, ENABLE);
}>
加载中...
查看其它15个回答
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
void TIM_PWM_Number_Init(u16 master_arr, u16 master_psc, u16 slave_arr, u16 slave_psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能定时器2的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//使能定时器3的时钟
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8|GPIO_Pin_9; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIO
//TIM2工作在单脉冲下
TIM_TimeBaseStructure.TIM_Period = master_arr;//TIM2per;//重装值
TIM_TimeBaseStructure.TIM_Prescaler = master_psc;//7200;//预分频值,每100us计数一次
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0;//TIM_CKD_DIV1;//没有时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);//设置TIM2在单脉冲模式,且是单一的脉冲,在下一个更新事件停止
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);//使能定时器2的通道1预装载寄存器
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref);
TIM_ITConfig( TIM2, TIM_IT_Update, ENABLE); // 中断源, TIM 触发中断源 使能
NVIC_InitStructure.NVIC_IRQChannel = TIM2_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寄存器
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;//在向上计数时,一旦TIMx_CNT<TIMx_CCR1时通道1为无效电平,否则为有效电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//OC1输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//有效电平为高
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = 1;//比较捕获1的预装载值
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_Cmd(TIM2,DISABLE);//先不使能能TIM2
//TIM1工作在从模式的门控模式下的PWM输出模式
TIM_TimeBaseStructure.TIM_Period = slave_arr; //TIM3per;设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = slave_psc; //720;设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//0; //TIM_CKD_DIV1;/设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned3; //TIM向上计数模式
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 10;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);//TIM3为门控模式
// TIM_SelectMasterSlaveMode(TIM1,TIM_MasterSlaveMode_Enable);//使能TIM3的主从模式
TIM_SelectInputTrigger(TIM1,TIM_TS_ITR1);//内部触发,从TIM2触发
//初始化TIM3 Channel2 WM模式
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_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM3 OC2
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//TIM_OCNPolarity_Low; //输出极性:TIM输出比较极性高
// TIM_OCInitStructure.TIM_Pulse = TIM3Compare1;//比较捕获1的预装载值
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1,ENABLE);//使能TIM3
}
void TIM2_IRQHandler(void) //TIM3中断
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源
{
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx的中断待处理位:TIM 中断源
// LED1=!LED1;
TIM_ForcedOC1Config(TIM1, TIM_ForcedAction_InActive);
TIM_ForcedOC2Config(TIM1, TIM_ForcedAction_InActive);
}
}
main()
{
//初始化TIM1,2使其固定数量脉冲输出
TIM_PWM_Number_Init(3999,719,999,71);
TIM_SetCompare1(TIM1,arr/2+400);
TIM_SetCompare2(TIM1,arr/2-400);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM_Cmd(TIM1, ENABLE);
TIM_SetCompare1(TIM2,10);
TIM_Cmd(TIM2, ENABLE);
}>
一周热门 更多>