【STM32】STM32学习笔记-DMA数据转运+AD多通道(24)

00. 目录

文章目录

    • 00. 目录
    • 01. DMA简介
    • 02. DMA相关API
      • 2.1 DMA_Init
      • 2.2 DMA_InitTypeDef
      • 2.3 DMA_Cmd
      • 2.4 DMA_SetCurrDataCounter
      • 2.5 DMA_GetFlagStatus
      • 2.6 DMA_ClearFlag
    • 03. DMA数据单通道接线图
    • 04. DMA数据单通道示例
    • 05. DMA数据多通道接线图
    • 06. DMA数据多通道示例一
    • 07. DMA数据多通道示例二
    • 08. 程序下载
    • 09. 附录

01. DMA简介

小容量产品是指闪存存储器容量在16K至32K字节之间的STM32F101xx、STM32F102xx和STM32F103xx微控制器。

中容量产品是指闪存存储器容量在64K至128K字节之间的STM32F101xx、STM32F102xx和STM32F103xx微控制器。

大容量产品是指闪存存储器容量在256K至512K字节之间的STM32F101xx和STM32F103xx微控制器。

互联型产品是指STM32F105xx和STM32F107xx微控制器。

直接存储器存取(DMA)用来提供在外设和存储器之间或者存储器和存储器之间的高速数据传输。无须CPU干预,数据可以通过DMA快速地移动,这就节省了CPU的资源来做其他操作。

两个DMA控制器有12个通道(DMA1有7个通道,DMA2有5个通道),每个通道专门用来管理来自于一个或多个外设对存储器访问的请求。还有一个仲裁器来协调各个DMA请求的优先权。

02. DMA相关API

2.1 DMA_Init

/*** @brief  Initializes the DMAy Channelx according to the specified*         parameters in the DMA_InitStruct.* @param  DMAy_Channelx: where y can be 1 or 2 to select the DMA and *   x can be 1 to 7 for DMA1 and 1 to 5 for DMA2 to select the DMA Channel.* @param  DMA_InitStruct: pointer to a DMA_InitTypeDef structure that*         contains the configuration information for the specified DMA Channel.* @retval None*/
void DMA_Init(DMA_Channel_TypeDef* DMAy_Channelx, DMA_InitTypeDef* DMA_InitStruct)
功能:根据 DMA_InitStruct 中指定的参数初始化 DMA 的通道 x 寄存器
参数:DMA Channelx:x 可以是 12…,或者 7 来选择 DMA 通道 xDMA_InitStruct:指向结构 DMA_InitTypeDef 的指针,包含了 DMA 通道 x 的配置信息    
返回值:

2.2 DMA_InitTypeDef

/** * @brief  DMA Init structure definition*/typedef struct
{uint32_t DMA_PeripheralBaseAddr; /*!< Specifies the peripheral base address for DMAy Channelx. */uint32_t DMA_MemoryBaseAddr;     /*!< Specifies the memory base address for DMAy Channelx. */uint32_t DMA_DIR;                /*!< Specifies if the peripheral is the source or destination.This parameter can be a value of @ref DMA_data_transfer_direction */uint32_t DMA_BufferSize;         /*!< Specifies the buffer size, in data unit, of the specified Channel. The data unit is equal to the configuration set in DMA_PeripheralDataSizeor DMA_MemoryDataSize members depending in the transfer direction. */uint32_t DMA_PeripheralInc;      /*!< Specifies whether the Peripheral address register is incremented or not.This parameter can be a value of @ref DMA_peripheral_incremented_mode */uint32_t DMA_MemoryInc;          /*!< Specifies whether the memory address register is incremented or not.This parameter can be a value of @ref DMA_memory_incremented_mode */uint32_t DMA_PeripheralDataSize; /*!< Specifies the Peripheral data width.This parameter can be a value of @ref DMA_peripheral_data_size */uint32_t DMA_MemoryDataSize;     /*!< Specifies the Memory data width.This parameter can be a value of @ref DMA_memory_data_size */uint32_t DMA_Mode;               /*!< Specifies the operation mode of the DMAy Channelx.This parameter can be a value of @ref DMA_circular_normal_mode.@note: The circular buffer mode cannot be used if the memory-to-memorydata transfer is configured on the selected Channel */uint32_t DMA_Priority;           /*!< Specifies the software priority for the DMAy Channelx.This parameter can be a value of @ref DMA_priority_level */uint32_t DMA_M2M;                /*!< Specifies if the DMAy Channelx will be used in memory-to-memory transfer.This parameter can be a value of @ref DMA_memory_to_memory */
}DMA_InitTypeDef;

