Wednesday, April 6, 2016

Contiki CC2650 ADC demo using DIO23 as ADC input

The following code shows how to use CC2650 DIO23 as ADC input to read analog signal in Contiki OS.

1. Replace the following code to hello-world.c

#include "contiki.h"
#include "ti-lib.h"
#include "driverlib/aux_adc.h"
#include "driverlib/aux_wuc.h"

#include /* For printf() */

#define CLOCK_SECOND 128

char buf[32];
static struct etimer et;

/*---------------------------------------------------------------------------*/
PROCESS(hello_world_process, "Hello world process");
AUTOSTART_PROCESSES(&hello_world_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(hello_world_process, ev, data)
{
  PROCESS_BEGIN();
    unsigned short i;
    uint16_t singleSample;

    etimer_set(&et, CLOCK_SECOND);
    printf("Contiki CC26xx ADC demo using DIO23 as ADC input\r\n");
    while(1)
    {
        if(ev == PROCESS_EVENT_TIMER && etimer_expired(&et)) {
        //intialisation of ADC
        ti_lib_aon_wuc_aux_wakeup_event(AONWUC_AUX_WAKEUP);
        while(!(ti_lib_aon_wuc_power_status_get() & AONWUC_AUX_POWER_ON))
        { }

        // Enable clock for ADC digital and analog interface (not currently enabled in driver)
        // Enable clocks
        ti_lib_aux_wuc_clock_enable(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK);
        while(ti_lib_aux_wuc_clock_status(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK) != AUX_WUC_CLOCK_READY)
        { }

        printf("clock selected\r\n");
       
        // Connect AUX IO7 (DIO23, but also DP2 on XDS110) as analog input.
        AUXADCSelectInput(ADC_COMPB_IN_AUXIO7); 
        printf("input selected\r\n");
       
        // Set up ADC range
        // AUXADC_REF_FIXED = nominally 4.3 V
        AUXADCEnableSync(AUXADC_REF_FIXED,  AUXADC_SAMPLE_TIME_2P7_US, AUXADC_TRIGGER_MANUAL);
        printf("init adc --- OK\r\n");

        //Trigger ADC converting
        AUXADCGenManualTrigger();
        printf("trigger --- OK\r\n");
       
        //reading adc value
        singleSample = AUXADCReadFifo();

        printf("%d mv on ADC\r\n",singleSample);
       
        //shut the adc down
        AUXADCDisable();
        printf("disable --- OK\r\n");

        }
        etimer_set(&et, CLOCK_SECOND);
        PROCESS_YIELD();
    }

  PROCESS_END();
}


2. Build by "make TARGET=srf06-cc26xx BOARD=srf06/cc26xx hello-world.bin" and download hello-world.bin using Flash Programmer 2 to CC2650 LaunchPad.

3. You can see ADC reading output on UART.


2 comments:

  1. Hi,

    Thanks for the tutorial! It saved me a lot of time.

    I have two questions:
    1) My board keeps restarting running this sample code. Usually after 4 successful reads. Why do you think this could be happening?
    2) Do we really need to initialize the ADC and shut it down everytime we read the analog port? Could it work if we create an ADC initialization method called before the while(1) loop and disable the ADC outside the while loop?

    ReplyDelete