Sie sind auf Seite 1von 16

GUGULOTH ANIL

192PS012

ASSIGNMENT
Write a Code for the below signal access through ADC port and reproduce through PWM
channel.
v(t) = 1.65Sin 314t

START

Initialize all the predefined Handles and required variables.

Set TBPRD=4096,PLL frequency=50MHz, ADC_result=0

Initialize and setup ADC configurationparameters, Select VOLTAGE


REFERENCE as internal voltage. Configure SOC0 to be used for ADCINA4 and
ADCINT1 to generate interrupt when EOC0 occurs and also as trigger source
of SOC0.Configure PIE, enable ADCINT1 in PIE. Configure GPIO as EPWM
output MODE

Start conversion using ADC force Conversion i.e., WAIT for ADC
interrupt(EOC0==1)

Digital result is stored in ADC_result register


DUTY ratio=ADC_result

CMPA=DUTY ratio

duringUP count , At TBCTR==CMPA , CLEAR EPWM1A


during DOWN count, At TBCTR==CMPA , Set EPWM1A

Clear interrupt flags


1.Set TBPRD=4096 since the range of digital result is going to be 0-4096.

2.Declare all the handles and point them to their corresponding addresses.

3.Basic system initialization steps including selection of system frequency (PLL), setting ADC clock etc.

4.Setup ADC using functions.

5.Initialize PWM and set GPIO 0 for ePWM.

6.Start conversion from AC port after the occurrence of interrupt using the function adc_isr().

7.The digital result is used to set duty ratio using the function set_duty().

8.Once conversion is done clear all the interrupt flags.

9.Repeat from step 6 as infinite loop until entire signal at ad is sampled and converted and produced
through PWM port.

TABLE

time x in degree x in radians 1.65*sinx

0 0 0 0

0.0001 1.8 0.031415927 0.051827752

0.0002 3.6 0.062831853 0.103604357

0.0003 5.4 0.09424778 0.155278717

0.0004 7.2 0.125663706 0.206799835

0.0005 9 0.157079633 0.258116867

0.0006 10.8 0.188495559 0.309179169


0.0007 12.6 0.219911486 0.359936348

0.0008 14.4 0.251327412 0.410338314

0.0009 16.2 0.282743339 0.460335325

0.001 18 0.314159265 0.509878041

0.0011 19.8 0.345575192 0.558917568

0.0012 21.6 0.376991118 0.607405512

0.0013 23.4 0.408407045 0.65529402

0.0014 25.2 0.439822972 0.702535831

0.0015 27 0.471238898 0.749084325

0.0016 28.8 0.502654825 0.794893562

0.0017 30.6 0.534070751 0.839918336

0.0018 32.4 0.565486678 0.884114212

0.0019 34.2 0.596902604 0.927437573

0.002 36 0.628318531 0.969845666

0.0021 37.8 0.659734457 1.011296639

0.0022 39.6 0.691150384 1.051749583

0.0023 41.4 0.72256631 1.091164578

0.0024 43.2 0.753982237 1.129502725

0.0025 45 0.785398163 1.166726189

0.0026 46.8 0.81681409 1.202798235

0.0027 48.6 0.848230016 1.237683265

0.0028 50.4 0.879645943 1.271346851


0.0029 52.2 0.91106187 1.30375577

0.003 54 0.942477796 1.334878041

0.0031 55.8 0.973893723 1.364682948

0.0032 57.6 1.005309649 1.393141077

0.0033 59.4 1.036725576 1.420224345

0.0034 61.2 1.068141502 1.445906022

0.0035 63 1.099557429 1.470160765

0.0036 64.8 1.130973355 1.492964637

0.0037 66.6 1.162389282 1.514295132

0.0038 68.4 1.193805208 1.534131202

0.0039 70.2 1.225221135 1.552453269

0.004 72 1.256637061 1.569243252

0.0041 73.8 1.288052988 1.584484581

0.0042 75.6 1.319468915 1.598162216

0.0043 77.4 1.350884841 1.610262657

