SNIU028D February 2016 – September 2020 UCD3138 , UCD3138064 , UCD3138064A , UCD3138128 , UCD3138A , UCD3138A64
UCD3138RGC (64-pin version) offers two timer PWM outputs (PWM0 and PWM1), but UCD3138RHA (40-pin version) offers only one timer PWM0 output. The C code below should provide a 50% PWM at approximately 10 KHz. Consult the UCD3138 device datasheet for ICLK speeds.
TimerRegs.T16PWM0CMP0DAT.bit.CMP_DAT = 1587; //value to reset counter
TimerRegs.T16PWM0CMP1DAT.bit.CMP_DAT = 793; //50%50 duty cycle half of comp 0
TimerRegs.T16PWM0CMPCTRL.bit.PWM_OUT_ACTION0 = 1;//1 is for clear pin
TimerRegs.T16PWM0CMPCTRL.bit.PWM_OUT_ACTION1 = 2;//2 is for clear pin
TimerRegs.T16PWM0CNTCTRL.bit.CMP_RESET_ENA = 1; //enable reset by comp 0
TimerRegs.T16PWM0CMPCTRL.bit.PWM_OUT = 0; //make sure that default is a 0
TimerRegs.T16PWM0CMPCTRL.bit.PWM_OUT_DRV = 1; //put zero into output latch
TimerRegs.T16PWM0CMPCTRL.bit.PWM_OUT_ENA = 1; //enable pin as an output
TimerRegs.T16PWM0CNTCTRL.bit.SW_RESET = 1; //allow counter to run
This program uses the Capture 0 block to set the PWM pin high at the end of the previous cycle and s simultaneously reset the counter. Then the Capture 1 block, which is loaded with a value 50% the size of Capture 0, clears the PWM pin value halfway through the period.