DMA_PeripheralBaseAddr

该参数用以定义 DMA 外设基地址

DMA_MemoryBaseAddr

该参数用以定义 DMA 内存基地址

DMA_DIR

/** @defgroup DMA_data_transfer_direction * @{*/#define DMA_DIR_PeripheralDST              ((uint32_t)0x00000010)
#define DMA_DIR_PeripheralSRC              ((uint32_t)0x00000000)

DMA_PeripheralInc

/** @defgroup DMA_peripheral_incremented_mode * @{*/#define DMA_PeripheralInc_Enable           ((uint32_t)0x00000040)
#define DMA_PeripheralInc_Disable          ((uint32_t)0x00000000)

DMA_MemoryInc

/** @defgroup DMA_memory_incremented_mode * @{*/#define DMA_MemoryInc_Enable               ((uint32_t)0x00000080)
#define DMA_MemoryInc_Disable              ((uint32_t)0x00000000)

DMA_PeripheralDataSize

/** @defgroup DMA_peripheral_data_size * @{*/#define DMA_PeripheralDataSize_Byte        ((uint32_t)0x00000000)
#define DMA_PeripheralDataSize_HalfWord    ((uint32_t)0x00000100)
#define DMA_PeripheralDataSize_Word        ((uint32_t)0x00000200)

DMA_MemoryDataSize

/** @defgroup DMA_memory_data_size * @{*/#define DMA_MemoryDataSize_Byte            ((uint32_t)0x00000000)
#define DMA_MemoryDataSize_HalfWord        ((uint32_t)0x00000400)
#define DMA_MemoryDataSize_Word            ((uint32_t)0x00000800)

DMA_Mode

/** @defgroup DMA_circular_normal_mode * @{*/#define DMA_Mode_Circular                  ((uint32_t)0x00000020)
#define DMA_Mode_Normal                    ((uint32_t)0x00000000)

DMA_Priority

/** @defgroup DMA_priority_level * @{*/#define DMA_Priority_VeryHigh              ((uint32_t)0x00003000)
#define DMA_Priority_High                  ((uint32_t)0x00002000)
#define DMA_Priority_Medium                ((uint32_t)0x00001000)
#define DMA_Priority_Low                   ((uint32_t)0x00000000)

DMA_M2M

/** @defgroup DMA_memory_to_memory * @{*/#define DMA_M2M_Enable                     ((uint32_t)0x00004000)
#define DMA_M2M_Disable                    ((uint32_t)0x00000000)

2.3 DMA_Cmd

/*** @brief  Enables or disables the specified DMAy Channelx.* @param  DMAy_Channelx: where y can be 1 or 2 to select the DMA and *   x can be 1 to 7 for DMA1 and 1 to 5 for DMA2 to select the DMA Channel.* @param  NewState: new state of the DMAy Channelx. *   This parameter can be: ENABLE or DISABLE.* @retval None*/
void DMA_Cmd(DMA_Channel_TypeDef* DMAy_Channelx, FunctionalState NewState)
功能:使能或者失能指定的通道 x
参数:DMA Channelx:x 可以是 12…,或者 7 来选择 DMA 通道 xNewState:DMA 通道 x 的新状态 这个参数可以取:ENABLE 或者 DISABLE  
返回值:

2.4 DMA_SetCurrDataCounter

/*** @brief  Sets the number of data units in the current DMAy Channelx transfer.* @param  DMAy_Channelx: where y can be 1 or 2 to select the DMA and *         x can be 1 to 7 for DMA1 and 1 to 5 for DMA2 to select the DMA Channel.* @param  DataNumber: The number of data units in the current DMAy Channelx*         transfer.   * @note   This function can only be used when the DMAy_Channelx is disabled.                 * @retval None.*/
void DMA_SetCurrDataCounter(DMA_Channel_TypeDef* DMAy_Channelx, uint16_t DataNumber)
功能:设置DMA转换数据个数
参数:DMA Channelx:x 可以是 12…,或者 7 来选择 DMA 通道 xDataNumber:数据个数
返回值:

2.5 DMA_GetFlagStatus

