void BuzzerInit(void)
{
stc_timer6_basecnt_cfg_t stcTIM6BaseCntCfg;
stc_timer6_port_output_cfg_t stcTIM6PWMxCfg;
stc_port_init_t stcPortInit;
stc_clk_freq_t stcClkTmp;
stc_timer6_gcmp_buf_cfg_t stcGCMPBufCfg;
MEM_ZERO_STRUCT(stcTIM6BaseCntCfg);
MEM_ZERO_STRUCT(stcTIM6PWMxCfg);
MEM_ZERO_STRUCT(stcPortInit);
MEM_ZERO_STRUCT(stcClkTmp);
MEM_ZERO_STRUCT(stcGCMPBufCfg);
CLK_GetClockFreq( &stcClkTmp );
PWC_Fcg2PeriphClockCmd(PWC_FCG2_PERIPH_TIM63, Enable);
PORT_SetFunc(PortB, Pin01, Func_Tim6, Disable); //Timer63 PWMA
stcTIM6BaseCntCfg.enCntMode = Timer6CntSawtoothMode; //Sawtooth wave mode
stcTIM6BaseCntCfg.enCntDir = Timer6CntDirUp; //Counter counting up
stcTIM6BaseCntCfg.enCntClkDiv = Timer6PclkDiv64; //Count clock: pclk0
Timer6_Init(M4_TMR63, &stcTIM6BaseCntCfg); //timer6 PWM frequency, count mode and clk config
uint16_t u16Period = SystemCoreClock / BUZ_PWM_PSC / BUZZER_FREQ_HZ;
Timer6_SetPeriod(M4_TMR63, Timer6PeriodB, u16Period); //period set
uint16_t u16Compare = u16Period / 2;
Timer6_SetGeneralCmpValue(M4_TMR63, Timer6GenCompareB, u16Compare); //Set General Compare RegisterA Value
/*PWMB output buf config*/
stcTIM6PWMxCfg.enPortMode = Timer6ModeCompareOutput; //Compare output function
stcTIM6PWMxCfg.bOutEn = true; //Output enable
stcTIM6PWMxCfg.enPerc = Timer6PWMxCompareLow; //PWMB port output Low level when CNTER value match PERAR
stcTIM6PWMxCfg.enCmpc = Timer6PWMxCompareHigh; //PWMB port output High level when CNTER value match with GCMAR
stcTIM6PWMxCfg.enStaStp = Timer6PWMxStateSelSS; //PWMB output status is decide by STACA STPCA when CNTER start and stop
stcTIM6PWMxCfg.enStaOut = Timer6PWMxPortOutHigh; //PWMB port output set low level when CNTER start
stcTIM6PWMxCfg.enStpOut = Timer6PWMxPortOutLow; //PWMB port output set low level when CNTER stop
stcTIM6PWMxCfg.enDisVal = Timer6PWMxDisValLow;
Timer6_PortOutputConfig(M4_TMR63, Timer6PWMB, &stcTIM6PWMxCfg);
M4_TMR63->CNTER_f.CNT = 0;
M4_TMR63->GCONR_f.START = 1;
BuzzerOff();
}
我现在想动态调整PB1的Pwm波来驱动蜂鸣器。
在以上代码最后一行(BuzzerOff)打断点,PB1引脚始终只输出40Hz的波形,手动调整以上代码中的局部变量u16Period为1000,2500等,都只有40Hz的Pwm波,还望哪位高人提点一二 |