# How To PWM Controlled By Potentiometer Using TMS320 ## Overview

In TMS320 PWM Tutorial, you are going to learn about what PWM is and how you can get the PWM output from the Analog pins of tms320. First, we will generate a PWM signal and then we will control it manually by adding the potentiometer.

## What is PWM

This project involves a combination of PWM (Pulse Width Modulation) and ADC (Analog-Digital Converter) to drive a DC motor at different speeds.

PWM is a concept where a variable pulse voltage can be modulated from 0% to 100% duty cycle.
The duty cycle represents the time a pulse is high compared to the total duration of the pulse.

This image below represents a different percentage of the duty cycles of an XXms wave.

Control the speed of the motor using a Potentiometer.

Potentiometer connection and reading logic built

Now talking about the ADC, it’s a 12 bit analog to digital converter that converts all the analog values to 12-bit digital data i.e. (0-4096 for which it requires a reference voltage. The resolution or step-up voltage is 19.531mV. This means that for every 1.22070Mv rise in the analog input, ADC will increase the digital value by 1 starting from 0 and ending at 4096.

Now talking about the ADC, it’s a 12 bit analog to digital converter that converts all the analog values to 12-bit digital data i.e. (0-4096 for which it requires a reference voltage. The resolution or step-up voltage is 1.22070mV. This means that for every 1.22070Mv rise in the analog input, ADC will increase the digital value by 1 starting from 0 and ending at 4096.

The resolution depends upon the reference voltage as well as the type of ADC you’re using. Here the reference voltage is 5v, while I’m working on a 12 bit ADC therefore:

Resolution = 5(Reference voltage)/4096(due to 12 bit i.e. 2^12=4096) = 1.22070mV

The output waveform of PWM signal

## 1st initialize the PWM GPIO Pins

```/////  ////

void PinMux_init()
{
EALLOW;
//EPWM1 -> myEPWM1 Pinmux

GpioCtrlRegs.GPAMUX1.all=0;// gpio 15 to gpio 0 a;; general purpouse I/O
GpioCtrlRegs.GPAMUX1.bit.GPIO0=1;// epwm active
EDIS;

}

```

## 2nd Configure the PWM Signal

```///////////////////////////////////////////////////////////

void initEPWM1()
{

EPwm1Regs.TBPRD = 4000;       // Set timer period 801 TBCLKs
EPwm1Regs.TBPHS.bit.TBPHS = 0x0000;        // Phase is 0
EPwm1Regs.TBCTR = 0x0000;
// Set Compare values
EPwm1Regs.CMPA.bit.CMPA = Adc_Result_1;    // Set compare A value
// Setup counter mode
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up and down
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;       // Clock ratio to SYSCLKOUT
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;

EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;            // Set PWM1A on event A, up
// count
EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;          // Clear PWM1A on event A,
// down count

}

```

Here  EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;

EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;    // Clear PWM1A on event A, // down count

Mean when your PWM pulses reach the setpoint(CMPA) then your pulse will be set to high and low

## 3rd configure the ADC resolution

```////////////////////////////////////////////

{
EALLOW;

{
}
{
}
#endif
DEVICE_DELAY_US(1000);
EDIS;
}

```

```void initADC_SOC(void)
{
{

}
{
}
#endif

}

```

// Convert, wait for completion, and store results

////           // Store results

## Final code

```// how to control pwm pulses using adc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "F2837xD_device.h"
#include "F28x_Project.h"
#include "F2837xD_Examples.h"

#include "driverlib.h"
#include "device.h"

void PinMux_init();
void initEPWM1();

void main(void)
{

Device_init();
Device_initGPIO();
PinMux_init();
Interrupt_initModule();
Interrupt_initVectorTable();
IER = 0x0000;
IFR = 0x0000;

SysCtl_disablePeripheral(SYSCTL_PERIPH_CLK_TBCLKSYNC);
SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_TBCLKSYNC);

EINT;
ERTM;
while(1)
{

// Convert, wait for completion, and store results

{

}

////           // Store results

initEPWM1();
DEVICE_DELAY_US(10000);

}

}

void PinMux_init()
{
EALLOW;
//EPWM1 -> myEPWM1 Pinmux

GpioCtrlRegs.GPAMUX1.all=0;// gpio 15 to gpio 0 a;; general purpouse I/O
GpioCtrlRegs.GPAMUX1.bit.GPIO0=1;// epwm active
EDIS;

}

void initEPWM1()
{

EPwm1Regs.TBPRD = 4000;       // Set timer period 801 TBCLKs
EPwm1Regs.TBPHS.bit.TBPHS = 0x0000;        // Phase is 0
EPwm1Regs.TBCTR = 0x0000;
// Set Compare values
EPwm1Regs.CMPA.bit.CMPA = Adc_Result_1;    // Set compare A value
// Setup counter mode
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count up and down
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;       // Clock ratio to SYSCLKOUT
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;

EPwm1Regs.AQCTLA.bit.CAU = AQ_SET;            // Set PWM1A on event A, up
// count
EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR;          // Clear PWM1A on event A,
// down count

}

{
EALLOW;

{
}
{
}
#endif
DEVICE_DELAY_US(1000);
EDIS;
}

{
{

}
{
}
#endif

}

```