adc.c
4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "adc.h"
void adc_batt_init()
{
adc_batt_peripheralInit();
}
uint32_t adc_batt_process()
{
uint16_t adcRegisterValue;
uint32_t analogValue;
adcRegisterValue = adc_batt_read();
analogValue = ADCbatt_VREF_MINUS + (((adcRegisterValue - ADCbatt_MIN_VALUE)*(ADCbatt_VREF_PLUS - ADCbatt_VREF_MINUS))/(ADCbatt_MAX_VALUE - ADCbatt_MIN_VALUE));
printf("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCbatt_GPIO_STR);
return analogValue;
}
void adc_batt_peripheralInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(ADCbatt_GPIO_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADCbatt_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADCbatt_GPIO_PORT, &GPIO_InitStructure);
RCC_ADCCLKConfig(ADCbatt_DIVIDER);
RCC_APB2PeriphClockCmd(ADCbatt_RCC, ENABLE);
ADC_DeInit(ADCbatt);
/* ADC1 and ADC2 operate independently */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
/* Disable the scan conversion so we do one at a time */
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
/* Don't do contimuous conversions - do them on demand */
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
/* Start conversin by software, not an external trigger */
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
/* Conversions are 12 bit - put them in the lower 12 bits of the result */
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
/* Say how many channels would be used by the sequencer */
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADCbatt, &ADC_InitStructure);
ADC_Cmd(ADCbatt, ENABLE);
/* Enable ADCbatt reset calibaration register */
ADC_ResetCalibration(ADCbatt);
/* Check the end of ADCbatt reset calibration register */
while(ADC_GetResetCalibrationStatus(ADCbatt));
/* Start ADCbatt calibaration */
ADC_StartCalibration(ADCbatt);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADCbatt));
}
uint16_t adc_batt_read()
{
ADC_RegularChannelConfig(ADCbatt, ADCbatt_CHANNEL, 1, ADCbatt_SAMPLETIME);
/* Start the conversion */
ADC_SoftwareStartConvCmd(ADCbatt, ENABLE);
/* Wait until conversion completion */
while(ADC_GetFlagStatus(ADCbatt, ADC_FLAG_EOC) == RESET);
/* Get the conversion value */
return ADC_GetConversionValue(ADCbatt);
}
void adc_sound_init()
{
adc_sound_peripheralInit();
}
uint32_t adc_sound_process()
{
uint16_t adcRegisterValue;
uint32_t analogValue;
adcRegisterValue = adc_sound_read();
analogValue = ADCsound_VREF_MINUS + (((adcRegisterValue - ADCsound_MIN_VALUE)*(ADCsound_VREF_PLUS - ADCsound_VREF_MINUS))/(ADCsound_MAX_VALUE - ADCsound_MIN_VALUE));
printf("Register value is %4u [%4u milliVolts are currently given to gpio %s]",adcRegisterValue, analogValue, ADCsound_GPIO_STR);
return analogValue;
}
void adc_sound_peripheralInit()
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(ADCsound_GPIO_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADCsound_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADCsound_GPIO_PORT, &GPIO_InitStructure);
RCC_ADCCLKConfig(ADCsound_DIVIDER);
RCC_APB2PeriphClockCmd(ADCsound_RCC, ENABLE);
ADC_DeInit(ADCsound);
/* ADC1 and ADC2 operate independently */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
/* Disable the scan conversion so we do one at a time */
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
/* Don't do contimuous conversions - do them on demand */
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
/* Start conversin by software, not an external trigger */
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
/* Conversions are 12 bit - put them in the lower 12 bits of the result */
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
/* Say how many channels would be used by the sequencer */
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADCsound, &ADC_InitStructure);
ADC_Cmd(ADCsound, ENABLE);
/* Enable ADCsound reset calibaration register */
ADC_ResetCalibration(ADCsound);
/* Check the end of ADCsound reset calibration register */
while(ADC_GetResetCalibrationStatus(ADCsound));
/* Start ADCsound calibaration */
ADC_StartCalibration(ADCsound);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADCsound));
}
uint16_t adc_sound_read()
{
ADC_RegularChannelConfig(ADCsound, ADCsound_CHANNEL, 1, ADCsound_SAMPLETIME);
/* Start the conversion */
ADC_SoftwareStartConvCmd(ADCsound, ENABLE);
/* Wait until conversion completion */
while(ADC_GetFlagStatus(ADCsound, ADC_FLAG_EOC) == RESET);
/* Get the conversion value */
return ADC_GetConversionValue(ADCsound);
}