28335 study notes 7-F28355 PWM output configuration

1. PWM output configuration steps

EPWM related library functions are in the DSP2833x_EPwm.c and DSP2833x_EPwm.h files.
(1) Turn on ePWM peripheral clock and disable time base module clock To
use ePWM peripherals, you need to turn on the corresponding clock. When configuring ePWM related registers, you need to turn off the time base module clock, and then turn it on after configuration to ensure synchronization.

    EALLOW;
	SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;   // Disable TBCLK within the ePWM要保证时基同步的话,首先在配置TB/CC寄存器时先把时钟关闭,即所有TBCLK停止,不产生。等全部配置后之后再打开,保证时钟同步
	SysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 1;  // 开启ePWM1外设时钟
	EDIS;

(2) Enable ePWM to correspond to the GPIO clock and initialize the configuration. The
PWM output channel corresponds to the IO port of F28335, so it is necessary to enable the corresponding port clock and configure the corresponding IO port as the ePWM output function. Here, the ePWM6A and ePWM6B functions of GPIO10 and GPIO11 are used, that is, to initialize the two IO ports and enable the pull-up and GPIO peripheral multiplexing functions.

    InitEPwm6Gpio();//开启时钟,对应ePWM复用功能的开启

(3) Initialize the time base module, that is, configure the TB related register value (determine the counting mode, period, phase).
By configuring the time base module, you can determine the counter's counting mode, cycle frequency, and whether it is synchronized. Here, set ePWM6 counting mode to count up, do not use phase synchronization function, counter counting frequency is system clock frequency, counter initial value is 0.

// Setup Sync
	EPwm6Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;  // 禁止(即不使用(输出))ePWMxSYNCO信号
	// Allow each timer to be sync'ed
	EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE;//禁止TBCTR加载相位寄存器TBPHS中的值
	EPwm6Regs.TBPHS.half.TBPHS = 0;//将相位寄存器中的值清零
	EPwm6Regs.TBCTR = 0x0000; // Clear counter时间基准计数寄存器设置为0
	EPwm6Regs.TBPRD = tbprd;//设定周期值
	EPwm6Regs.TBCTL.bit.CTRMODE = TB_COUNT_UP;    // Count up向上计数模式
	EPwm6Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;//采用系统时钟作为TBCLK
	EPwm6Regs.TBCTL.bit.CLKDIV=TB_DIV1;//同样是配置TBCLK的时钟

(4) Initialize the comparison module and configure the CC related registers.
By configuring the CC module, you can determine the loading method of the comparison register value, the comparator value, and the duty cycle. Here, set the loading mode of ePWM6 to be 0 for the counter and 0 for the comparator.

// Setup shadow register load on ZERO
	EPwm6Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;// 使用影子寄存器
	EPwm6Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;// 使用影子寄存器
	EPwm6Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO;//CTR等于0时加载
	EPwm6Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO;

	// Set Compare values
	EPwm6Regs.CMPA.half.CMPA = 0;    // Set compare A value
	EPwm6Regs.CMPB = 0;              // Set Compare B value

(5) Initialize the action limitation module and configure the AQ related register value.
By configuring the AQ module, the PWM output waveform mode can be determined. Configure the ePWM6 output here. When the ePWMA counter counts to 0, it outputs a low level. When the ePWMA counter counts to CMPA, it outputs a high level. When the ePWMB counter counts to 0, it outputs a low level. When the ePWMB counter counts to CMPA, it outputs a high level. Level.

// Set actions
	EPwm6Regs.AQCTLA.bit.ZRO = AQ_CLEAR;            // Set PWM1A on Zero
	EPwm6Regs.AQCTLA.bit.CAU = AQ_SET;          // Clear PWM1A on event A, up count
	EPwm6Regs.AQCTLB.bit.ZRO = AQ_CLEAR;            // Set PWM1B on Zero
	EPwm6Regs.AQCTLB.bit.CBU = AQ_SET;          // Clear PWM1B on event B, up count

(6) Initialize the event trigger module, that is, configure ET-related register values.
When event-triggered output control is needed, ET-related registers need to be configured. Here, when the counter counts to 0, the event trigger interrupt is enabled at the same time, and PWM is output every time a trigger event occurs.

    EPwm6Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;     // Select INT on Zero event
	EPwm6Regs.ETSEL.bit.INTEN = 1;  // Enable INT
	EPwm6Regs.ETPS.bit.INTPRD = ET_1ST;           // Generate INT on 1st event每发生一次事件产生中断信号EPWM6_INT

(7) Initialize the dead zone module and chopper module, that is, configure the DB and PC related register values.
Generally, the dead zone and chopper module are not configured.
(8) Enable the time base counter clock After
each module register is configured, finally turn on the time base counter clock. After this step, the corresponding IO port can output the PWM wave

EALLOW;
	SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;         // Start all the timers synced
	EDIS;

2. Output PWM wave through two pins ePWM6A and ePWM6B to control the brightness of D6 and D7 indicator lights respectively

The main function code is as follows

#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File

#include "leds.h"
#include "time.h"//在定时器中控制灯的闪烁
#include "epwm.h"//存放PWM的驱动程序
void main()
{
    
    
	int i=0;
	unsigned char fx=0;//fx指示了一个方向

	InitSysCtrl();

	InitPieCtrl();
	IER = 0x0000;
	IFR = 0x0000;
	InitPieVectTable();

//	LED_Init();
	EPWM6_Init(500);//EPWM管脚也即控制了LED,也就是说周期值是500个系统时钟

	while(1)
	{
    
    
		if(fx==0)
		{
    
    
			i++;
			if(i==300)
			{
    
    
				fx=1;
			}
		}
		else
		{
    
    
			i--;
			if(i==0)
			{
    
    
				fx=0;
			}
		}
		EPwm6A_SetCompare(i);  //i值最大可以取499,因为ARR最大值是499.
		EPwm6B_SetCompare(300-i);  //i值最大可以取499,因为ARR最大值是499.
		DELAY_US(10*1000);
	}
}

Guess you like

Origin blog.csdn.net/qq_39529052/article/details/106409050