This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC1310: 采样问题

Part Number: CC1310
Other Parts Discussed in Thread: CC1350

ADC采样到microVoltBuffer中的数值不准确,甚至会出现0值
以下使我采集发送端的代码:

#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <ti/drivers/ADCBuf.h>
#include <ti/drivers/UART.h>
#include "Board.h"
#include <stdlib.h>
#include <ti/devices/DeviceFamily.h>
#include DeviceFamily_constructPath(driverlib/rf_prop_mailbox.h)
#include "smartrf_settings/smartrf_settings.h"

#include <ti/display/Display.h> //为最后的Display函数而添加的
#include <ti/drivers/UART.h> //为最后的Display函数而添加的
#include <ti/drivers/GPIO.h>
#include <ti/display/DisplayUart.h>
#include <ti/display/DisplayExt.h>
#include <ti/drivers/GPIO.h>
#include <unistd.h>

static Display_Handle display; //为最后的Display函数而添加的

#define ADCBUFFERSIZE (10)
uint16_t sampleBufferOne[ADCBUFFERSIZE];
uint16_t sampleBufferTwo[ADCBUFFERSIZE];
uint32_t microVoltBuffer[ADCBUFFERSIZE];
uint32_t buffersCompletedCounter = 0;

uint32_t Mag[7]; //adc转换,取平均值后放置的地方
uint32_t MagVoltX1; //DIO24
uint32_t MagVoltX2; //DIO25
uint32_t MagVoltY1; //DIO26
uint32_t MagVoltY2; //DIO27
uint32_t MagVoltZ1; //DIO28
uint32_t MagVoltZ2; //DIO29
uint32_t TemVolt; //DIO23, 温度传感器测量到的转换为电压值的温度,单位为微伏
double Temp; //公式转换后的温度值,单位为摄氏度
double MagX1; //
double MagX2; //
double MagY1; //
double MagY2; //
double MagZ1; //
double MagZ2; //
int q=0; //用于采样循环
int k=0; //循环指示位,用于跳出循环
int i=0; //用于找出microvoltbuffer的最大值的循环
uint32_t max; //找到microvoltbuffer最大值后的放置位置
uint32_t max2; //找到microvoltbuffer第二大值后的放置位置

void bubble_sort(uint32_t arr[10]);

