zoukankan      html  css  js  c++  java
  • 基于STM32 8通道ADC采样实现源代码(转) 以后设计参考使用

     #include "stm32f10x_lib.h"
    #include <stdio.h>

    extern void board_Configuration(void);
    extern unsigned short ADC_ConvertedValue[8];


    int main(void)
    {
      unsigned int i="0";
      unsigned short  AD_scaled_ex[8];
      unsigned short  AD_scaled[8];

      board_Configuration();
      while (1)
        {
      for(i=0;i<8;i++)
      {
           AD_scaled[i]=ADC_ConvertedValue[i];
        if (AD_scaled[i] != AD_scaled_ex[i])
         {
          printf("AD%d value = %d\r\n",i,AD_scaled[i]);  
             AD_scaled_ex[i] = AD_scaled[i];
         }
          
      }
      for(i=0;i<60000000;i++);
        } 
    }

    /* Includes ------------------------------------------------------------------*/
    #include "stm32f10x_lib.h"
    #include <stdio.h>

    /* Private typedef -----------------------------------------------------------*/
    /* Private define ------------------------------------------------------------*/
    #define ADC1_DR_Address    ((u32)0x4001244C)

    /* Private macro -------------------------------------------------------------*/
    /* Private variables ---------------------------------------------------------*/
    ADC_InitTypeDef ADC_InitStructure;
    DMA_InitTypeDef DMA_InitStructure;
    unsigned short ADC_ConvertedValue[8];
    ErrorStatus HSEStartUpStatus;

    /* Private function prototypes -----------------------------------------------*/
    void RCC_Configuration(void);
    void NVIC_Configuration(void);
    void GPIO_Configuration(void);
    void USART_Configuration(void);
    void DMA_Configuration(void);
    void ADC_Configuration(void);

    /* Private functions ---------------------------------------------------------*/

    /*******************************************************************************
    * Function Name  : main
    * Description    : Main program.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void board_Configuration(void)

    #ifdef DEBUG
      debug();
    #endif

      /* Configure the system clocks */
      RCC_Configuration();
       
      /* NVIC Configuration */
      NVIC_Configuration();

      /* Configure the GPIOs */
      GPIO_Configuration();
     
      /* Configure the USART1 */
      USART_Configuration();
      /* Configure the DMA */
      DMA_Configuration();
      /* Configure the DMA */
      ADC_Configuration();


    }

    #ifdef  DEBUG
    /*******************************************************************************
    * Function Name  : assert_failed
    * Description    : Reports the name of the source file and the source line number
    *                  where the assert error has occurred.
    * Input          : - file: pointer to the source file name
    *                  - line: assert error line source number
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void assert_failed(u8* file, u32 line)
    {
      /* User can add his own implementation to report the file name and line number */

      printf("\n\r Wrong parameter value detected on\r\n");
      printf("       file  %s\r\n", file);
      printf("       line  %d\r\n", line);
       
      /* Infinite loop */
      /* while (1)
      {
      } */
    }
    #endif
    /*******************************************************************************
    * Function Name  : DMA_Configuration
    * Description    : Configures the different system clocks.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void DMA_Configuration(void)
    {
      DMA_DeInit(DMA_Channel1);
      DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
      DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;
      DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
      DMA_InitStructure.DMA_BufferSize = 8;
      DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
      DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
      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(DMA_Channel1, &DMA_InitStructure);
     
      /* Enable DMA channel1 */
      DMA_Cmd(DMA_Channel1, ENABLE);
    }
    /*******************************************************************************
    * Function Name  : ADC_Configuration
    * Description    : Configures the different system clocks.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void ADC_Configuration(void)
    {
     /* ADC1 configuration ------------------------------------------------------*/
      ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
      ADC_InitStructure.ADC_ScanConvMode = ENABLE;
      ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
      ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
      ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
      ADC_InitStructure.ADC_NbrOfChannel = 8;
      ADC_Init(ADC1, &ADC_InitStructure);

     /* ADC1 regular channel8 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel9 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel10 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel11 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 4, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel12 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 5, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel13 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 6, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel14 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 7, ADC_SampleTime_55Cycles5);
     /* ADC1 regular channel15 configuration */
      ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 8, ADC_SampleTime_55Cycles5);


      /* Enable ADC1 DMA */
      ADC_DMACmd(ADC1, ENABLE);
     
      /* Enable ADC1 */
      ADC_Cmd(ADC1, ENABLE);

      /* Enable ADC1 reset calibaration register */  
      ADC_ResetCalibration(ADC1);
      /* Check the end of ADC1 reset calibration register */
      while(ADC_GetResetCalibrationStatus(ADC1));

      /* Start ADC1 calibaration */
      ADC_StartCalibration(ADC1);
      /* Check the end of ADC1 calibration */
      while(ADC_GetCalibrationStatus(ADC1));
        
      /* Start ADC1 Software Conversion */
      ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    }

    /*******************************************************************************
    * Function Name  : RCC_Configuration
    * Description    : Configures the different system clocks.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void RCC_Configuration(void)
    {
      /* RCC system reset(for debug purpose) */
      RCC_DeInit();

      /* Enable HSE */
      RCC_HSEConfig(RCC_HSE_ON);

      /* Wait till HSE is ready */
      HSEStartUpStatus = RCC_WaitForHSEStartUp();

      if(HSEStartUpStatus == SUCCESS)
      {
        /* HCLK = SYSCLK */
        RCC_HCLKConfig(RCC_SYSCLK_Div1);
     
        /* PCLK2 = HCLK */
        RCC_PCLK2Config(RCC_HCLK_Div1);

        /* PCLK1 = HCLK/2 */
        RCC_PCLK1Config(RCC_HCLK_Div2);
       /* ADCCLK = PCLK2/4 */
        RCC_ADCCLKConfig(RCC_PCLK2_Div4);

        /* Flash 2 wait state */
        FLASH_SetLatency(FLASH_Latency_2);
        /* Enable Prefetch Buffer */
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        /* PLLCLK = 8MHz * 9 = 72 MHz */
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

        /* Enable PLL */
        RCC_PLLCmd(ENABLE);

        /* Wait till PLL is ready */
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
        {
        }

        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        /* Wait till PLL is used as system clock source */
        while(RCC_GetSYSCLKSource() != 0x08)
        {
        }
      }
      
      /* Enable USART1 and GPIOA clock */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
      /* Enable DMA clock */
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);
      /* Enable ADC1 and GPIOC clock */
      RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC|
                             RCC_APB2Periph_GPIOB, ENABLE);

    }

    /*******************************************************************************
    * Function Name  : NVIC_Configuration
    * Description    : Configures Vector Table base location.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void NVIC_Configuration(void)
    {
    #ifdef  VECT_TAB_RAM 
      /* Set the Vector Table base location at 0x20000000 */
      NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
    #else  /* VECT_TAB_FLASH  */
      /* Set the Vector Table base location at 0x08000000 */
      NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);  
    #endif
    }


    /*******************************************************************************
    * Function Name  : GPIO_Configuration
    * Description    : Configures the different GPIO ports.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void GPIO_Configuration(void)
    {
      GPIO_InitTypeDef GPIO_InitStructure;

      /* Configure USART1 Tx (PA.09) as alternate function push-pull */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_Init(GPIOA, &GPIO_InitStructure);
       
      /* Configure USART1 Rx (PA.10) as input floating */
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
      GPIO_Init(GPIOA, &GPIO_InitStructure);

      /* Configure PC.0\1\2\3\4\5 (ADC Channel10\11\12\13\14\15) as analog input*/
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|
                                    GPIO_Pin_2|GPIO_Pin_3|
            GPIO_Pin_4|GPIO_Pin_5;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
      GPIO_Init(GPIOC, &GPIO_InitStructure);

      /* Configure PB.0\1 (ADC Channel8\9) as analog input*/
      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
                                   
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

    }

    /*******************************************************************************
    * Function Name  : USART_Configuration
    * Description    : Configures the USART1.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    void USART_Configuration(void)
    {
     
      USART_InitTypeDef USART_InitStructure;
    /* USART1 configuration ------------------------------------------------------*/
      /* USART1 configured as follow:
            - BaudRate = 115200 baud 
            - Word Length = 8 Bits
            - One Stop Bit
            - No parity
            - Hardware flow control disabled (RTS and CTS signals)
            - Receive and transmit enabled
            - USART Clock disabled
            - USART CPOL: Clock is active low
            - USART CPHA: Data is captured on the middle
            - USART LastBit: The clock pulse of the last data bit is not output to
                             the SCLK pin
      */
      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_InitStructure.USART_Clock = USART_Clock_Disable;
      USART_InitStructure.USART_CPOL = USART_CPOL_Low;
      USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
      USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

      USART_Init(USART1, &USART_InitStructure);
       
      /* Enable USART1 */
      USART_Cmd(USART1, ENABLE);
    }

    /*******************************************************************************
    * Function Name  : fputc
    * Description    : Retargets the C library printf function to the USART.
    * Input          : None
    * Output         : None
    * Return         : None
    *******************************************************************************/
    int fputc(int ch, FILE *f)
    {
      /* Place your implementation of fputc here */
      /* e.g. write a character to the USART */
      USART_SendData(USART1, (u8) ch);

      /* Loop until the end of transmission */
      while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
      {
      }

      return ch;
    }

  • 相关阅读:
    十三、asp.net中Repeater控件用法笔记
    九、chart控件的使用(图表数据的展示)
    一、在开发数据同步系统中的开发问题:
    ubuntu编译最新版本WebKit
    android apk 防止反编译技术第四篇-对抗JD-GUI
    小菜学Chromium之OpenGL学习之二
    webkit浏览器常见开发问题
    Android图片开发内幕--基础篇
    【转载】Android Metro风格的Launcher开发系列第二篇
    world 替换+正则表达式命令
  • 原文地址:https://www.cnblogs.com/pulan/p/2921647.html
Copyright © 2011-2022 走看看