本帖最后由 逍遥李 于 2015-10-10 16:36 编辑
开发环境:
keil v5.16
cubemx v4.10
主要功能:
1、实现DMA方式循环采样8个ADC通道,并经串口发送到PC;
2、串口发送使用UART2经printf重定向;
实现创建过程:
1】打开cubemx,选择STM32F446RE,创建工程,使能UART2,如下图所示:
2】选择ADC2,选中图中AIN0、……AIN10的8个通道
3】时钟配置,无需更改,如下图所示使用默认设置
4】配置ADC参数,这一步非常关键,一定要严格按照图示配置每一项!
5】使能ADC2的DMA功能,并进行图中所示配置:
6】UART2的配置信息使用默认即可,如图所示:
7】配置信息已经完成,可以生成代码,记得选中生成所有的.c和.h文件!
8】打开cubemx生成的工程,只需要按照下面更改main.c即可,
手边只有2个可调精密电阻,所以只测试了通道AIN0和通道AIN10,效果不错,接线和串口输出如下图所示:
9】完整工程文件见附件!新手,求指教,欢迎跟帖讨论!
/* Includes ------------------------------------------------------------------*/
#include "stm32f4xx_hal.h"
#include "adc.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/
#undef __GNUC__
#ifdef __GNUC__
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
uint16_t ADC_value[8]={1,1,1,1,1,1,1,1};
/* USER CODE END 0 */
int main(void)
{
uint8_t i;
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC2_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
HAL_Delay(10);
HAL_ADC_Start_DMA(&hadc2,(uint32_t *)&ADC_value,8);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
printf("Current time =: %d\r\n",HAL_GetTick());
printf("ADC_Value= %d,%d,%d,%d,%d,%d,%d,%d;\r\n",ADC_value[0],ADC_value[1],ADC_value[2],ADC_value[3],ADC_value[4],ADC_value[5],ADC_value[6],ADC_value[7]);
printf("\n\r\n\r");
for(i=0;i<8;i++)
{
ADC_value=0;
}
HAL_Delay(1000);
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
………………
………………
………………
/* USER CODE BEGIN 4 */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
/* USER CODE END 4 */
|