void adcBufCallback(ADCBuf_Handle handle, ADCBuf_Conversion *conversion,
void *completedADCBuffer, uint32_t completedChannel)
{
/* Adjust raw ADC values and convert them to microvolts */
ADCBuf_adjustRawValues(handle, completedADCBuffer, ADCBUFFERSIZE,
completedChannel);
ADCBuf_convertAdjustedToMicroVolts(handle, completedChannel,
completedADCBuffer, microVoltBuffer, ADCBUFFERSIZE);
}
#define PAYLOAD_LENGTH 28 // Packet的容量范围为Packet[0]~Packet[27]
#ifdef POWER_MEASUREMENT
#define PACKET_INTERVAL 5 /* For power measurement set packet interval to 5s */
#else
#define PACKET_INTERVAL 500000 /* Set packet interval to 500000us or 500ms */
#endif
static RF_Object rfObject;
static RF_Handle rfHandle;
static PIN_Handle ledPinHandle;
static PIN_State ledPinState;
static uint8_t packet[PAYLOAD_LENGTH];
PIN_Config pinTable[] =
{
Board_PIN_LED1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
#if defined Board_CC1352R1_LAUNCHXL
Board_DIO30_RFSW | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
#endif
#ifdef POWER_MEASUREMENT
#if defined(Board_CC1350_LAUNCHXL)
Board_DIO30_SWPWR | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
#endif
#endif
PIN_TERMINATE
};
void *mainThread(void *arg0)
{
Display_init();
/* Initialize display and try to open both UART and LCD types of display. */
Display_Params params;
Display_Params_init(&params);
params.lineClearMode = DISPLAY_CLEAR_BOTH;
display = Display_open(Display_Type_UART, &params);
if (display == NULL ) {
/* Failed to open a display */
while (1);
}
if (display) {
Display_printf(display, 0, 0, "Hello Serial!");
}

if (display == NULL) {
while(1); //程序运行停留于此,无法跳出继续向下运行
}

ADCBuf_Handle adcBuf;
ADCBuf_Params adcBufParams;
ADCBuf_Conversion continuousConversion[7]; //BMS中这个为8(可能是为了冗余)
RF_Params rfParams;
RF_Params_init(&rfParams);
ledPinHandle = PIN_open(&ledPinState, pinTable);
if (ledPinHandle == NULL)
{
while(1);
}
#ifdef POWER_MEASUREMENT
#if defined(Board_CC1350_LAUNCHXL)
PINCC26XX_setMux(ledPinHandle, Board_DIO30_SWPWR, PINCC26XX_MUX_RFC_GPO1);
#endif
#endif
RF_cmdPropTx.pktLen = PAYLOAD_LENGTH; // Packet的容量范围为Packet[0]~Packet[27]
RF_cmdPropTx.pPkt = packet; // 需要发送的数据放入名为Packet的packet中
RF_cmdPropTx.startTrigger.triggerType = TRIG_NOW;
rfHandle = RF_open(&rfObject, &RF_prop, (RF_RadioSetup*)&RF_cmdPropRadioDivSetup, &rfParams);
RF_postCmd(rfHandle, (RF_Op*)&RF_cmdFs, RF_PriorityNormal, NULL, 0);

ADCBuf_init();
ADCBuf_Params_init(&adcBufParams);
adcBufParams.callbackFxn = adcBufCallback;
adcBufParams.recurrenceMode = ADCBuf_RECURRENCE_MODE_CONTINUOUS;
adcBufParams.returnMode = ADCBuf_RETURN_MODE_CALLBACK;
adcBufParams.samplingFrequency = 200;

while(1)
{
for(q=0;q<7;q++)
{
adcBuf = ADCBuf_open(Board_ADCBUF0, &adcBufParams);
continuousConversion[q].arg = NULL;
continuousConversion[q].adcChannel = q;
continuousConversion[q].sampleBuffer = sampleBufferOne;
continuousConversion[q].sampleBufferTwo = sampleBufferTwo;
continuousConversion[q].samplesRequestedCount = ADCBUFFERSIZE;
if (adcBuf == NULL){
while(1);
}

if (ADCBuf_convert(adcBuf, &continuousConversion[q], 7) !=
ADCBuf_STATUS_SUCCESS) {
while(1);
}
usleep(50000); // 一定要加这个,不加的话microvoltbuffer会读不出数据
ADCBuf_convertCancel(adcBuf); //读出microvoltbuffer
ADCBuf_close(adcBuf);

/* 使用取中位数的方法估计值
bubble_sort(microVoltBuffer);
Mag[q] = microVoltBuffer[5];
*/

Mag[q] = (microVoltBuffer[0]+microVoltBuffer[1]+microVoltBuffer[2]+microVoltBuffer[3]+microVoltBuffer[4]+microVoltBuffer[5]
+microVoltBuffer[6]+microVoltBuffer[7]+microVoltBuffer[8]+microVoltBuffer[9])/10;
packet[q] = (Mag[q]);
packet[q+7] = (Mag[q]>>8);
packet[q+14] = (Mag[q]>>16);
packet[q+21] = (Mag[q]>>24);

// 重点,非常奇怪的采样现象
if( q == 0 )
{
int index = 0;
for(index = 0; index < 10; ++index)
Display_printf(display,0, 0, "microVoltBuffer[%d]=%d", index, microVoltBuffer[index]);

Display_printf(display,0, 0, "Mag[0]=%d", Mag[q]);
}
}

RF_EventMask terminationReason = RF_runCmd(rfHandle, (RF_Op*)&RF_cmdPropTx,
RF_PriorityNormal, NULL, 0);
switch(terminationReason)
{
case RF_EventLastCmdDone:
break;
case RF_EventCmdCancelled:
break;
case RF_EventCmdAborted:
break;
case RF_EventCmdStopped:
break;
default:
while(1);
}
uint32_t cmdStatus = ((volatile RF_Op*)&RF_cmdPropTx)->status;
switch(cmdStatus)
{
case PROP_DONE_OK:
break;
case PROP_DONE_STOPPED:
break;
case PROP_DONE_ABORT:
break;
case PROP_ERROR_PAR:
break;
case PROP_ERROR_NO_SETUP:
break;
case PROP_ERROR_NO_FS:
break;
case PROP_ERROR_TXUNF:
break;
default:
while(1);
}
#ifndef POWER_MEASUREMENT
PIN_setOutputValue(ledPinHandle, Board_PIN_LED1,!PIN_getOutputValue(Board_PIN_LED1));
#endif
RF_yield(rfHandle);
#ifdef POWER_MEASUREMENT
sleep(PACKET_INTERVAL);
#else
usleep(PACKET_INTERVAL);
#endif
}
}

void bubble_sort(uint32_t arr[10]) {
int i = 0;
int j = 0;

for (i = 0; i < 9; i++) { // 外层循环:n-1次遍历(n=10)
for (j = 0; j < 9 - i; j++) { // 内层循环:处理未排序部分
if (arr[j] > arr[j + 1]) { // 比较相邻元素
// 交换元素
uint32_t temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

但串口中获取的值
microVoltBuffer[0]=1831312
microVoltBuffer[1]=1566688
microVoltBuffer[2]=0
microVoltBuffer[3]=0
microVoltBuffer[4]=0
microVoltBuffer[5]=0
microVoltBuffer[6]=0
microVoltBuffer[7]=1763056
microVoltBuffer[8]=1770400
microVoltBuffer[9]=1855456
Mag[0]=878691
非常不正常