【极海APM32M3514电机通用评估板】 ADC输入测试
[复制链接]
ADC输入测试,串口输出ADC采样的电压值。
一、硬件部分
测试使用开发板上的电位器RV1,调整RV1改变ADC输入电压。
电路图部分如下:
对应MCU输入端口
二、程序部分
2.1、adc.c
#include "main.h"
void init_adc(void)
{
GPIO_Config_T gpioConfig;
ADC_Config_T adcConfig;
/* RCM Enable*/
RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOA);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC);
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
/* GPIO Configuration */
gpioConfig.pin = GPIO_PIN_7;
gpioConfig.mode = GPIO_MODE_AN;
gpioConfig.pupd = GPIO_PUPD_PU;
GPIO_Config(GPIOA, &gpioConfig);
/* ADC Configuration */
ADC_Reset();
ADC_ConfigStructInit(&adcConfig);
/* Set resolution*/
adcConfig.resolution = ADC_RESOLUTION_12B;
/* Set dataAlign*/
adcConfig.dataAlign = ADC_DATA_ALIGN_RIGHT;
/* Set scanDir*/
adcConfig.scanDir = ADC_SCAN_DIR_UPWARD;
/* Set convMode continous*/
adcConfig.convMode = ADC_CONVERSION_CONTINUOUS;
/* Set extTrigConv*/
adcConfig.extTrigConv1 = ADC_EXT_TRIG_CONV_TRG0;
/* Set TrigEdge*/
adcConfig.extTrigEdge1 = ADC_EXT_TRIG_EDGE_NONE;
ADC_Config(&adcConfig);
ADC_ConfigChannel(ADC_CHANNEL_7, ADC_SAMPLE_TIME_239_5);
/* Enable Interrupt*/
ADC_EnableInterrupt(ADC_INT_CS);
NVIC_EnableIRQRequest(ADC_COMP_IRQn, 2);
/* Calibration*/
ADC_ReadCalibrationFactor();
/* Enable ADC*/
ADC_Enable();
}
void ADC_Isr(void)
{
uint32_t adcData = 0;
uint32_t voltage = 0;
if (ADC_ReadIntFlag(ADC_INT_FLAG_CS) == SET)
{
ADC_ClearIntFlag(ADC_INT_FLAG_CS);
/* Read ADC Conversion value */
adcData = ADC_ReadConversionValue();
/* voltage(mV) = adcData * (3300mV / 4095) */
voltage = (adcData * 3300) / 4095;
/* output to serial port */
printf("voltage : %d mV\r\n", voltage);
}
}
2.2、adc.h
#ifndef _ADC_H
#define _ADC_H
void init_adc(void);
void ADC_Isr(void);
#endif
2.3、main.c
#include "main.h"
int main(void)
{
APM_DelayInit();
APM_DelayMs(5000);
init_usart();
init_led();
init_adc();
/* Wait until ADC is ready */
while (!ADC_ReadStatusFlag(ADC_FLAG_ADRDY));
ADC_StartConversion();
while (1)
{
led_on();
APM_DelayMs(100);
led_off();
APM_DelayMs(100);
}
}
2.4、main.h
#ifndef __MAIN_H
#define __MAIN_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes */
#include <stdio.h>
#include <string.h>
#include "Board.h"
#include "apm32m35xx.h"
#include "bsp_delay.h"
#include "apm32m35xx_rcm.h"
#include "apm32m35xx_gpio.h"
#include "apm32m35xx_misc.h"
#include "apm32m35xx_usart.h"
#include "apm32m35xx_adc.h"
#include "led/led.h"
#include "usart/usart.h"
#include "adc/adc.h"
#ifdef __cplusplus
}
#endif
#endif
三、程序运行结果
下载程序后,串口打印ADC采集的电压值
|