本帖最后由 SingleYork2021 于 2021-11-17 15:02 编辑
想用HC32F460KETA TIMER6输出三路PWM,使用到的是TIMER6_1_PWMB、TIMER6_2_PWMB、TIMER6_3_PWMB,其中部分配置代码如下:
/*---------------------------------------------------*/
stcTIM6BaseCntCfg.enCntMode = Timer6CntTriangularModeA; //Triangular wave mode
stcTIM6BaseCntCfg.enCntDir = Timer6CntDirUp; //Counter counting up
stcTIM6BaseCntCfg.enCntClkDiv = Timer6PclkDiv1; //Count clock: pclk 168M
Timer6_Init(M4_TMR61, &stcTIM6BaseCntCfg); //timer6 PWM frequency, count mode and clk config
u16Period_1 = u32Pclk0/(TIMER6_PWM1B_Out_Fre*2);
Timer6_SetPeriod(M4_TMR61, Timer6PeriodA, u16Period_1); //period set
u16Compare_1 = u16Period_1/2; //50%占空比
// Timer6_SetGeneralCmpValue(M4_TMR61, Timer6GenCompareA, u16Compare_1); //Set General Compare RegisterA Value
Timer6_SetGeneralCmpValue(M4_TMR61, Timer6GenCompareC, u16Compare_1); //Set General Compare RegisterC Value as buffer register of GCMAR
/*PWMA/PWMB output buf config*/
stcGCMPBufCfg.bEnGcmpTransBuf = true;
stcGCMPBufCfg.enGcmpBufTransType = Timer6GcmpPrdSingleBuf; //Single buffer transfer
Timer6_SetGeneralBuf(M4_TMR61, Timer6PWMA, &stcGCMPBufCfg); //GCMAR buffer transfer set
Timer6_SetGeneralBuf(M4_TMR61, Timer6PWMB, &stcGCMPBufCfg); //GCMBR buffer transfer set
// stcTIM6PWMxCfg.bOutEn = true; //Output enable
// stcTIM6PWMxCfg.enPerc = Timer6PWMxCompareKeep; //PWMA port output keep former level when CNTER value match PERAR
// stcTIM6PWMxCfg.enCmpc = Timer6PWMxCompareInv; //PWMA port output inverse level when CNTER value match with GCMAR
// stcTIM6PWMxCfg.enStaStp = Timer6PWMxStateSelSS; //PWMA output status is decide by STACA STPCA when CNTER start and stop
// stcTIM6PWMxCfg.enStaOut = Timer6PWMxPortOutLow; //PWMA port output set low level when CNTER start
// stcTIM6PWMxCfg.enStpOut = Timer6PWMxPortOutLow; //PWMA port output set low level when CNTER stop
// stcTIM6PWMxCfg.enDisVal = Timer6PWMxDisValLow;
// Timer6_PortOutputConfig(M4_TMR61, Timer6PWMA, &stcTIM6PWMxCfg);
// Timer6_SetFunc(M4_TMR61, Timer6PWMA, Timer6ModeCompareOutput); //Compare output function
stcTIM6PWMxCfg.bOutEn = true; //Output enable
stcTIM6PWMxCfg.enPerc = Timer6PWMxCompareKeep; //PWMB port output keep former level when CNTER value match PERAR
stcTIM6PWMxCfg.enCmpc = Timer6PWMxCompareInv; //PWMB port output inverse level when CNTER value match with GCMBR
stcTIM6PWMxCfg.enStaStp = Timer6PWMxStateSelSS; //PWMB output status is decide by STACB STPCB when CNTER start and stop
stcTIM6PWMxCfg.enStaOut = Timer6PWMxPortOutHigh; //PWMB port output set high level when CNTER start
stcTIM6PWMxCfg.enStpOut = Timer6PWMxPortOutLow; //PWMB port output set low level when CNTER stop
stcTIM6PWMxCfg.enDisVal = Timer6PWMxDisValLow;
Timer6_PortOutputConfig(M4_TMR61, Timer6PWMB, &stcTIM6PWMxCfg);
Timer6_SetFunc(M4_TMR61, Timer6PWMB, Timer6ModeCompareOutput); //Compare output function
Timer6_SetDeadTimeValue(M4_TMR61, Timer6DeadTimUpAR, 0u); //Set dead time value (up count)
stcDeadTimeCfg.bEnDeadtime = true; //Enable Hardware DeadTime
stcDeadTimeCfg.bEnDtBufUp = false; //Disable buffer transfer
stcDeadTimeCfg.bEnDtBufDwn = false; //Disable buffer transfer
stcDeadTimeCfg.bEnDtEqualUpDwn = true; //Make the down count dead time value equal to the up count dead time setting
Timer6_ConfigDeadTime(M4_TMR61, &stcDeadTimeCfg); //Hardware dead time function config
/*config interrupt*/
/* Enable timer61 under flow interrupt */
Timer6_ConfigIrq(M4_TMR61, Timer6INTENUDF, true);
stcIrqRegiConf.enIRQn = Int018_IRQn; //Register INT_TMR62_GUDF Int to Vect.No.019
stcIrqRegiConf.enIntSrc = INT_TMR61_GUDF; //Select Event interrupt function
stcIrqRegiConf.pfnCallback = &Timer61_UnderFlow_CallBack; //Callback function
enIrqRegistration(&stcIrqRegiConf); //Registration IRQ
NVIC_ClearPendingIRQ(stcIrqRegiConf.enIRQn); //Clear Pending
NVIC_SetPriority(stcIrqRegiConf.enIRQn, DDL_IRQ_PRIORITY_03); //Set priority
NVIC_EnableIRQ(stcIrqRegiConf.enIRQn); //Enable NVIC
有几个地方不太明白:
1、Timer6_SetPeriod(M4_TMR61, Timer6PeriodA, u16Period_1); 这里为什么要设置Timer6PeriodA,看到库配置里面还有Timer6PeriodB和Timer6PeriodC可选,这里有什么特别含义吗?手册上实在没看懂!
2、下面这两个配置:
Timer6_SetGeneralBuf(M4_TMR61, Timer6PWMA, &stcGCMPBufCfg); //GCMAR buffer transfer set
Timer6_SetGeneralBuf(M4_TMR61, Timer6PWMB, &stcGCMPBufCfg); //GCMBR buffer transfer set
如果我只是使用PWMB的话,第一句“Timer6_SetGeneralBuf(M4_TMR61, Timer6PWMA, &stcGCMPBufCfg); ”是否可以去掉?我试过直接去掉,但是输出PWM就有问题了,跟我配置的不一样,是不是还需要修改其他地方?我试了屏蔽下面这部分代码,对PWMB输出没有影响:
stcTIM6PWMxCfg.bOutEn = true; //Output enable
stcTIM6PWMxCfg.enPerc = Timer6PWMxCompareKeep; //PWMA port output keep former level when CNTER value match PERAR
stcTIM6PWMxCfg.enCmpc = Timer6PWMxCompareInv; //PWMA port output inverse level when CNTER value match with GCMAR
stcTIM6PWMxCfg.enStaStp = Timer6PWMxStateSelSS; //PWMA output status is decide by STACA STPCA when CNTER start and stop
stcTIM6PWMxCfg.enStaOut = Timer6PWMxPortOutLow; //PWMA port output set low level when CNTER start
stcTIM6PWMxCfg.enStpOut = Timer6PWMxPortOutLow; //PWMA port output set low level when CNTER stop
stcTIM6PWMxCfg.enDisVal = Timer6PWMxDisValLow;
Timer6_PortOutputConfig(M4_TMR61, Timer6PWMA, &stcTIM6PWMxCfg);
Timer6_SetFunc(M4_TMR61, Timer6PWMA, Timer6ModeCompareOutput); //Compare output function
|