0.0044 79.2 1.382300768 1.620773964

0.0045 81 1.413716694 1.629685762

0.0046 82.8 1.445132621 1.636989257

0.0047 84.6 1.476548547 1.642677242

0.0048 86.4 1.507964474 1.646744102

0.0049 88.2 1.5393804 1.649185825

0.005 90 1.570796327 1.65


0.0051 91.8 1.602212253 1.649185825

0.0052 93.6 1.63362818 1.646744102

0.0053 95.4 1.665044106 1.642677242

0.0054 97.2 1.696460033 1.636989257

0.0055 99 1.727875959 1.629685762

0.0056 100.8 1.759291886 1.620773964

0.0057 102.6 1.790707813 1.610262657

0.0058 104.4 1.822123739 1.598162216

0.0059 106.2 1.853539666 1.584484581

0.006 108 1.884955592 1.569243252

0.0061 109.8 1.916371519 1.552453269

0.0062 111.6 1.947787445 1.534131202

0.0063 113.4 1.979203372 1.514295132

0.0064 115.2 2.010619298 1.492964637

0.0065 117 2.042035225 1.470160765

0.0066 118.8 2.073451151 1.445906022

0.0067 120.6 2.104867078 1.420224345

0.0068 122.4 2.136283004 1.393141077

0.0069 124.2 2.167698931 1.364682948

0.007 126 2.199114858 1.334878041

0.0071 127.8 2.230530784 1.30375577

0.0072 129.6 2.261946711 1.271346851


0.0073 131.4 2.293362637 1.237683265

0.0074 133.2 2.324778564 1.202798235

0.0075 135 2.35619449 1.166726189

0.0076 136.8 2.387610417 1.129502725

0.0077 138.6 2.419026343 1.091164578

0.0078 140.4 2.45044227 1.051749583

0.0079 142.2 2.481858196 1.011296639

0.008 144 2.513274123 0.969845666

0.0081 145.8 2.544690049 0.927437573

0.0082 147.6 2.576105976 0.884114212

0.0083 149.4 2.607521902 0.839918336

0.0084 151.2 2.638937829 0.794893562

0.0085 153 2.670353756 0.749084325

0.0086 154.8 2.701769682 0.702535831

0.0087 156.6 2.733185609 0.65529402

0.0088 158.4 2.764601535 0.607405512

0.0089 160.2 2.796017462 0.558917568

0.009 162 2.827433388 0.509878041

0.0091 163.8 2.858849315 0.460335325

0.0092 165.6 2.890265241 0.410338314

0.0093 167.4 2.921681168 0.359936348

0.0094 169.2 2.953097094 0.309179169


0.0095 171 2.984513021 0.258116867

0.0096 172.8 3.015928947 0.206799835

0.0097 174.6 3.047344874 0.155278717

0.0098 176.4 3.078760801 0.103604357

0.0099 178.2 3.110176727 0.051827752

0.01 180 3.141592654 2.40039E-15

0.0101 181.8 3.17300858 -0.051827752

0.0102 183.6 3.204424507 -0.103604357

0.0103 185.4 3.235840433 -0.155278717

0.0104 187.2 3.26725636 -0.206799835

0.0105 189 3.298672286 -0.258116867

0.0106 190.8 3.330088213 -0.309179169

0.0107 192.6 3.361504139 -0.359936348

0.0108 194.4 3.392920066 -0.410338314

0.0109 196.2 3.424335992 -0.460335325

0.011 198 3.455751919 -0.509878041

0.0111 199.8 3.487167845 -0.558917568

0.0112 201.6 3.518583772 -0.607405512

0.0113 203.4 3.549999699 -0.65529402

0.0114 205.2 3.581415625 -0.702535831

0.0115 207 3.612831552 -0.749084325

0.0116 208.8 3.644247478 -0.794893562


0.0117 210.6 3.675663405 -0.839918336

0.0118 212.4 3.707079331 -0.884114212

0.0119 214.2 3.738495258 -0.927437573

0.012 216 3.769911184 -0.969845666

