Advertisement
pongfactory

ADC Simple

Jan 28th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. /**
  2.   *****************************************************************************
  3.   * @title   ADC_simple.c
  4.   * @author  Claude
  5.   * @date    19 Jul 2012
  6.   * @brief  
  7.   *******************************************************************************
  8.   */
  9. ////// The above comment is automatically generated by CoIDE ///////////////////
  10.  
  11. /**
  12.   *****************************************************************************
  13.   * @title   ADC_simple.c
  14.   * @author  Claude
  15.   * @date    2010 Dec 29
  16.   * @brief   ADC Example, Blink a LED according to ADC value
  17.   *******************************************************************************
  18.   */
  19.  
  20. #include<stm32f10x_rcc.h>
  21. #include<stm32f10x_gpio.h>
  22. #include<stm32f10x_adc.h>
  23.  
  24. #include "stm32f10x.h"
  25. #include "stm32f10x_conf.h"
  26.  
  27. GPIO_InitTypeDef GPIO_InitStructure;
  28. ADC_InitTypeDef ADC_InitStructure;
  29. int i,j;
  30.  
  31. /* Blink a LED, blink speed is set by ADC value */
  32. void ADC_simple(void)
  33. {
  34. // init for GPIO (LED)
  35.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  36.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
  38.     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9 ;   // two LED (guess on what pin!!)
  39.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  40.  
  41. // input of ADC (it doesn't seem to be needed, as default GPIO state is floating input)
  42.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AIN;
  43.     GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_1 ;        // that's ADC1 (PA1 on STM32)
  44.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  45.  
  46. //clock for ADC (max 14MHz --> 72/6=12MHz)
  47.     RCC_ADCCLKConfig (RCC_PCLK2_Div6);
  48. // enable ADC system clock
  49.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  50.  
  51. // define ADC config
  52.     ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  53.     ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  54.     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;  // we work in continuous sampling mode
  55.     ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  56.     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  57.     ADC_InitStructure.ADC_NbrOfChannel = 1;
  58.  
  59.     ADC_RegularChannelConfig(ADC1,ADC_Channel_1, 1,ADC_SampleTime_28Cycles5); // define regular conversion config
  60.     ADC_Init ( ADC1, &ADC_InitStructure);   //set config of ADC1
  61.  
  62. // enable ADC
  63.     ADC_Cmd (ADC1,ENABLE);  //enable ADC1
  64.  
  65. //  ADC calibration (optional, but recommended at power on)
  66.     ADC_ResetCalibration(ADC1); // Reset previous calibration
  67.     while(ADC_GetResetCalibrationStatus(ADC1));
  68.     ADC_StartCalibration(ADC1); // Start new calibration (ADC must be off at that time)
  69.     while(ADC_GetCalibrationStatus(ADC1));
  70.  
  71. // start conversion
  72.     ADC_Cmd (ADC1,ENABLE);  //enable ADC1
  73.     ADC_SoftwareStartConvCmd(ADC1, ENABLE); // start conversion (will be endless as we are in continuous mode)
  74.  
  75. // debug information
  76.     RCC_ClocksTypeDef forTestOnly;
  77.     RCC_GetClocksFreq(&forTestOnly);    //this could be used with debug to check to real speed of ADC clock
  78.  
  79.     j= 50000;
  80.     while(1)
  81.     {
  82.         GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_RESET);
  83.         GPIO_WriteBit(GPIOB,GPIO_Pin_9,Bit_SET);
  84.         for (i=0;i<j;i++);
  85.         GPIO_WriteBit(GPIOB,GPIO_Pin_9,Bit_RESET);
  86.         GPIO_WriteBit(GPIOB,GPIO_Pin_8,Bit_SET);
  87.  
  88.         // adc is in free run, and we get the value asynchronously, this is not a really nice way of doing, but it work!
  89.         j = ADC_GetConversionValue(ADC1) * 1000 ; // value from 0 to 4095000
  90.         for (i=0;i<j;i++);
  91.     }
  92.     /* possible change :
  93.      * ADC_ContinuousConvMode = DISABLE
  94.      * then on the infinite loop, something like :
  95.      *
  96.      *  ADC_SoftwareStartConvCmd(ADC1, ENABLE);                 // start ONE conversion
  97.      *  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);  // wait end of conversion
  98.      *  j = ADC_GetConversionValue(ADC1) * 500;                 // get value
  99.      *
  100.      */
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement