TI论坛
直播中

李涛

7年用户 182经验值
私信 关注
[问答]

TM4C123G的ADC多通道采样请问配置采样率500k是先使能在配置吗?

我想配置一个PE4,PE5两通道采样,配置采样率500k是先使能在配置,还是其他?
在获得数据的时候只有一个函数ADCSequenceDataGet();会不会两通道的数据获得重合?怎样才能精确的获得2通道数据?
void ADC0_init(void)
[
// Enable GPIO for ADC
SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_4);// Enable pin PE4_AIN9
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_5);// Enable pinPE5 _AIN8
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH8|ADC_CTL_CH9| ADC_CTL_IE |ADC_CTL_END);
ADCSequenceEnable(ADC0_BASE, 1);
ADCIntClear(ADC0_BASE, 1);//
// ADCIntRegister(ADC0_BASE,1,ADC0IntHandler);//
ADCIntEnable(ADC0_BASE,1);//
]

回帖(20)

苏笛笛

2018-8-14 07:51:16
默认的采样速度1M就ok。
设置采样PE4,PE5两个通道时,按照楼主的配置,采样结束后,可以从fifo中读出2个数据,那么第一个数据对应的就是CH8的,第二个就是CH9的。
举报

李涛

2018-8-14 08:09:37
引用: Evawang 发表于 2018-8-14 07:51
默认的采样速度1M就ok。
设置采样PE4,PE5两个通道时,按照楼主的配置,采样结束后,可以从fifo中读出2个数据,那么第一个数据对应的就是CH8的,第二个就是CH9的。

谢谢回复,你的意思是说比如我设置一个数组unsigned long ulADC0_Value[2];那我用函数才回来的值  ADCSequenceDataGet(ADC0_BASE, 1, ulADC0_Value);  ulADC0_Value[0]放的CH8,ulADC0_Value[1]放的CH9是这样的嘛?;;还有这个主要涉及到一个闭环系统,采样过快调节不过来,所以我这样设置采样率对嘛
举报

苏笛笛

2018-8-14 08:27:21
引用: zoms0 发表于 2018-8-14 08:09
谢谢回复,你的意思是说比如我设置一个数组unsigned long ulADC0_Value[2];那我用函数才回来的值  ADCSequenceDataGet(ADC0_BASE, 1, ulADC0_Value);  ulADC0_Value[0]放的CH8,ulADC0_Value[1]放的CH9是这样的嘛?;;还有这个主要涉及到一个闭环系统,采样过快调节不过来,所以我这样设置采样率对嘛 ...

是这样理解。关于采样率设置,我记得早的TivaWare的库函数有点问题,后来我改成直接操作寄存器的了。把这个代码分享给您吧。
这里采样的是ADC的CH0~CH7,使能了中断,在中断中就可以直接读出8个数据,对应0~7通道的。
采样率的设置和查询我自己写的函数。
上代码:

#include
#include
#include "inc/hw_memmap.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/interrupt.h"
#include "inc/hw_ints.h"
#include "inc/hw_types.h"
#include "inc/hw_adc.h"
//*****************************************************************************
//
//! addtogroup adc_examples_list
//!

Single Ended ADC (single_ended)


//!
//! This example shows how to setup ADC0 as a single ended input and take a
//! single sample on AIN0/PE7.
//!
//! This example uses the following peripherals and I/O signals. You must
//! review these and change as needed for your own board:
//! - ADC0 peripheral
//! - GPIO Port E peripheral (for AIN0 pin)
//! - AIN0 - PE7
//!
//! The following UART signals are configured only for displaying console
//! messages for this example. These are not required for operation of the
//! ADC.
//! - UART0 peripheral
//! - GPIO Port A peripheral (for UART0 pins)
//! - UART0RX - PA0
//! - UART0TX - PA1
//!
//! This example uses the following interrupt handlers. To use this example
//! in your own application you must add these interrupt handlers to your
//! vector table.
//! - None.
//
//*****************************************************************************
#define ADCSAMPLERATE_1MSPS 0x7 // 1,000,000 samples per second
#define ADCSAMPLERATE_500KSPS 0x5 // 500,000 samples per second
#define ADCSAMPLERATE_250KSPS 0x3 // 250,000 samples per second
#define ADCSAMPLERATE_125KSPS 0x1 // 125,000 samples per second
//*****************************************************************************
//
// This function sets up UART0 to be used for a console to display information
// as the example is running.
//
//*****************************************************************************
uint32_t pui32ADC0Value[8];
uint32_t Data[8]=[0];
uint32_t TempVal = 0;
uint32_t TempVal2 = 0;
void ADCSampleRateSet(uint32_t ui32Speed)
[
//
// Set the ADC speed
//
HWREG(ADC0_BASE + ADC_O_PC) = (ui32Speed);
]
uint32_t ADCSampleRateGet(void)
[
//
// Return the current ADC speed.
//
return((HWREG(ADC0_BASE + ADC_O_PC) & 0x0f));
]

void
InitConsole(void)
[
//
// Enable GPIO port A which is used for UART0 pins.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

//
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
// TODO: change this to select the port/pin you are using.
//
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);

//
// Enable UART0 so that we can configure the clock.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

//
// Use the internal 16MHz oscillator as the UART clock source.
//
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

//
// Select the alternate (UART) function for these pins.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

//
// Initialize the UART for console I/O.
//
UARTStdioConfig(0, 115200, 16000000);
]

void ADC0Sequence0Isr(void)
[
uint16_t i;
//
// Clear the ADC interrupt flag.
//
ADCIntClear(ADC0_BASE, 0);
//
// Read ADC Value.
//
ADCSequenceDataGet(ADC0_BASE, 0, pui32ADC0Value);

for(i = 0;i < 8;i ++)
[
Data = pui32ADC0Value*3300/4096;
]
SysCtlDelay(1);
]