0.0121 217.8 3.801327111 -1.011296639

0.0122 219.6 3.832743037 -1.051749583

0.0123 221.4 3.864158964 -1.091164578

0.0124 223.2 3.89557489 -1.129502725

0.0125 225 3.926990817 -1.166726189

0.0126 226.8 3.958406744 -1.202798235

0.0127 228.6 3.98982267 -1.237683265

0.0128 230.4 4.021238597 -1.271346851

0.0129 232.2 4.052654523 -1.30375577

0.013 234 4.08407045 -1.334878041

0.0131 235.8 4.115486376 -1.364682948

0.0132 237.6 4.146902303 -1.393141077

0.0133 239.4 4.178318229 -1.420224345

0.0134 241.2 4.209734156 -1.445906022

0.0135 243 4.241150082 -1.470160765

0.0136 244.8 4.272566009 -1.492964637

0.0137 246.6 4.303981935 -1.514295132

0.0138 248.4 4.335397862 -1.534131202


0.0139 250.2 4.366813788 -1.552453269

0.014 252 4.398229715 -1.569243252

0.0141 253.8 4.429645642 -1.584484581

0.0142 255.6 4.461061568 -1.598162216

0.0143 257.4 4.492477495 -1.610262657

0.0144 259.2 4.523893421 -1.620773964

0.0145 261 4.555309348 -1.629685762

0.0146 262.8 4.586725274 -1.636989257

0.0147 264.6 4.618141201 -1.642677242

0.0148 266.4 4.649557127 -1.646744102

0.0149 268.2 4.680973054 -1.649185825

0.015 270 4.71238898 -1.65

0.0151 271.8 4.743804907 -1.649185825

0.0152 273.6 4.775220833 -1.646744102

0.0153 275.4 4.80663676 -1.642677242

0.0154 277.2 4.838052687 -1.636989257

0.0155 279 4.869468613 -1.629685762

0.0156 280.8 4.90088454 -1.620773964

0.0157 282.6 4.932300466 -1.610262657

0.0158 284.4 4.963716393 -1.598162216

0.0159 286.2 4.995132319 -1.584484581

0.016 288 5.026548246 -1.569243252


0.0161 289.8 5.057964172 -1.552453269

0.0162 291.6 5.089380099 -1.534131202

0.0163 293.4 5.120796025 -1.514295132

0.0164 295.2 5.152211952 -1.492964637

0.0165 297 5.183627878 -1.470160765

0.0166 298.8 5.215043805 -1.445906022

0.0167 300.6 5.246459731 -1.420224345

0.0168 302.4 5.277875658 -1.393141077

0.0169 304.2 5.309291585 -1.364682948

0.017 306 5.340707511 -1.334878041

0.0171 307.8 5.372123438 -1.30375577

0.0172 309.6 5.403539364 -1.271346851

0.0173 311.4 5.434955291 -1.237683265

0.0174 313.2 5.466371217 -1.202798235

0.0175 315 5.497787144 -1.166726189

0.0176 316.8 5.52920307 -1.129502725

0.0177 318.6 5.560618997 -1.091164578

0.0178 320.4 5.592034923 -1.051749583

0.0179 322.2 5.62345085 -1.011296639

0.018 324 5.654866776 -0.969845666

0.0181 325.8 5.686282703 -0.927437573

0.0182 327.6 5.71769863 -0.884114212


0.0183 329.4 5.749114556 -0.839918336

0.0184 331.2 5.780530483 -0.794893562

0.0185 333 5.811946409 -0.749084325

0.0186 334.8 5.843362336 -0.702535831

0.0187 336.6 5.874778262 -0.65529402

0.0188 338.4 5.906194189 -0.607405512

0.0189 340.2 5.937610115 -0.558917568

0.019 342 5.969026042 -0.509878041

0.0191 343.8 6.000441968 -0.460335325

0.0192 345.6 6.031857895 -0.410338314

0.0193 347.4 6.063273821 -0.359936348

0.0194 349.2 6.094689748 -0.309179169

