【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.rhkb.cn/news/235797.html

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

相关文章

jupyter notebook 配置conda 虚拟环境python

conda创建python环境 conda create -n openvoice python3.9 激活环境 source activate openvoice 在虚拟环境中安装ipykernel pip install ipykernel 添加虚拟环境进到 jupyter notebook python -m ipykernel install --user --name openvoice --display-name openvoice …

base64与BytesIO图片进行编码、解码;api调用

base64与BytesIO简单介绍 io.BytesIO 和 Base64 编码都是用于在内存中处理二进制数据的方法&#xff0c;但它们的目的和使用场景有所不同。 1&#xff09; io.BytesIO io.BytesIO 是 Python io 库中的一个类&#xff0c;它提供了一个在内存中处理二进制数据的接口&#xff0…

Linux-shell简单学习

我是南城余&#xff01;阿里云开发者平台专家博士证书获得者&#xff01; 欢迎关注我的博客&#xff01;一同成长&#xff01; 一名从事运维开发的worker&#xff0c;记录分享学习。 专注于AI&#xff0c;运维开发&#xff0c;windows Linux 系统领域的分享&#xff01; 其他…

Qt6入门教程 4:Qt Creator常用技巧

在上一篇Qt6入门教程 3&#xff1a;创建Hello World项目中&#xff0c;通过创建一个Qt项目&#xff0c;对Qt Creator已经有了比较直观的认识&#xff0c;本文将介绍它的一些常用技巧。 Qt Creator启动后默认显示欢迎页面 创建项目已经用过了&#xff0c;打开项目也很简单&#…

C++力扣题目226--翻转二叉树

给你一棵二叉树的根节点 root &#xff0c;翻转这棵二叉树&#xff0c;并返回其根节点。 示例 1&#xff1a; 输入&#xff1a;root [4,2,7,1,3,6,9] 输出&#xff1a;[4,7,2,9,6,3,1]示例 2&#xff1a; 输入&#xff1a;root [2,1,3] 输出&#xff1a;[2,3,1]示例 3&#x…

Apache Doris 入门 10 问

基于 Apache Doris 在读写流程、副本一致性机制、 存储机制、高可用机制等方面的常见疑问点进行梳理&#xff0c;并以问答形式进行解答。在开始之前&#xff0c;我们先对本文相关的名词进行解释&#xff1a; FE&#xff1a;Frontend&#xff0c;即 Doris 的前端节点。主要负责接…

《堆排序》与《Top—k》

目录 ​编辑 前言&#xff1a; 关于《堆排序》&#xff1a; 第一步&#xff1a;建堆 第二步&#xff1a;排序 《Top—K问题》 关于Top—k问题&#xff1a; 前言&#xff1a; 我们在前面的blog中&#xff0c;对于《堆》已经有了初步的概念&#xff0c;那么接下来我们可以…

FineBI实战项目一(18):每小时上架商品个数分析开发

点击新建组件&#xff0c;创建每小时上架商品个数组件。 选择线图&#xff0c;拖拽cnt&#xff08;总数&#xff09;到纵轴&#xff0c;拖拽hourStr到横轴。 修改横轴和纵轴的文字。 调节连线样式。 添加组件到仪表板。

MYSQL的学习——单行函数详解

目录 1. 数值函数 1) 基本函数 2) 角度与弧度互换函数 3) 三角函数 4) 指数与对数函数 5) 进制间的转换 2. 字符串函数 3. 日期和时间函数 1) 获取日期、时间 2) 日期与时间戳的转换 3) 获取月份、星期、星期数、天数等函数 4) 日期的操作函数 5) 时间和秒钟转换的…

程序员有哪些接单的渠道?

这题我会&#xff01;程序员接单的渠道那可太多了&#xff0c;想要接到合适的单子&#xff0c;筛选一个合适的平台很重要。如果你也在寻找一个合适的接单渠道&#xff0c;可以参考以下这些方向。 首先&#xff0c;程序员要对接单有一个基本的概念&#xff1a;接单渠道可以先粗略…

