// OK testé le 15 /06 /11 sur 64MC802
// exemple de mise en oeuvre de convertion ADC
// scénario: automatic sample , manual conv

#include "p33FJ64MC802.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include "ConfigSystem.h"  // definition des labels Fcy etc....
#include "../../../drivers/delay.h"
#include "../../../drivers/drvUART.h"


//config oscillateur
_FOSCSEL(FNOSC_PRIPLL);								// Choix du oscillateur + PLL
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF  & POSCMD_XT);	// Pas de basculement d'horloge
													// pas de OSC2
													// type d'oscillateur XT
_FWDT(FWDTEN_OFF); // watchdog off;


// les differentes configs on été eclatées dans des fichiers distinct
// meilleure visibilité
extern  	Init_ADC_AutoSampleManualConv();( void );
extern void UART_puts(char * string);
extern  void InitClock();
extern void InitUART();
extern void InitPORT_AND_PIN();
extern void Init_ADC_AutoSampleManualConv();



unsigned char BUFFER[30]={0};
unsigned char HelloMsg[7]="Bonjour";



int main ( void )
{
	unsigned int ADCValue=0;

	

	InitClockNoSwitch();	// This is the PLL settings	
	InitPORT_AND_PIN();
	InitUART();
	Init_ADC_AutoSampleManualConv();


	UART_puts(HelloMsg);



    /* Infinite Loop */
    while ( 1 )
	{
		//1: Sampling is started automatically after conversion completion of the previous sample.
		//2: Conversion is started by clearing the SAMP bit in software.
		//3: Conversion is complete.
		//4: Sampling is started automatically after conversion completion of the previous sample.
		//5: Conversion is started by clearing the SAMP bit in software.


		AD1CON1bits.SAMP = 0; 		// Start converting
		// pas besoin de tempo pour l'echantillonnage /blocage car mode auto


		while (!AD1CON1bits.DONE);  // Wait for the conversion to complete
		AD1CON1bits.DONE = 0;
		ADCValue = ADC1BUF0; 	    // Read the conversion result
		sprintf(BUFFER,"La valeur est %d \n", ADCValue);
		UART_puts(BUFFER);
		Delay(Delay_1S_Cnt); //delay BLOQUANT 1s pour demo uniquement
		Delay(Delay_1S_Cnt); //delay BLOQUANT 1s pour demo uniquement
	}	


		
	
	
}


