本帖最后由 woai32lala 于 2024-10-23 19:34 编辑
HC32F460KETA PWM 一堆问题需要技术支持
使用TIMA_4_PWM3和PWM4管脚 对应PB8和PB9,两个通道之间有影响,一个通道输出PWM,另一个通道也输出一样的波形,被折磨很久,想问一下没有没有技术支持//PWM 初始化
/* TIMERA unit and clock definition */
#define TIMERA_TUBE_HEAT_UNIT (M4_TMRA4) //管道加热
#define TIMERA_AIR_CHAMBER_UNIT (M4_TMRA4) //气室加热
#define TIMERA_TUBE_HEAT_CLOCK (PWC_FCG2_PERIPH_TIMA4)
#define TIMERA_AIR_CHAMBER_CLOCK (PWC_FCG2_PERIPH_TIMA4)
/* TIMERA_6 channel 6 Port/Pin definition */
#define TIMERA_UNIT_TUBE_HEAT_CH (TimeraCh4)
#define TIMERA_UNIT_TUBE_HEAT_CH_PORT (PortB)
#define TIMERA_UNIT_TUBE_HEAT_CH_PIN (Pin09)
#define TIMERA_UNIT_TUBE_HEAT_CH_FUNC (Func_Tima0)
/* TIMERA_5 channel 5 Port/Pin definition */
#define TIMERA_UNIT_AIR_CHAMBER_CH (TimeraCh3)
#define TIMERA_UNIT_AIR_CHAMBER_CH_PORT (PortB)
#define TIMERA_UNIT_AIR_CHAMBER_CH_PIN (Pin08)
#define TIMERA_UNIT_AIR_CHAMBER_CH_FUNC (Func_Tima0)
static void heat_pwm_init(void)
{
stc_timera_base_init_t stcTimeraInit;
stc_timera_compare_init_t stcTimerCompareInit;
stc_irq_regi_conf_t stcIrqRegiConf;
stc_timera_hw_startup_config_t stcTimeraHwConfig;
stc_port_init_t stcPortInit;
/* configuration structure initialization */
MEM_ZERO_STRUCT(stcTimeraInit);
MEM_ZERO_STRUCT(stcIrqRegiConf);
MEM_ZERO_STRUCT(stcTimerCompareInit);
MEM_ZERO_STRUCT(stcTimeraHwConfig);
MEM_ZERO_STRUCT(stcPortInit);
/* Configuration peripheral clock */
PWC_Fcg2PeriphClockCmd(TIMERA_TUBE_HEAT_CLOCK, Enable);
PWC_Fcg2PeriphClockCmd(TIMERA_AIR_CHAMBER_CLOCK, Enable);
/* Configuration TIMERA compare pin */
PORT_SetFunc(TIMERA_UNIT_TUBE_HEAT_CH_PORT, TIMERA_UNIT_TUBE_HEAT_CH_PIN, TIMERA_UNIT_TUBE_HEAT_CH_FUNC, Disable);
PORT_SetFunc(TIMERA_UNIT_AIR_CHAMBER_CH_PORT, TIMERA_UNIT_AIR_CHAMBER_CH_PIN, TIMERA_UNIT_AIR_CHAMBER_CH_FUNC, Disable);
/* Configuration timera unit 1 base structure */
stcTimeraInit.enClkDiv = TimeraPclkDiv1;
stcTimeraInit.enCntMode = TimeraCountModeSawtoothWave;//TimeraCountModeTriangularWave;
stcTimeraInit.enCntDir = TimeraCountDirUp;
stcTimeraInit.enSyncStartupEn = Disable;
stcTimeraInit.u16PeriodVal = 5000; //20Khz
TIMERA_BaseInit(TIMERA_TUBE_HEAT_UNIT, &stcTimeraInit);
TIMERA_BaseInit(TIMERA_AIR_CHAMBER_UNIT, &stcTimeraInit);
/* Configuration timera unit 1 compare structure */
// stcTimerCompareInit.u16CompareVal = 5000; //占空比70% //stcTimeraInit.u16PeriodVal * 5u / 10u; //占空比70%
stcTimerCompareInit.enStartCountOutput = TimeraCountStartOutputHigh;
stcTimerCompareInit.enStopCountOutput = TimeraCountStopOutputLow;
stcTimerCompareInit.enCompareMatchOutput = TimeraCompareMatchOutputLow;//TimeraCompareMatchOutputReverse;
stcTimerCompareInit.enPeriodMatchOutput = TimeraPeriodMatchOutputHigh;//TimeraPeriodMatchOutputKeep;
stcTimerCompareInit.enSpecifyOutput = TimeraSpecifyOutputInvalid; //该寄存器可用于实现PWM输出占空比0%或100%
stcTimerCompareInit.enCacheEn = Enable;
stcTimerCompareInit.enTriangularTroughTransEn = Enable;
stcTimerCompareInit.enTriangularCrestTransEn = Disable;
stcTimerCompareInit.u16CompareCacheVal = stcTimerCompareInit.u16CompareVal;
/* Configure Channel 1 */
TIMERA_CompareInit(TIMERA_TUBE_HEAT_UNIT, TIMERA_UNIT_TUBE_HEAT_CH, &stcTimerCompareInit);
TIMERA_CompareInit(TIMERA_AIR_CHAMBER_UNIT, TIMERA_UNIT_AIR_CHAMBER_CH, &stcTimerCompareInit);
TIMERA_CompareCmd(TIMERA_TUBE_HEAT_UNIT, TIMERA_UNIT_TUBE_HEAT_CH, Enable);
TIMERA_CompareCmd(TIMERA_AIR_CHAMBER_UNIT, TIMERA_UNIT_AIR_CHAMBER_CH, Enable);
// TIMERA_CompareCacheCmd(TIMERA_AIR_CHAMBER_UNIT, TIMERA_UNIT_AIR_CHAMBER_CH, Enable);
TIMERA_Cmd(TIMERA_TUBE_HEAT_UNIT,Enable);
TIMERA_Cmd(TIMERA_AIR_CHAMBER_UNIT,Enable);
real.tube.TIMERAx = TIMERA_TUBE_HEAT_UNIT;
real.tube.enChannel = TIMERA_UNIT_TUBE_HEAT_CH;
real.air_charmber.TIMERAx = TIMERA_AIR_CHAMBER_UNIT;
real.air_charmber.enChannel = TIMERA_UNIT_AIR_CHAMBER_CH;
}
int pwm_out = 2000;
TIMERA_SetCompareValue(real.tube.TIMERAx, real.tube.enChannel,(pwm_out + pwm_out));
TIMERA_SetCompareValue(real.air_charmber.TIMERAx, real.air_charmber.enChannel,pwm_out);
这两个各种组合都试了,都受影响
黄色是air_charmber,这个波形经常条,偶尔有一个正确波形
|