0.0195 351 6.126105675 -0.258116867

0.0196 352.8 6.157521601 -0.206799835

0.0197 354.6 6.188937528 -0.155278717

0.0198 356.4 6.220353454 -0.103604357

0.0199 358.2 6.251769381 -0.051827752

0.02 360 6.283185307 -3.41107E-14


GRAPH :

1.5

0.5
time
0
1.65*sinx
0 50 100 150 200 250
-0.5

-1

-1.5

-2

CODE:

#include"DSP28x_Project.h"// Device Headerfile and Examples Include File


#include"f2802x_common/include/adc.h"
#include"f2802x_common/include/clk.h"
#include"f2802x_common/include/flash.h"
#include"f2802x_common/include/gpio.h"
#include"f2802x_common/include/pie.h"
#include"f2802x_common/include/pll.h"
#include"f2802x_common/include/timer.h"
#include"f2802x_common/include/wdog.h"
#include"f2802x_common/include/pwr.h"
#include"f2802x_common/include/pwm.h"

voidpwm_Init_();

unsignedint TBPRD = 4096;


unsignedint CMPA=0; //32500;

ADC_HandlemyAdc;
CLK_HandlemyClk;
FLASH_HandlemyFlash;
GPIO_HandlemyGpio;
PIE_HandlemyPie;
TIMER_HandlemyTimer;
CPU_HandlemyCpu;
PLL_HandlemyPll;
WDOG_HandlemyWDog;
PWM_Handle myPwm1;
PWR_HandlemyPwr;

uint16_tDigital_Result =0;

voidglobaldisable();
voidglobalenable();
voidADC_INIT_Fn();
voidADC_SETUP_Fn();
voidset_duty(int a);

interruptvoidadc_isr(void)
{
//discard ADCRESULT0 as part of the workaround to the 1st sample errata for rev0
Digital_Result = ADC_readResult(myAdc, ADC_ResultNumber_0);

adcresult =Digital_Result;

set_duty(adcresult);
// pwm_Init_();

ADC_clearIntFlag(myAdc, ADC_IntNumber_1); // Clear ADCINT1 flag reinitialize for


next SOC
PIE_clearInt(myPie, PIE_GroupNumber_10);// Acknowledge interrupt to PIE
return;
}

voidmain(void)
{
myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));
myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
myCpu = CPU_init((void *)NULL, sizeof(CPU_Obj));
myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));
myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));
myPie = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));
myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
myTimer = TIMER_init((void *)TIMER0_BASE_ADDR, sizeof(TIMER_Obj));
myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
myPwm1 = PWM_init((void *)PWM_ePWM1_BASE_ADDR, sizeof(PWM_Obj));
myPwr = PWR_init((void *)PWR_BASE_ADDR, sizeof(PWR_Obj));

globaldisable();
// Perform basic system initialization
WDOG_disable(myWDog);
CLK_enableAdcClock(myClk);
CLK_setOscSrc(myClk, CLK_OscSrc_Internal); //Select the internal oscillator 1 as the
clock source
PLL_setup(myPll, PLL_Multiplier_10, PLL_DivideSelect_ClkIn_by_2); // Setup the PLL
for x10 /2 which will yield 50Mhz = 10Mhz * 10 / 2

globalenable();
ADC_INIT_Fn();
ADC_SETUP_Fn();
//set_freq(10);
//set_dutycycle(75);

CLK_disableTbClockSync(myClk);
pwm_Init_();
// ADC_SocSampleWindow_eADC_getSocSampleWindow(myAdc,ADC_SocSampleWindow_40_cycles);

