stm32 pwm 输出一定数量脉冲.docx

上传人:牧羊曲112 文档编号:3062602 上传时间:2023-03-10 格式:DOCX 页数:5 大小:37.53KB
返回 下载 相关 举报
stm32 pwm 输出一定数量脉冲.docx_第1页
第1页 / 共5页
stm32 pwm 输出一定数量脉冲.docx_第2页
第2页 / 共5页
stm32 pwm 输出一定数量脉冲.docx_第3页
第3页 / 共5页
stm32 pwm 输出一定数量脉冲.docx_第4页
第4页 / 共5页
stm32 pwm 输出一定数量脉冲.docx_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《stm32 pwm 输出一定数量脉冲.docx》由会员分享,可在线阅读,更多相关《stm32 pwm 输出一定数量脉冲.docx(5页珍藏版)》请在三一办公上搜索。

1、stm32 pwm 输出一定数量脉冲利用定时器PWM模式输出一定数量脉冲 方式:定时器的同步,以一个定时器作为另一个定时器的与分频器方式。 具体代码实现: void Tim4_Slave_Init(void) TIM_DeInit(TIM4); 器,使之进入初始状态 /主要用于复位TIM4定时 /自动重装 TIM_TimeBaseStructure.TIM_Period=pulnum; 载寄存器的值 / void Tim1_PWM_Init(void) CCR1_Val = (timer1_period+1)/2; TIM_SelectInputTrigger(TIM4,TIM_TS_ITR0

2、); TIM_InternalClockConfig(TIM4); TIM4-SMCR = 0X07;/设定从模式控制寄存器 /TIM_UpdateDisableConfig(TIM4,ENABLE); TIM_ARRPreloadConfig(TIM4, ENABLE); TIM_ClearFlag(TIM4,TIM_FLAG_Update); TIM_TimeBaseStructure.TIM_Prescaler=0; /时钟预分频数 /采样分频 TIM_TimeBaseStructure.TIM_ClockDivision=0; TIM_TimeBaseStructure.TIM_Cou

3、nterMode=TIM_CounterMode_Up;/向上溢出 TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure); /清除溢出中断标志 TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); TIM_Cmd(TIM4,ENABLE);/是否开启时钟 CCR2_Val = (timer1_period+1)/2; /* - TIM1 Configuration: generate 4 PWM signals with 4 different duty cycles: TIM1CLK = 36 MHz, Prescaler =

4、 0x0, TIM1 counter clock = 36 MHz TIM1 ARR Register = 999 = TIM1 Frequency = TIM1 counter clock/(ARR + 1) TIM1 Frequency = 36 KHz. TIM1 Channel1 duty cycle = (TIM1_CCR1/ TIM1_ARR)* 100 = 50% TIM1 Channel2 duty cycle = (TIM1_CCR2/ TIM1_ARR)* 100 = 50% TIM1 Channel3 duty cycle = (TIM1_CCR3/ TIM1_ARR)*

5、 100 = 50% TIM1 Channel4 duty cycle = (TIM1_CCR4/ TIM1_ARR)* 100 = 50% - */ /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = timer1_period; TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up

6、; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM1-CR1 &= (u16)0x03FD);/ UDIS enable /* PWM1 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR1_Val; TIM_OCInitStructu

7、re.TIM_OCPolarity = TIM_OCPolarity_High; CCR3_Val = (timer1_period+1)/2; CCR4_Val = (timer1_period+1)/2; GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel2 */ TIM_OCInitStru

8、cture.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructu

9、re.TIM_Pulse = CCR3_Val; TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val; TIM_OC4Init(TIM1, &TIM_OCInitStructure);

10、 TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM1, ENABLE);/apre TIM_SelectOutputTrigger(TIM1,TIM_TRGOSource_Update); /* TIM1 enable counter */ TIM_Cmd(TIM1, ENABLE); /* Main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); void TIM4_IRQHandler(void) GPIO_InitTypeDef GPI

11、O_InitStructure; if (TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) TIM_ClearITPendingBit(TIM4,TIM_IT_Update); /进入中断后把通道GPIO关闭,达到控制脉冲目的 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_13 | GPIO_Pin_14; GPIO_Init(GPIOE, &GPIO_InitStructure); TIM_Cmd(TIM4,DISABLE);/关闭定时器4 GPIO_SetBits(GPIOE, GPIO_Pin_9); GPIO_SetBits(GPIOE, GPIO_Pin_11); GPIO_SetBits(GPIOE, GPIO_Pin_13); GPIO_SetBits(GPIOE, GPIO_Pin_14); GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 当想发送一定脉冲时,配置TIM4ARR寄存器,使能TIM4,即可发送一定数量脉冲

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号