这篇学习下ADC数据采集测试。
一、硬件部分
使用开发板上的R105电位器来测试,采集电源的分压,数据输入的变化。
硬件电路图部分
二、程序部分
ADC采集端口是PA3。
2.1、adc.c
#include "main.h"
void init_adc(void)
{
stc_adc_init_t stcAdcInit;
stc_gpio_init_t stcGpioInit;
CLK_SetClockDiv((CLK_BUS_PCLK2 | CLK_BUS_PCLK4), (CLK_PCLK2_DIV8 | CLK_PCLK4_DIV2));
/* 2. Specifies the clock source of ADC. */
CLK_SetPeriClockSrc(CLK_PERIPHCLK_PCLK);
FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC1, ENABLE);
(void)ADC_StructInit(&stcAdcInit);
(void)ADC_Init(CM_ADC1, &stcAdcInit);
(void)GPIO_StructInit(&stcGpioInit);
stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
(void)GPIO_Init(GPIO_PORT_A, GPIO_PIN_03, &stcGpioInit);
ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH3, ENABLE);
ADC_SetSampleTime(CM_ADC1, ADC_CH3, 0x40U);
//ADC_ConvDataAverageConfig(CM_ADC1, ADC_AVG_CNT8);
//ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH3, ENABLE);
}
void adc_test(void)
{
uint16_t u16AdcValue;
int32_t iRet = LL_ERR;
uint16_t adc_val=0;
__IO uint32_t u32TimeCount = 0UL;
/* Can ONLY start sequence A conversion.
Sequence B needs hardware trigger to start conversion. */
ADC_Start(CM_ADC1);
do {
if (ADC_GetStatus(CM_ADC1, ADC_FLAG_EOCA ) == SET)
{
ADC_ClearStatus(CM_ADC1, ADC_FLAG_EOCA);
iRet = LL_OK;
break;
}
} while (u32TimeCount++ < 1000);
if (iRet == LL_OK)
{
/* Get any ADC value of sequence A channel that needed. */
u16AdcValue = ADC_GetValue(CM_ADC1, ADC_CH3);
adc_val=(uint16_t)((((float32_t)(u16AdcValue) * 3.3) / ((float32_t)(1UL << 12U))) * 1000.F);
DDL_Printf("The ADC voltage is %u mV\r\n",adc_val);
}
else
{
ADC_Stop(CM_ADC1);
DDL_Printf("ADC exception.\r\n");
}
}
2.2、adc.h
#ifndef _ADC_H
#define _ADC_H
void init_adc(void);
void sdc_test(void);
#endif
2.3、main.c
#include "main.h"
#include "lcd.h"
#include "sram.h"
#include "timer0.h"
#include "led.h"
#include "touch.h"
#include "can.h"
#include "adc.h"
stc_touchpad_data_t touchdat;
void SysTick_Handler(void)
{
SysTick_IncTick();
led1_tog();
}
int32_t main(void)
{
uint16_t u=0;
uint8_t i=0;
uint16_t cord[6]={WHITE, BLUE, BRED, GBLUE, RED, YELLOW};
/* Register write enable for some required peripherals. */
LL_PERIPH_WE(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_EFM | LL_PERIPH_SRAM);
BSP_CLK_Init();
/* EXCLK 60MHz */
CLK_SetClockDiv(CLK_BUS_EXCLK, CLK_EXCLK_DIV4);
DDL_PrintfInit(BSP_PRINTF_DEVICE, BSP_PRINTF_BAUDRATE, BSP_PRINTF_Preinit);
BSP_IO_Init();
BSP_LED_Init();
BSP_LCD_IO_Init();
init_led();
init_touch();
init_lcd();
init_sram();
init_timer0();
init_can();
init_adc();
SysTick_Init(1000U);
LL_PERIPH_WP(LL_PERIPH_GPIO | LL_PERIPH_FCG | LL_PERIPH_PWC_CLK_RMU | LL_PERIPH_EFM | LL_PERIPH_SRAM);
POINT_COLOR=RED;
LCD_Clear(WHITE);
//sram_test();
for (;;)
{
SysTick_Delay(500U);
adc_test();
}
}
三、串口输出
调解电位器,输出输出采集的电压值。
|