/*** @brief  Checks whether the specified DMAy Channelx flag is set or not.* @param  DMAy_FLAG: specifies the flag to check.*   This parameter can be one of the following values:*     @arg DMA1_FLAG_GL1: DMA1 Channel1 global flag.*     @arg DMA1_FLAG_TC1: DMA1 Channel1 transfer complete flag.*     @arg DMA1_FLAG_HT1: DMA1 Channel1 half transfer flag.*     @arg DMA1_FLAG_TE1: DMA1 Channel1 transfer error flag.*     @arg DMA1_FLAG_GL2: DMA1 Channel2 global flag.*     @arg DMA1_FLAG_TC2: DMA1 Channel2 transfer complete flag.*     @arg DMA1_FLAG_HT2: DMA1 Channel2 half transfer flag.*     @arg DMA1_FLAG_TE2: DMA1 Channel2 transfer error flag.*     @arg DMA1_FLAG_GL3: DMA1 Channel3 global flag.*     @arg DMA1_FLAG_TC3: DMA1 Channel3 transfer complete flag.*     @arg DMA1_FLAG_HT3: DMA1 Channel3 half transfer flag.*     @arg DMA1_FLAG_TE3: DMA1 Channel3 transfer error flag.*     @arg DMA1_FLAG_GL4: DMA1 Channel4 global flag.*     @arg DMA1_FLAG_TC4: DMA1 Channel4 transfer complete flag.*     @arg DMA1_FLAG_HT4: DMA1 Channel4 half transfer flag.*     @arg DMA1_FLAG_TE4: DMA1 Channel4 transfer error flag.*     @arg DMA1_FLAG_GL5: DMA1 Channel5 global flag.*     @arg DMA1_FLAG_TC5: DMA1 Channel5 transfer complete flag.*     @arg DMA1_FLAG_HT5: DMA1 Channel5 half transfer flag.*     @arg DMA1_FLAG_TE5: DMA1 Channel5 transfer error flag.*     @arg DMA1_FLAG_GL6: DMA1 Channel6 global flag.*     @arg DMA1_FLAG_TC6: DMA1 Channel6 transfer complete flag.*     @arg DMA1_FLAG_HT6: DMA1 Channel6 half transfer flag.*     @arg DMA1_FLAG_TE6: DMA1 Channel6 transfer error flag.*     @arg DMA1_FLAG_GL7: DMA1 Channel7 global flag.*     @arg DMA1_FLAG_TC7: DMA1 Channel7 transfer complete flag.*     @arg DMA1_FLAG_HT7: DMA1 Channel7 half transfer flag.*     @arg DMA1_FLAG_TE7: DMA1 Channel7 transfer error flag.*     @arg DMA2_FLAG_GL1: DMA2 Channel1 global flag.*     @arg DMA2_FLAG_TC1: DMA2 Channel1 transfer complete flag.*     @arg DMA2_FLAG_HT1: DMA2 Channel1 half transfer flag.*     @arg DMA2_FLAG_TE1: DMA2 Channel1 transfer error flag.*     @arg DMA2_FLAG_GL2: DMA2 Channel2 global flag.*     @arg DMA2_FLAG_TC2: DMA2 Channel2 transfer complete flag.*     @arg DMA2_FLAG_HT2: DMA2 Channel2 half transfer flag.*     @arg DMA2_FLAG_TE2: DMA2 Channel2 transfer error flag.*     @arg DMA2_FLAG_GL3: DMA2 Channel3 global flag.*     @arg DMA2_FLAG_TC3: DMA2 Channel3 transfer complete flag.*     @arg DMA2_FLAG_HT3: DMA2 Channel3 half transfer flag.*     @arg DMA2_FLAG_TE3: DMA2 Channel3 transfer error flag.*     @arg DMA2_FLAG_GL4: DMA2 Channel4 global flag.*     @arg DMA2_FLAG_TC4: DMA2 Channel4 transfer complete flag.*     @arg DMA2_FLAG_HT4: DMA2 Channel4 half transfer flag.*     @arg DMA2_FLAG_TE4: DMA2 Channel4 transfer error flag.*     @arg DMA2_FLAG_GL5: DMA2 Channel5 global flag.*     @arg DMA2_FLAG_TC5: DMA2 Channel5 transfer complete flag.*     @arg DMA2_FLAG_HT5: DMA2 Channel5 half transfer flag.*     @arg DMA2_FLAG_TE5: DMA2 Channel5 transfer error flag.* @retval The new state of DMAy_FLAG (SET or RESET).*/
FlagStatus DMA_GetFlagStatus(uint32_t DMAy_FLAG)
功能:检查指定的 DMA 通道 x 标志位设置与否
参数:DMA_FLAG:待检查的 DMA 标志位
返回值:DMA_FLAG 的新状态(SET 或者 RESET)     

2.6 DMA_ClearFlag

