美文网首页
cc1310 NTC的单次ADC读取数据

cc1310 NTC的单次ADC读取数据

作者: 骑上我心爱的小蜗牛 | 来源:发表于2020-08-12 15:55 被阅读0次
    #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;
    }
    

    相关文章

      网友评论

          本文标题:cc1310 NTC的单次ADC读取数据

          本文链接:https://www.haomeiwen.com/subject/sccmdktx.html