有幸能够获得这次测评的机会,由于最近工作上的原因非常的忙,赶进度,此次将进行简单的ADC采样和串口通讯,再串口更具上显示采样值。拿到板子感到整个板子非常大气,如下:
程序主要是配置了ADC和串口的外设,开启滴答定时器。主要程序如下:
adc外设的初始化和中断的开启:
#include "main.h"
#include "hd_adc.h"
#include "apm32f4xx_gpio.h"
#include "apm32f4xx_adc.h"
#include "apm32f4xx_misc.h"
#include "apm32f4xx_rcm.h"
float voltage = 0.0f;
void HD_ADC_Init(void)
{
GPIO_Config_T gpioConfig;
ADC_Config_T adcConfig;
ADC_CommonConfig_T adcCommonConfig;
/* Enable GPIOA clock */
RCM_EnableAHB1PeriphClock(RCM_AHB1_PERIPH_GPIOA);
/* ADC channel 0 configuration */
GPIO_ConfigStructInit(&gpioConfig);
gpioConfig.mode = GPIO_MODE_AN;
gpioConfig.pupd = GPIO_PUPD_NOPULL;
gpioConfig.pin = GPIO_PIN_0;
GPIO_Config(GPIOA, &gpioConfig);
/* Enable ADC clock */
RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1);
/* ADC configuration */
ADC_Reset();
ADC_ConfigStructInit(&adcConfig);
adcConfig.resolution = ADC_RESOLUTION_12BIT;
adcConfig.continuousConvMode = ENABLE;
adcConfig.dataAlign = ADC_DATA_ALIGN_RIGHT;
adcConfig.extTrigEdge = ADC_EXT_TRIG_EDGE_NONE;
adcConfig.scanConvMode = DISABLE;
ADC_Config(ADC1, &adcConfig);
/*Set ADC Clock Prescaler. ADC_Clock = APB2_Clock/4, 84/4=21MHz*/
adcCommonConfig.prescaler = ADC_PRESCALER_DIV4;
ADC_CommonConfig(&adcCommonConfig);
/* ADC channel 10 Convert configuration */
ADC_ConfigRegularChannel(ADC1, ADC_CHANNEL_0, 1, ADC_SAMPLETIME_112CYCLES);
/* Enable complete conversion interupt */
ADC_EnableInterrupt(ADC1, ADC_INT_EOC);
/* NVIC configuration */
NVIC_EnableIRQRequest(ADC_IRQn, 1, 1);
/* Enable ADC */
ADC_Enable(ADC1);
/* ADC start conversion */
ADC_SoftwareStartConv(ADC1);
}
void ADC_Irq_GetData(void)
{
uint16_t adcData = 0;
if (ADC_ReadStatusFlag(ADC1, ADC_FLAG_EOC))
{
ADC_ClearStatusFlag(ADC1, ADC_FLAG_EOC);
adcData = ADC_ReadConversionValue(ADC1);
voltage = (float)adcData*3.3f/4095.0f;
}
}
void ADC_IRQHandler(void)
{
ADC_Irq_GetData();
}
串口的配置:
/*!
* @file usart.c
*
* @brief
*
* @version V1.0.1
*
* @date 2023-07-13
*
*
* @attention
*
*/
#include "main.h"
#include "apm32f4xx_gpio.h"
#include "apm32f4xx_adc.h"
#include "apm32f4xx_misc.h"
#include "apm32f4xx_rcm.h"
#include "hd_usart.h"
#include "Board.h"
void HD_Usart_Init(void)
{
USART_Config_T usartConfigStruct;
/* USART configuration */
USART_ConfigStructInit(&usartConfigStruct);
usartConfigStruct.baudRate = 115200;
usartConfigStruct.mode = USART_MODE_TX_RX;
usartConfigStruct.parity = USART_PARITY_NONE;
usartConfigStruct.stopBits = USART_STOP_BIT_1;
usartConfigStruct.wordLength = USART_WORD_LEN_8B;
usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
/* COM1 init*/
APM_TINY_COMInit(COM1, &usartConfigStruct);
}
总体的运行:
uint32_t sys_clock;
int main(void)
{
APM_TINY_LEDInit(LED2);
APM_TINY_LEDInit(LED3);
APM_TINY_LEDOff(LED2);
APM_TINY_LEDOn(LED3);
HD_ADC_Init();
HD_Usart_Init();
SysTick_Init();
sys_clock= RCM_ReadSYSCLKFreq();
while (1)
{
RUN_BLOCK(1000,GPIOE->ODATA ^= GPIO_PIN_6;);
RUN_BLOCK(1000,GPIOE->ODATA ^= GPIO_PIN_5;);
RUN_BLOCK(1000,printf("ADC采样电压 : %f V\r\n", voltage););
}
}
上图可以看出:此时系统时钟168mhz,ADC采样值再3.3v是系统的电源di电压。
串口1s打印一次测试到的经过计算的ADC的值,总体实验显示LED灯1S闪烁一次,串口1s打印一次。
自此,初步测试结束。后期将进行更加详细的测试。
|