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.

我想写一个外接四路传感器的ADC代码



int
main(void)
{
#if defined(TARGET_IS_TM4C129_RA0) || \
defined(TARGET_IS_TM4C129_RA1) || \
defined(TARGET_IS_TM4C129_RA2)
uint32_t ui32SysClock;
#endif

//
// 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[1];

//
// These variables are used to store the temperature conversions for
// Celsius and Fahrenheit.
//
uint32_t ui32TempValueC;
uint32_t ui32TempValueF;

//
// 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.
//
#if defined(TARGET_IS_TM4C129_RA0) || \
defined(TARGET_IS_TM4C129_RA1) || \
defined(TARGET_IS_TM4C129_RA2)
ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
SYSCTL_OSC_MAIN |
SYSCTL_USE_PLL |
SYSCTL_CFG_VCO_480), 20000000);
#else
SysCtlClockSet(SYSCTL_SYSDIV_10 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
#endif

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

//
// Display the setup on the console.
//
UARTprintf("ADC ->\n");
UARTprintf(" Type: Internal Temperature Sensor\n");
UARTprintf(" Samples: One\n");
UARTprintf(" Update Rate: 250ms\n");
UARTprintf(" Input Pin: Internal temperature sensor\n\n");

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

//
// Enable sample sequence 3 with a processor signal trigger. Sequence 3
// will do a single sample when the processor sends a singal 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, 3, ADC_TRIGGER_PROCESSOR, 0);

//
// Configure step 0 on sequence 3. Sample the temperature sensor
// (ADC_CTL_TS) 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, 3, 0, ADC_CTL_TS | ADC_CTL_IE |
ADC_CTL_END);

//
// Since sample sequence 3 is now configured, it must be enabled.
//
ADCSequenceEnable(ADC0_BASE, 3);

//
// Clear the interrupt status flag. This is done to make sure the
// interrupt flag is cleared before we sample.
//
ADCIntClear(ADC0_BASE, 3);

//
// Sample the temperature sensor forever. Display the value on the
// console.
//
while(1)
{
//
// Trigger the ADC conversion.
//
ADCProcessorTrigger(ADC0_BASE, 3);

//
// Wait for conversion to be completed.
//
while(!ADCIntStatus(ADC0_BASE, 3, false))
{
}

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

//
// Read ADC Value.
//
ADCSequenceDataGet(ADC0_BASE, 3, pui32ADC0Value);

//
// Use non-calibrated conversion provided in the data sheet. Make
// sure you divide last to avoid dropout.
//
ui32TempValueC = ((1475 * 1023) - (2250 * pui32ADC0Value[0])) / 10230;

//
// Get Fahrenheit value. Make sure you divide last to avoid dropout.
//
ui32TempValueF = ((ui32TempValueC * 9) + 160) / 5;

//
// Display the temperature value on the console.
//
UARTprintf("Temperature = %3d*C or %3d*F\r", ui32TempValueC,
ui32TempValueF);

//
// This function provides a means of generating a constant length
// delay. The function delay (in cycles) = 3 * parameter. Delay
// 250ms arbitrarily.
//
#if defined(TARGET_IS_TM4C129_RA0) || \
defined(TARGET_IS_TM4C129_RA1) || \
defined(TARGET_IS_TM4C129_RA2)
SysCtlDelay(ui32SysClock / 12);
#else
SysCtlDelay(SysCtlClockGet() / 12);
#endif
}
}

这是固件库里面的adc温度传感器的一段代码,里面的黄色部分是什么意思,这是如何与板子里面的传感器建立联系的,如果要外接该如何去定义,我没学过c++,

只学过VB,好烦

谢谢

  • 你这个是官方的例程,黄色部分是为了兼容TM4C129x的程序而做的宏定义,可以不用管他,或者删掉就可以了。至于温度传感器,直接连接一个内部的采样序列,使能一个寄存器就好了。数据手册的描述如下:

    在程序中表现为,

    ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_TS | ADC_CTL_IE |
    ADC_CTL_END);

    这个 ADC_CTL_TS就是使能稳定传感器了。