The documentation discusses how to generate phase-shift PWM signals based on SCTimer/PWM module, the code is developed based on MCUXpresso IDE version 10.3 and LPCXpresso5411x board.
The LPC family has SCTimer/PWM module and CTimer modules, both of them can generate PWM signals, but only the SCTimer/PWM module can generate phase-shift PWM signals.
In the code, only the match registers are used to generate events, I/O signals are not used.
The match0 register is set up as (SystemCoreClock/100), which determines the PWM signal frequency.
The the match1 register is set up as 0x00, which generate event1.
The the match2 register is set up as (SystemCoreClock/100)/2;, which generate event2.
The duty cycle is (SystemCoreClock/100)/2-0x00= (SystemCoreClock/100)/2, which is 50% duty cycle, the cycle time is (SystemCoreClock/100).
The event1 sets the SCT0_OUT1, event2 clears the SCT0_OUT1, so SCT0_OUT1 has 50% duty cycle.
The the match3 register is set up as (SystemCoreClock/100)/4;, which generate even3.
The the match4 register is set up as 3*(SystemCoreClock/100)/4, which generate event4.
The duty cycle is 3*(SystemCoreClock/100)/4 - (SystemCoreClock/100)/4= (SystemCoreClock/100)/2, which is 50% duty cycle.
The event3 sets the SCT0_OUT2, event4 clears the SCT0_OUT2, so SCT0_OUT2 has 50% duty cycle.
The phase shift is (SystemCoreClock/100)/4 - 0x00= (SystemCoreClock/100)/4, which corresponds 90 degree phase shift.
PWM initilization code:
//The SCT0_OUT1 can output PWM signal with 50 duty cycle from PIO0_8 pin
//The SCT_OUT2 can output PWM signal with 50 duty cycle fron PIO0_9 pin
//The SCT0_OUT1 and SCT0_OUT2 PWM signal has 90 degree phase shift.
void SCT0_PWM(void)
{
SYSCON->AHBCLKCTRL[1]|=(1<<2); //SET SCT0 bit
SCT0->CONFIG = (1 << 0) | (1 << 17); // unified 32-bit timer, auto limit
SCT0->SCTMATCHREL[0] = SystemCoreClock/100; // match 0 @ 100 Hz = 10 msec
SCT0->EVENT[0].STATE = 0xFFFFFFFF; // event 0 happens in all states
//set event1
SCT0->SCTMATCHREL[1]=0x00;
SCT0->EVENT[1].STATE = 0xFFFFFFFF; // event 1 happens in all states
SCT0->EVENT[1].CTRL = (1 << 12)|(1<<0); // match 1 condition only
//set event2
SCT0->SCTMATCHREL[2]=(SystemCoreClock/100)/2;
SCT0->EVENT[2].STATE = 0xFFFFFFFF; // event 2 happens in all states
SCT0->EVENT[2].CTRL = (1 << 12)|(2<<0); // match 2 condition only
//set event3
SCT0->SCTMATCHREL[3]=(SystemCoreClock/100)/4;
SCT0->EVENT[3].STATE = 0xFFFFFFFF; // event 3 happens in all states
SCT0->EVENT[3].CTRL = (1 << 12)|(3<<0); // match 3 condition only
//set event4
SCT0->SCTMATCHREL[4]=3*(SystemCoreClock/100)/4;
SCT0->EVENT[4].STATE = 0xFFFFFFFF; // event 4 happens in all states
SCT0->EVENT[4].CTRL = (1 << 12)|(4<<0); // match 4 condition only
//PWM output1 signal
SCT0->OUT[1].SET = (1 << 1); // event 1 will set SCT1_OUT0
SCT0->OUT[1].CLR = (1 << 2); // event 2 will clear SCT1_OUT0
SCT0->RES |= (3 << 2); // output 0 toggles on conflict
//PWM output2 signal
SCT0->OUT[2].SET = (1 << 3); // event 3 will set SCT1_OUT0
SCT0->OUT[2].CLR = (1 << 4); // event 4 will clear SCT1_OUT0
SCT0->RES = (3 << 4); // output 0 toggles on conflict
//PWM start
SCT0->CTRL &= ~(1 << 2); // unhalt by clearing bit 2 of the CTRL
}
Pin initialization code:
//PIO0_8 PIO0_8 FC2_RXD_SDA_MOSI SCT0_OUT1 CTIMER0_MAT3
//PIO0_9 PIO0_9 FC2_TXD_SCL_MISO SCT0_OUT2 CTIMER3_CAP0 - FC3_CTS_SDA_SSEL0
void SCTimerPinInit(void)
{
//Enable the SCTimer clock
SYSCON->AHBCLKCTRL[0]|=(1<<13); //set IOCON bit
//SCTimer pin assignment
IOCON->PIO[0][8]=0x182;
IOCON->PIO[0][9]=0x182;
IOCON->PIO[0][10]=0x182;
}
Main Code:
#include <stdio.h>
#include "board.h"
#include "peripherals.h"
#include "pin_mux.h"
#include "clock_config.h"
#include "LPC54114_cm4.h"
void SCT0_Init(void);
void SCTimerPinInit(void);
void P1_9_GPIO(void);
void SCT0_PWM(void);
int main(void) {
/* Init board hardware. */
BOARD_InitBootPins();
BOARD_InitBootClocks();
BOARD_InitBootPeripherals();
printf("Hello World\n");
// SCT0_Init();
// P1_9_GPIO();
SCTimerPinInit();
SCT0_PWM();
/* Force the counter to be placed into memory. */
volatile static int i = 0 ;
/* Enter an infinite loop, just incrementing a counter. */
while(1) {
i++ ;
}
return 0 ;
}
The Yellow channel is PIO0_8 pin output signal, which is SCT0_OUT1 PWM output signal.
The Bule channel is PIO0_9 pin output signal, which is SCT0_OUT2 PWM output signal.