cc1310 NTC的单次ADC读取数据

#include <stdint.h>
#include <stdio.h>
/* For sleep() */
#include <unistd.h>

/* Driver Header files */
#include <ti/drivers/ADCBuf.h>
#include <ti/drivers/UART.h>

/* Example/Board Header files */
#include "Board.h"

#include "math.h"

#define ADCBUFFERSIZE    (50)
#define UARTBUFFERSIZE   ((20 * ADCBUFFERSIZE) + 24)    //

unsigned int tempcum=0;
unsigned int Rnte=0;
double T1temp=0;

uint16_t sampleBufferOne[ADCBUFFERSIZE];
uint16_t sampleBufferTwo[ADCBUFFERSIZE];
uint32_t microVoltBuffer[ADCBUFFERSIZE];
uint32_t buffersCompletedCounter = 0;
char uartTxBuffer[UARTBUFFERSIZE];

/* Driver handle shared between the task and the callback function */
UART_Handle uart;

/*
 * This function is called whenever an ADC buffer is full.
 * The content of the buffer is then converted into human-readable format and
 * sent to the PC via UART.
 */
void adcBufCallback(ADCBuf_Handle handle, ADCBuf_Conversion *conversion,
    void *completedADCBuffer, uint32_t completedChannel)
{
    uint_fast16_t i;
    uint_fast16_t uartTxBufferOffset = 0;
//    unsigned int Unte=0;
//    float x1,x2;
    /* Adjust raw ADC values and convert them to microvolts 调整原始ADC值,并将其转换为微伏 */
    ADCBuf_adjustRawValues(handle, completedADCBuffer, ADCBUFFERSIZE,
        completedChannel);
    ADCBuf_convertAdjustedToMicroVolts(handle, completedChannel,
        completedADCBuffer, microVoltBuffer, ADCBUFFERSIZE);

    /* Start with a header message. */
    uartTxBufferOffset = snprintf(uartTxBuffer,
        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nBuffer %u finished.",
        (unsigned int)buffersCompletedCounter++);

    /* Write raw adjusted values to the UART buffer if there is room. */
    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nRaw Buffer: ");

    for (i = 0; i < ADCBUFFERSIZE && uartTxBufferOffset < UARTBUFFERSIZE; i++) {
        uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
            UARTBUFFERSIZE - uartTxBufferOffset, "%u,",
        *(((uint16_t *)completedADCBuffer) + i));
//        tempcum+= *(((uint16_t *)completedADCBuffer) + i);
    }
//            uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                UARTBUFFERSIZE - uartTxBufferOffset, "%u,",
//                tempcum/ADCBUFFERSIZE);
//            Unte=tempcum/ADCBUFFERSIZE;
//                tempcum=0;
    /* Write microvolt values to the UART buffer if there is room.电压值 */
    if (uartTxBufferOffset < UARTBUFFERSIZE) {
        uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
            UARTBUFFERSIZE - uartTxBufferOffset, "\r\nMicrovolts: ");

        for (i = 0; i < ADCBUFFERSIZE && uartTxBufferOffset < UARTBUFFERSIZE; i++) {
            uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
                UARTBUFFERSIZE - uartTxBufferOffset, "%u,",
                (unsigned int)microVoltBuffer[i]);
//            tempcum+= (unsigned int)microVoltBuffer[i];
        }
//                    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                        UARTBUFFERSIZE - uartTxBufferOffset, "%u,",
//                        tempcum/ADCBUFFERSIZE);
//                    Rnte=((3300-Unte)*10000/Unte);
//
//                    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nRntc:%u,",
//                        Rnte);
//
//                    x1=log(((float)Rnte)/10000);
//                    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nx1:%f,",
//                        x1);
//
//                    x2=1/(273.25+25);
//                    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nx2:%f,",
//                        x2);
//
//                    T1temp=(x1/3435)+x2;
//                    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nT1temp1:%f,",
//                        T1temp);
//
//                    T1temp=(1/T1temp)-273.15;
//                    uartTxBufferOffset += snprintf(uartTxBuffer + uartTxBufferOffset,
//                        UARTBUFFERSIZE - uartTxBufferOffset, "\r\nT1temp2:%f,",
//                        T1temp);
//                    tempcum=0;
    }

    /*
     * Ensure we don't write outside the buffer.
     * Append a newline after the data.
     */
    if (uartTxBufferOffset < UARTBUFFERSIZE) {
        uartTxBuffer[uartTxBufferOffset++] = '\n';
    }
    else {
        uartTxBuffer[UARTBUFFERSIZE-1] = '\n';
    }

    /* Display the data via UART */
    UART_write(uart, uartTxBuffer, uartTxBufferOffset);

}
int ad_read(void);
/*
 * Callback function to use the UART in callback mode. It does nothing.
 */
void uartCallback(UART_Handle handle, void *buf, size_t count) {
   return;
}


/*
 *  ======== mainThread ========
 */