/*** @brief  Clears the DMAy Channelx's pending flags.* @param  DMAy_FLAG: specifies the flag to clear.*   This parameter can be any combination (for the same DMA) of the following values:*     @arg DMA1_FLAG_GL1: DMA1 Channel1 global flag.*     @arg DMA1_FLAG_TC1: DMA1 Channel1 transfer complete flag.*     @arg DMA1_FLAG_HT1: DMA1 Channel1 half transfer flag.*     @arg DMA1_FLAG_TE1: DMA1 Channel1 transfer error flag.*     @arg DMA1_FLAG_GL2: DMA1 Channel2 global flag.*     @arg DMA1_FLAG_TC2: DMA1 Channel2 transfer complete flag.*     @arg DMA1_FLAG_HT2: DMA1 Channel2 half transfer flag.*     @arg DMA1_FLAG_TE2: DMA1 Channel2 transfer error flag.*     @arg DMA1_FLAG_GL3: DMA1 Channel3 global flag.*     @arg DMA1_FLAG_TC3: DMA1 Channel3 transfer complete flag.*     @arg DMA1_FLAG_HT3: DMA1 Channel3 half transfer flag.*     @arg DMA1_FLAG_TE3: DMA1 Channel3 transfer error flag.*     @arg DMA1_FLAG_GL4: DMA1 Channel4 global flag.*     @arg DMA1_FLAG_TC4: DMA1 Channel4 transfer complete flag.*     @arg DMA1_FLAG_HT4: DMA1 Channel4 half transfer flag.*     @arg DMA1_FLAG_TE4: DMA1 Channel4 transfer error flag.*     @arg DMA1_FLAG_GL5: DMA1 Channel5 global flag.*     @arg DMA1_FLAG_TC5: DMA1 Channel5 transfer complete flag.*     @arg DMA1_FLAG_HT5: DMA1 Channel5 half transfer flag.*     @arg DMA1_FLAG_TE5: DMA1 Channel5 transfer error flag.*     @arg DMA1_FLAG_GL6: DMA1 Channel6 global flag.*     @arg DMA1_FLAG_TC6: DMA1 Channel6 transfer complete flag.*     @arg DMA1_FLAG_HT6: DMA1 Channel6 half transfer flag.*     @arg DMA1_FLAG_TE6: DMA1 Channel6 transfer error flag.*     @arg DMA1_FLAG_GL7: DMA1 Channel7 global flag.*     @arg DMA1_FLAG_TC7: DMA1 Channel7 transfer complete flag.*     @arg DMA1_FLAG_HT7: DMA1 Channel7 half transfer flag.*     @arg DMA1_FLAG_TE7: DMA1 Channel7 transfer error flag.*     @arg DMA2_FLAG_GL1: DMA2 Channel1 global flag.*     @arg DMA2_FLAG_TC1: DMA2 Channel1 transfer complete flag.*     @arg DMA2_FLAG_HT1: DMA2 Channel1 half transfer flag.*     @arg DMA2_FLAG_TE1: DMA2 Channel1 transfer error flag.*     @arg DMA2_FLAG_GL2: DMA2 Channel2 global flag.*     @arg DMA2_FLAG_TC2: DMA2 Channel2 transfer complete flag.*     @arg DMA2_FLAG_HT2: DMA2 Channel2 half transfer flag.*     @arg DMA2_FLAG_TE2: DMA2 Channel2 transfer error flag.*     @arg DMA2_FLAG_GL3: DMA2 Channel3 global flag.*     @arg DMA2_FLAG_TC3: DMA2 Channel3 transfer complete flag.*     @arg DMA2_FLAG_HT3: DMA2 Channel3 half transfer flag.*     @arg DMA2_FLAG_TE3: DMA2 Channel3 transfer error flag.*     @arg DMA2_FLAG_GL4: DMA2 Channel4 global flag.*     @arg DMA2_FLAG_TC4: DMA2 Channel4 transfer complete flag.*     @arg DMA2_FLAG_HT4: DMA2 Channel4 half transfer flag.*     @arg DMA2_FLAG_TE4: DMA2 Channel4 transfer error flag.*     @arg DMA2_FLAG_GL5: DMA2 Channel5 global flag.*     @arg DMA2_FLAG_TC5: DMA2 Channel5 transfer complete flag.*     @arg DMA2_FLAG_HT5: DMA2 Channel5 half transfer flag.*     @arg DMA2_FLAG_TE5: DMA2 Channel5 transfer error flag.* @retval None*/
void DMA_ClearFlag(uint32_t DMAy_FLAG)
功能:清除 DMA 通道 x 待处理标志位
参数:DMA_FLAG:待清除的 DMA 标志位,使用操作符“|”可以同时选中多个DMA 标志位
返回值:

