Commit 502f62695f2fc34aa46e450e0f1a7f7a86081cdf
1 parent
b5f7fce9
--no commit message
Showing
2 changed files
with
127 additions
and
0 deletions
Project/applications/smartcities/adc.c
0 → 100644
1 | +#include "adc.h" | ||
2 | + | ||
3 | +#define DBG(fmt,...) if(1){printf("[ADC] "fmt"\r\n", ##__VA_ARGS__);}else{({});} | ||
4 | + | ||
5 | + | ||
6 | +#define ADC_MIN_VALUE (0) | ||
7 | +#define ADC_MAX_VALUE (0xfff) /* 12-bit adc */ | ||
8 | +#define ADC_VREF_PLUS (3300) /* Vref+ is connected with Vdda (3.3v) */ | ||
9 | +#define ADC_VREF_MINUS (0) /* Vref- is connected with Vssa (0v) */ | ||
10 | + | ||
11 | +#define ADCbatt ADC1 | ||
12 | +#define ADCbatt_RCC RCC_APB2Periph_ADC1 /* */ | ||
13 | +#define ADCbatt_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */ | ||
14 | +#define ADCbatt_CHANNEL ADC_Channel_12 /* 0/17 */ | ||
15 | +#define ADCbatt_SAMPLETIME ADC_SampleTime_239Cycles5 | ||
16 | +#define ADCbatt_GPIO_PIN GPIO_Pin_2 | ||
17 | +#define ADCbatt_GPIO_PORT GPIOC | ||
18 | +#define ADCbatt_GPIO_RCC RCC_APB2Periph_GPIOC | ||
19 | +#define ADCbatt_GPIO_STR "PC2" | ||
20 | + | ||
21 | +#define ADCsound ADC1 | ||
22 | +#define ADCsound_RCC RCC_APB2Periph_ADC1 /* */ | ||
23 | +#define ADCsound_DIVIDER RCC_PCLK2_Div4 /* Can be 2/4/6/8. PCLK2 runs at 32Mhz, and max ADC clock is 14Mhz */ | ||
24 | +#define ADCsound_CHANNEL ADC_Channel_12 /* 0/17 */ | ||
25 | +#define ADCsound_SAMPLETIME ADC_SampleTime_239Cycles5 | ||
26 | +#define ADCsound_GPIO_PIN GPIO_Pin_2 | ||
27 | +#define ADCsound_GPIO_PORT GPIOC | ||
28 | +#define ADCsound_GPIO_RCC RCC_APB2Periph_GPIOC | ||
29 | +#define ADCsound_GPIO_STR "PC2" | ||
30 | + | ||
31 | +void adc_init() | ||
32 | +{ | ||
33 | + adc_peripheralInit(); | ||
34 | +} | ||
35 | + | ||
36 | +uint32_t adc_process() | ||
37 | +{ | ||
38 | + uint16_t adcRegisterValue; | ||
39 | + uint32_t analogValue; | ||
40 | + | ||
41 | + adcRegisterValue = adc_read(); | ||
42 | + | ||
43 | + /* | ||
44 | + * Calculate the register value to volts | ||
45 | + * 1. adcRegisterValue Range is [ADC_MIN_VALUE, ADC_MAX_VALUE] | ||
46 | + * 2. analogValue Range is [ADC_VREF_MINUS, ADC_VREF_PLUS] | ||
47 | + */ | ||
48 | + analogValue = ADC_VREF_MINUS + (((adcRegisterValue - ADC_MIN_VALUE)*(ADC_VREF_PLUS - ADC_VREF_MINUS))/(ADC_MAX_VALUE - ADC_MIN_VALUE)); | ||
49 | + DBG("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR); | ||
50 | + return analogValue; | ||
51 | +} | ||
52 | + | ||
53 | +void adc_peripheralInit() | ||
54 | +{ | ||
55 | + GPIO_InitTypeDef GPIO_InitStructure; | ||
56 | + ADC_InitTypeDef ADC_InitStructure; | ||
57 | + RCC_APB2PeriphClockCmd(ADCbatt_GPIO_RCC, ENABLE); | ||
58 | + GPIO_InitStructure.GPIO_Pin = ADCbatt_GPIO_PIN; | ||
59 | + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; | ||
60 | + GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; | ||
61 | + GPIO_Init(ADCbatt_GPIO_PORT, &GPIO_InitStructure); | ||
62 | + | ||
63 | + | ||
64 | + /* Max ADC clk is 14mhz and because PCLK2 runs @32Mhz */ | ||
65 | + RCC_ADCCLKConfig(ADCbatt_DIVIDER); | ||
66 | + /* Enable ADCbatt clock so that we can talk to it */ | ||
67 | + RCC_APB2PeriphClockCmd(ADCbatt_RCC, ENABLE); | ||
68 | + /* Put everything back to power-on defaults */ | ||
69 | + ADC_DeInit(ADCbatt); | ||
70 | + | ||
71 | + | ||
72 | + /* ADC1 and ADC2 operate independently */ | ||
73 | + ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; | ||
74 | + /* Disable the scan conversion so we do one at a time */ | ||
75 | + ADC_InitStructure.ADC_ScanConvMode = DISABLE; | ||
76 | + /* Don't do contimuous conversions - do them on demand */ | ||
77 | + ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; | ||
78 | + /* Start conversin by software, not an external trigger */ | ||
79 | + ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; | ||
80 | + /* Conversions are 12 bit - put them in the lower 12 bits of the result */ | ||
81 | + ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; | ||
82 | + /* Say how many channels would be used by the sequencer */ | ||
83 | + ADC_InitStructure.ADC_NbrOfChannel = 1; | ||
84 | + | ||
85 | + /* Now do the setup */ | ||
86 | + ADC_Init(ADCbatt, &ADC_InitStructure); | ||
87 | + /* Enable ADCbatt */ | ||
88 | + ADC_Cmd(ADCbatt, ENABLE); | ||
89 | + | ||
90 | + /* Enable ADCbatt reset calibaration register */ | ||
91 | + ADC_ResetCalibration(ADCbatt); | ||
92 | + /* Check the end of ADCbatt reset calibration register */ | ||
93 | + while(ADC_GetResetCalibrationStatus(ADCbatt)); | ||
94 | + /* Start ADCbatt calibaration */ | ||
95 | + ADC_StartCalibration(ADCbatt); | ||
96 | + /* Check the end of ADC1 calibration */ | ||
97 | + while(ADC_GetCalibrationStatus(ADCbatt)); | ||
98 | +} | ||
99 | + | ||
100 | +uint16_t adc_read() | ||
101 | +{ | ||
102 | + ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME); | ||
103 | + /* Start the conversion */ | ||
104 | + ADC_SoftwareStartConvCmd(ADCbatt, ENABLE); | ||
105 | + /* Wait until conversion completion */ | ||
106 | + while(ADC_GetFlagStatus(ADCbatt, ADC_FLAG_EOC) == RESET); | ||
107 | + /* Get the conversion value */ | ||
108 | + return ADC_GetConversionValue(ADCbatt); | ||
109 | +} | ||
110 | + | ||
111 | + | ||
112 | + |
Project/applications/smartcities/adc.h
0 → 100644
1 | +#ifndef ADC_H | ||
2 | +#define ADC_H | ||
3 | + | ||
4 | +#include "libwismart.h" | ||
5 | +#include "ch.h" | ||
6 | + | ||
7 | +#include "stm32f10x_gpio.h" | ||
8 | +#include "stm32f10x_adc.h" | ||
9 | + | ||
10 | +void adc_init(void); | ||
11 | +uint32_t adc_process(void); | ||
12 | +uint16_t adc_read(void); | ||
13 | +void adc_peripheralInit(void); | ||
14 | + | ||
15 | +#endif | ||
0 | \ No newline at end of file | 16 | \ No newline at end of file |