输出频率 =238KHz ≈ 24M/100
Duty = 90%/2- 1%=44% 1%为系统预留死区时间
#include <avr/io.h>
#define ON_TIME_START_WOA 10
#define ON_TIME_CYCLES_WOA 90
#define ON_TIME_START_WOB 10
#define ON_TIME_CYCLES_WOB 90
void CLK_Init(void);
void PORT_Init(void);
void TCD_Init(void);
void CLK_Init(void)
{
/* Set OSCHF as main clock source */
_PROTECTED_WRITE(CLKCTRL.MCLKCTRLA, CLKCTRL_CLKSEL_OSCHF_gc);
/* Wait for main clock oscillator changing to finish */
while (CLKCTRL.MCLKSTATUS & CLKCTRL_SOSC_bm)
{
;
}
/* Change OSCHF frequency to 24 MHz */
_PROTECTED_WRITE(CLKCTRL.OSCHFCTRLA, CLKCTRL_FREQSEL_24M_gc);
/* Set the multiplication factor for PLL to 2x */
_PROTECTED_WRITE(CLKCTRL.PLLCTRLA, CLKCTRL_MULFAC_2x_gc);
}
void PORT_Init(void)
{
/* Configure PORT A PIN4 and PIN5 as output low */
PORTA.DIRSET |= PIN4_bm | PIN5_bm;
PORTA.OUTSET |= PIN4_bm | PIN5_bm;
}
void TCD_Init()
{
/* Select the Two Ramp mode */
TCD0.CTRLB |= TCD_WGMODE_TWORAMP_gc;
TCD0.CMPASET = ON_TIME_START_WOA;
TCD0.CMPACLR = ON_TIME_START_WOA + ON_TIME_CYCLES_WOA;
TCD0.CMPBSET = ON_TIME_START_WOB;
TCD0.CMPBCLR = ON_TIME_START_WOB + ON_TIME_CYCLES_WOB;
/* Enable the PWM channels */
_PROTECTED_WRITE(TCD0.FAULTCTRL, TCD_CMPAEN_bm | TCD_CMPA_bm
| TCD_CMPBEN_bm | TCD_CMPB_bm);
/* Wait for TCD to be ready for enabling */
while (!(TCD0.STATUS & TCD_ENRDY_bm))
{
;
}
/* Select PLL as input clock and enable TCD */
TCD0.CTRLA |= TCD_CLKSEL_PLL_gc | TCD_ENABLE_bm;
}
int main(void)
{
CLK_Init();
PORT_Init();
TCD_Init();
while (1)
{
}
}
|