STM32F7,3ADC通过PA0采样,使用规则同步方式传输到DMA2 传输到ADC1,2,3所对应的Stream上。。。
我现在可以采样,数据都储存在ADC1_DR,ADC2_DR和ADC3_DR内。但是使用HAL_ADCEx_MultiModeStart_DMA(ADC_HandleTypeDef* hadc, uint32_t* pData, uint32_t Length),无法传输到DMA中(设置的buffer中)
如何能将ADC采样后,储存到buffer中/dma中
谢谢~~~感谢大神们的帮助啊
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"
#include "lcd.h"
#include "sdram.h"
#include "adc.h"
#include "dma_tx.h"
#define BUF_SIZE 7800
uint32_t BUFFER[BUF_SIZE];
#define ADC1_DR_Add 0x4001204C
#define ADC2_DR_Add 0x4001214C
#define ADC3_DR_Add 0x4001224C
#define DataLength_ADC 32
int main(void)
{
//u16 i;
u8 z;
//u8 t=0;
Cache_Enable(); //打开L1-Cache
HAL_Init(); //初始化HAL库
Stm32_Clock_Init(432,25,2,9); //设置时钟,216Mhz
delay_init(216); //延时初始化
uart_init(115200); //串口初始化
LED_Init(); //初始化LED
KEY_Init(); //初始化按键
SDRAM_Init(); //初始化SDRAM
MY_ADC123_Init();
//HAL_DMA_Start(&DMA_Adc1_Handler,ADC1_DR_Add,*BUFFER,DataLength_ADC);
HAL_ADCEx_MultiModeStart_DMA(&ADC1_Handler,BUFFER,DataLength_ADC);
HAL_ADCEx_MultiModeStart_DMA(&ADC2_Handler,BUFFER,DataLength_ADC);
HAL_ADCEx_MultiModeStart_DMA(&ADC3_Handler,BUFFER,DataLength_ADC);
z=HAL_ADCEx_MultiModeGetValue(&ADC1_Handler);
printf("
DMA_Tx: %d
",z);
}
这个是我的adc.c
#include "adc.h"
#include "delay.h"
#include "led.h"
ADC_HandleTypeDef ADC1_Handler;//ADC句柄
ADC_HandleTypeDef ADC2_Handler;
ADC_HandleTypeDef ADC3_Handler;
ADC_ChannelConfTypeDef ADC1_ChanConf;
ADC_ChannelConfTypeDef ADC2_ChanConf;
ADC_ChannelConfTypeDef ADC3_ChanConf;
DMA_HandleTypeDef DMA_Adc1_Handler;
DMA_HandleTypeDef DMA_Adc2_Handler;
DMA_HandleTypeDef DMA_Adc3_Handler;
ADC_MultiModeTypeDef ADC_MultiMode_Handler;
//初始化ADC
//ch: ADC_channels
//通道值 0~16取值范围为:ADC_CHANNEL_0~ADC_CHANNEL_16
void MY_ADC123_Init(void)
{
/*** ADC1 **************************************************************************************************/
ADC1_Handler.Instance=ADC1;
ADC1_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4分频,ADCCLK=PCLK2/4=108/4=27MHZ
ADC1_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12位模式
ADC1_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //右对齐
ADC1_Handler.Init.ScanConvMode=DISABLE; //扫描模式
ADC1_Handler.Init.EOCSelection=ENABLE; //EOC中断
ADC1_Handler.Init.ContinuousConvMode=DISABLE; //连续转换
ADC1_Handler.Init.NbrOfConversion=1; //1个转换在规则序列中 也就是只转换规则序列1
ADC1_Handler.Init.DiscontinuousConvMode=DISABLE; //禁止不连续采样模式
ADC1_Handler.Init.NbrOfDiscConversion=0; //不连续采样通道数为0
ADC1_Handler.Init.ExternalTrigConv=ADC_SOFTWARE_START; //软件触发
ADC1_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;//使用软件触发
ADC1_Handler.Init.DMAContinuousRequests=ENABLE; //DMA请求
ADC1_ChanConf.Channel=ADC_CHANNEL_0; //通道
ADC1_ChanConf.Rank=1; //1个序列,每触发一次只转换一条通道
ADC1_ChanConf.SamplingTime=ADC_SAMPLETIME_3CYCLES; //采样时间
ADC1_ChanConf.Offset=0;
HAL_ADC_ConfigChannel(&ADC1_Handler,&ADC1_ChanConf); //通道配置
HAL_ADC_Init(&ADC1_Handler); //初始化
HAL_ADC_ConfigChannel(&ADC1_Handler, &ADC1_ChanConf);
HAL_ADC_Start(&ADC1_Handler);
/*** ADC2 **************************************************************************************************/
ADC2_Handler.Instance=ADC2;
ADC2_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4分频,ADCCLK=PCLK2/4=108/4=27MHZ
ADC2_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12位模式
ADC2_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //右对齐
ADC2_Handler.Init.ScanConvMode=DISABLE; //扫描模式
ADC2_Handler.Init.EOCSelection=ENABLE; //EOC中断
ADC2_Handler.Init.ContinuousConvMode=DISABLE; //连续转换
ADC2_Handler.Init.NbrOfConversion=1; //1个转换在规则序列中 也就是只转换规则序列1
ADC2_Handler.Init.DiscontinuousConvMode=DISABLE; //禁止不连续采样模式
ADC2_Handler.Init.NbrOfDiscConversion=0; //不连续采样通道数为0
ADC2_Handler.Init.ExternalTrigConv=ADC_SOFTWARE_START; //软件触发
ADC2_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;//使用软件触发
ADC2_Handler.Init.DMAContinuousRequests=ENABLE; //DMA请求
HAL_ADC_Init(&ADC2_Handler); //初始化
ADC2_ChanConf.Channel=ADC_CHANNEL_2; //通道
ADC2_ChanConf.Rank=2; //1个序列,每触发一次只转换一条通道
ADC2_ChanConf.SamplingTime=ADC_SAMPLETIME_3CYCLES; //采样时间
ADC2_ChanConf.Offset=0;
HAL_ADC_ConfigChannel(&ADC2_Handler,&ADC2_ChanConf); //通道配置
HAL_ADC_ConfigChannel(&ADC2_Handler, &ADC2_ChanConf);
HAL_ADC_Start(&ADC2_Handler);
/*** ADC3 **************************************************************************************************/
ADC3_Handler.Instance=ADC3;
ADC3_Handler.Init.ClockPrescaler=ADC_CLOCK_SYNC_PCLK_DIV4; //4分频,ADCCLK=PCLK2/4=108/4=27MHZ
ADC3_Handler.Init.Resolution=ADC_RESOLUTION_12B; //12位模式
ADC3_Handler.Init.DataAlign=ADC_DATAALIGN_RIGHT; //右对齐
ADC3_Handler.Init.ScanConvMode=DISABLE; //扫描模式
ADC3_Handler.Init.EOCSelection=ENABLE; //EOC中断
ADC3_Handler.Init.ContinuousConvMode=DISABLE; //关闭连续转换
ADC3_Handler.Init.NbrOfConversion=1; //1个转换在规则序列中 也就是只转换规则序列1
ADC3_Handler.Init.DiscontinuousConvMode=DISABLE; //禁止不连续采样模式
ADC3_Handler.Init.NbrOfDiscConversion=0; //不连续采样通道数为0
ADC3_Handler.Init.ExternalTrigConv=ADC_SOFTWARE_START ; //软件触发
ADC3_Handler.Init.ExternalTrigConvEdge=ADC_EXTERNALTRIGCONVEDGE_NONE;//使用软件触发
ADC3_Handler.Init.DMAContinuousRequests=ENABLE; //DMA请求
HAL_ADC_Init(&ADC3_Handler);
ADC3_ChanConf.Channel=ADC_CHANNEL_6; //通道
ADC3_ChanConf.Rank=3; //1个序列,每触发一次只转换一条通道
ADC3_ChanConf.SamplingTime=ADC_SAMPLETIME_3CYCLES; //采样时间
ADC3_ChanConf.Offset=0;
HAL_ADC_ConfigChannel(&ADC3_Handler,&ADC3_ChanConf); //通道配置
HAL_ADC_ConfigChannel(&ADC3_Handler, &ADC3_ChanConf); //初始化
HAL_ADC_Start(&ADC3_Handler);
/*** Multi **************************************************************************************************/
ADC_MultiMode_Handler.Mode = ADC_TRIPLEMODE_REGSIMULT;
ADC_MultiMode_Handler.DMAAccessMode=ADC_CCR_DMA_0;
ADC_MultiMode_Handler.TwoSamplingDelay=0;
HAL_ADCEx_MultiModeConfigChannel(&ADC1_Handler,&ADC_MultiMode_Handler);
HAL_ADCEx_MultiModeConfigChannel(&ADC2_Handler,&ADC_MultiMode_Handler);
HAL_ADCEx_MultiModeConfigChannel(&ADC3_Handler,&ADC_MultiMode_Handler);
}
//ADC底层驱动,引脚配置,时钟使能
//此函数会被HAL_ADC_Init()调用
//hadc:ADC句柄
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_Initure;
__HAL_RCC_ADC1_CLK_ENABLE(); //使能ADC1时钟
__HAL_RCC_ADC2_CLK_ENABLE();
__HAL_RCC_ADC3_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE(); //开启GPIOA时钟
__HAL_RCC_DMA2_CLK_ENABLE();
GPIO_Initure.Pin=GPIO_PIN_0;
GPIO_Initure.Mode=GPIO_MODE_ANALOG; //模拟
GPIO_Initure.Pull=GPIO_NOPULL; //不带上下拉
HAL_GPIO_Init(GPIOA,&GPIO_Initure); //PA0 for ADC123
__HAL_LINKDMA(hadc, DMA_Handle, DMA_Adc1_Handler);
//Tx DMA config
DMA_Adc1_Handler.Instance = DMA2_Stream0; //数据流选择
DMA_Adc1_Handler.Init.Channel = DMA_CHANNEL_0; //通道选择
DMA_Adc1_Handler.Init.Direction = DMA_PERIPH_TO_MEMORY; //存储器到外设
DMA_Adc1_Handler.Init.PeriphInc = DMA_PINC_DISABLE; //外设非增量模式
DMA_Adc1_Handler.Init.MemInc = DMA_MINC_ENABLE; //存储器增量模式
DMA_Adc1_Handler.Init.PeriphDataAlignment= DMA_PDATAALIGN_BYTE; //外设数据长度:8位
DMA_Adc1_Handler.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; //存储器数据长度:8位
DMA_Adc1_Handler.Init.Mode = DMA_CIRCULAR; //外设流控模式
DMA_Adc1_Handler.Init.Priority = DMA_PRIORITY_HIGH; //高优先级
DMA_Adc1_Handler.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
DMA_Adc1_Handler.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
DMA_Adc1_Handler.Init.MemBurst = DMA_MBURST_INC8 ; //存储器突发单次传输
DMA_Adc1_Handler.Init.PeriphBurst = DMA_PBURST_SINGLE; //外设突发单次传输
HAL_DMA_Init(&DMA_Adc1_Handler);
//Tx DMA config
DMA_Adc2_Handler.Instance = DMA2_Stream2; //数据流选择
DMA_Adc2_Handler.Init.Channel = DMA_CHANNEL_1; //通道选择
DMA_Adc2_Handler.Init.Direction = DMA_PERIPH_TO_MEMORY; //存储器到外设
DMA_Adc2_Handler.Init.PeriphInc = DMA_PINC_DISABLE; //外设非增量模式
DMA_Adc2_Handler.Init.MemInc = DMA_MINC_ENABLE; //存储器增量模式
DMA_Adc2_Handler.Init.PeriphDataAlignment= DMA_PDATAALIGN_BYTE; //外设数据长度:8位
DMA_Adc2_Handler.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; //存储器数据长度:8位
DMA_Adc2_Handler.Init.Mode = DMA_CIRCULAR; //外设流控模式
DMA_Adc2_Handler.Init.Priority = DMA_PRIORITY_HIGH; //高优先级
DMA_Adc2_Handler.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
DMA_Adc2_Handler.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
DMA_Adc2_Handler.Init.MemBurst = DMA_MBURST_INC8 ; //存储器突发单次传输
DMA_Adc2_Handler.Init.PeriphBurst = DMA_PBURST_SINGLE; //外设突发单次传输
HAL_DMA_Init(&DMA_Adc2_Handler);
//Tx DMA config
DMA_Adc3_Handler.Instance = DMA2_Stream1; //数据流选择
DMA_Adc3_Handler.Init.Channel = DMA_CHANNEL_2; //通道选择
DMA_Adc3_Handler.Init.Direction = DMA_PERIPH_TO_MEMORY; //存储器到外设
DMA_Adc3_Handler.Init.PeriphInc = DMA_PINC_DISABLE; //外设非增量模式
DMA_Adc3_Handler.Init.MemInc = DMA_MINC_ENABLE; //存储器增量模式
DMA_Adc3_Handler.Init.PeriphDataAlignment= DMA_PDATAALIGN_BYTE; //外设数据长度:8位
DMA_Adc3_Handler.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; //存储器数据长度:8位
DMA_Adc3_Handler.Init.Mode = DMA_CIRCULAR; //外设流控模式
DMA_Adc3_Handler.Init.Priority = DMA_PRIORITY_HIGH; //高优先级
DMA_Adc3_Handler.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
DMA_Adc3_Handler.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
DMA_Adc3_Handler.Init.MemBurst = DMA_MBURST_INC8 ; //存储器突发单次传输
DMA_Adc3_Handler.Init.PeriphBurst = DMA_PBURST_SINGLE; //外设突发单次传输
HAL_DMA_Init(&DMA_Adc3_Handler);
__HAL_LINKDMA(hadc, DMA_Handle, DMA_Adc2_Handler);
__HAL_LINKDMA(hadc, DMA_Handle, DMA_Adc3_Handler);
}
一周热门 更多>