This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

INCORRECT RESPONSE FROM ADCINA4 TO GPIO0

Other Parts Discussed in Thread: CONTROLSUITE

#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"

#define EPWM1_MAX_CMPA 4096
#define EPWM1_MIN_CMPA 0

void pwm_Init_();

unsigned int TBPRD = 4096;
unsigned int CMPA=0; //32500;

ADC_Handle myAdc;
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
TIMER_Handle myTimer;
CPU_Handle myCpu;
PLL_Handle myPll;
WDOG_Handle myWDog;
PWM_Handle myPwm1;
PWR_Handle myPwr;

uint16_t Digital_Result =0;

void globaldisable();
void globalenable();
void ADC_INIT_Fn();
void ADC_SETUP_Fn();
void set_duty(int a);

int adcresult=0;

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

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;
}


void main(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();

GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_EPWM1A);

//set_freq(10);
//set_dutycycle(75);

CLK_disableTbClockSync(myClk);
pwm_Init_();

//

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

}

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

void globalenable()
{
PIE_enable(myPie);
// Register interrupt handlers in the PIE vector table
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);

// Configure XINT1
PIE_setExtIntPolarity(myPie, CPU_ExtIntNumber_1, PIE_ExtIntPolarity_RisingEdge);

// Enable XINT1 and XINT2
PIE_enableExtInt(myPie, CPU_ExtIntNumber_1);
}

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

void ADC_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_EOC1); //setup EOC0 to trigger ADCINT1 to fire
ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A4); //set SOC0 channel select to ADCINA4
ADC_setSocChanNumber (myAdc, ADC_SocNumber_1, 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_setSocTrigSrc(myAdc, ADC_SocNumber_1, 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)
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_1, ADC_SocSampleWindow_7_cycles);
}

void pwm_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
// Set Compare values

//CMPA = (TBPRD/100)*adcresult;

// interrupt void adc_isr(void);

// CMPA = adcresult;

//PWM_setCmpA(myPwm1, CMPA); // Set compare A value

// set_duty();

