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.

ADC problem

Other Parts Discussed in Thread: CONTROLSUITE

Hello All,

I need to use a IR sensor to detect an object and output a signal to the switch with a time(let say 1ms). I have no programming experience before so I don't know to write the sentence to call the ADC result. Can someone help me?Thank you so much!

#include "DSP28x_Project.h" // DSP28x Headerfile
#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/wdog.h"
#include "f2802x_common/include/sci.h"
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
ADC_Handle myAdc;
int16_t temp; //raw ADC Result Data
void main() {

ADC_Handle myAdc;
CPU_Handle myCpu;
PLL_Handle myPll;
WDOG_Handle myWDog;

// Initialize all the handles needed for this application
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));
myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));

// Perform basic system initialization
WDOG_disable(myWDog);
CLK_enableAdcClock(myClk);
(*Device_cal)();

//Select the internal oscillator 1 as the clock source
CLK_setOscSrc(myClk, CLK_OscSrc_Internal);

// Setup the PLL for x12 /2 which will yield 60Mhz = 10Mhz * 12 / 2
PLL_setup(myPll, PLL_Multiplier_12, PLL_DivideSelect_ClkIn_by_2);

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

// If running from flash copy RAM only functions to RAM
#ifdef _FLASH
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif
//DELAY_US(1000000);
// PIE_setDebugIntVectorTable(myPie);
PIE_enable(myPie);

// Initialize the ADC
ADC_enableBandGap(myAdc);
ADC_enableRefBuffers(myAdc);
ADC_powerUp(myAdc);
ADC_enable(myAdc);
ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);
ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A4); //Set SOC0 channel select to ADCINA5
ADC_setSocChanNumber (myAdc, ADC_SocNumber_1, ADC_SocChanNumber_A4); //Set SOC1 channel select to ADCINA5
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles); //Set SOC0 acquisition period to 7 ADCCLK
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_1, ADC_SocSampleWindow_7_cycles); //Set SOC1 acquisition period to 7 ADCCLK
ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC1); //Connect ADCINT1 to EOC1
ADC_enableInt(myAdc, ADC_IntNumber_1); //Enable ADCINT1
FLASH_setup(myFlash);
GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_2_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_3, GPIO_3_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_3, GPIO_Direction_Output);
GPIO_setHigh(myGpio, GPIO_Number_0);
GPIO_setHighmyGpio, GPIO_Number_1);
GPIO_setHigh(myGpio, GPIO_Number_2);
GPIO_setHigh(myGpio, GPIO_Number_3);