void *mainThread(void *arg0)
{
    UART_Params uartParams;
    ADCBuf_Params adcBufParams;
    /* Call driver init functions */
    ADCBuf_init();
    UART_init();

    /* Create a UART with data processing off. */
    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.writeMode = UART_MODE_CALLBACK;
    uartParams.writeCallback = uartCallback;
    uartParams.baudRate = 115200;
    uart = UART_open(Board_UART0, &uartParams);

    /* Set up an ADCBuf peripheral in ADCBuf_RECURRENCE_MODE_CONTINUOUS */
    ADCBuf_Params_init(&adcBufParams);

//    adcBufParams.callbackFxn = NULL;
////    adcBufParams.blockingTimeout=10000;
//    adcBufParams.recurrenceMode = ADCBuf_RECURRENCE_MODE_ONE_SHOT;//循环模式设置为继续
//    adcBufParams.returnMode = ADCBuf_RETURN_MODE_BLOCKING;//返回模式为callback
//    adcBufParams.samplingFrequency = 100*1000;//差不多200ms发送一次echo
//    adcBuf = ADCBuf_open(Board_ADCBUF0,&adcBufParams); //打开adcbuf

//    ADCBuf_Conversion continuousConversion;
//    /* Configure the conversion struct */
//    continuousConversion.arg = NULL;
//    continuousConversion.adcChannel = Board_ADCBUF0CHANNEL0;//选用通道0
//    continuousConversion.sampleBuffer = sampleBufferOne;//转换数据存储数组的关联,stop
//    continuousConversion.sampleBufferTwo = sampleBufferTwo;//转换数据存储数组的关联.continuous
//    continuousConversion.samplesRequestedCount = ADCBUFFERSIZE;//样本数量

//    if (adcBuf == NULL){
//        /* ADCBuf failed to open. */
//        while(1);
//    }

//    /* Start converting. */
//    if (ADCBuf_convert(adcBuf, &continuousConversion, 1) !=
//        ADCBuf_STATUS_SUCCESS) {
//        /* Did not start conversion process correctly. */
//        while(1);
//    }


    while(1) {
     ad_read();
//        sleep(1000);
      sleep(3); //测低参与
    }
}
uint32_t value= 0;
uint32_t value_Sample= 0;
int_fast16_t test_data=0;
int ad_read(void)
{

    ADCBuf_Handle adcBuf;
    ADCBuf_Params adcBufParams;
    ADCBuf_Conversion continuousConversion;
    uint16_t i;
    float x1,x2;
    /* Set up an ADCBuf peripheral in ADCBuf_RECURRENCE_MODE_CONTINUOUS */
    ADCBuf_Params_init(&adcBufParams);

    adcBufParams.callbackFxn = NULL;
//    adcBufParams.blockingTimeout=10000;
    adcBufParams.recurrenceMode = ADCBuf_RECURRENCE_MODE_ONE_SHOT;//循环模式设置为继续
    adcBufParams.returnMode = ADCBuf_RETURN_MODE_BLOCKING;//返回模式为callback
    adcBufParams.samplingFrequency = 100*1000;//差不多200ms发送一次echo
    adcBuf = ADCBuf_open(Board_ADCBUF0,&adcBufParams); //打开adcbuf

    if(adcBuf)
    {
    /* Configure the conversion struct */
    continuousConversion.arg = NULL;
    continuousConversion.adcChannel = Board_ADCBUF0CHANNEL0;//选用通道0
    continuousConversion.sampleBuffer = sampleBufferOne;//转换数据存储数组的关联,stop
    continuousConversion.sampleBufferTwo = sampleBufferTwo;//转换数据存储数组的关联.continuous
    continuousConversion.samplesRequestedCount = ADCBUFFERSIZE;//样本数量

    test_data =ADCBuf_convert(adcBuf, &continuousConversion, 1);
    if (test_data == ADCBuf_STATUS_SUCCESS) {
        /* 调整原始ADC值并将其转换为微伏 */
        ADCBuf_adjustRawValues(adcBuf,sampleBufferOne,ADCBUFFERSIZE,
        Board_ADCBUF0CHANNEL0);
        test_data=ADCBuf_convertAdjustedToMicroVolts(adcBuf,Board_ADCBUF0CHANNEL0,
        sampleBufferOne,microVoltBuffer,ADCBUFFERSIZE);

        for(i = 0; i <ADCBUFFERSIZE; i++)
        {
        value += microVoltBuffer[i];
        value_Sample +=sampleBufferOne[i];
        }
        value /= ADCBUFFERSIZE;
        value_Sample /= ADCBUFFERSIZE;

        Rnte=((3300-value_Sample)*10000/value_Sample);
        x1=log(((float)Rnte)/10000);
        x2=1/(273.25+25);
        T1temp=(x1/3435)+x2;
        T1temp=(1/T1temp)-273.15;
     }
    ADCBuf_close(adcBuf); //关闭adcbuf
    return T1temp;
    }
    return -1;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,033评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,725评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,473评论 0 338
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,846评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,848评论 5 368
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,691评论 1 282
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,053评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,700评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 42,856评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,676评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,787评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,430评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,034评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,990评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,218评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,174评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,526评论 2 343