// Setup counter mode
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
}
void set_duty( int a)
{
CMPA = a;
if(CMPA<(4096/2)&&CMPA>=0)
{
CMPA*=2;
PWM_setCmpA(myPwm1, CMPA); // Set compare A value
}
else if(CMPA>=0)
{
PWM_setCmpA(myPwm1, CMPA);
}

}

  • Hello,

    First of all, did you get correct result on adcresult or Digital_Result ?

    Best regards,

    Maria

  • Hi,

    actually i found a workaround, which is to send the same values digitally via scia, then do the pwm and set output to gpio0, 1, 2 etc. But now there is an another problem, which is, I manage to see the correct values from gpio0 and 1, but gpio2 never returns an output though I think I have initialized it correctly, following is the complete code for my workaround:

    #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/sci.h"
    #include "f2802x_common/include/wdog.h"
    
    interrupt void sciaRxIsr(void);
    void scia_init();
    void scia_xmit(int a);
    void scia_intpt_en();
    void send_msg();
    unsigned char msg[]="\nsend duty cycle:";
    
    void pwm_Init_();
    void set_duty(int x);
    unsigned int TBPRD = 65535; // Period register
    unsigned int CMPA=0; //32500;
    int duty = 50;
    int c=0;
    
    CLK_Handle myClk;
    GPIO_Handle myGpio;
    PIE_Handle myPie;
    SCI_Handle mySci;
    CPU_Handle myCpu;
    PLL_Handle myPll;
    WDOG_Handle myWDog;
    PWM_Handle myPwm1;
    PWM_Handle myPwm2;
    void main(void)
    {
    myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
    myCpu = CPU_init((void *)NULL, sizeof(CPU_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));
    mySci = SCI_init((void *)SCIA_BASE_ADDR, sizeof(SCI_Obj));
    myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));
    myPwm1 = PWM_init((void *)PWM_ePWM1_BASE_ADDR, sizeof(PWM_Obj));
    myPwm2 = PWM_init((void *)PWM_ePWM2_BASE_ADDR, sizeof(PWM_Obj));
    WDOG_disable(myWDog);
    CLK_setOscSrc(myClk, CLK_OscSrc_Internal);
    PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);
    
    // Initalize GPIO
    GPIO_setPullUp(myGpio, GPIO_Number_28, GPIO_PullUp_Enable);
    GPIO_setPullUp(myGpio, GPIO_Number_29, GPIO_PullUp_Disable);
    GPIO_setQualification(myGpio, GPIO_Number_28, GPIO_Qual_ASync);
    GPIO_setMode(myGpio, GPIO_Number_28, GPIO_28_Mode_SCIRXDA);
    GPIO_setMode(myGpio, GPIO_Number_29, GPIO_29_Mode_SCITXDA);
    
    scia_intpt_en();
    scia_init();
    send_msg();
    // GPIO_setPullUp(myGpio, GPIO_Number_0, GPIO_PullUp_Disable);
    // GPIO_setPullUp(myGpio, GPIO_Number_1, GPIO_PullUp_Disable);
    // GPIO_setPullUp(myGpio, GPIO_Number_2, GPIO_PullUp_Disable);
    GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_EPWM1A);
    GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_EPWM1B);
    GPIO_setMode(myGpio, GPIO_Number_2, GPIO_2_Mode_EPWM2A);
    CLK_disableTbClockSync(myClk);
    pwm_Init_();
    CLK_enableTbClockSync(myClk);
    
    while(1);
    }
    
    //-------------------------- UART -----------------------------------
    interrupt void sciaRxIsr(void)
    {
    
    duty = SCI_getData(mySci);
    set_duty(c);
    if(c==2)
    c=0;
    else c++;
    PIE_clearInt(myPie, PIE_GroupNumber_9);
    }
    
    void scia_init()
    {
    CLK_enableSciaClock(myClk);
    SCI_disableParity(mySci);
    SCI_setNumStopBits(mySci, SCI_NumStopBits_One);
    SCI_setCharLength(mySci, SCI_CharLength_8_Bits);
    SCI_enableRx(mySci);
    SCI_enableTx(mySci);
    SCI_enableRxInt(mySci);
    SCI_setBaudRate(mySci, SCI_BaudRate_9_6_kBaud);
    SCI_enable(mySci);
    }
    
    void scia_intpt_en()
    {
    PIE_enable(myPie);
    PIE_registerPieIntHandler(myPie, PIE_GroupNumber_9, PIE_SubGroupNumber_1, (intVec_t)&sciaRxIsr);
    PIE_enableInt(myPie, PIE_GroupNumber_9, PIE_InterruptSource_SCIARX);
    CPU_enableInt(myCpu, CPU_IntNumber_9);
    CPU_enableGlobalInts(myCpu); // Enable Global Interrupts
    }
    
    void scia_xmit(int a)
    {
    while(SCI_getTxFifoStatus(mySci) != SCI_FifoStatus_Empty); //while (SciaRegs.SCIFFTX.bit.TXFFST != 0) {}
    SCI_putDataBlocking(mySci, a); // SciaRegs.SCITXBUF=a;
    }
    
    void send_msg()
    {
    int i=0;
    while(msg[i]!='\0')
    {
    scia_xmit(msg[i]);
    i++;
    }
    }
    //--------------------------------- PWM ------------------------------------
    
    void pwm_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_setPeriod(myPwm2, TBPRD); // Set timer period 801 TBCLKs
    PWM_setPhase(myPwm2, 0x0000); // Phase is 0
    PWM_setCount(myPwm2, 0x0000); // Clear counter
    // Set Compare values for duty cycle
    // set_duty();
    // Setup counter mode
    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_4);
    PWM_setCounterMode(myPwm2, PWM_CounterMode_UpDown); // Count up and down
    PWM_disableCounterLoad(myPwm2); // Disable phase loading
    PWM_setHighSpeedClkDiv(myPwm2, PWM_HspClkDiv_by_10); // Clock ratio to SYSCLKOUT
    PWM_setClkDiv(myPwm2, PWM_ClkDiv_by_4);
    // Setup shadowing
    PWM_setShadowMode_CmpA(myPwm1, PWM_ShadowMode_Shadow);
    PWM_setLoadMode_CmpA(myPwm1, PWM_LoadMode_Zero);
    PWM_setShadowMode_CmpB(myPwm1, PWM_ShadowMode_Shadow);
    PWM_setLoadMode_CmpB(myPwm1, PWM_LoadMode_Zero);
    PWM_setShadowMode_CmpA(myPwm2, PWM_ShadowMode_Shadow);
    PWM_setLoadMode_CmpA(myPwm2, PWM_LoadMode_Zero);
    PWM_setShadowMode_CmpB(myPwm2, PWM_ShadowMode_Shadow);
    PWM_setLoadMode_CmpB(myPwm2, 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
    PWM_setActionQual_CntUp_CmpB_PwmB(myPwm1, PWM_ActionQual_Clear); // Set PWM1A on event A, up count
    PWM_setActionQual_CntDown_CmpB_PwmB(myPwm1, PWM_ActionQual_Set); // Clear PWM1A on event A, down count
    PWM_setActionQual_CntUp_CmpA_PwmA(myPwm2, PWM_ActionQual_Clear); // Set PWM1A on event A, up count
    PWM_setActionQual_CntDown_CmpA_PwmA(myPwm2, PWM_ActionQual_Set); // Clear PWM1A on event A, down count
    // PWM_setActionQual_CntUp_CmpB_PwmB(myPwm2, PWM_ActionQual_Clear); // Set PWM1A on event A, up count
    // PWM_setActionQual_CntDown_CmpB_PwmB(myPwm2, PWM_ActionQual_Set); // Clear PWM1A on event A, down count
    }
    // Some useful Period vs Frequency values
    // SYSCLKOUT = 60 MHz 40 MHz
    // --------------------------------------
    // Period Frequency Frequency
    // 1000 60 kHz 40 kHz
    // 800 75 kHz 50 kHz
    // 600 100 kHz 67 kHz
    // 500 120 kHz 80 kHz
    // 250 240 kHz 160 kHz
    // 200 300 kHz 200 kHz
    // 100 600 kHz 400 kHz
    // 50 1.2 Mhz 800 kHz
    // 25 2.4 Mhz 1.6 MHz
    // 20 3.0 Mhz 2.0 MHz
    // 12 5.0 MHz 3.3 MHz
    // 10 6.0 MHz 4.0 MHz
    // 9 6.7 MHz 4.4 MHz
    // 8 7.5 MHz 5.0 MHz
    // 7 8.6 MHz 5.7 MHz
    // 6 10.0 MHz 6.6 MHz
    // 5 12.0 MHz 8.0 MHz
    
    void set_duty(int x)
    {
    
    CMPA = (duty*256);
    if(x==0)
    {
    PWM_setCmpA(myPwm1, CMPA);
    }// Set compare A value
    else if(x==1)
    {
    PWM_setCmpB(myPwm1, CMPA);
    }
    else if(x==2)
    {
    PWM_setCmpA(myPwm2, CMPA);
    }
    }

    P.S. I have also checked the EPWM's are fine along with their GPIO output using the f2802x_examples_ccsv4/epwm_up_aq/Example_F2802xEPwm3UpAQ.c program found in controlSUITE. Can you help me to correct my error?

  • Hello,

    I am not sure you have put it or not, did you already put this line?

    CLK_enablePwmClock(myClk, PWM_Number_2);
    Best regards,
    Maria
  • Thank you Maria, I forgot to enable the clock for epwm2!! 
    One thing that comes to my mind is, whether one can use the example as a Power DAC example, what do you think?

  • Debasish Banerjee1 said:
    whether one can use the example as a Power DAC example

    Which example that you mean?

    That's great that your problem is solved. Good luck!

    Best regards,

    Maria