/*===================================================================================+
|                                                                                    |
|     Universidade de Aveiro - Departamento de Engenharia Mecanica                   |
|                      (Projecto Humanoide - MODULROB)                               |
|                                                                                    |
|     Data: Quinta - 24/Marco/2004                                                   |
|                                                                                    |
|     Programadores: David Manuel Costa Gameiro  N: 20281                            |
|                    Filipe Carvalho Mostardinha N: 22085                            |
|                                                                                    |
|     Nome Ficheiro: adc.c                                                           |
|                                                                                    |
+===================================================================================*/

#include <p18f258.h>
#include "can18xx8.h"

#ifndef ADC_H

#define ADC_H


typedef unsigned char byte;

/*--------------------------------------------------------------------------+
|                            Declaracao de Mascaras                         |
+--------------------------------------------------------------------------*/

static enum ADC_CONFIG_BITS
{

	ADC0_FOSC_BITS = 0b11000000,//0bxx000000 selec‡„o da velocidade de conversao, ADCS1:ADCS0
	ADC_CH_BITS    = 0b00111000,//0b00xxx000 bits com os canais activos
	ADC1_ADFM_BIT  = 0b00000100,//0b00000x00 valores em ADRESL(1) ou ADRESH(0)
	ADC1_FOSC_BIT  = 0b00000010,//0b000000x0 bit ADCON1 seleccao velocidade ADCS2
};



/*-------------------------------------------------------------------------+
|              void SetADC(byte config1, byte config2)                     |
| Input: config1 - 	configura‡„o da ADC velocidade, jun‡„o do enumerador   |
|                    ADC_CONFIG_FLAGS                                      |
|        config2 - quais os pinos que vao ser entradas analogicas          |
|                                                                          |
| Sem outputs                                                              |
|                                                                          |
| Objectivo: Configura os diversos registos do modulo ADC                  |
+-------------------------------------------------------------------------*/

void SetADC(byte config1, byte config2)
{

//retirar a velocidade de convers„o pra ADCON0
ADCON0 = config1 & ADC0_FOSC_BITS;

ADCON0 &= 0b11000111;//Activa o canal 0
// registos ADCON1 

if (config1 & ADC1_ADFM_BIT)
	ADCON1bits.ADFM = 1;//justifica‡„o … direita
else
	ADCON1bits.ADFM = 0;

if (config1 & ADC1_FOSC_BIT)
	ADCON1bits.ADCS2 = 1;

//selec‡„o do numero de canais a activar

ADCON1 |= config2;

ADCON0bits.ADON = 1;// ligar o modulo adc
}


/*-------------------------------------------------------------------------+
|                  void SetChanel(byte Canal)                              |
|                                                                          |
| Input: Canal - numero do canal que pretendemos activar                   |
|                                                                          |
| Sem outputs                                                              |
|                                                                          |
| Objectivo: Activa o canal 0 a 4 do PIC                                   |
+-------------------------------------------------------------------------*/

void SetChanel(byte Canal)
{
//Flag para auxiliar a config
#define ADC_CH_FLAGS 0b00111000

	ADCON0 &= (~ADC_CH_FLAGS);

	ADCON0 |= (ADC_CH_FLAGS & Canal<<3);
}


/*-------------------------------------------------------------------------+
|                     byte LerADC(byte canal)                              |
|                                                                          |
| Input: Canal - numero do canal que pretendemos converter                 |
|                                                                          |
| Output: valor lido para aquele canal                                     |
|                                                                          |
| Objectivo: Activa o canal 0 a 4 do PIC e le o seu valor                  |
+-------------------------------------------------------------------------*/

byte LerADC(byte canal)
{
	SetChanel(canal);

	ADCON0bits.DONE = 1;
	
	while(ADCON0bits.DONE);

	return ADRESH; // ignora-se 2 bits da leitura para as flutua‡”es
}

#endif
