共同学习单片机吧 关注:7贴子:28
  • 12回复贴,共1

stm32 ADC实验(DMA方式)

只看楼主收藏回复

#include "stm32f10x.h"
#include "bsp_usart1.h"
#include "bsp_adc.h"
// ADC1转换的电压值通过DMA方式到SRAM
extern __IO uint16_t ADC_ConvertedValue;
//局部变量,用于保存转换计算后的电压值
float ADC_ConvertedValueLocal;
//软件延时
void Delay(__IO uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
//主函数
int main(void)
{
/* USART1 config */
USART1_Config();
/* enable adc1 and config adc1 to dma mode */
ADC1_Init();
printf("\r\n ---- ADC实验(DMA传输)----\r\n");
while (1)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3; // 读取转换的AD的值
printf("\r\n The current AD value = 0x%04X \r\n", ADC_ConvertedValue);
printf("\r\n The current AD value = %f V \r\n",ADC_ConvertedValueLocal);
Delay(0xffffee);
}
}


IP属地:广东1楼2015-07-29 16:15回复
    此为所写函数的 main.c 中的内容,其他编写的.c和.h下面标出


    IP属地:广东2楼2015-07-29 16:17
    回复
      2025-09-01 10:12:28
      广告
      不感兴趣
      开通SVIP免广告
      //usart1.c 文件内容
      #include "bsp_usart1.h"
      //配置 USART1 GPIO ,工作模式配置。 115200 8-N-1
      void USART1_Config(void)
      {
      //定义两个结构体 配置 GPIO USART
      GPIO_InitTypeDef GPIO_InitStructure;
      USART_InitTypeDef USART_InitStructure;
      //时钟开启
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
      //填充GPIO结构体,对GPIO 进行设置
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //STM32 PA9和PA10为接收和发送的管脚
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//用了复用功能,所以设置为复用推挽输出
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//设置频率为50Hz
      GPIO_Init(GPIOA, &GPIO_InitStructure); //利用函数将设置送入寄存器
      //同上,填充设置PA10
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
      //填充USART结构体
      USART_InitStructure.USART_BaudRate = 115200;
      USART_InitStructure.USART_WordLength = USART_WordLength_8b;
      USART_InitStructure.USART_StopBits = USART_StopBits_1;
      USART_InitStructure.USART_Parity = USART_Parity_No ;
      USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
      USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
      USART_Init(USART1, &USART_InitStructure);
      USART_Cmd(USART1, ENABLE);//使能外设USART
      }
      //重定向c库函数printf到USART1
      int fputc(int ch, FILE *f)
      {
      //发送一个字节数据到USART1
      USART_SendData(USART1, (uint8_t) ch);
      //等待发送完毕
      while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
      return (ch);
      }
      //重定向c库函数scanf到USART1
      int fgetc(FILE *f)
      {
      //等待串口1输入数据
      while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
      return (int)USART_ReceiveData(USART1);
      }


      IP属地:广东3楼2015-07-29 16:35
      回复
        //adc.h
        #include "bsp_adc.h"
        #define ADC1_DR_Address ((u32)0x40012400+0x4c)
        __IO uint16_t ADC_ConvertedValue;
        //__IO u16 ADC_ConvertedValueLocal;
        //使能ADC1 , DMA1时钟,初始化PC.0
        static void ADC1_GPIO_Config(void)
        {
        GPIO_InitTypeDef GPIO_InitStructure;
        /* Enable DMA clock */
        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
        /* Enable ADC1 and GPIOC clock */
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
        /* Configure PC.0 as analog input */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
        GPIO_Init(GPIOC, &GPIO_InitStructure);// PC0,输入不用设置速率
        }
        //配置ADC的工作模式为DMA模式
        static void ADC1_Mode_Config(void)
        {
        DMA_InitTypeDef DMA_InitStructure;
        ADC_InitTypeDef ADC_InitStructure;
        /* DMA channel1 configuration */
        DMA_DeInit(DMA1_Channel1);
        DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //ADC地址
        DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//内存地址
        DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
        DMA_InitStructure.DMA_BufferSize = 1;
        DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设地址固定
        DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //内存地址固定
        DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//半字
        DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
        DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环传输
        DMA_InitStructure.DMA_Priority = DMA_Priority_High;
        DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
        DMA_Init(DMA1_Channel1, &DMA_InitStructure);
        /* Enable DMA channel1 */
        DMA_Cmd(DMA1_Channel1, ENABLE);
        /* ADC1 configuration */
        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//独立ADC模式
        ADC_InitStructure.ADC_ScanConvMode = DISABLE ; //禁止扫描模式,扫描模式多用于多通道采集
        ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//开启连续转换模式,即不停进行ADC转换
        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//不使用外部触发转换
        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //采集数据右对齐
        ADC_InitStructure.ADC_NbrOfChannel = 1; //要转换的通道数目1
        ADC_Init(ADC1, &ADC_InitStructure);
        //配置ADC时钟为8分频,即9MHz
        RCC_ADCCLKConfig(RCC_PCLK2_Div8);
        /*/配置ADC通道11为55,5个采样周期,序列为1
        ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);
        /* Enable ADC1 DMA */
        ADC_DMACmd(ADC1, ENABLE);
        /* Enable ADC1 */
        ADC_Cmd(ADC1, ENABLE);
        //复位寄存器
        ADC_ResetCalibration(ADC1);
        //等待校准寄存器复位完成
        while(ADC_GetResetCalibrationStatus(ADC1));
        //ADC校准
        ADC_StartCalibration(ADC1);
        //等待校准完成
        while(ADC_GetCalibrationStatus(ADC1));
        //因无外部触发,软件触发
        ADC_SoftwareStartConvCmd(ADC1, ENABLE);
        }
        //ADC1初始化
        void ADC1_Init(void)
        {
        ADC1_GPIO_Config();
        ADC1_Mode_Config();
        }


        IP属地:广东4楼2015-07-29 16:48
        回复
          //usart1.h
          #ifndef __USART1_H
          #define__USART1_H
          #include "stm32f10x.h"
          #include <stdio.h>
          void USART1_Config(void);
          #endif /* __USART1_H */


          IP属地:广东5楼2015-07-29 16:51
          回复
            //adc.h
            #ifndef __ADC_H
            #define__ADC_H
            #include "stm32f10x.h"
            void ADC1_Init(void);
            #endif /* __ADC_H */


            IP属地:广东6楼2015-07-29 16:52
            回复
              还需添加官方库,和中断函数


              IP属地:广东7楼2015-07-29 16:52
              回复
                这多代码,看着都晕


                8楼2015-07-30 10:35
                收起回复