03. DMA数据单通道接线图

在这里插入图片描述

04. DMA数据单通道示例

dma.h

#ifndef __DMA_H__#define __DMA_H__#include "stm32f10x.h"                  // Device headervoid dma_init(uint32_t src, uint32_t dest, uint32_t size);void dma_trasfer(uint32_t size);#endif

dma.c


#include "dma.h"void dma_init(uint32_t src, uint32_t dest, uint32_t size)
{DMA_InitTypeDef DMA_InitStruct;//开启时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);DMA_InitStruct.DMA_MemoryBaseAddr = dest;DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;DMA_InitStruct.DMA_PeripheralBaseAddr = src;DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Enable;DMA_InitStruct.DMA_Priority = DMA_Priority_Low;DMA_InitStruct.DMA_BufferSize = size;DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;DMA_InitStruct.DMA_M2M = DMA_M2M_Enable;DMA_Init(DMA1_Channel1, &DMA_InitStruct);DMA_Cmd(DMA1_Channel1, DISABLE);}void dma_trasfer(uint32_t size)
{DMA_Cmd(DMA1_Channel1, DISABLE);DMA_SetCurrDataCounter(DMA1_Channel1, size);DMA_Cmd(DMA1_Channel1, ENABLE);while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);DMA_ClearFlag(DMA1_FLAG_TC1);
}

测试程序1 main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"
#include "dma.h"const uint8_t src[] = {0x1, 0x2, 0x3, 0x4};uint8_t dest[] = {0, 0, 0, 0};int main(void){		 //初始化OLED_Init();dma_init((uint32_t)src, (uint32_t)dest, 4);OLED_ShowHexNum(1, 1, src[0], 2);OLED_ShowHexNum(1, 4, src[1], 2);OLED_ShowHexNum(1, 7, src[2], 2);OLED_ShowHexNum(1, 10, src[3], 2);OLED_ShowHexNum(2, 1, dest[0], 2);OLED_ShowHexNum(2, 4, dest[1], 2);OLED_ShowHexNum(2, 7, dest[2], 2);OLED_ShowHexNum(2, 10, dest[3], 2); dma_trasfer(4);OLED_ShowHexNum(3, 1, src[0], 2);OLED_ShowHexNum(3, 4, src[1], 2);OLED_ShowHexNum(3, 7, src[2], 2);OLED_ShowHexNum(3, 10, src[3], 2);OLED_ShowHexNum(4, 1, dest[0], 2);OLED_ShowHexNum(4, 4, dest[1], 2);OLED_ShowHexNum(4, 7, dest[2], 2);OLED_ShowHexNum(4, 10, dest[3], 2); 	 while(1){}return 0;}

测试程序2 main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"
#include "dma.h"uint8_t src[] = {0x1, 0x2, 0x3, 0x4};uint8_t dest[] = {0, 0, 0, 0};int main(void){		 //初始化OLED_Init();dma_init((uint32_t)src, (uint32_t)dest, 4);OLED_ShowString(1, 1, "DataA");OLED_ShowString(3, 1, "DataB");OLED_ShowHexNum(1, 8, (uint32_t)src, 8);OLED_ShowHexNum(3, 8, (uint32_t)dest, 8);while(1){src[0] ++;src[1] ++;src[2] ++;src[3] ++;OLED_ShowHexNum(2, 1, src[0], 2);		 OLED_ShowHexNum(2, 4, src[1], 2);OLED_ShowHexNum(2, 7, src[2], 2);OLED_ShowHexNum(2, 10, src[3], 2);OLED_ShowHexNum(4, 1, dest[0], 2);OLED_ShowHexNum(4, 4, dest[1], 2);OLED_ShowHexNum(4, 7, dest[2], 2);OLED_ShowHexNum(4, 10, dest[3], 2); delay_ms(1000);dma_trasfer(4);OLED_ShowHexNum(2, 1, src[0], 2);		 OLED_ShowHexNum(2, 4, src[1], 2);OLED_ShowHexNum(2, 7, src[2], 2);OLED_ShowHexNum(2, 10, src[3], 2);OLED_ShowHexNum(4, 1, dest[0], 2);OLED_ShowHexNum(4, 4, dest[1], 2);OLED_ShowHexNum(4, 7, dest[2], 2);OLED_ShowHexNum(4, 10, dest[3], 2); delay_ms(1000);}return 0;}

05. DMA数据多通道接线图

