OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3071|回复: 1

定时器级联产生pwm不起作用

[复制链接]

2

主题

17

帖子

0

精华

新手上路

积分
47
金钱
47
注册时间
2015-3-18
在线时间
1 小时
发表于 2015-11-18 23:43:39 | 显示全部楼层 |阅读模式
5金钱
[mw_shl_code=c,true] http://www.openedv.com/posts/list/0/48514.htm?privmsg=33039&&sysid=4#316814 [/mw_shl_code] [mw_shl_code=c,true]我以前曾经在上面的链接里分享过一个永级定时器产生pwm控制电机的方法。[/mw_shl_code] [mw_shl_code=c,true]这次做硬件引脚改了一下,把原来帖子里的timer2 和timer3 倒着用,结果就不灵了,压根没有波形输出。执行发现根本没有产生定时器中断。[/mw_shl_code] [mw_shl_code=c,true]那位朋友眼尖的帮忙看看哪里改得不对?[/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code] [mw_shl_code=c,true]#define PWM_BASE 36000 //2MS CYCLE,SO That it can start up slower #define PWM_BASE_EXT (54000) //1.5 CYCLE long for zero pulse #define PIN_DIR GPIO_Pin_5 #define PIN_STEP GPIO_Pin_6 char hasInterrupt=0; char prevdir=3; //preview motor dir /*---------------------------------------------------------------------------------------------------------------------------------------*/ /* Just make parameter name shorter */ #define _GetInternalTrigger(S,M) \ (S == TIM1 ? (M == TIM5 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM3 ? TIM_TS_ITR2 M == TIM4 ? TIM_TS_ITR3 : (u16)-1)))): \ (S == TIM8 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM4 ? TIM_TS_ITR2 M == TIM5 ? TIM_TS_ITR3 : (u16)-1)))): \ (S == TIM2 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM8 ? TIM_TS_ITR1 : (M == TIM3 ? TIM_TS_ITR2 M == TIM4 ? TIM_TS_ITR3 : (u16)-1)))): \ (S == TIM3 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM5 ? TIM_TS_ITR2 M == TIM4 ? TIM_TS_ITR3 : (u16)-1)))): \ (S == TIM4 ? (M == TIM1 ? TIM_TS_ITR0 : (M == TIM2 ? TIM_TS_ITR1 : (M == TIM3 ? TIM_TS_ITR2 M == TIM8 ? TIM_TS_ITR3 : (u16)-1)))): \ (S == TIM5 ? (M == TIM2 ? TIM_TS_ITR0 : (M == TIM3 ? TIM_TS_ITR1 : (M == TIM4 ? TIM_TS_ITR2 M == TIM8 ? TIM_TS_ITR3 : (u16)-1)))): \ (u16)-1)))))) #define GetInternalTrigger(SlaveTimer,MasterTimer) _GetInternalTrigger(SlaveTimer,MasterTimer) #define compile_assert(exp) {extern char lxyppc_at_163_dot_com[(exp) ? 1:-1];} void pwm_init(void) { GPIO_InitTypeDef GPIO_InitStructure; /* TIM2 clock enable */ //RCC_PCLK1Config(RCC_HCLK_Div16); // USB SETTIN IT AS PCLK1CFG DIV2 IN hw_config.c RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); GPIO_InitStructure.GPIO_Pin = PIN_STEP; //TIM3 CH1 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50M时钟速度 GPIO_Init(GPIOA, &GPIO_InitStructure); /* TIM2 Main Output Enable */ //------------------------------------------------------------------- /* Setup timer3 channel 1, Timer3 is slave timer This timer is used to output waveforms */ { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); TIM_TimeBaseStructure.TIM_Prescaler = 7; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = PWM_BASE_EXT;//PWM_BASE*2; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); /* Timer2 Channel 3 Configuration in PWM2 mode, this is used for enable Recive clcok */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = PWM_BASE_EXT;//PWM_BASE*2; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM3,&TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM3, ENABLE); } //----------------------------------------------------------------------- /* Setup Timer2 channel 3, Timer2 is master timer This timer is used to control the waveform count of timer1 */ { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; TIM_DeInit(TIM2); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); TIM_TimeBaseStructure.TIM_Prescaler =7; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period =PWM_BASE ; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse =PWM_BASE; // 1 pulse shorter them 2 ms ;36 second has one more 2ms cycle TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC3Init(TIM2,&TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM2, ENABLE); TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single); { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn ; //选择定时器TIM3 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //选择抢先式优先级(与中断嵌套级别有关) NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //选择子优先级(同抢先式优先级的响应顺序) NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //选择使能中断源 NVIC_Init(&NVIC_InitStructure); TIM_ClearFlag(TIM2,TIM_FLAG_CC3); //中断标志位清零 TIM_ITConfig(TIM2, TIM_IT_CC3,ENABLE); //允许更新中断 } } //-------------------------------------------------------------------------- /* Create relationship between timer1 and timer3, timer3 is master, timer1 is slave timer1 is work under gate control mode, and controled by timer3 timer3's channel 4 is used as the control signal */ /* Enable timer's master/slave work mode */ TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable); TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); /* timer3's channel 4 is used as the control signal */ //TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_OC3Ref ); TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_Enable); /* Check the master/slave is valid or not */ compile_assert((u16)GetInternalTrigger(TIM3,TIM2) != (U16)-1); /* Config timer1's external clock */ TIM_ITRxExternalClockConfig(TIM3, GetInternalTrigger(TIM3,TIM2)); TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Gated); /* Enable the slave tiemr*/ //TIM_Cmd(TIM2,ENABLE); //----------------------------------------------------------------------------- TIM3->CR1|=1; //we should enable it after TIM2->CR1|=1; //--------------------------------------------------------------------------- //set ios set speed select and }[/mw_shl_code]

做一只高尚的猿,一只纯粹的猿,一只有益于世界的猿!
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

14

主题

1592

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
2622
金钱
2622
注册时间
2014-7-17
在线时间
350 小时
发表于 2015-11-19 00:54:23 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-20 07:55

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表