in main.c
volatile int i;
SYS_PROT = PSW_PROT;
SYS_CLK_CFG = 0x01FF;
SYS_SFT_RST = 0x00FF;
SYS_SFT_RST = 0x0000;
SYS_CLK_FEN = 0x0008;
//mcpwm config
MCPWM_PRT = 0x0000DEAD; //enter password to unlock write protection
MCPWM_TH = 0x00000BCD;
//-------
//all 1
MCPWM_TH00 = -0x00000BCD;
MCPWM_TH01 = 0x00000BCD;
MCPWM_DTH00 = 0;
MCPWM_DTH01 = 0;
MCPWM_SDCFG = 0x1 << 4; //t0 update
MCPWM_FAIL |= 0x00000040; //moe
MCPWM_IO01 |= 0x00000000; //cmp mode
//-------
MCPWM_TCLK = 0x0000000C; //ton, tclk_en, tclk_div[1:0], no div
MCPWM_AUEN = 0x3; //enable auto update of th00 & th01
//enable t1 event irq
MCPWM_IE = 0x2;
MCPWM_UPDATE = 0x0000FFFF; //write corresponding bit to trigger update
//MCPWM_PRT = 0x0000CAFE; //write any value other than 0xDEAD to enable write protection
NVIC_EnableIRQ(MCPWM_IRQn); //enable the adc0 interrupt
__enable_irq(); //clr PRIMASK
SCB->SCR &= ~0x00000010; //clr SEVONPEND
GPIO1_POE = 0x1 << 4 | 0x1 << 5;
GPIO1_F7654 = 0x0033;
i = 10;
while(i--){
while(0x1 != (0x1 & MCPWM_IF)); //wait for MCPWM_IF[0] -TH event
MCPWM_IF |= 0x1;
}
PASS;
in interrupt.c
void MCPWM_IRQHandler(void)
{
MCPWM_IF |= 0xFF;
GPIO0_PDO = ~GPIO0_PDO;
if(GPIO0_PDO==0){
if(MCPWM_TH00 != MCPWM_TH){
GPIO1_PDO = ~GPIO1_PDO;
MCPWM_TH00 = MCPWM_TH;
}
else{
MCPWM_TH00 = -MCPWM_TH;
GPIO2_PDO = ~GPIO2_PDO;
}
}
}
综上,
TH00 = TH01 可实现mcpwm输出全0
TH00 = -TH TH01 = TH,可实现mcpwm输出全1 |