[足式机器人]Part3 机构运动学与动力学分析与建模 Ch00-3(2) 刚体的位形 Configuration of Rigid Body

本文仅供学习使用&#xff0c;总结很多本现有讲述运动学或动力学书籍后的总结&#xff0c;从矢量的角度进行分析&#xff0c;方法比较传统&#xff0c;但更易理解&#xff0c;并且现有的看似抽象方法&#xff0c;两者本质上并无不同。 2024年底本人学位论文发表后方可摘抄 若有…

SpringBoot中使用LocalDateTime踩坑记录

文章目录 前言一、为什么推荐使用java.time包的LocalDateTime而不是java.util的Date&#xff1f;二、使用LocalDateTime和LocalDate时遇到了哪些坑&#xff1f;2.1 Redis序列化报错2.1.1 问题现象2.1.2 问题分析2.1.3 解决方案 2.2 LocalDateTime和LocalDate类型的属性返回给前…

python_数据可视化_pandas_导入excel数据

目录 1.1导入库 1.2读取excel文件 1.3读取excel&#xff0c;指定sheet2工作表 1.4指定行索引 1.5指定列索引 1.6指定导入列 案例速览&#xff1a; 1.1导入库 import pandas as pd 1.2读取excel文件 pd.read_excel(文件路径) data pd.read_excel(D:/desktop/TestExcel…

Docker安装Jenkins,配置Maven和Java

前言 这是一个java的springboot项目&#xff0c;使用maven构建 安装准备 需要将maven和jdk安装在服务器上&#xff0c;Jenkins需要用到&#xff0c;还有创建一个jenkins的目录&#xff0c;安装命令如下&#xff1a; docker run -d -uroot -p 9095:8080 -p 50000:50000 --n…

Vue-8、Vue事件处理

1、点击事件 <!DOCTYPE html> <html lang"en" xmlns:v-model"http://www.w3.org/1999/xhtml" xmlns:v-bind"http://www.w3.org/1999/xhtml"xmlns:v-on"http://www.w3.org/1999/xhtml"> <head><meta charset&quo…

微信小程序:发送小程序订阅消息

文档&#xff1a;小程序订阅消息&#xff08;用户通过弹窗订阅&#xff09;开发指南 目录 步骤一&#xff1a;获取模板 ID步骤二&#xff1a;小程序端获取参数2.1、获取消息下发权限2.2、获取登录凭证&#xff08;code&#xff09; 步骤三&#xff1a;后端调用接口下发订阅消息…

vue知识-03

购物车案例 要实现的功能&#xff1a; 1、计算商品总价格 2、全选框和取消全选框 3、商品数量的增加和减少 <body> <div id"app"><div class"row"><div class"col-md-6 col-md-offset-3"><h1 class"text-center…

激活/注册navicat15

一、获取软件 链接&#xff1a;https://pan.baidu.com/s/1F_tiLuLvVFMEz8pDfIvDjw?pwdjjfj 提取码&#xff1a;jjfj 二、安装 安装的过程我就不放了&#xff0c;重点如下 安装完不要打开软件&#xff01; 安装完不要打开软件&#xff01; 安装完不要打开软件&#xff01;…

Kafka集群部署 (KRaft模式集群)

KRaft 模式是 Kafka 在 3.0 版本中引入的新模式。KRaft 模式使用了 Raft 共识算法来管理 Kafka 集群元数据。Raft 算法是一种分布式共识算法&#xff0c;具有高可用性、可扩展性和安全性等优势。 在 KRaft 模式下&#xff0c;Kafka 集群中的每个 Broker 都具有和 Zookeeper 类…

直流负载的基础知识

直流负载的主要特性包括电阻、电感和电容。电阻是直流负载的基本特性&#xff0c;它决定了负载消耗电能的能力。电感和电容则是直流负载的动态特性&#xff0c;它们决定了负载对电压和电流变化的响应速度。此外&#xff0c;直流负载还具有非线性特性&#xff0c;即负载的电压和…