//*****************************************************************************
//
// Configure ADC0 for a single-ended input and a single sample. Once the
// sample is ready, an interrupt flag will be set. Using a polling method,
// the data will be read then displayed on the console via UART0.
//
//*****************************************************************************
int
main(void)
[
//
// This array is used for storing the data read from the ADC FIFO. It
// must be as large as the FIFO for the sequencer in use. This example
// uses sequence 3 which has a FIFO depth of 1. If another sequence
// was used with a deeper FIFO, then the array size must be changed.
//
//uint32_t pui32ADC0Value[8];
//uint32_t Data[8]=[0];
//
// Set the clocking to run at 20 MHz (200 MHz / 10) using the PLL. When
// using the ADC, you must either use the PLL or supply a 16 MHz clock
// source.
// TODO: The SYSCTL_XTAL_ value must be changed to match the value of the
// crystal on your board.
//
SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);

//
// Set up the serial console to use for displaying messages. This is
// just for this example program and is not needed for ADC operation.
//
InitConsole();

// //
// // Display the setup on the console.
// //
UARTprintf("ADC ->n");
//UARTprintf(" Type: Single Endedn");
//UARTprintf(" Samples: Onen");
//UARTprintf(" Update Rate: 250msn");
//UARTprintf(" Input Pin: AIN0/PE7nn");

//
// The ADC0 peripheral must be enabled for use.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);

//
// For this example ADC0 is used with AIN0 on port E7.
// The actual port and pins used may be different on your part, consult
// the data sheet for more information. GPIO port E needs to be enabled
// so these pins can be used.
// TODO: change this to whichever GPIO port you are using.
//
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//
// Select the analog ADC function for these pins.
// Consult the data sheet to see which functions are allocated per pin.
// TODO: change this to select the port/pin you are using.
//
GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);

//set sample rate
ADCSampleRateSet(ADCSAMPLERATE_1MSPS);
//
// Enable sample sequence 3 with a processor signal trigger. Sequence 3
// will do a single sample when the processor sends a signal to start the
// conversion. Each ADC module has 4 programmable sequences, sequence 0
// to sequence 3. This example is arbitrarily using sequence 3.
//
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);

//
// Configure step 0 on sequence 3. Sample channel 0 (ADC_CTL_CH0) in
// single-ended mode (default) and configure the interrupt flag
// (ADC_CTL_IE) to be set when the sample is done. Tell the ADC logic
// that this is the last conversion on sequence 3 (ADC_CTL_END). Sequence
// 3 has only one programmable step. Sequence 1 and 2 have 4 steps, and
// sequence 0 has 8 programmable steps. Since we are only doing a single
// conversion using sequence 3 we will only configure step 0. For more
// information on the ADC sequences and steps, reference the datasheet.
//
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0 );//PE3
ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1 );//PE2
ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH2 );//PE1
ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH3 );//PE0
ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_CH4 );//PD3
ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_CH5 );//PD2
ADCSequenceStepConfigure(ADC0_BASE, 0, 6, ADC_CTL_CH6 );//PD1
ADCSequenceStepConfigure(ADC0_BASE, 0, 7, ADC_CTL_CH7 | ADC_CTL_IE |ADC_CTL_END);
ADCIntEnable(ADC0_BASE, 0);
IntEnable(INT_ADC0SS0);
IntMasterEnable();
ADCIntClear(ADC0_BASE, 0);
//
// Since sample sequence 3 is now configured, it must be enabled.
//
ADCSequenceEnable(ADC0_BASE, 0);

//
// Clear the interrupt status flag. This is done to make sure the
// interrupt flag is cleared before we sample.
//
TempVal = ADCSampleRateGet();
//
// Sample AIN0 forever. Display the value on the console.
//
while(1)
[
//
// Trigger the ADC conversion.
//
ADCProcessorTrigger(ADC0_BASE, 0);
/*
//
// Wait for conversion to be completed.
//
while(!ADCIntStatus(ADC0_BASE, 0, false))
[
]

//
// Clear the ADC interrupt flag.
//
ADCIntClear(ADC0_BASE, 0);

//
// Read ADC Value.
//
ADCSequenceDataGet(ADC0_BASE, 0, pui32ADC0Value);
*/
/*
Data[0] = pui32ADC0Value[0]*3300/4096;
Data[1] = pui32ADC0Value[1]*3300/4096;
Data[2] = pui32ADC0Value[2]*3300/4096;
Data[3] = pui32ADC0Value[3]*3300/4096;
Data[4] = pui32ADC0Value[4]*3300/4096;
Data[5] = pui32ADC0Value[5]*3300/4096;
Data[6] = pui32ADC0Value[6]*3300/4096;
Data[7] = pui32ADC0Value[7]*3300/4096;
*/
//
// Display the AIN0 (PE7) digital value on the console.
//
// UARTprintf("AIN0 = %4dr", pui32ADC0Value[0]);

//
// This function provides a means of generating a constant length
// delay. The function delay (in cycles) = 3 * parameter. Delay
// 250ms arbitrarily.
//
SysCtlDelay(SysCtlClockGet() / 12);
]
]
举报

韦明

2018-8-14 08:44:24
引用: Evawang 发表于 2018-8-14 08:27
是这样理解。关于采样率设置,我记得早的TivaWare的库函数有点问题,后来我改成直接操作寄存器的了。把这个代码分享给您吧。
这里采样的是ADC的CH0~CH7,使能了中断,在中断中就可以直接读出8个数据,对应0~7通道的。
采样率的设置和查询我自己写的函数。

好长的代码。。。
举报

更多回帖

×
20
完善资料,
赚取积分