问答

汇集网友智慧,解决技术难题

21ic问答首页 - TAG - PWM
  • 如何使用stm32控制 提供了pwm接口的电机

    [img]https://img.alicdn.com/imgextra/i4/2217847171116/O1CN01wolC621K7AxvkDGAH_!!2217847171116.jpg[/img]如上图是电机的接口,高电平都是5V左右,PWM与速度反馈信号可以直接连接到3.3V的stm32芯片引脚吗?

    STM32 接口 控制 电机 使用 PWM

    1230浏览量 1回复量 关注量
  • 使用三个定时器输出PWM卡死 赏100家园币

    请教各位大牛一个问题,卡住我几天了。我使用如下定时器配置PWM,如下示: PA6 -> PWM0 tim1_cha(4) PA7 -> PWM1 tim2_cha(5) PB0 -> PWM2 tim3_ch1b(2) 在应行如下初始化代码后,直接卡死了,请问是什么原因呢 void pwm_init() { Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //端口外设时钟使能 TimerPortCfg(PWM0); TimerPortCfg(PWM1); TimerPortCfg(PWM2); Timer1Cfg(100, PWM_DUTY); Bt_M23_EnPWM_Output(TIM1, TRUE, FALSE); //端口输出使能 Timer2Cfg(100, PWM_DUTY); Bt_M23_EnPWM_Output(TIM2, TRUE, FALSE); //端口输出使能 Timer3Cfg(100, PWM_DUTY); Tim3_M23_EnPWM_Output(TRUE, FALSE); //端口输出使能 Bt_M23_Run(TIM2); //运行。 Bt_M23_Run(TIM1); //运行。 Tim3_M23_Run(); //运行。 } 完整模块代码发下: #include "pwm.h" #include "timer3.h" #include "bt.h" #include "gpio.h" #include "motor.h" volatile char pwm_duty = 95; // PA6 -> PWM0 tim1_cha(4) // PA7 -> PWM1 tim2_cha(5) 或 tim3_ch0b(4) // PB0 -> PWM2 tim3_ch1b(2) typedef enum { PWM0 = 0u, PWM1, PWM2, }pwm_index_t; /******************************************************************************* * 中断服务函数 ******************************************************************************/ void Tim1_IRQHandler(void) { //Timer1 模式23 更新中断 if(TRUE == Bt_GetIntFlag(TIM1,BtUevIrq)){ Bt_M23_CCR_Set(TIM1, BtCCR0A, 100-pwm_duty); //设置通道A比较值 Bt_ClearIntFlag(TIM1,BtUevIrq); //清中断标志 } } void Tim2_IRQHandler(void) { //Timer2 模式23 更新中断 if(TRUE == Bt_GetIntFlag(TIM2,BtUevIrq)) { Bt_M23_CCR_Set(TIM2, BtCCR0A, 100-pwm_duty); //设置通道A比较值 Bt_ClearIntFlag(TIM2,BtUevIrq); //清中断标志 } } void Tim3_IRQHandler(void) { //Timer3 模式23 更新中断 if(TRUE == Tim3_GetIntFlag(Tim3UevIrq)){ Tim3_M23_CCR_Set(Tim3CCR1B, 100-pwm_duty); //设置CH1 通道B比较值 Tim3_ClearIntFlag(Tim3UevIrq); //清中断标志 } } //Timer1 配置 void Timer1Cfg(uint16_t u16Period, uint16_t u16CHxACompare) { uint16_t u16CntValue; uint8_t u8ValidPeriod; stc_bt_mode23_cfg_t stcTim1BaseCfg; stc_bt_m23_compare_cfg_t stcTim1PortCmpCfg; //结构体初始化清零 DDL_ZERO_STRUCT(stcTim1BaseCfg); DDL_ZERO_STRUCT(stcTim1PortCmpCfg); Sysctrl_SetPeripheralGate(SysctrlPeripheralBTim, TRUE); //Timer1外设时钟使能 stcTim1BaseCfg.enWorkMode = BtWorkMode2; //锯齿波模式 stcTim1BaseCfg.enCT = BtTimer; //定时器功能,计数时钟为内部PCLK stcTim1BaseCfg.enPRS = BtPCLKDiv1; //PCLK stcTim1BaseCfg.enCntDir = BtCntUp; //向上计数,在三角波模式时只读 stcTim1BaseCfg.enPWMTypeSel = BtIndependentPWM; //独立输出PWM stcTim1BaseCfg.enPWM2sSel = BtSinglePointCmp; //单点比较功能 stcTim1BaseCfg.bOneShot = FALSE; //循环计数 stcTim1BaseCfg.bURSSel = FALSE; //上下溢更新 Bt_Mode23_Init(TIM1, &stcTim1BaseCfg); //TIM3 的模式23功能初始化 Bt_M23_ARRSet(TIM1, u16Period, TRUE); //设置重载值,并使能缓存 Bt_M23_CCR_Set(TIM1, BtCCR0A, u16CHxACompare); //设置CH0比较值A stcTim1PortCmpCfg.enCH0ACmpCtrl = BtPWMMode2; //OCREFA输出控制OCMA:PWM模式2 stcTim1PortCmpCfg.enCH0APolarity = BtPortPositive; //正常输出 stcTim1PortCmpCfg.bCh0ACmpBufEn = TRUE; //A通道缓存控制 stcTim1PortCmpCfg.enCh0ACmpIntSel = BtCmpIntNone; //A通道比较中断控制:无 Bt_M23_PortOutput_Cfg(TIM1, &stcTim1PortCmpCfg); //比较输出端口配置 u8ValidPeriod = 0; //事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期 Bt_M23_SetValidPeriod(TIM1, u8ValidPeriod); //间隔周期设置 u16CntValue = 0; Bt_M23_Cnt16Set(TIM1, u16CntValue); //设置计数初值 Bt_ClearAllIntFlag(TIM1); //清中断标志 Bt_Mode23_EnableIrq(TIM1,BtUevIrq); //使能TIM1 UEV更新中断 EnableNvic(TIM1_IRQn, IrqLevel0, TRUE); //TIM1中断使能 } //Timer2 配置 void Timer2Cfg(uint16_t u16Period, uint16_t u16CHxACompare) { uint16_t u16CntValue; uint8_t u8ValidPeriod; stc_bt_mode23_cfg_t stcTim2BaseCfg; stc_bt_m23_compare_cfg_t stcTim2PortCmpCfg; //结构体初始化清零 DDL_ZERO_STRUCT(stcTim2BaseCfg); DDL_ZERO_STRUCT(stcTim2PortCmpCfg); Sysctrl_SetPeripheralGate(SysctrlPeripheralBTim, TRUE); //Timer1外设时钟使能 stcTim2BaseCfg.enWorkMode = BtWorkMode2; //锯齿波模式 stcTim2BaseCfg.enCT = BtTimer; //定时器功能,计数时钟为内部PCLK stcTim2BaseCfg.enPRS = BtPCLKDiv1; //PCLK stcTim2BaseCfg.enCntDir = BtCntUp; //向上计数,在三角波模式时只读 stcTim2BaseCfg.enPWMTypeSel = BtIndependentPWM; //独立输出PWM stcTim2BaseCfg.enPWM2sSel = BtSinglePointCmp; //单点比较功能 stcTim2BaseCfg.bOneShot = FALSE; //循环计数 stcTim2BaseCfg.bURSSel = FALSE; //上下溢更新 Bt_Mode23_Init(TIM2, &stcTim2BaseCfg); //TIM3 的模式23功能初始化 Bt_M23_ARRSet(TIM2, u16Period, TRUE); //设置重载值,并使能缓存 Bt_M23_CCR_Set(TIM2, BtCCR0A, u16CHxACompare); //设置CH0比较值B stcTim2PortCmpCfg.enCH0ACmpCtrl = BtPWMMode2; //OCREFA输出控制OCMA:PWM模式2 stcTim2PortCmpCfg.enCH0APolarity = BtPortPositive; //正常输出 stcTim2PortCmpCfg.bCh0ACmpBufEn = TRUE; //A通道缓存控制 stcTim2PortCmpCfg.enCh0ACmpIntSel = BtCmpIntNone; //A通道比较中断控制:无 Bt_M23_PortOutput_Cfg(TIM2, &stcTim2PortCmpCfg); //比较输出端口配置 u8ValidPeriod = 0; //事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期 Bt_M23_SetValidPeriod(TIM2, u8ValidPeriod); //间隔周期设置 u16CntValue = 0; Bt_M23_Cnt16Set(TIM2, u16CntValue); //设置计数初值 Bt_ClearAllIntFlag(TIM2); //清中断标志 Bt_Mode23_EnableIrq(TIM2,BtUevIrq); //使能TIM3 UEV更新中断 EnableNvic(TIM2_IRQn, IrqLevel0, TRUE); //TIM3中断使能 } //Timer3 配置 void Timer3Cfg(uint16_t u16Period, uint16_t u16CHxBCompare) { uint16_t u16CntValue; uint8_t u8ValidPeriod; stc_tim3_mode23_cfg_t stcTim3BaseCfg; stc_tim3_m23_compare_cfg_t stcTim3PortCmpCfg; //结构体初始化清零 DDL_ZERO_STRUCT(stcTim3BaseCfg); DDL_ZERO_STRUCT(stcTim3PortCmpCfg); Sysctrl_SetPeripheralGate(SysctrlPeripheralTim3, TRUE); //Timer3外设时钟使能 stcTim3BaseCfg.enWorkMode = Tim3WorkMode2; //锯齿波模式 stcTim3BaseCfg.enCT = Tim3Timer; //定时器功能,计数时钟为内部PCLK stcTim3BaseCfg.enPRS = Tim3PCLKDiv1; //PCLK stcTim3BaseCfg.enCntDir = Tim3CntUp; //向上计数,在三角波模式时只读 stcTim3BaseCfg.enPWMTypeSel = Tim3IndependentPWM; //独立输出PWM stcTim3BaseCfg.enPWM2sSel = Tim3SinglePointCmp; //单点比较功能 stcTim3BaseCfg.bOneShot = FALSE; //循环计数 stcTim3BaseCfg.bURSSel = FALSE; //上下溢更新 Tim3_Mode23_Init(&stcTim3BaseCfg); //TIM3 的模式23功能初始化 Tim3_M23_ARRSet(u16Period, TRUE); //设置重载值,并使能缓存 Tim3_M23_CCR_Set(Tim3CCR1B, u16CHxBCompare); //设置CH1比较值B stcTim3PortCmpCfg.enCHxBCmpCtrl = Tim3PWMMode2; //OCREFB输出控制OCMB:PWM模式2(PWM互补模式下也要设置,避免强制输出) stcTim3PortCmpCfg.enCHxBPolarity = Tim3PortPositive; //正常输出 stcTim3PortCmpCfg.bCHxBCmpBufEn = TRUE; //B通道缓存控制使能 stcTim3PortCmpCfg.enCHxBCmpIntSel = Tim3CmpIntNone; //B通道比较中断控制:无 // Tim3_M23_PortOutput_Cfg(Tim3CH0, &stcTim3PortCmpCfg); //比较输出端口配置 Tim3_M23_PortOutput_Cfg(Tim3CH1, &stcTim3PortCmpCfg); //比较输出端口配置 u8ValidPeriod = 0; //事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期 Tim3_M23_SetValidPeriod(u8ValidPeriod); //间隔周期设置 u16CntValue = 0; Tim3_M23_Cnt16Set(u16CntValue); //设置计数初值 Tim3_ClearAllIntFlag(); //清中断标志 Tim3_Mode23_EnableIrq(Tim3UevIrq); //使能TIM3 UEV更新中断 EnableNvic(TIM3_IRQn, IrqLevel0, TRUE); //TIM3中断使能 } void TimerPortCfg(pwm_index_t pwm_index) { stc_gpio_cfg_t stcTIMPort; DDL_ZERO_STRUCT(stcTIMPort); stcTIMPort.enDir = GpioDirOut; switch (pwm_index){ case PWM0: Gpio_Init(GpioPortA, GpioPin6, &stcTIMPort); Gpio_SetAfMode(GpioPortA,GpioPin6,GpioAf4); //PA6 -> PWM0 tim1_cha(4) break; case PWM1: Gpio_Init(GpioPortA, GpioPin7, &stcTIMPort); Gpio_SetAfMode(GpioPortA,GpioPin7,GpioAf5); //PA7 -> PWM1 tim2_cha(5) break; case PWM2: Gpio_Init(GpioPortB, GpioPin0, &stcTIMPort); Gpio_SetAfMode(GpioPortB,GpioPin0,GpioAf2); //PB0 -> PWM2 tim3_ch1b(2) break; default: break; } } void TimerPortDeiniCfg(pwm_index_t pwm_index) { stc_gpio_cfg_t stcTIMPort; DDL_ZERO_STRUCT(stcTIMPort); stcTIMPort.enDir = GpioDirOut; switch (pwm_index){ case PWM0: Gpio_Init(GpioPortA, GpioPin6, &stcTIMPort); Gpio_SetAfMode(GpioPortA,GpioPin6,GpioAf0); Gpio_ClrIO(GpioPortA, GpioPin6); break; case PWM1: Gpio_Init(GpioPortA, GpioPin7, &stcTIMPort); Gpio_SetAfMode(GpioPortA,GpioPin7,GpioAf0); //PA7 -> PWM1 tim2_cha(5) Gpio_ClrIO(GpioPortA, GpioPin7); break; case PWM2: Gpio_Init(GpioPortB, GpioPin0, &stcTIMPort); Gpio_SetAfMode(GpioPortB,GpioPin0,GpioAf0); //PB0 -> PWM2 tim3_ch1b(2) Gpio_ClrIO(GpioPortB, GpioPin0); break; default: break; } } // -----------pwm enable void pwm0_enable(uint8_t duty) { TimerPortCfg(PWM0); if (duty > 100){ duty = 100; } pwm_duty = duty; Bt_M23_Run(TIM1); } void pwm1_enable(uint8_t duty) { TimerPortCfg(PWM1); if (duty > 100){ duty = 100; } pwm_duty = duty; Bt_M23_Run(TIM2); } void pwm2_enable(uint8_t duty) { TimerPortCfg(PWM2); if (duty > 100){ duty = 100; } pwm_duty = duty; Tim3_M23_Run(); } // -----------pwm disable void pwm0_disable(void) { Bt_M23_Stop(TIM1); TimerPortDeiniCfg(PWM0); } void pwm1_disable(void) { Bt_M23_Stop(TIM2); TimerPortDeiniCfg(PWM1); } void pwm2_disable(void) { Tim3_M23_Stop(); TimerPortDeiniCfg(PWM2); }

    输出 使用 PWM 嵌入式 国产芯片 单片机

    5159浏览量 5回复量 关注量
  • 使用两个定时器输出PWM卡死

    请教各位大牛一个问题,卡住我几天了。我使用如下定时器配置PWM,如下示: PA6 -> PWM0 tim1_cha(4) PA7 -> PWM1 tim2_cha(5) PB0 -> PWM2 tim3_ch1b(2) 在应行如下初始化代码后,直接卡死了,请问是什么原因呢 [color=#d4d4d4][backcolor=rgb(30, 30, 30)][font=Consolas, "][color=#569cd6]void[/color] [color=#dcdcaa]pwm_init[/color]() { [color=#dcdcaa]Sysctrl_SetPeripheralGate[/color](SysctrlPeripheralGpio, [color=#569cd6]TRUE[/color]); [color=#6a9955]//端口外设时钟使能 [/color] [color=#dcdcaa]TimerPortCfg[/color](PWM0); [color=#dcdcaa]TimerPortCfg[/color](PWM1); [color=#dcdcaa]TimerPortCfg[/color](PWM2); [color=#dcdcaa]Timer1Cfg[/color]([color=#b5cea8]100[/color], PWM_DUTY); [color=#dcdcaa]Bt_M23_EnPWM_Output[/color](TIM1, [color=#569cd6]TRUE[/color], [color=#569cd6]FALSE[/color]); [color=#6a9955]//端口输出使能 [/color] [color=#dcdcaa]Timer2Cfg[/color]([color=#b5cea8]100[/color], PWM_DUTY); [color=#dcdcaa]Bt_M23_EnPWM_Output[/color](TIM2, [color=#569cd6]TRUE[/color], [color=#569cd6]FALSE[/color]); [color=#6a9955]//端口输出使能 [/color] [color=#dcdcaa]Timer3Cfg[/color]([color=#b5cea8]100[/color], PWM_DUTY); [color=#dcdcaa]Tim3_M23_EnPWM_Output[/color]([color=#569cd6]TRUE[/color], [color=#569cd6]FALSE[/color]); [color=#6a9955]//端口输出使能 [/color] [color=#dcdcaa]Bt_M23_Run[/color](TIM2); [color=#6a9955]//运行。 [/color] [color=#dcdcaa]Bt_M23_Run[/color](TIM1); [color=#6a9955]//运行。 [/color] [color=#dcdcaa]Tim3_M23_Run[/color](); [color=#6a9955]//运行。[/color] } [/font][/backcolor][/color] 完整模块代码发下: [color=#d4d4d4][backcolor=rgb(30, 30, 30)][font=Consolas, "][color=#c586c0]#include[/color][color=#569cd6] [/color][color=#ce9178]"pwm.h"[/color] [color=#c586c0]#include[/color][color=#569cd6] [/color][color=#ce9178]"timer3.h"[/color] [color=#c586c0]#include[/color][color=#569cd6] [/color][color=#ce9178]"bt.h"[/color] [color=#c586c0]#include[/color][color=#569cd6] [/color][color=#ce9178]"gpio.h"[/color] [color=#c586c0]#include[/color][color=#569cd6] [/color][color=#ce9178]"motor.h"[/color] [color=#569cd6]volatile[/color] [color=#569cd6]char[/color] pwm_duty = [color=#b5cea8]95[/color]; [color=#6a9955]// PA6 -> PWM0 tim1_cha(4)[/color] [color=#6a9955]// PA7 -> PWM1 tim2_cha(5) 或 tim3_ch0b(4) [/color] [color=#6a9955]// PB0 -> PWM2 tim3_ch1b(2)[/color] [color=#569cd6]typedef[/color] [color=#569cd6]enum[/color] { PWM0 = [color=#b5cea8]0u[/color], PWM1, PWM2, }[color=#4ec9b0]pwm_index_t[/color]; [color=#6a9955]/*******************************************************************************[/color] [color=#6a9955] * 中断服务函数[/color] [color=#6a9955] ******************************************************************************/[/color] [color=#569cd6]void[/color] [color=#dcdcaa]Tim1_IRQHandler[/color]([color=#569cd6]void[/color]) { [color=#6a9955]//Timer1 模式23 更新中断[/color] [color=#c586c0]if[/color]([color=#569cd6]TRUE[/color] == [color=#dcdcaa]Bt_GetIntFlag[/color](TIM1,BtUevIrq)){ [color=#dcdcaa]Bt_M23_CCR_Set[/color](TIM1, BtCCR0A, [color=#b5cea8]100[/color]-pwm_duty); [color=#6a9955]//设置通道A比较值[/color] [color=#dcdcaa]Bt_ClearIntFlag[/color](TIM1,BtUevIrq); [color=#6a9955]//清中断标志[/color] } } [color=#569cd6]void[/color] [color=#dcdcaa]Tim2_IRQHandler[/color]([color=#569cd6]void[/color]) { [color=#6a9955]//Timer2 模式23 更新中断[/color] [color=#c586c0]if[/color]([color=#569cd6]TRUE[/color] == [color=#dcdcaa]Bt_GetIntFlag[/color](TIM2,BtUevIrq)) { [color=#dcdcaa]Bt_M23_CCR_Set[/color](TIM2, BtCCR0A, [color=#b5cea8]100[/color]-pwm_duty); [color=#6a9955]//设置通道A比较值[/color] [color=#dcdcaa]Bt_ClearIntFlag[/color](TIM2,BtUevIrq); [color=#6a9955]//清中断标志[/color] } } [color=#569cd6]void[/color] [color=#dcdcaa]Tim3_IRQHandler[/color]([color=#569cd6]void[/color]) { [color=#6a9955]//Timer3 模式23 更新中断[/color] [color=#c586c0]if[/color]([color=#569cd6]TRUE[/color] == [color=#dcdcaa]Tim3_GetIntFlag[/color](Tim3UevIrq)){ [color=#dcdcaa]Tim3_M23_CCR_Set[/color](Tim3CCR1B, [color=#b5cea8]100[/color]-pwm_duty); [color=#6a9955]//设置CH1 通道B比较值 [/color] [color=#dcdcaa]Tim3_ClearIntFlag[/color](Tim3UevIrq); [color=#6a9955]//清中断标志[/color] } } [color=#6a9955]//Timer1 配置[/color] [color=#569cd6]void[/color] [color=#dcdcaa]Timer1Cfg[/color]([color=#4ec9b0]uint16_t[/color] u16Period, [color=#4ec9b0]uint16_t[/color] u16CHxACompare) { [color=#4ec9b0]uint16_t[/color] u16CntValue; [color=#4ec9b0]uint8_t[/color] u8ValidPeriod; [color=#4ec9b0]stc_bt_mode23_cfg_t[/color] stcTim1BaseCfg; [color=#4ec9b0]stc_bt_m23_compare_cfg_t[/color] stcTim1PortCmpCfg; [color=#6a9955]//结构体初始化清零[/color] [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTim1BaseCfg); [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTim1PortCmpCfg); [color=#dcdcaa]Sysctrl_SetPeripheralGate[/color](SysctrlPeripheralBTim, [color=#569cd6]TRUE[/color]); [color=#6a9955]//Timer1外设时钟使能[/color] stcTim1BaseCfg.[color=#9cdcfe]enWorkMode[/color] = BtWorkMode2; [color=#6a9955]//锯齿波模式[/color] stcTim1BaseCfg.[color=#9cdcfe]enCT[/color] = BtTimer; [color=#6a9955]//定时器功能,计数时钟为内部PCLK[/color] stcTim1BaseCfg.[color=#9cdcfe]enPRS[/color] = BtPCLKDiv1; [color=#6a9955]//PCLK[/color] stcTim1BaseCfg.[color=#9cdcfe]enCntDir[/color] = BtCntUp; [color=#6a9955]//向上计数,在三角波模式时只读[/color] stcTim1BaseCfg.[color=#9cdcfe]enPWMTypeSel[/color] = BtIndependentPWM; [color=#6a9955]//独立输出PWM[/color] stcTim1BaseCfg.[color=#9cdcfe]enPWM2sSel[/color] = BtSinglePointCmp; [color=#6a9955]//单点比较功能[/color] stcTim1BaseCfg.[color=#9cdcfe]bOneShot[/color] = [color=#569cd6]FALSE[/color]; [color=#6a9955]//循环计数[/color] stcTim1BaseCfg.[color=#9cdcfe]bURSSel[/color] = [color=#569cd6]FALSE[/color]; [color=#6a9955]//上下溢更新[/color] [color=#dcdcaa]Bt_Mode23_Init[/color](TIM1, &stcTim1BaseCfg); [color=#6a9955]//TIM3 的模式23功能初始化[/color] [color=#dcdcaa]Bt_M23_ARRSet[/color](TIM1, u16Period, [color=#569cd6]TRUE[/color]); [color=#6a9955]//设置重载值,并使能缓存[/color] [color=#dcdcaa]Bt_M23_CCR_Set[/color](TIM1, BtCCR0A, u16CHxACompare); [color=#6a9955]//设置CH0比较值A[/color] stcTim1PortCmpCfg.[color=#9cdcfe]enCH0ACmpCtrl[/color] = BtPWMMode2; [color=#6a9955]//OCREFA输出控制OCMA:PWM模式2[/color] stcTim1PortCmpCfg.[color=#9cdcfe]enCH0APolarity[/color] = BtPortPositive; [color=#6a9955]//正常输出[/color] stcTim1PortCmpCfg.[color=#9cdcfe]bCh0ACmpBufEn[/color] = [color=#569cd6]TRUE[/color]; [color=#6a9955]//A通道缓存控制[/color] stcTim1PortCmpCfg.[color=#9cdcfe]enCh0ACmpIntSel[/color] = BtCmpIntNone; [color=#6a9955]//A通道比较中断控制:无[/color] [color=#dcdcaa]Bt_M23_PortOutput_Cfg[/color](TIM1, &stcTim1PortCmpCfg); [color=#6a9955]//比较输出端口配置[/color] u8ValidPeriod = [color=#b5cea8]0[/color]; [color=#6a9955]//事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期[/color] [color=#dcdcaa]Bt_M23_SetValidPeriod[/color](TIM1, u8ValidPeriod); [color=#6a9955]//间隔周期设置[/color] u16CntValue = [color=#b5cea8]0[/color]; [color=#dcdcaa]Bt_M23_Cnt16Set[/color](TIM1, u16CntValue); [color=#6a9955]//设置计数初值[/color] [color=#dcdcaa]Bt_ClearAllIntFlag[/color](TIM1); [color=#6a9955]//清中断标志[/color] [color=#dcdcaa]Bt_Mode23_EnableIrq[/color](TIM1,BtUevIrq); [color=#6a9955]//使能TIM1 UEV更新中断[/color] [color=#dcdcaa]EnableNvic[/color](TIM1_IRQn, IrqLevel0, [color=#569cd6]TRUE[/color]); [color=#6a9955]//TIM1中断使能[/color] } [color=#6a9955]//Timer2 配置[/color] [color=#569cd6]void[/color] [color=#dcdcaa]Timer2Cfg[/color]([color=#4ec9b0]uint16_t[/color] u16Period, [color=#4ec9b0]uint16_t[/color] u16CHxACompare) { [color=#4ec9b0]uint16_t[/color] u16CntValue; [color=#4ec9b0]uint8_t[/color] u8ValidPeriod; [color=#4ec9b0]stc_bt_mode23_cfg_t[/color] stcTim2BaseCfg; [color=#4ec9b0]stc_bt_m23_compare_cfg_t[/color] stcTim2PortCmpCfg; [color=#6a9955]//结构体初始化清零[/color] [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTim2BaseCfg); [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTim2PortCmpCfg); [color=#dcdcaa]Sysctrl_SetPeripheralGate[/color](SysctrlPeripheralBTim, [color=#569cd6]TRUE[/color]); [color=#6a9955]//Timer1外设时钟使能[/color] stcTim2BaseCfg.[color=#9cdcfe]enWorkMode[/color] = BtWorkMode2; [color=#6a9955]//锯齿波模式[/color] stcTim2BaseCfg.[color=#9cdcfe]enCT[/color] = BtTimer; [color=#6a9955]//定时器功能,计数时钟为内部PCLK[/color] stcTim2BaseCfg.[color=#9cdcfe]enPRS[/color] = BtPCLKDiv1; [color=#6a9955]//PCLK[/color] stcTim2BaseCfg.[color=#9cdcfe]enCntDir[/color] = BtCntUp; [color=#6a9955]//向上计数,在三角波模式时只读[/color] stcTim2BaseCfg.[color=#9cdcfe]enPWMTypeSel[/color] = BtIndependentPWM; [color=#6a9955]//独立输出PWM[/color] stcTim2BaseCfg.[color=#9cdcfe]enPWM2sSel[/color] = BtSinglePointCmp; [color=#6a9955]//单点比较功能[/color] stcTim2BaseCfg.[color=#9cdcfe]bOneShot[/color] = [color=#569cd6]FALSE[/color]; [color=#6a9955]//循环计数[/color] stcTim2BaseCfg.[color=#9cdcfe]bURSSel[/color] = [color=#569cd6]FALSE[/color]; [color=#6a9955]//上下溢更新[/color] [color=#dcdcaa]Bt_Mode23_Init[/color](TIM2, &stcTim2BaseCfg); [color=#6a9955]//TIM3 的模式23功能初始化[/color] [color=#dcdcaa]Bt_M23_ARRSet[/color](TIM2, u16Period, [color=#569cd6]TRUE[/color]); [color=#6a9955]//设置重载值,并使能缓存[/color] [color=#dcdcaa]Bt_M23_CCR_Set[/color](TIM2, BtCCR0A, u16CHxACompare); [color=#6a9955]//设置CH0比较值B[/color] stcTim2PortCmpCfg.[color=#9cdcfe]enCH0ACmpCtrl[/color] = BtPWMMode2; [color=#6a9955]//OCREFA输出控制OCMA:PWM模式2[/color] stcTim2PortCmpCfg.[color=#9cdcfe]enCH0APolarity[/color] = BtPortPositive; [color=#6a9955]//正常输出[/color] stcTim2PortCmpCfg.[color=#9cdcfe]bCh0ACmpBufEn[/color] = [color=#569cd6]TRUE[/color]; [color=#6a9955]//A通道缓存控制[/color] stcTim2PortCmpCfg.[color=#9cdcfe]enCh0ACmpIntSel[/color] = BtCmpIntNone; [color=#6a9955]//A通道比较中断控制:无[/color] [color=#dcdcaa]Bt_M23_PortOutput_Cfg[/color](TIM2, &stcTim2PortCmpCfg); [color=#6a9955]//比较输出端口配置[/color] u8ValidPeriod = [color=#b5cea8]0[/color]; [color=#6a9955]//事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期[/color] [color=#dcdcaa]Bt_M23_SetValidPeriod[/color](TIM2, u8ValidPeriod); [color=#6a9955]//间隔周期设置[/color] u16CntValue = [color=#b5cea8]0[/color]; [color=#dcdcaa]Bt_M23_Cnt16Set[/color](TIM2, u16CntValue); [color=#6a9955]//设置计数初值[/color] [color=#dcdcaa]Bt_ClearAllIntFlag[/color](TIM2); [color=#6a9955]//清中断标志[/color] [color=#dcdcaa]Bt_Mode23_EnableIrq[/color](TIM2,BtUevIrq); [color=#6a9955]//使能TIM3 UEV更新中断[/color] [color=#dcdcaa]EnableNvic[/color](TIM2_IRQn, IrqLevel0, [color=#569cd6]TRUE[/color]); [color=#6a9955]//TIM3中断使能[/color] } [color=#6a9955]//Timer3 配置[/color] [color=#569cd6]void[/color] [color=#dcdcaa]Timer3Cfg[/color]([color=#4ec9b0]uint16_t[/color] u16Period, [color=#4ec9b0]uint16_t[/color] u16CHxBCompare) { [color=#4ec9b0]uint16_t[/color] u16CntValue; [color=#4ec9b0]uint8_t[/color] u8ValidPeriod; [color=#4ec9b0]stc_tim3_mode23_cfg_t[/color] stcTim3BaseCfg; [color=#4ec9b0]stc_tim3_m23_compare_cfg_t[/color] stcTim3PortCmpCfg; [color=#6a9955]//结构体初始化清零[/color] [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTim3BaseCfg); [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTim3PortCmpCfg); [color=#dcdcaa]Sysctrl_SetPeripheralGate[/color](SysctrlPeripheralTim3, [color=#569cd6]TRUE[/color]); [color=#6a9955]//Timer3外设时钟使能[/color] stcTim3BaseCfg.[color=#9cdcfe]enWorkMode[/color] = Tim3WorkMode2; [color=#6a9955]//锯齿波模式[/color] stcTim3BaseCfg.[color=#9cdcfe]enCT[/color] = Tim3Timer; [color=#6a9955]//定时器功能,计数时钟为内部PCLK[/color] stcTim3BaseCfg.[color=#9cdcfe]enPRS[/color] = Tim3PCLKDiv1; [color=#6a9955]//PCLK[/color] stcTim3BaseCfg.[color=#9cdcfe]enCntDir[/color] = Tim3CntUp; [color=#6a9955]//向上计数,在三角波模式时只读[/color] stcTim3BaseCfg.[color=#9cdcfe]enPWMTypeSel[/color] = Tim3IndependentPWM; [color=#6a9955]//独立输出PWM[/color] stcTim3BaseCfg.[color=#9cdcfe]enPWM2sSel[/color] = Tim3SinglePointCmp; [color=#6a9955]//单点比较功能[/color] stcTim3BaseCfg.[color=#9cdcfe]bOneShot[/color] = [color=#569cd6]FALSE[/color]; [color=#6a9955]//循环计数[/color] stcTim3BaseCfg.[color=#9cdcfe]bURSSel[/color] = [color=#569cd6]FALSE[/color]; [color=#6a9955]//上下溢更新[/color] [color=#dcdcaa]Tim3_Mode23_Init[/color](&stcTim3BaseCfg); [color=#6a9955]//TIM3 的模式23功能初始化[/color] [color=#dcdcaa]Tim3_M23_ARRSet[/color](u16Period, [color=#569cd6]TRUE[/color]); [color=#6a9955]//设置重载值,并使能缓存[/color] [color=#dcdcaa]Tim3_M23_CCR_Set[/color](Tim3CCR1B, u16CHxBCompare); [color=#6a9955]//设置CH1比较值B[/color] stcTim3PortCmpCfg.[color=#9cdcfe]enCHxBCmpCtrl[/color] = Tim3PWMMode2; [color=#6a9955]//OCREFB输出控制OCMB:PWM模式2(PWM互补模式下也要设置,避免强制输出)[/color] stcTim3PortCmpCfg.[color=#9cdcfe]enCHxBPolarity[/color] = Tim3PortPositive; [color=#6a9955]//正常输出[/color] stcTim3PortCmpCfg.[color=#9cdcfe]bCHxBCmpBufEn[/color] = [color=#569cd6]TRUE[/color]; [color=#6a9955]//B通道缓存控制使能[/color] stcTim3PortCmpCfg.[color=#9cdcfe]enCHxBCmpIntSel[/color] = Tim3CmpIntNone; [color=#6a9955]//B通道比较中断控制:无[/color] [color=#6a9955]// Tim3_M23_PortOutput_Cfg(Tim3CH0, &stcTim3PortCmpCfg); //比较输出端口配置[/color] [color=#dcdcaa]Tim3_M23_PortOutput_Cfg[/color](Tim3CH1, &stcTim3PortCmpCfg); [color=#6a9955]//比较输出端口配置[/color] u8ValidPeriod = [color=#b5cea8]0[/color]; [color=#6a9955]//事件更新周期设置,0表示锯齿波每个周期更新一次,每+1代表延迟1个周期[/color] [color=#dcdcaa]Tim3_M23_SetValidPeriod[/color](u8ValidPeriod); [color=#6a9955]//间隔周期设置[/color] u16CntValue = [color=#b5cea8]0[/color]; [color=#dcdcaa]Tim3_M23_Cnt16Set[/color](u16CntValue); [color=#6a9955]//设置计数初值[/color] [color=#dcdcaa]Tim3_ClearAllIntFlag[/color](); [color=#6a9955]//清中断标志[/color] [color=#dcdcaa]Tim3_Mode23_EnableIrq[/color](Tim3UevIrq); [color=#6a9955]//使能TIM3 UEV更新中断[/color] [color=#dcdcaa]EnableNvic[/color](TIM3_IRQn, IrqLevel0, [color=#569cd6]TRUE[/color]); [color=#6a9955]//TIM3中断使能[/color] } [color=#569cd6]void[/color] [color=#dcdcaa]TimerPortCfg[/color]([color=#4ec9b0]pwm_index_t[/color] pwm_index) { [color=#4ec9b0]stc_gpio_cfg_t[/color] stcTIMPort; [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTIMPort); stcTIMPort.[color=#9cdcfe]enDir[/color] = GpioDirOut; [color=#c586c0]switch[/color] (pwm_index){ [color=#c586c0]case[/color] PWM0: [color=#dcdcaa]Gpio_Init[/color](GpioPortA, GpioPin6, &stcTIMPort); [color=#dcdcaa]Gpio_SetAfMode[/color](GpioPortA,GpioPin6,GpioAf4); [color=#6a9955]//PA6 -> PWM0 tim1_cha(4)[/color] [color=#c586c0]break[/color]; [color=#c586c0]case[/color] PWM1: [color=#dcdcaa]Gpio_Init[/color](GpioPortA, GpioPin7, &stcTIMPort); [color=#dcdcaa]Gpio_SetAfMode[/color](GpioPortA,GpioPin7,GpioAf5); [color=#6a9955]//PA7 -> PWM1 tim2_cha(5) [/color] [color=#c586c0]break[/color]; [color=#c586c0]case[/color] PWM2: [color=#dcdcaa]Gpio_Init[/color](GpioPortB, GpioPin0, &stcTIMPort); [color=#dcdcaa]Gpio_SetAfMode[/color](GpioPortB,GpioPin0,GpioAf2); [color=#6a9955]//PB0 -> PWM2 tim3_ch1b(2)[/color] [color=#c586c0]break[/color]; [color=#c586c0]default[/color]: [color=#c586c0]break[/color]; } } [color=#569cd6]void[/color] [color=#dcdcaa]TimerPortDeiniCfg[/color]([color=#4ec9b0]pwm_index_t[/color] pwm_index) { [color=#4ec9b0]stc_gpio_cfg_t[/color] stcTIMPort; [color=#dcdcaa]DDL_ZERO_STRUCT[/color](stcTIMPort); stcTIMPort.[color=#9cdcfe]enDir[/color] = GpioDirOut; [color=#c586c0]switch[/color] (pwm_index){ [color=#c586c0]case[/color] PWM0: [color=#dcdcaa]Gpio_Init[/color](GpioPortA, GpioPin6, &stcTIMPort); [color=#dcdcaa]Gpio_SetAfMode[/color](GpioPortA,GpioPin6,GpioAf0); [color=#dcdcaa]Gpio_ClrIO[/color](GpioPortA, GpioPin6); [color=#c586c0]break[/color]; [color=#c586c0]case[/color] PWM1: [color=#dcdcaa]Gpio_Init[/color](GpioPortA, GpioPin7, &stcTIMPort); [color=#dcdcaa]Gpio_SetAfMode[/color](GpioPortA,GpioPin7,GpioAf0); [color=#6a9955]//PA7 -> PWM1 tim2_cha(5) [/color] [color=#dcdcaa]Gpio_ClrIO[/color](GpioPortA, GpioPin7); [color=#c586c0]break[/color]; [color=#c586c0]case[/color] PWM2: [color=#dcdcaa]Gpio_Init[/color](GpioPortB, GpioPin0, &stcTIMPort); [color=#dcdcaa]Gpio_SetAfMode[/color](GpioPortB,GpioPin0,GpioAf0); [color=#6a9955]//PB0 -> PWM2 tim3_ch1b(2)[/color] [color=#dcdcaa]Gpio_ClrIO[/color](GpioPortB, GpioPin0); [color=#c586c0]break[/color]; [color=#c586c0]default[/color]: [color=#c586c0]break[/color]; } } [color=#6a9955]// -----------pwm enable[/color] [color=#569cd6]void[/color] [color=#dcdcaa]pwm0_enable[/color]([color=#4ec9b0]uint8_t[/color] duty) { [color=#dcdcaa]TimerPortCfg[/color](PWM0); [color=#c586c0]if[/color] (duty > [color=#b5cea8]100[/color]){ duty = [color=#b5cea8]100[/color]; } pwm_duty = duty; [color=#dcdcaa]Bt_M23_Run[/color](TIM1); } [color=#569cd6]void[/color] [color=#dcdcaa]pwm1_enable[/color]([color=#4ec9b0]uint8_t[/color] duty) { [color=#dcdcaa]TimerPortCfg[/color](PWM1); [color=#c586c0]if[/color] (duty > [color=#b5cea8]100[/color]){ duty = [color=#b5cea8]100[/color]; } pwm_duty = duty; [color=#dcdcaa]Bt_M23_Run[/color](TIM2); } [color=#569cd6]void[/color] [color=#dcdcaa]pwm2_enable[/color]([color=#4ec9b0]uint8_t[/color] duty) { [color=#dcdcaa]TimerPortCfg[/color](PWM2); [color=#c586c0]if[/color] (duty > [color=#b5cea8]100[/color]){ duty = [color=#b5cea8]100[/color]; } pwm_duty = duty; [color=#dcdcaa]Tim3_M23_Run[/color](); } [color=#6a9955]// -----------pwm disable[/color] [color=#569cd6]void[/color] [color=#dcdcaa]pwm0_disable[/color]([color=#569cd6]void[/color]) { [color=#dcdcaa]Bt_M23_Stop[/color](TIM1); [color=#dcdcaa]TimerPortDeiniCfg[/color](PWM0); } [color=#569cd6]void[/color] [color=#dcdcaa]pwm1_disable[/color]([color=#569cd6]void[/color]) { [color=#dcdcaa]Bt_M23_Stop[/color](TIM2); [color=#dcdcaa]TimerPortDeiniCfg[/color](PWM1); } [color=#569cd6]void[/color] [color=#dcdcaa]pwm2_disable[/color]([color=#569cd6]void[/color]) { [color=#dcdcaa]Tim3_M23_Stop[/color](); [color=#dcdcaa]TimerPortDeiniCfg[/color](PWM2); } [/font][/backcolor][/color]

    定时器 输出 使用 PWM 国产芯片 单片机

    3390浏览量 8回复量 关注量
  • CW32 的高级时钟如何同时独立控制6路的占空比 sos

    刚接触这个芯片,这一个问题搞了两天的,真的不友好,连个寄存器手册也没!!!!CW32 的高级时钟如何同时控制6路pwm,并且能够独立控制6路的占空比?

    寄存器 PWM 占空比 CW32 时钟 控制

    927浏览量 0回复量 关注量
  • 辉芒FT60E121-SOP8 中断问题 sos

    PA5触发中断时理应只有[color=#80080]BLUE_PIN ([/color][color=#80080]PA0) [/color]输出高电平,但实际情况也[color=#800080]YELLOW_PIN (PA2)[/color]有输出,有没有大佬懂,帮帮小弟 #include "SYSCFG.h" [color=#800080]//***********************宏定义****************************[/color] #define unchar unsigned char unchar FCount; unchar ReadAPin; [color=#800080]#define BLUE_PIN PA0 [/color] #define WHITE_PIN PA1[color=#800080] [/color] [color=#800080]#define YELLOW_PIN PA2[/color][color=#800080] [/color] //定义输出引脚 [color=#800080]#define turn_signal PA5 [/color] #define width_signal PA4 volatile unsigned char is_running = 0; volatile float pwm_counter = 0; #define PWM_PERIOD 10 // PWM周期,根据定时器频率调整 volatile float pwm_blue_duty = 0;//占空比 volatile float pwm_white_duty = 0;//占空比 volatile unsigned int cycle_count=0; volatile unsigned int step=10; int step1=0; [color=#808080]/*-------------------------------------------------[/color] [color=#808080]* 函数名:interrupt ISR[/color] [color=#808080]* 功能: 中断处理[/color] [color=#808080]* 输入: 无[/color] [color=#808080]* 输出: 无[/color] [color=#808080] --------------------------------------------------*/[/color] void interrupt ISR(void) { [color=#800080] if (PA5) { [/color] [color=#800080] YELLOW_PIN = 1; [/color] [color=#800080] BLUE_PIN = 0; [/color] [color=#800080] WHITE_PIN = 0;[/color] [color=#800080] }[/color] if (T0IE && T0IF) {T0IF = 0;} if(TMR2IE && TMR2IF) { TMR2IF = 0; if (pwm_counter >= PWM_PERIOD) pwm_counter = 0; else{pwm_counter+=0.1;} if (pwm_blue_duty>PWM_PERIOD){pwm_blue_duty=0;} } } [color=#808080]/*-------------------------------------------------[/color] [color=#808080]* 函数名:POWER_INITIAL[/color] [color=#808080]* 功能: 上电系统初始化[/color] [color=#808080]* 输入: 无[/color] [color=#808080]* 输出: 无[/color] [color=#808080] --------------------------------------------------*/ [/color] void POWER_INITIAL (void) { OSCCON = 0B01110001; //IRCF=111=16MHz/2=8MHz,0.125us INTCON = 0; //暂禁止所有中断 PORTA = 0B00000000; TRISA = 0B00110000; //PA输入输出 0-输出 1-输入 //PA4-OUT PORTC = 0B00000000; TRISC = 0B00000000; //PC输入输出 0-输出 1-输入 WPUA = 0B00110000; //PA端口上拉控制 1-开上拉 0-关上拉 //开PA6上拉 OPTION = 0B00000000; //Bit3=0 TMR0 MODE,PS=000=TMR0 Rate 1:2 PSRCA = 0B11111111; //源电流设置最大 PSRCC = 0B11111111; PSINKA = 0B11111111; //灌电流设置最大 PSINKC = 0B11111111; MSCON = 0B00110000; //Bit5: PSRCAH4和PSRCA[4]共同决定源电流。00:4mA; 11: 33mA; 01、10:8mA //Bit4: PSRCAH3和PSRCA[3]共同决定源电流。00:4mA; 11: 33mA; 01、10:8mA //Bit3: UCFG1<1:0>为01时此位有意义。0:禁止LVR;1:打开LVR //Bit2: 快时钟测量慢周期的平均模式。0:关闭平均模式;1:打开平均模式 //Bit1: 0:关闭快时钟测量慢周期;1:打开快时钟测量慢周期 //Bit0: 当T2时钟不是选择指令时钟的时候 // 0:睡眠时停止工作:1: 睡眠时保持工作。 } [color=#808080]/*----------------------------------------------------[/color] [color=#808080]* 函数名称:TIMER0_INITIAL[/color] [color=#808080]* 功能:初始化设置定时器[/color] [color=#808080]* 相关寄存器:T0CS T0SE PSA [/color] [color=#808080]* 设置TMR0定时时长=(1/系统时钟频率)*指令周期*预分频值*255[/color] [color=#808080]* =(1/16000000)*2*256*255=8.192ms[/color] [color=#808080] ----------------------------------------------------*/[/color] void TIMER0_INITIAL (void) { OPTION = 0B00000000; //Bit5 T0CS Timer0时钟源选择 // 1-外部引脚电平变化T0CKI 0-内部时钟(FOSC/2) //Bit4 T0CKI引脚触发方式 1-下降沿 0-上升沿 //Bit3 PSA 预分频器分配位 0-Timer0 1-WDT //Bit[2:0] PS 8个预分频比 111 - 1:256 T0IF = 0; //清空T0软件中断 } void TIMER2_INITIAL (void) { T2CON0 = 0B00000001; //T2预分频1:4,后分频1:1 //Bit[6:3]: 定时器2输出后分频比 0000-1:1 //Bit2: 定时器2输出是能位 0-关闭 1-使能 //Bit[1:0]: 定时器2预分频比 01-1:4 T2CON1 = 0B00000000; //T2时钟来自系统时钟,PWM1连续模式 //Bit4: PWM单脉冲模式选择 0-连续 1-单脉冲 //Bit3: PWM蜂鸣器模式选择 0-PWM模式 1:蜂鸣器模式 //Bit[2:0]:Timer2时钟源选择 000-指令时钟 TMR2H = 0; //TMR2赋值 TMR2L = 0; PR2H = 0; //PR赋值 PR2L = 200; TMR2IF = 0; //清TMER2中断标志 TMR2IE = 1; //使能TMER2的中断(配置成timer定时器时不注释) TMR2ON = 1; //使能TMER2启动 PEIE = 1; //使能外设中断 GIE = 1; //使能全局中断 } [color=#800080]void B_UP (void){[/color] [color=#800080] switch(step1)[/color] [color=#800080] { case 0:{pwm_blue_duty+=0.001;step1=1;}[/color] [color=#800080] break;[/color] [color=#800080] case 1:{if (pwm_counter < pwm_blue_duty) BLUE_PIN = 1; [/color] [color=#800080] else BLUE_PIN = 0; step1=0;}[/color] [color=#800080] break;[/color] [color=#800080] pwm_counter=0;[/color] [color=#800080] }[/color] [color=#800080]}[/color] [color=#808080]/*-------------------------------------------------[/color] [color=#808080]* 函数名: main [/color] [color=#808080]* 功能: 主函数[/color] [color=#808080]* 输入: 无[/color] [color=#808080]* 输出: 无[/color] [color=#808080] --------------------------------------------------*/[/color] void main(void) { POWER_INITIAL(); //系统初始化 TIMER0_INITIAL(); TIMER2_INITIAL(); GIE = 1; //开中断 T0IE = 1; //开定时器/计数器0中断 TMR2IE = 1; while(1) { [color=#800080] B_UP ();[/color] } }

    PWM 11 TI FT 辉芒 单片机

    2515浏览量 0回复量 关注量
  • 请教一下STC12H1K输出PWM的问题 sos

    [i=s] 本帖最后由 chenczyaa 于 2024-7-3 08:49 编辑 [/i] 代码如下,我的P3.5脚 没PWM输出的,好奇怪。void pwm_init() { CCON = 0x00; // CL = 0; //PCA计数器 低8位 CH = 0; //PCA计数器 高8位 CMOD = 0x00; //选择时钟源为系统时钟源的1/12 CCAPM0 = 0x42; //0100 0010 允许比较器,允许PWM0引脚输出 //PCA_PWM0=0x00; //8位PWM PCA_PWM0=0x40; //7位PWM CCAP0H = CCAP0L = 0x00; //设置占空比为 0 PWMA_PS = 0xff; PWMB_PS = 0xf3;//P3.5输出PWM CR = 1; //启动 PCA 计数器阵列 } main() { uchar key1up=0,key2up=0,gear1=0,gear2=0; /* TMOD =0x00; //0模式16位自动重装载 TL0=0x66; //1ms初值 TH0=0xfc; TR0=1;// 启动定时器0 ET0=1;// 开启定时器0中断 EA=1;// 开启整个中断系统 */ P1M0=0x00;//设置P1为准双向口 P1M1=0x00; P3M0=0x00;//设置P3为准双向口 P3M1=0x00; //P1=0xff; //P35=0; pwm_init(); //PCA_PWM0=0x02;CCAP0H= CCAP0L=0xFF; //固定输出低 //PCA_PWM0=0x00;CCAP0H= CCAP0L=0x00; //固定输出高 //CCAP0L=0x20;//PWM占空比(100H-20H)/100H(87.5%)//8位PWM //CCAP0H=0x20; CR = 0; //CCAP0L=0xCC;//PWM占空比(100H-CCH)/100H(20%)//8位PWM //CCAP0H=0xCC; CCAP0L=0x20;//PWM占空比(80H-20H)/80H(75%)//7位PWM CCAP0H=0x20; CR = 1; P34=0; while(1) { delay(4000); P34 = ~P34; } }

    占空比 pc ap 输出 PWM stc12

    887浏览量 0回复量 关注量
  • 关于HCF32460的PWM问题

    请教一下各位,关于小华MCU,timerA的PWM输出,使用IO口为PC15,配置功能如下:但是目前从示博器来看输出波形一直保持不变,但是从打印来看,定时器是已经启动开始计数了。 void timer_init(uint16_t CompareValue) { /* MCU Peripheral registers write unprotected. */ LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU); stc_tmra_init_t stcTmraInit; stc_tmra_pwm_init_t stcPwmInit; /************************gpio config***************************/ stc_gpio_init_t stcGpioInit; (void)GPIO_StructInit(&stcGpioInit); stcGpioInit.u16PinDir = PIN_DIR_OUT; stcGpioInit.u16PinOutputType = PIN_OUT_TYPE_CMOS; //CMOS正常输出 (void)GPIO_Init(ENCODER_OUT_PORT, ENCODER_OUT_PIN, &stcGpioInit); /* 1. Enable TimerA peripheral clock. */ FCG_Fcg2PeriphClockCmd(FCG2_PERIPH_TMRA_4, ENABLE); /* 2. Set a default initialization value for stcTmraInit. */ (void)TMRA_StructInit(&stcTmraInit); /* 3. Modifies the initialization values depends on the application. */ stcTmraInit.sw_count.u16CountMode = TMRA_MD_SAWTOOTH; //锯齿波计数模式 stcTmraInit.sw_count.u16CountDir = TMRA_DIR_UP; //向上计数 stcTmraInit.u32PeriodValue = 10000U-1U; //自动重装载值 (void)TMRA_Init(CM_TMRA_4, &stcTmraInit); /* 4. Set the comparison reference value. */ (void)TMRA_PWM_StructInit(&stcPwmInit); stcPwmInit.u32CompareValue = 5000U-1; //20/40 50%占空比 GPIO_SetFunc(ENCODER_OUT_PORT, ENCODER_OUT_PIN, GPIO_FUNC_4); (void)TMRA_PWM_Init(CM_TMRA_4, TMRA_CH6, &stcPwmInit); TMRA_PWM_OutputCmd(CM_TMRA_4, TMRA_CH6, ENABLE); /*(void)TMRA_PWM_StructInit(&stcPwmInit); stcPwmInit.u32CompareValue = 20U-1; //20/40 50%占空比 GPIO_SetFunc(ENCODER_IN_PORT, ENCODER_IN_PIN, GPIO_FUNC_4); (void)TMRA_PWM_Init(CM_TMRA_4, TMRA_CH5, &stcPwmInit); TMRA_PWM_OutputCmd(CM_TMRA_4, TMRA_CH5, ENABLE);*/ /* MCU Peripheral registers write protected. */ LL_PERIPH_WP(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU); /* Starts TimerA. */ TMRA_Start(CM_TMRA_4); }

    2460 PWM HC timerA PWM输出 hc32f460

    1802浏览量 2回复量 关注量
  • STM32F407 同一个定时器的两个通道产生的PWM脉冲不对

    STM32F407VE ,使用定时器1同时产生两路PWM波 两路PWM产生的脉冲数不一样时,实际产生的脉冲不对,会少1个 实际上是不能使用这种方式做吗?

    定时器 stm32f407 PWM STM32 MCU

    3259浏览量 16回复量 关注量
  • MM32F014 144MHZ PWM

    请问MM32F014 可以实现144MHZ的PWM输出吗?听有些工程师说可以实现,但是看主频最高才72MHZ。

    工程师 PWM输出 PWM MM32 hz MUC

    3101浏览量 14回复量 关注量
  • MM32G0001A 2个Timer各出1PWM 设置占空比不同 实际结果确一样

    MM32G0001A 使用两个定时器 TIM14和TIM3 各出一路PWM 设置不一样的占空比 但是实际结果确一样 哪里设置有问题吗? void TIM3_Configure(void) { GPIO_InitTypeDef GPIO_InitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct; /* Compute the value to be set in ARR regiter to generate signal frequency at 100 Khz */ uint32_t TimerPeriod = (TIM_GetTIMxClock(TIM3) / 5000 ) - 1; /* Compute CCR1 value to generate a duty cycle at 80% for channel 1 */ uint32_t Channel1Pulse = ((uint32_t)800 * (TimerPeriod - 1)) / 1000; RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM3, ENABLE); TIM_TimeBaseStructInit(&TIM_TimeBaseStruct); TIM_TimeBaseStruct.TIM_Prescaler = 0; TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStruct.TIM_Period = TimerPeriod; TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_Div1; TIM_TimeBaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStruct); TIM_OCStructInit(&TIM_OCInitStruct); TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = 0; TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStruct.TIM_Pulse = Channel1Pulse; TIM_OC1Init(TIM3, &TIM_OCInitStruct); RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_3); /* TIM1_CH1 */ GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_11; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStruct); TIM_Cmd(TIM3, ENABLE); TIM_CtrlPWMOutputs(TIM3, ENABLE); } /*********************************************************************************************************************** * @brief * [url=home.php?mod=space&uid=536309]@NOTE[/url] none * @param none * @retval none *********************************************************************************************************************/ void TIM14_Configure(void) { GPIO_InitTypeDef GPIO_InitStruct; TIM_OCInitTypeDef TIM_OCInitStruct; TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct; /* Compute the value to be set in ARR regiter to generate signal frequency at 100 Khz */ uint32_t TimerPeriod = (TIM_GetTIMxClock(TIM14) / 5000 ) - 1; /* Compute CCR1 value to generate a duty cycle at 80% for channel 1 */ uint32_t Channel1Pulse = ((uint32_t)500 * (TimerPeriod - 1)) / 1000; RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM14, ENABLE); TIM_TimeBaseStructInit(&TIM_TimeBaseStruct); TIM_TimeBaseStruct.TIM_Prescaler = 0; TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStruct.TIM_Period = TimerPeriod; TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_Div1; TIM_TimeBaseStruct.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStruct); TIM_OCStructInit(&TIM_OCInitStruct); TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_Pulse = 0; TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStruct.TIM_Pulse = Channel1Pulse; TIM_OC1Init(TIM14, &TIM_OCInitStruct); RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_3); /* TIM1_CH1 */ GPIO_StructInit(&GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_High; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStruct); TIM_Cmd(TIM14, ENABLE); TIM_CtrlPWMOutputs(TIM14, ENABLE); }

    ST 1A MM32G0001 占空比 PWM timer

    1142浏览量 1回复量 关注量
  • 全桥逆变电路烧了两个mos管 赏100家园币

    [color=#333333][backcolor=rgb(242, 249, 253)][font=Tahoma, "][size=16px][font="][size=14px]关于全桥逆变电路烧mos管的问题想请教大佬,首先这是我的原理图,用STM32产生的PWM驱动。[/size][/font][/size][/font][/backcolor][/color] [img=740,462]https://bbs.21ic.com/data/attachment/forum/202403/08/102609kth1w2q7zmhv72dd.jpg.thumb.jpg[/img] [color=#444444][backcolor=rgb(242, 249, 253)][/backcolor][/color][img=740,358]https://bbs.21ic.com/data/attachment/forum/202403/08/102617kqf36q7157uz67qu.jpg.thumb.jpg[/img][color=#444444][backcolor=rgb(242, 249, 253)][/backcolor][/color] [color=#333333][backcolor=rgb(242, 249, 253)][font=Tahoma, "][size=16px][font="][size=14px]逆变电路在给定12伏的直流电时,输出一般,但是好歹是正常的。[/size][/font][/size][/font][/backcolor][/color][color=#444444][backcolor=rgb(242, 249, 253)][font=Tahoma, "][size=16px] [/size][/font][/backcolor][/color][img=740,416]https://bbs.21ic.com/data/attachment/forum/202403/08/102458s840xlz8l0hlx4rn.jpg.thumb.jpg[/img][color=#444444][backcolor=rgb(242, 249, 253)][/backcolor][/color][font=Helvetica Neue, Helvetica, Arial, PingFang SC, Hiragino Sans GB, WenQuanYi Micro Hei, Microsoft Yahei, sans-serif][color=rgb(68, 68, 68)][backcolor=rgb(242, 249, 253)][size=16px][color=#333333][size=14px] [/size][/color][/size][/backcolor][/color][/font][color=#333333][backcolor=rgb(242, 249, 253)][font=Tahoma, "][size=16px][font="][size=14px]但是一旦加到四五十伏,左边两个mos就烧坏了,右边两个反而没问题,从原理图上看,左边两个mos管分别是两路的高端和低端啊,请问这可能是什么原因造成的呢?难道是哪里的元件接反了(我检查了好几遍,确认过IRF840的手册,感觉没啥问题啊),还是说我pwm波死区设置的太小了?[/size][/font][/size][/font][/backcolor][/color] [img=740,416]https://bbs.21ic.com/data/attachment/forum/202403/08/102804qfvzngcsq5z5fzjt.jpg.thumb.jpg[/img] [color=#444444][backcolor=rgb(242, 249, 253)][/backcolor][/color][img=740,554]https://bbs.21ic.com/data/attachment/forum/202403/08/102809en1emhrrpjny2p22.jpg.thumb.jpg[/img]

    原理图 PWM MOS管 MOSFET 全桥逆变电路

    8642浏览量 4回复量 关注量
  • FPGA程序,不能正确输出,各位帮忙看下

    我编写了两个文件,配置了输出口,,LASER_PWM 始终输出不了波形,请问下,这个是什么原因呀?modesiem仿真是有波形输出的//////////////-------PWM.V 文件 `timescale 1 ns / 1 ps module pwm( input rsn, input clk, input [15:0]pwm_counter, input [15:0]pwm_duty, output pwm_out ); //reg[15:0]pwm_counter; //reg [15:0]pwm_duty; reg [15:0]counter=16'd0; reg pwm_out_r; always@(posedge clk or negedge rsn) //begin if(!rsn) begin counter <= 16'd0; end else begin if(counter >= pwm_counter)counter <= 16'd0; else counter <= counter + 1'd1; end // end always@(posedge clk or negedge rsn) if(!rsn) begin pwm_out_r <= 1'd0; end else if(counter>=pwm_duty) pwm_out_r<=1'b0; else pwm_out_r<=1'b1; //wire pwm_out; assign pwm_out = pwm_out_r;//((counter > pwm_duty) ? 1'd0 : 1'd1); endmodule ////// top_indepen.v 文件 //-------------------------Timescale----------------------------// `timescale 1 ns / 1 ps //--------------------FSMC_SIG---------------------// module FSMC_INDEP( FPGA_CLK, //鏉堟挸鍙嗛弶鑳祰閺呰埖灏烥PGA_CLK,25M FPGA_LEDR, FPGA_LEDG, FPGA_LEDB, WR, //FSMC閸愭瑤淇婇崣 RD, //FSMC鐠囪淇婇崣 CS0, //FSMC閻楀洭鈧 A, //FSMC閸︽澘**冮幀鑽ゅ殠 DB, //FSMC閺佺増宓侀幀鑽ゅ殠 NADV, //FSMC閻ㄥ嚞ADV //LASER PWM LASER_PWM, //interpn_exti Interp_ex, testclk, testclk_100, testclk_50, FPGA_KEY ); input FPGA_KEY; input FPGA_CLK,NADV; input WR,RD,CS0; inout [15:0]DB; input [24:16]A; output FPGA_LEDB,FPGA_LEDG,FPGA_LEDR; assign FPGA_LEDR = 1'd1; assign FPGA_LEDG = 1'd0; assign FPGA_LEDB = 1'd1; output LASER_PWM; output Interp_ex,testclk,testclk_100,testclk_50; //-------------------------MY_PLL-------------------------------// wire PLL_100M; wire PLL_8M; wire PLL_50M; wire PLL_12_5M; reg LaserCtrl1; reg [15:0] dbPower1;//閸旂喓宸 閸楃姷鈹栧В reg [15:0]dbQFreq1;//妫版垹宸 reg [15:0]counter; initial begin dbQFreq1 <= 16'd106; dbPower1 <= 16'd53; LaserCtrl1 <= 1'd1; end MY_PLL U1( .inclk0(FPGA_CLK), .c0(PLL_100M), .c1(PLL_50M), .c2(PLL_8M), .c3(PLL_12_5M) );// //------------------------RST_Ctrl-----------------------------// wire RST_n; RST_Ctrl U2( .FPGA_CLK(FPGA_CLK), .RST_n(RST_n) ); //娓氬瀵睷ST_Ctrl濡€虫健,鏉堟挸鍤崗銊ョ湰婢跺秳缍呮穱鈥冲娇RST_n pwm U5( .rsn(RST_n), .clk(PLL_8M), .pwm_counter(dbQFreq1), .pwm_duty(dbPower1),//閸旂喓宸 .pwm_out(LASER_PWM) ); endmodule

    FPGA PWM counter TE npu

    948浏览量 2回复量 关注量
  • HC32F030如何实现不用中断更新CCR调节互补PWM占空比?

    因为要使用互补PWM控制H桥来驱动电机,参考HC32F030驱动下的产生互补PWM的例子“HC32F030_DDL_Rev1.2.0\example\timer3\timer3_m23_tpcmp_pwm”,可以产生控制互补PWM占空比控制电机转动。但问题是这种方式需要在定时器溢出中断频繁更新CCR的值,频繁的中断服务程序频繁占用系统处理时间,而我只需要输出互补PWM而已。 尝试在参考例子中关掉中断,使能PWM自动输出。 //stcTim3BaseCfg.pfnTim3Cb = Tim3_LRMotorPWM_Int; //中断函数入口 //Tim3_ClearAllIntFlag(); //清中断标志 //Tim3_Mode23_EnableIrq(Tim3UevIrq); //使能TIM3 UEV更新中断 //EnableNvic(TIM3_IRQn, IrqLevel0, TRUE); //TIM3中断使能 Tim3_M23_EnPWM_Output(TRUE, TRUE); //端口输出使能 应用程序中改变CCR的值,试图改变PWM占空比: Init_LRMotor_PWM(); Tim3_M23_CCR_Set(Tim3CCR0A, 250); Tim3_M23_CCR_Set(Tim3CCR0B, 750); Tim3_M23_CCR_Set(Tim3CCR1A, 250); Tim3_M23_CCR_Set(Tim3CCR1B, 750); Tim3_M23_Run(); Delay_MS(2000); Tim3_M23_Stop(); Delay_MS(2000); Tim3_M23_CCR_Set(Tim3CCR0A, 750); Tim3_M23_CCR_Set(Tim3CCR0B, 250); Tim3_M23_CCR_Set(Tim3CCR1A, 750); Tim3_M23_CCR_Set(Tim3CCR1B, 250); Tim3_M23_Run(); 下载视频

    HC PWM TI se ev

    1576浏览量 2回复量 关注量
  • 为什么A会有half,B没有啊?

    为什么装载PWM的时候,会写成EPwm1Regs.CMPA.half.CMPA = int,和 EPwm4Regs.CMPB = int。为什么A会有half,B没有啊?

    PWM

    1015浏览量 12回复量 关注量
  • 散热风扇,3.3v io接到fg和pwm吗

    请教这个风扇调速pwm管脚可以直接接到单片机3.3v IO口吗,fg反馈管脚上拉3k电阻后可以直接接到3.3v IO口吗

    PWM IO 管脚 vi 单片机

    1211浏览量 2回复量 关注量
1234下一页