-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc-driver.c
109 lines (95 loc) · 2.36 KB
/
adc-driver.c
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
#include <stdint.h>
#include <stdbool.h>
#include "adc-driver.h"
static uint8_t started_ADC[]={0,0,0}; /* Há 3 termos para o ídice coincidir com o numero do ADC*/
extern const uint8 adc_pin_map[] = { PA0,PA1,PA2,PA3,PA4,PA5,PA6,PA7,PB0,PB1 };
adc_smp_rate get_sample_time(float);
adc_dev* getADC(uint8_t adc_num);
uint8_t adc_config(uint8_t adc_num, uint8_t adc_chan, uint8_t adc_res, uint16_t adc_freq)
{
uint8_t sucesso=0;
/*o adc_res não é usado pois o STM32f1 utiliza o ADC sempre com 12 bits*/
if(adc_num>=1&&adc_num<=2)
{
adc_dev *dev=getADC(adc_num);
if(started_ADC[adc_num]!=1)
{
adc_set_prescaler(ADC_PRE_PCLK2_DIV_6);
adc_init(dev);
adc_set_extsel(dev, ADC_SWSTART);
adc_set_exttrig(dev, 1);
adc_enable(dev);
adc_calibrate(dev);
started_ADC[adc_num]=1;
}
if (adc_chan<16) //adicionar suporte a leitura de GPIOs
{
uint8_t num_gpio = MAX_NUM-MAX_LEDS-(adc_chan+16*(adc_num-1));
gpio_config(num_gpio, adc_pin_map[num_gpio], GPIO_INPUT_ANALOG);
}
adc_set_sample_rate(dev, get_sample_time(1.0/adc_freq));
sucesso=1;
}
return sucesso;
}
uint16_t adc_read(uint8_t adc_num, uint8_t adc_chan)
{
uint16_t saida=0;
if(adc_num>=1&&adc_num<=2)
{
adc_dev *dev=getADC(adc_num);
saida = adc_reads(dev, adc_chan);
}
return saida;
}
adc_dev* getADC(uint8_t adc_num)
{
adc_dev *ADC = NULL;
switch (adc_num)
{
case 1:
ADC = ADC1;
break;
case 2:
ADC = ADC2;
break;
}
return ADC;
}
adc_smp_rate get_sample_time(float sample_time)
{
adc_smp_rate sample_time_escolhido;
if(239.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_239_5;
}
else if(71.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_71_5;
}
else if(55.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_55_5;
}
else if(41.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_41_5;
}
else if(28.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_28_5;
}
else if(13.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_13_5;
}
else if(7.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_7_5;
}
else if(1.5*(float)6/F_CPU<sample_time)
{
sample_time_escolhido=ADC_SMPR_1_5;
}
return sample_time_escolhido;
}