在这里插入图片描述

06. DMA数据多通道示例一

单次转换 扫描模式

adc.h

#ifndef __ADC_H__
#define __ADC_H__#include "stm32f10x.h"                  // Device headerextern uint16_t adc_value[4];void adc_init(void);void adc_getvalue(void);#endif /*__ADC_H__*/

adc.c

#include "adc.h"uint16_t adc_value[4] = {0};void adc_init(void)
{GPIO_InitTypeDef GPIO_InitStructure;ADC_InitTypeDef ADC_InitStruct;DMA_InitTypeDef DMA_InitStruct;//开启ADC时钟  PA0 --> ADC1_0RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//开启GPIOA的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//开启时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//设置为6分频  72M / 6 = 12M RCC_ADCCLKConfig(RCC_PCLK2_Div6);//GPIO配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;GPIO_InitStructure.GPIO_Speed =   GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);// 4个ADC通道ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);//ADC配置ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; //单次转换ADC_InitStruct.ADC_ScanConvMode = ENABLE; //扫描模式ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;ADC_InitStruct.ADC_NbrOfChannel = 4; //4个通道ADC_Init(ADC1, &ADC_InitStruct);DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)adc_value;DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMA_InitStruct.DMA_Priority = DMA_Priority_Low;DMA_InitStruct.DMA_BufferSize = 4;DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel1, &DMA_InitStruct);DMA_Cmd(DMA1_Channel1, DISABLE);ADC_DMACmd(ADC1, ENABLE);//使能ADCADC_Cmd(ADC1, ENABLE);//校准ADCADC_ResetCalibration(ADC1);while(ADC_GetResetCalibrationStatus(ADC1));ADC_StartCalibration(ADC1);while(ADC_GetCalibrationStatus(ADC1));}void adc_getvalue(void)
{DMA_Cmd(DMA1_Channel1, DISABLE);DMA_SetCurrDataCounter(DMA1_Channel1, 4);DMA_Cmd(DMA1_Channel1, ENABLE);ADC_SoftwareStartConvCmd(ADC1, ENABLE);while(DMA_GetFlagStatus(DMA1_FLAG_TC1) == RESET);DMA_ClearFlag(DMA1_FLAG_TC1);
}

main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"
#include "adc.h"int main(void){	//初始化OLED_Init();adc_init();//显示字符串OLED_ShowString(1, 1, "AD0: ");OLED_ShowString(2, 1, "AD1: ");OLED_ShowString(3, 1, "AD2: ");OLED_ShowString(4, 1, "AD3: ");while(1){adc_getvalue();OLED_ShowNum(1, 5, adc_value[0], 4);OLED_ShowNum(2, 5, adc_value[1], 4);OLED_ShowNum(3, 5, adc_value[2], 4);OLED_ShowNum(4, 5, adc_value[3], 4);		 	 delay_ms(100);}}

07. DMA数据多通道示例二

连续扫描,循环转换

adc.h

#ifndef __ADC_H__
#define __ADC_H__#include "stm32f10x.h"                  // Device headerextern uint16_t adc_value[4];void adc_init(void);#endif /*__ADC_H__*/

adc.c

#include "adc.h"uint16_t adc_value[4] = {0};void adc_init(void)
{GPIO_InitTypeDef GPIO_InitStructure;ADC_InitTypeDef ADC_InitStruct;DMA_InitTypeDef DMA_InitStruct;//开启ADC时钟  PA0 --> ADC1_0RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//开启GPIOA的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//开启时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//设置为6分频  72M / 6 = 12M RCC_ADCCLKConfig(RCC_PCLK2_Div6);//GPIO配置 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;GPIO_InitStructure.GPIO_Speed =   GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);// 4个ADC通道ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_55Cycles5);ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);//ADC配置ADC_InitStruct.ADC_ContinuousConvMode = ENABLE; //连续模式ADC_InitStruct.ADC_ScanConvMode = ENABLE; //扫描模式ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;ADC_InitStruct.ADC_NbrOfChannel = 4; //4个通道ADC_Init(ADC1, &ADC_InitStruct);DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)adc_value;DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;DMA_InitStruct.DMA_Mode = DMA_Mode_Circular; //DMA循环模式DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;DMA_InitStruct.DMA_Priority = DMA_Priority_Low;DMA_InitStruct.DMA_BufferSize = 4;DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;DMA_Init(DMA1_Channel1, &DMA_InitStruct);DMA_Cmd(DMA1_Channel1, ENABLE);ADC_DMACmd(ADC1, ENABLE);//使能ADCADC_Cmd(ADC1, ENABLE);//校准ADCADC_ResetCalibration(ADC1);while(ADC_GetResetCalibrationStatus(ADC1));ADC_StartCalibration(ADC1);while(ADC_GetCalibrationStatus(ADC1));//ADC触发ADC_SoftwareStartConvCmd(ADC1, ENABLE);}

main.c

#include "stm32f10x.h"#include "delay.h"
#include "oled.h"
#include "adc.h"int main(void){	//初始化OLED_Init();adc_init();//显示字符串OLED_ShowString(1, 1, "AD0: ");OLED_ShowString(2, 1, "AD1: ");OLED_ShowString(3, 1, "AD2: ");OLED_ShowString(4, 1, "AD3: ");while(1){	 OLED_ShowNum(1, 5, adc_value[0], 4);OLED_ShowNum(2, 5, adc_value[1], 4);OLED_ShowNum(3, 5, adc_value[2], 4);OLED_ShowNum(4, 5, adc_value[3], 4);		 delay_ms(100);}}

08. 程序下载

19-DMA单通道.rar

20-DMA-ADC多通道1.rar

21-DMA-ADC多通道2.rar

09. 附录

参考: 【STM32】江科大STM32学习笔记汇总

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/603104.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

关于一个热成像仪的总结(一)硬件篇电源电路

1、电源部分 电源部分电路原理是这样的通过3.7V的锂电池供电&#xff0c;用Type-C选用TP4056作为充电电路给电池充电。使用MP2161开关电源作为5转3.3V 电源为MCU供电。 1-1电池 待定 1-2充电管理芯片TP4056 参考datasheet&#xff1a;https://atta.szlcsc.com/upload/publi…

数据传输:连接数字世界的纽带

在当今数字化时代&#xff0c;数据传输已经成为了我们日常生活和工作中不可或缺的一部分。无论是通过互联网发送电子邮件、传输文件&#xff0c;还是在智能手机上浏览网页、观看视频&#xff0c;都离不开数据传输的支持。本文将对数据传输进行深度分析&#xff0c;探讨其原理、…

Vue 3中toRaw和markRaw的使用

Vue 3的响应性系统 在Vue 3中&#xff0c;响应性系统是构建动态Web应用程序的关键部分。Vue使用响应性系统来跟踪依赖关系&#xff0c;使数据更改能够自动更新视图。这使得Vue应用程序在数据变化时能够高效地更新DOM。Vue 3引入了新的Proxy对象来替代Vue 2中的Object.definePro…

vue3+echart绘制中国地图并根据后端返回的坐标实现涟漪动画效果

1.效果图 2.前期准备 main.js app.use(BaiduMap, {// ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */ak: sRDDfAKpCSG5iF1rvwph4Q95M6tDCApL,// v:3.0, // 默认使用3.0// type: WebGL // ||API 默认API (使用此模式 BMapBMapGL) });i…

CSDN博客重新更新

说来惭愧&#xff0c;好久没更新博客文章&#xff0c;导致个人博客网站&#xff1a;https://lenky.info/ 所在的网络空间和域名都过期了都没发觉&#xff0c;直到有个同事在Dim上问我我的个人博客为啥打不开了。。。幸好之前有做整站备份&#xff0c;后续慢慢把内容都迁回CSDN上…

学习笔记——C++运算符之比较运算符

作用&#xff1a;用于表达式的比较&#xff0c;并返回一个真值或假值 比较运算符有以下符号&#xff1a; #include<bits/stdc.h> using namespace std; int main(){//int a10;int b20;cout<<(ab)<<endl;//0//!cout<<(a!b)<<endl;//1//>cout&…

第十二章 编写测试报告/项目总结(笔记)

一、编写测试报告 测试报告的侧重点是什么&#xff1f; 结果分析 1.bug数据统计&#xff0c;bug状态&#xff0c;bug严重级别统计&#xff0c;测试各阶段统计 2.需求覆盖率&#xff0c;用例数&#xff0c;用例执行率&#xff0c;通过率&#xff0c;bug关闭率&#xff0c;bug遗…

CSS实现瀑布流

column 多行布局实现瀑布流 1.column 实现瀑布流主要依赖两个属性。 2.column-count 属性&#xff0c;是控制屏幕分为多少列。 3.column-gap 属性&#xff0c;是控制列与列之间的距离。 <!DOCTYPE html> <html lang"en"> <head><meta charset&q…

链 表

3_1 删除链表中的节点 Answer-将被删节点下一个val复制到待删除节点&#xff0c;然后将待删节点直接连接到下下一个节点即可。 学到&#xff1a; /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) …

The most simple way to use Postman

Open Postman: Launch the Postman app.Create a Request: Click on the “New” button, then select “Request”. This creates a new tab for a request.Set Request Type to POST: On the new request tab, you’ll see a dropdown menu next to the URL field. Select “…

第十天:信息打点-APPamp;小程序篇amp;抓包封包amp;XP框架amp;反编译amp;资产提取

信息打点-APP&小程序 一、内在收集-代码 从app代码中去收集 1、移动端AppInfoScanner工具信息收集 安卓语法&#xff1a; python app.py android -i <Your apk file> 这个是从app代码中提取信息。 有些app会限制代理抓包&#xff0c;需要进行解壳。 类似CDN的…

【TypeScript】泛型

文章目录 1、介绍2、泛型函数- 定义泛型- 使用泛型 3、泛型接口3.1 约束对象形状&#xff1a;3.2 泛型接口作为函数类型3.3 泛型接口作为字典的类型3.4 泛型接口与多个类型参数 4、泛型类4.1 定义一个泛型类4.2 定义多个类型参数 5、泛型约束5.1 定义约束条件5.2 在泛型约束中使…

Vue中的组件通信方式及应用场景

在Vue中&#xff0c;组件通信有以下几种方式&#xff1a; Props / $emit&#xff1a;父组件通过给子组件传递props属性&#xff0c;子组件通过$emit事件将数据传递给父组件。适用于父组件向子组件传递数据。 自定义事件&#xff1a;父组件通过$on监听子组件触发的事件&#xf…

数据链路层(Data Link Layer)

数据链路层&#xff08;Data Link Layer&#xff09;是计算机网络体系结构中的一层&#xff0c;位于物理层和网络层之间。它的主要功能是在物理传输媒体上建立和管理数据链路。数据链路层的设计和实现对于网络通信的可靠性和效率至关重要。在本文中&#xff0c;我们将探讨数据链…

C#-接口

接口 (interface) 定义了一个可由类和结构实现的协定。接口可以包含方法、属性、事件和索引器。接口不提供它所定义的成员的实现 — 它仅指定实现该接口的类或结构必须提供的成员。 接口可支持多重继承。在下面的示例中&#xff0c;接口 IComboBox 同时从 ITextBox 和 IListBox…

IIS+SDK+VS2010+SP1+SQL server2012全套工具包及安装教程

前言 今天花了两个半小时安装这一整套配置&#xff0c;这个文章的目标是将安装时间缩短到1个小时 正文 安装步骤如下&#xff1a; VS2010 —> service pack 1 —>SQL server2012 —> IIS —> SDK 工具包链接如下&#xff1a; https://pan.baidu.com/s/1WQD-KfiUW…

[技术杂谈]使用VLC将视频转成一个可循环rtsp流

通过vlc播放器&#xff0c;将一个视频转成rtsp流&#xff0c;搭建一个rtsp服务器。rtsp客户端可访问这个视频的rtsp流。 1. 打开vlc播放器&#xff0c;使用的版本如下 2. 菜单&#xff1a;媒体 ---> 流 3. 添加视频文件&#xff0c;点击添加一个mp4 文件 4. 选择串流&…

如何安装和使用夜神模拟器连接Android Studio

目录 简介 一、安装 二、使用 三、更多资源 简介 夜神模拟器是一款在Windows平台上运行的Android模拟器软件。它能够模拟Android操作系统环境&#xff0c;让用户在电脑上轻松体验Android应用程序。夜神模拟器的功能强大&#xff0c;可以满足各种需求&#xff0c;无论是娱乐…

故障诊断 | 基于FFT频谱与小波时频图的双流CNN轴承故障诊断模型(matlab +python)

目录 效果一览基本介绍程序设计参考资料 效果一览 基本介绍 故障诊断 | 基于FFT频谱与小波时频图的双流CNN轴承故障诊断模型&#xff08;matlab python&#xff09; 基于FFT频谱与小波时频图的双流CNN轴承故障诊断模型 特征拼接 python&#xff08;pytorch&#xff09; 基于2D-…

可视化速通知识点

本文仅仅是我对可视化主要知识的一些记忆与思考&#xff0c;欢迎大家学习与批评指正。 首先&#xff0c;什么是可视化&#xff1f;可视化是利用计算机图形学和图像处理技术&#xff0c;将数据在屏幕上显示出来&#xff0c;并使用交互式处理的方法、理论和技术。 可视化的基本流…