我正在开发一个项目,使用 Texas Instruments EK-TM4C123GXL LaunchPad 来测量面包板电源模块 (hw-131) 的电压。我正在使用板载 ADC 模块读取电压,但遇到一些问题:
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/pin_map.h"
#define NUM_SAMPLES 8
// Function to configure UART0 for communication
void ConfigureUART(void)
{
// Enable UART0 and GPIOA peripherals
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// Configure PA0 and PA1 as UART pins
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Initialize UART0 with a baud rate of 115200
UARTStdioConfig(0, 115200, SysCtlClockGet());
}
// ADC0 interrupt handler
void ADC0IntHandler(void)
{
// Clear the ADC interrupt flag
ADCIntClear(ADC0_BASE, 0);
uint32_t ui32ADC0Value[NUM_SAMPLES];
uint32_t ui32Sum = 0;
uint32_t i = 0;
// Get the ADC sequence data
ADCSequenceDataGet(ADC0_BASE, 0, ui32ADC0Value);
// Sum the ADC values
while(i < NUM_SAMPLES){
ui32Sum += ui32ADC0Value[i];
i++;
}
// Calculate the average ADC value
uint32_t ui32Average = ui32Sum / NUM_SAMPLES;
// Print the average ADC value to the UART
UARTprintf("ADC Average Value: %d\n", ui32Average);
// Calculate the input voltage in volts (assuming 3.3V reference voltage)
float voltage = (ui32Average * 3.3) / 4096.0;
uint32_t v_int_part = voltage; // Integer part of the voltage
uint32_t v_frac_part = (voltage - v_int_part) * 1000; // Fractional part in millivolts
// Print the calculated voltage to the UART
UARTprintf("Input Voltage: %d.%03d V\n", v_int_part, v_frac_part);
}
int main(void)
{
// Set the system clock to 50MHz (200MHz PLL / 4)
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
// Configure UART for debugging
ConfigureUART();
// Enable the ADC0 and GPIOE peripherals
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
// Wait for the peripherals to be ready
while(!SysCtlPeripheralReady(SYSCTL_PERIPH_ADC0) || !SysCtlPeripheralReady(SYSCTL_PERIPH_GPIOE))
{
}
// Configure PE3 as ADC input
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);
// Set the ADC clock source and rate
ADCClockConfigSet(ADC0_BASE, ADC_CLOCK_SRC_PIOSC | ADC_CLOCK_RATE_FULL, 8);
// Configure ADC sequence 0 to be triggered by the processor
ADCSequenceConfigure(ADC0_BASE, 0, ADC_TRIGGER_PROCESSOR, 0);
// Configure all 8 steps in the sequence to sample from CH0 (PE3)
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 6, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 7, ADC_CTL_CH0 | ADC_CTL_IE | ADC_CTL_END); // Enable interrupt on last step
// Enable ADC sequence 0
ADCSequenceEnable(ADC0_BASE, 0);
// Clear any pending ADC interrupts
ADCIntClear(ADC0_BASE, 0);
// Enable ADC interrupts
ADCIntEnable(ADC0_BASE, 0);
// Enable the ADC0 sequence 0 interrupt in the NVIC
IntEnable(INT_ADC0SS0);
// Set ADC0 sequence 0 interrupt priority to highest (0)
IntPrioritySet(INT_ADC0SS0, 0x00);
while(1)
{
// Trigger the ADC conversion
ADCProcessorTrigger(ADC0_BASE, 0);
// Delay to simulate some processing time
SysCtlDelay(SysCtlClockGet() / 6);
}
}
看来电源模块OFF时的波动比电源模块ON时的波动更显着。
任何建议或指导将不胜感激!谢谢!
较大的波动是调试输出中的错误,而不是“真实的”。例如,当您的 ADC 值
9
对应于 (9 x 3.3) / 4096 = 0.007 伏特时,您的调试代码将输出 0.7 伏特。