-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.c
47 lines (41 loc) · 1.2 KB
/
adc.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
/*adc.c
Código:
void adc_init(unsigned char fosc, unsigned char pcfg, unsigned char config);
void adc_close(void);
unsigned int adc_read(unsigned char canal);
void adc_startread(unsigned char canal);
*/
#include "adc.h"
#include "delay.h"
void adc_init(unsigned char fosc, unsigned char pcfg, unsigned char config)
{
ADCON0 = 0;
ADCON0 = (fosc & 0x03) << 6; // establecer frecuencia
ADCON1 = (pcfg & 0x0F) ; // establecer entradas
ADFM = 1;
if (fosc & 0x04)
ADCS2 = 1;
if (config & INT_ON) // establecer interrupciones
{
ADIF = 0;
ADIE = 1;
PEIE = 1;
}
ADON = 1; // habilitar módulo ADC
}
void adc_close(void) // deshabilitar módulo ADC
{
ADON = 0;
ADIE = 0;
}
unsigned int adc_read(unsigned char canal)
{
unsigned int valor;
ADCON0 &= 0xC7; // borrar anteriores selecciones
ADCON0 |= (canal & 0x07) << 3; // establecer canal seleccionado
delay_ms(10);
GO = 1; // iniciar conversión
while (GO); // esperar que termine
valor = (ADRESH << 8) | ADRESL; // leer valor
return valor;
}