NO.18 ADC的基本概念

2022-12-20  

  ADC,就是模拟量转换成数字量的一种器件,一种将自然界连续的信号转化成离散的电信号发送给设备。


  在MSP432中,自带ADC14,一个14位的ADC,好像按照官方的说法这个ADC的速度比MSP430的速度快10倍。


  具体ADC的内容数电书上都有,什么并联比较型啊,逐次逼近型啊之类,上网百度一下就有。


  关于我们在MSP432中如何使用这个ADC呢,TIDrivers里有非常强大的库我们可以直接使用。

  

  首先我们要了解几个概念:量程分辨率采样速率。调用十分简单


/*

 *  ======== adcsinglechannel.c ========

 */

#include

#include


/* POSIX Header files */

#include


/* Driver Header files */

#include

#include


/* Driver configuration */

#include "ti_drivers_config.h"


/* ADC sample count */

#define ADC_SAMPLE_COUNT  (10)


#define THREADSTACKSIZE   (768)


/* ADC conversion result variables */

uint16_t adcValue0;

uint32_t adcValue0MicroVolt;

uint16_t adcValue1[ADC_SAMPLE_COUNT];

uint32_t adcValue1MicroVolt[ADC_SAMPLE_COUNT];


static Display_Handle display;


/*

 *  ======== 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(&params);

    adc = ADC_open(CONFIG_ADC_0, &params);


    if (adc == NULL) {

        Display_printf(display, 0, 0, "Error initializing ADC0n");

        while (1);

    }


    /* Blocking mode conversion */

    res = ADC_convert(adc, &adcValue0);


    if (res == ADC_STATUS_SUCCESS) {


        adcValue0MicroVolt = ADC_convertRawToMicroVolts(adc, adcValue0);


        Display_printf(display, 0, 0, "ADC0 raw result: %dn", adcValue0);

        Display_printf(display, 0, 0, "ADC0 convert result: %d uVn",

            adcValue0MicroVolt);

    }

    else {

        Display_printf(display, 0, 0, "ADC0 convert failedn");

    }


    ADC_close(adc);


    return (NULL);

}


/*

 *  ======== threadFxn1 ========

 *  Open a ADC handle and get an 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(&params);

    adc = ADC_open(CONFIG_ADC_1, &params);


    if (adc == NULL) {

        Display_printf(display, 0, 0, "Error initializing ADC1n");

        while (1);

    }


    for (i = 0; i < ADC_SAMPLE_COUNT; i++) {

        res = ADC_convert(adc, &adcValue1[i]);


        if (res == ADC_STATUS_SUCCESS) {


            adcValue1MicroVolt[i] = ADC_convertRawToMicroVolts(adc, adcValue1[i]);


            Display_printf(display, 0, 0, "ADC1 raw result (%d): %dn", i,

                           adcValue1[i]);

            Display_printf(display, 0, 0, "ADC1 convert result (%d): %d uVn", i,

                adcValue1MicroVolt[i]);

        }

        else {

            Display_printf(display, 0, 0, "ADC1 convert failed (%d)n", i);

        }

    }


    ADC_close(adc);


    return (NULL);

}


/*

 *  ======== mainThread ========

 */

void *mainThread(void *arg0)

{

    pthread_t           thread0, thread1;

    pthread_attr_t      attrs;

    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 examplen");


    /* Create application threads */

    pthread_attr_init(&attrs);


    detachState = PTHREAD_CREATE_DETACHED;

    /* Set priority and stack size attributes */

    retc = pthread_attr_setdetachstate(&attrs, detachState);

    if (retc != 0) {

        /* pthread_attr_setdetachstate() failed */

        while (1);

    }


    retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);

    if (retc != 0) {

        /* pthread_attr_setstacksize() failed */

        while (1);

    }


    /* Create threadFxn0 thread */

    priParam.sched_priority = 1;

    pthread_attr_setschedparam(&attrs, &priParam);


    retc = pthread_create(&thread0, &attrs, threadFxn0, NULL);

    if (retc != 0) {

        /* pthread_create() failed */

        while (1);

    }


    /* Create threadFxn1 thread */

    retc = pthread_create(&thread1, &attrs, threadFxn1, (void* )0);

    if (retc != 0) {

        /* pthread_create() failed */

        while (1);

    }


    return (NULL);

}


文章来源于:电子工程世界    原文链接
本站所有转载文章系出于传递更多信息之目的,且明确注明来源,不希望被转载的媒体或个人可与我们联系,我们将立即进行删除处理。