CC264X/CC13x0 ADC

来源:互联网 发布:matlab绘制网络拓扑图 编辑:程序博客网 时间:2024/06/05 21:13

参考程序:
/* * Copyright (c) 2016, Texas Instruments Incorporated * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * *  Redistributions of source code must retain the above copyright *    notice, this list of conditions and the following disclaimer. * * *  Redistributions in binary form must reproduce the above copyright *    notice, this list of conditions and the following disclaimer in the *    documentation and/or other materials provided with the distribution. * * *  Neither the name of Texas Instruments Incorporated nor the names of *    its contributors may be used to endorse or promote products derived *    from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* *  ======== adcsinglechannel.c ======== */#include <stdint.h>#include <stddef.h>/* POSIX Header files */#include <pthread.h>/* Driver Header files */#include <ti/drivers/ADC.h>#if defined(CC2650DK_7ID) || defined(CC1310DK_7XD)#include <ti/drivers/PIN.h>#endif#include <ti/display/Display.h>/* Example/Board Header files */#include "Board.h"/* ADC sample count */#define ADC_SAMPLE_COUNT  (10)#define THREADSTACKSIZE   (768)/* ADC conversion result variables */uint16_t adcValue0;uint16_t adcValue1[ADC_SAMPLE_COUNT];static Display_Handle display;xdc_runtime_SysCallback_defaultAbort/* *  ======== threadFxn0 ======== *  Open an ADC instance and get a sampling result from a one-shot conversion. */void *threadFxn0(void *arg0){    ADC_Handle   adc;    ADC_Params   params;    int_fast16_t res;    ADC_Params_init(¶ms);    adc = ADC_open(Board_ADC0, ¶ms);    if (adc == NULL) {        Display_printf(display, 0, 0, "Error initializing ADC channel 0\n");        while (1);    }    else {        Display_printf(display, 0, 0, "ADC channel 0 initialized\n");    }    /* Blocking mode conversion */    res = ADC_convert(adc, &adcValue0);    if (res == ADC_STATUS_SUCCESS) {        Display_printf(display, 0, 0, "ADC channel 0 convert result: 0x%x\n", adcValue0);    }    else {        Display_printf(display, 0, 0, "ADC channel 0 convert failed\n");    }    ADC_close(adc);    return (NULL);}/* *  ======== threadFxn1 ======== *  Open a ADC handle and get a array of sampling results after *  calling several conversions. */void *threadFxn1(void *arg0){    uint16_t     i;    ADC_Handle   adc;    ADC_Params   params;    int_fast16_t res;    ADC_Params_init(¶ms);    adc = ADC_open(Board_ADC1, ¶ms);    if (adc == NULL) {        Display_printf(display, 0, 0, "Error initializing ADC channel 1\n");        while (1);    }    else {        Display_printf(display, 0, 0, "ADC channel 1 initialized\n");    }    for (i = 0; i < ADC_SAMPLE_COUNT; i++) {        res = ADC_convert(adc, &adcValue1[i]);        if (res == ADC_STATUS_SUCCESS) {            Display_printf(display, 0, 0, "ADC channel 1 convert result (%d): 0x%x\n", i,                adcValue1[i]);        }        else {            Display_printf(display, 0, 0, "ADC channel 1 convert failed (%d)\n", i);        }    }    ADC_close(adc);    return (NULL);}/* *  ======== mainThread ======== */void *mainThread(void *arg0){    pthread_t           thread0, thread1;    pthread_attr_t      pAttrs;    struct sched_param  priParam;    int                 retc;    int                 detachState;    /* Call driver init functions */    ADC_init();    Display_init();    /* Open the display for output */    display = Display_open(Display_Type_UART, NULL);    if (display == NULL) {        /* Failed to open display driver */        while (1);    }    Display_printf(display, 0, 0, "Starting the acdsinglechannel example\n");    /*     * The CC2650DK_7ID and CC1310DK_7XD measure an ambient light sensor in this example.     * It is not powered by default to avoid high current consumption in other examples.     * The code below turns on the power to the sensor.     */    #if defined(CC2650DK_7ID) || defined(CC1310DK_7XD)        PIN_State pinState;        PIN_Config AlsPinTable[] =        {            Board_ALS_PWR    | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH    | PIN_PUSHPULL, /* Turn on ALS power */            PIN_TERMINATE                                                            /* Terminate list */        };        /* Turn on the power to the ambient light sensor */        PIN_open(&pinState, AlsPinTable);    #endif    /* Create application threads */    pthread_attr_init(&pAttrs);    detachState = PTHREAD_CREATE_DETACHED;    /* Set priority and stack size attributes */    retc = pthread_attr_setdetachstate(&pAttrs, detachState);    if (retc != 0) {        /* pthread_attr_setdetachstate() failed */        while (1);    }    retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);    if (retc != 0) {        /* pthread_attr_setstacksize() failed */        while (1);    }    /* Create threadFxn0 thread */    priParam.sched_priority = 1;    pthread_attr_setschedparam(&pAttrs, &priParam);    retc = pthread_create(&thread0, &pAttrs, threadFxn0, NULL);    if (retc != 0) {        /* pthread_create() failed */        while (1);    }    /* Create threadFxn1 thread */    retc = pthread_create(&thread1, &pAttrs, threadFxn1, (void* )0);    if (retc != 0) {        /* pthread_create() failed */        while (1);    }    return (NULL);}