TI F281x PWM Configuration
This piece of code contains a function which configures the basic features of the PWM module in TI F281x family. In this case it generates a period of 200 Hz with 60% duty cycle in PWM1 output. It is very simple to modify these parameters just by changing the value in CMPR1 AND T1PR registers.
/**********************************************************************/
#include "DSP281x_Device.h"
/***********************************************************************/
void InitPwm(void)
{
asm( "EALLOW"); //Enable write operations in protected registers
/*** Independent Compare Output Enable */
EvaRegs.EXTCONA.bit.INDCOE= 1;
/********************************************************************
* Write values in the two following registers until all the other
* registers have been written, as their value depends on the other
* registers. *
********************************************************************/
/*** Timer 1 Period Regiser */
EvaRegs.T1PR = 0xB71A; //use this value for a frequency of 200 Hz assuming
//sysclkout=150 MHz and its pre-scaler=16
/*** CMPR1 Register Configuration */
EvaRegs.CMPR1= 0x493D; //to get 60% of duty cycle this value equals the 1-0.6= 40% period
/********************************************************************/
EvaRegs.GPTCONA.all= 0x0050;
// bit 15 0 Reserved
// bit 14 0 T2STAT GP timer 2 Status.
// bit 13 0 T1STAT GP timer 1 Status.
// bit 12 0 T2CTRIPE T2CTRIP Enable.
// bit 11 0 T1CTRIPE T1CTRIP Enable.
// bit 10-9 00 T2TOADC Start ADC with timer 2 event
// bit 8-7 00 T1TOADC Start ADC with timer 1 event
// bit 6 1 TCMPOE Timer compare output enable.
// bit 5 0 T2CMPOE Timer 2 compare output enable.
// bit 4 1 T1CMPOE Timer 1 Compare Output Enable.
// bit 3-2 00 T2PIN Polarity of GP timer 2 compare output
// bit 1-0 00 T1PIN Polarity of GP timer 1 compare output
/*** COMCONA Register Configuration*/
EvaRegs.COMCONA.all = 0x8020;
// bit 15 1: CENABLE: Compare Enable
// bit 14-13 00: CLD1, CLD0: CMPRx Reload Condition
// bit 12 0: SVENABLE: PWM Vector Enable
// bit 11-10 00: ACTRLD1,ACTRLD0: Action Control Regiser Reload Condition
// bit 9 0: FCMPOE: Full Compare Output Enable
// bit 8 0: PDPINTA: PDPINTA Status
// bit 7 0: FCMP3OE: Compare 3 Output Enable
// bit 6 0: FCMP2OE: Compare 2 Output Enable
// bit 5 1: FCMP1OE: Compare 1 Output Enable
// bit 4-3 00: Reserved
// bit 2 0: C3TRIPE: C3TRIP Enable
// bit 1 0: C2TRIPE: C2TRIP Enable
// bit 0 0: C1TRIPE: C1TRIP Enable
/** ACTRA Register Configuration*/
EvaRegs.ACTRA.all = 0x0002;
// bit 15 0: SVRDIR: PWM Vector rotate direction
// bit 14-12 000: D2-D: PWM Vector basic bits
// bit 11-10 00: CMP6ACT1-0 Compare Action in pin 6, CMP6.
// bit 9-8 00: CMP5ACT1-0 Compare Action in pin 5, CMP5.
// bit 7-6 00: CMP4ACT1-0 Compare Action in pin 4, CMP4.
// bit 5-4 00: CMP3ACT1-0 Compare Action in pin 3, CMP3
// bit 3-2 00: CMP2ACT1-0 Compare Action in pin 2, CMP2
// bit 1-0 10 CMP1ACT1-0 Compare Action in el pin 1, CMP1
/*** T1CON Register Configuration*/
EvaRegs.T1CON.all = 0x9442;
// bit 15:14 10: Emulation Control bits
// bit 13 0: Reserved
// bit 12-11 10: TMODE1-TMODE0 Counter Mode Selection
// bit 10-8 100: TPS2-TPS0 Pre-scaler: 100= HSPCLK/16
// bit 7 0: T2SWT1 Timer 2 trigger by timer 1
// bit 6 1: TENABLE Timer Enable
// bit 5-4 00: TCLKS(1,0) Clock Source Selection
// bit 3-2 00: TCLD(1,0) Timer Compare Register Reload Condition
// bit 1 1: TECMPR Compare Operations Enable
// bit 0 0: SELT1PR: Period Register Selection
asm( "EDIS"); //Protected Registers Write Disabled
}
/**End of function*/