CLK_enableTbClockSync(myClk);
while(1)
{
ADC_forceConversion(myAdc,ADC_SocNumber_0);// Wait for ADC interrupt

voidgpio_init()
{
// Initialize GPIO for the EPWM1A
GPIO_setPullUp(myGpio, GPIO_Number_0, GPIO_PullUp_Disable);

GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_EPWM1A);

GPIO_setDirection(myGpio,GPIO_Number_0,GPIO_Direction_Output);

voidglobaldisable()
{
// Disable the PIE and all interrupts
PIE_disable(myPie);
PIE_disableAllInts(myPie);
CPU_disableGlobalInts(myCpu);
CPU_clearIntFlags(myCpu);
}

voidglobalenable()
{
// Register interrupt handlers in the PIE vector table
PIE_enable(myPie);
// Configure XINT1
PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_1, PIE_ExtIntPolarity_RisingEdge);

// Enable XINT1 and XINT2


PIE_enableExtInt(myPie, CPU_ExtIntNumber_1);
CPU_enableInt(myCpu, CPU_IntNumber_10); // Enable CPU Interrupt 1
CPU_enableGlobalInts(myCpu); // Enable Global interrupt INTM
CPU_enableDebugInt(myCpu); // Enable Global realtime interrupt DBGM

// Enable XINT1 in the PIE: Group 1 interrupt 4 & 5


// Enable INT1 which is connected to WAKEINT
PIE_enableInt(myPie, PIE_GroupNumber_1, PIE_InterruptSource_XINT_1);
CPU_enableInt(myCpu, CPU_IntNumber_1);

// GPIO0 is XINT1, GPIO1 is XINT2


GPIO_setExtInt(myGpio, GPIO_Number_12, CPU_ExtIntNumber_1);

voidADC_INIT_Fn()
{
ADC_enableBandGap(myAdc);
ADC_enableRefBuffers(myAdc);
ADC_powerUp(myAdc);
ADC_enable(myAdc);
ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);
}

voidADC_SETUP_Fn()
{
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_10, PIE_SubGroupNumber_1,
(intVec_t)&adc_isr);
PIE_enableAdcInt(myPie, ADC_IntNumber_1); // Enable ADCINT1 in PIE

//Note: Channel ADCINA1 will be double sampled to workaround the ADC 1st sample
issue for rev0 silicon errata
ADC_setIntPulseGenMode(myAdc, ADC_IntPulseGenMode_Prior); //ADCINT1
trips after AdcResults latch
ADC_enableInt(myAdc, ADC_IntNumber_1); //Enabled
ADCINT1
ADC_setIntMode(myAdc, ADC_IntNumber_1, ADC_IntMode_ClearFlag); //Disable
ADCINT1 Continuous mode
ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC0); //setup EOC0
to trigger ADCINT1 to fire
ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A4); //set SOC0
channel select to ADCINA4
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_0, ADC_SocTrigSrc_Sw); //set SOC0 start
trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles); //set
SOC0 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
}

voidpwm_Init_()
{
CLK_enablePwmClock(myClk, PWM_Number_1);
// Setup TBCLK
PWM_setPeriod(myPwm1, TBPRD); // Set timer period 801 TBCLKs
// PWM_setPhase(myPwm1, 0x0000); // Phase is 0
PWM_setCount(myPwm1, 0x0000); // Clear counter
PWM_setCounterMode(myPwm1, PWM_CounterMode_UpDown); // Count up and down
// PWM_disableCounterLoad(myPwm1); // Disable phase loading
PWM_setHighSpeedClkDiv(myPwm1, PWM_HspClkDiv_by_10); // Clock ratio to SYSCLKOUT
PWM_setClkDiv(myPwm1, PWM_ClkDiv_by_1);
// Setup shadowing
PWM_setShadowMode_CmpA(myPwm1, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm1, PWM_LoadMode_Zero);
// Set actions
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm1, PWM_ActionQual_Clear); // Set PWM1A on
event A, up count
PWM_setActionQual_CntDown_CmpA_PwmA(myPwm1, PWM_ActionQual_Set); // Clear PWM1A on
event A, down count
}
voidset_duty( int a)
{
CMPA = a;
PWM_setCmpA(myPwm1, CMPA); // Set compare A value
}

To get sinusoidal waveform from the PWM pin output PWM pulse, connect the PWM pin
as input to the RC filter.

Das könnte Ihnen auch gefallen