while (1)
{

??????????????How to call ADC result ??????????????


GPIO_setHigh(myGpio, GPIO_Number_0);
DELAY_US(30000);
GPIO_setLow(myGpio, GPIO_Number_3);
DELAY_US(30000);

}


}

  • Hi Lok Bun Tong,

    Break your application down and get the individual parts working (and understood) first.  

    As far as the ADC, start with the ADC examples provided in ControlSUITE.  It is important not only that you are able to get these to work, but that you thoroughly understand how they work.  Once you understand these examples, pick one example, copy it, and begin to gradually change the example to more closely meet your end application.  

  • Hello Lok Bun Tong,

    As additional from what Devin said above, the simple ADC example can be found in C:\ti\controlSUITE\device_support\f2802x\v220\f2802x_examples_drivers\adc_soc

    There you can find how to call ADC result.

    Best regards,

    Maria

  • I have try my best to learn and understand  but still not working, Can someone help me? Thanks.

    #include "DSP28x_Project.h" // DSP28x Headerfile
    #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/wdog.h"
    #include "f2802x_common/include/sci.h"

    #define CONV_WAIT 1L //Micro-seconds to wait for ADC conversion. Longer than necessary.


    CLK_Handle myClk;
    FLASH_Handle myFlash;
    GPIO_Handle myGpio;
    PIE_Handle myPie;

    void main()
    {

    ADC_Handle myAdc;
    CPU_Handle myCpu;
    PLL_Handle myPll;
    WDOG_Handle myWDog;

    // Initialize all the handles needed for this application
    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));
    myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));

    // Perform basic system initialization
    WDOG_disable(myWDog);
    CLK_enableAdcClock(myClk);
    (*Device_cal)();

    //Select the internal oscillator 1 as the clock source
    CLK_setOscSrc(myClk, CLK_OscSrc_Internal);

    // Setup the PLL for x10 /2 which will yield 50Mhz = 10Mhz * 10 / 2
    PLL_setup(myPll, PLL_Multiplier_10, PLL_DivideSelect_ClkIn_by_2);

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

    // If running from flash copy RAM only functions to RAM
    #ifdef _FLASH
    memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
    #endif

    // Initalize GPIO
    // Enable XCLOCKOUT to allow monitoring of oscillator 1
    GPIO_setMode(myGpio, GPIO_Number_18, GPIO_18_Mode_XCLKOUT);
    CLK_setClkOutPreScaler(myClk, CLK_ClkOutPreScaler_SysClkOut_by_1);


    // Setup a debug vector table and enable the PIE
    //PIE_setDebugIntVectorTable(myPie);
    PIE_enable(myPie);

    // Initialize the ADC
    ADC_enableBandGap(myAdc);
    ADC_enableRefBuffers(myAdc);
    ADC_powerUp(myAdc);
    ADC_enable(myAdc);
    ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);

    ADC_enableTempSensor(myAdc); //Connect channel A5 internally to the temperature sensor
    ADC_setSocChanNumber (myAdc, ADC_SocNumber_0, ADC_SocChanNumber_A4); //Set SOC0 channel select to ADCINA5
    ADC_setSocChanNumber (myAdc, ADC_SocNumber_1, ADC_SocChanNumber_A4); //Set SOC1 channel select to ADCINA5
    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_0, ADC_SocSampleWindow_7_cycles); //Set SOC0 acquisition period to 7 ADCCLK
    ADC_setSocSampleWindow(myAdc, ADC_SocNumber_1, ADC_SocSampleWindow_7_cycles); //Set SOC1 acquisition period to 7 ADCCLK
    ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC1); //Connect ADCINT1 to EOC1
    ADC_enableInt(myAdc, ADC_IntNumber_1); //Enable ADCINT1

    // Configure GPIO 0-3 as outputs
    GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_GeneralPurpose);
    GPIO_setMode(myGpio, GPIO_Number_1, GPIO_0_Mode_GeneralPurpose);
    GPIO_setMode(myGpio, GPIO_Number_2, GPIO_0_Mode_GeneralPurpose);
    GPIO_setMode(myGpio, GPIO_Number_3, GPIO_0_Mode_GeneralPurpose);

    GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);
    GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
    GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
    GPIO_setDirection(myGpio, GPIO_Number_3, GPIO_Direction_Output);

    GPIO_setLow(myGpio, GPIO_Number_0);
    GPIO_setLow(myGpio, GPIO_Number_1);
    GPIO_setLow(myGpio, GPIO_Number_2);
    GPIO_setLow(myGpio, GPIO_Number_3);

    // Note: two channels have been connected to the temp sensor
    // so that the first sample can be discarded to avoid the
    // ADC first sample issue. See the device errata.

    // Set the flash OTP wait-states to minimum. This is important
    // for the performance of the temperature conversion function.
    FLASH_setup(myFlash);


    while (1)
    {
    ADC_forceConversion(myAdc, ADC_SocNumber_0);
    ADC_forceConversion(myAdc, ADC_SocNumber_1);

    //Wait for end of conversion.
    while(ADC_getIntStatus(myAdc, ADC_IntNumber_1) == 0) {
    }
    if (ADC_readResult(myAdc, ADC_ResultNumber_1) > 1) ;
    GPIO_setHigh(myGpio, GPIO_Number_1);
    DELAY_US(50000);
    // Clear ADCINT1
    ADC_clearIntFlag(myAdc, ADC_IntNumber_1);
    }


    }

  • Hi Lok Bun Tong,

    You need to be more specific about what is and isn't working.  

    What tests have you done to determine which parts of the code are working? Do you get expected ADC results but turning this into switch controls doesn't work? Or can you control the switch, but you can't get the ADC to work? Or is something else wrong with the code?

  • Hello,

    Actually,  I  connected a changeable resistor to the board and set the LED (GPIO0) as output, but even I tune the resistor or disconnect it, the LED still not have any change. What I want to do is when the board received a signal from  adcint1, then turn on the led. If no signal input, then the LED keep turn off. Thanks a lot. 

  • Maybe this is the problem:

    Lok Bun Tong said:
    if (ADC_readResult(myAdc, ADC_ResultNumber_1) > 1) ;
    GPIO_setHigh(myGpio, GPIO_Number_1);

    It should be:

    if (ADC_readResult(myAdc, ADC_ResultNumber_1) > 1)

    {
      GPIO_setHigh(myGpio, GPIO_Number_1);

    }

    else

    {

    ...

    }

    But however, the ADC value will be fluctuated when you don't connect it to anything so these lines of code also will not work based on your purpose (when you connect-disconnect different resistors to turn on-off the LED) because no signal on ADC will still give some ADC value that usually more than 1.

    You should tie the ADC to GND to get 0 value (<= than 1) like you wrote in your code, or else the value will always be likely bigger than 1 (it means the LED will always ON).

    Or if you use potentiometer to test your ADC code, check the minimum ADC value (it is when the potentiometer in lowest value), and use it to turn ON-OFF the LED.

    Good luck!

    Best regards,

    Maria