/*===================================================================================+
|                                                                                    |
|     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 "adc.h"
#include "Usart.h"
#include <delays.h>


/*-------------------------------------------------------------------------+
|              void SetADC(byte config1, byte config2)                     |
| Input: config1 - 	configuracao da ADC velocidade, juncao 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;//justificacao a direita
else
	ADCON1bits.ADFM = 0;

if (config1 & ADC1_FOSC_BIT)
	ADCON1bits.ADCS2 = 1;

//selecao 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                                   |
+-------------------------------------------------------------------------*/

static void SetChanel(byte Canal)
{
//Flag para auxiliar a config
#define ADC_CH_FLAGS 0b00111000

	ADCON0 &= 0b11000111; 
	
	Canal &= 0x03;//diferente de 3

	ADCON0 |= (Canal<<3);//ADC_CH_FLAGS & 
}


/*-------------------------------------------------------------------------+
|                     int 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                  |
+-------------------------------------------------------------------------*/

int LerADC(byte canal)
{
unsigned int a, b;
	
	SetChanel(canal);

	ADCON0bits.GO = 1;
	
	while(ADCON0bits.DONE);//espera de fim conversao
	
	a = (int)ADRESH;
	//EnvCh(&a,1);
	b = (int)(ADRESL >> 6);
	//EnvCh(&b,1);

	return ((a<<2) + b); // ignora-se 2 bits da leitura para as flutua‡”es
}

