ARMEBS4  revision-26.06.2015
pot.c
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////
2 /// \file heivs/pot.c
3 /// \brief ARMEBS4 potentiometer access functions
4 /// \author Pascal Sartoretti / Marc Pignat
5 //////////////////////////////////////////////////////////////////////////////////
6 #include "heivs/pot.h"
7 #include "stm32/stm32f4xx_rcc.h"
8 #include "heivs/stm32_gpio.h"
9 #include "stm32/stm32f4xx_adc.h"
10 
11 //////////////////////////////////////////////////////////////////////////////////
12 /// \brief Initialise ADC to measure potentiometer
13 //////////////////////////////////////////////////////////////////////////////////
14 void pot_init(void)
15 {
16  ADC_InitTypeDef ADC_init_structure;
17  GPIO_InitTypeDef GPIO_initStructre; //Structure for analog input pin
18  //Clock configuration
19  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);//The ADC1 is connected the APB2 peripheral bus thus we will use its clock source
20  RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOAEN,ENABLE);//Clock for the ADC port!! Do not forget about this one ;)
21  //Analog pin configuration
22  GPIO_initStructre.GPIO_Pin = GPIO_Pin_0;//The pin PA0 is connected to the potentiometer
23  GPIO_initStructre.GPIO_Mode = GPIO_Mode_AN; //Set it as analog
24  GPIO_initStructre.GPIO_PuPd = GPIO_PuPd_NOPULL; //We don't need any pull up or pull down
25  GPIO_Init(GPIOA,&GPIO_initStructre);//Affecting the port with the initialization structure configuration
26  //ADC structure configuration
27  ADC_DeInit();
28  ADC_init_structure.ADC_DataAlign = ADC_DataAlign_Right;//data converted will be shifted to right
29  ADC_init_structure.ADC_Resolution = ADC_Resolution_12b;//Input voltage is converted into a 12bit number giving a maximum value of 4096
30  ADC_init_structure.ADC_ContinuousConvMode = ENABLE; //the conversion is continuous, the input data is converted more than once
31  ADC_init_structure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;// conversion is synchronous with TIM1 and CC1 (actually I'm not sure about this one :/)
32  ADC_init_structure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;//no trigger for conversion
33  ADC_init_structure.ADC_NbrOfConversion = 1;//I think this one is clear :p
34  ADC_init_structure.ADC_ScanConvMode = DISABLE;//The scan is configured in one channel
35  ADC_Init(ADC1,&ADC_init_structure);//Initialize ADC with the previous configuration
36  //Enable ADC conversion
37  ADC_Cmd(ADC1,ENABLE);
38  //Select the channel to be read from
39  ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_144Cycles);
40 }
41 
42 //////////////////////////////////////////////////////////////////////////////////
43 /// \brief Measure the potentiometer value
44 /// \return Measured value between 0 to #POT_MAX_VALUE
45 //////////////////////////////////////////////////////////////////////////////////
46 uint32_t pot_value(void)
47 {
48 #define NB_SAMPLE 16
49  uint32_t result=0;
50  uint8_t i;
51  // just an average because ADC is very poor
52  for(i=0;i<NB_SAMPLE;i++)
53  {
54  ADC_SoftwareStartConv(ADC1);
55  while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
56  result += ADC_GetConversionValue(ADC1);
57  }
58  return result / NB_SAMPLE;
59 }
This file contains all the functions prototypes for the RCC firmware library.
uint32_t ADC_ExternalTrigConv
Definition: stm32f4xx_adc.h:62
ARMEBS4 potentiometer access functions.
ADC Init structure definition.
Definition: stm32f4xx_adc.h:47
FunctionalState ADC_ContinuousConvMode
Definition: stm32f4xx_adc.h:55
void pot_init(void)
Initialise ADC to measure potentiometer.
Definition: pot.c:14
This file contains all the functions prototypes for the ADC firmware library.
uint32_t ADC_DataAlign
Definition: stm32f4xx_adc.h:66
GPIO Init structure definition.
board specific defines
uint32_t pot_value(void)
Measure the potentiometer value.
Definition: pot.c:46
uint8_t ADC_NbrOfConversion
Definition: stm32f4xx_adc.h:69
GPIOMode_TypeDef GPIO_Mode
FunctionalState ADC_ScanConvMode
Definition: stm32f4xx_adc.h:51
uint32_t ADC_Resolution
Definition: stm32f4xx_adc.h:49
GPIOPuPd_TypeDef GPIO_PuPd
uint32_t ADC_ExternalTrigConvEdge
Definition: stm32f4xx_adc.h:58