//-----------------------------------------------------------------------------------//
// Nom du projet 		:	Coursera_Microcontroleur
// Nom du fichier 		:   main_2_8.c
// Date de création 	:   30.01.2014
// Date de modification : 	06.02.2014
// 
// Auteur 				: 	Philou (Ph. Bovey) 
//
// Description 			: 	utilisation de deux switchs (S4-->RD13) et (S3-->RD6),
//							l'un servir à allumer une led (D3-->RA0) et l'autre 
//							servira à l'éteindre  						
//
// Remarques			: 
// 	    chemin pour trouver le headerfile 
//		C:\Program Files\Microchip\MPLAB C30\support\dsPIC33F\h
//
//	    doc pour le DSP : 
//		http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en546064
// 
//----------------------------------------------------------------------------------//
//--- librairie à inclure ---// 
#if defined(__dsPIC33F__)
#include "p33Fxxxx.h"
#endif

//#include "antirebond.h"

//--- Fusibles ---//
_FOSCSEL(FNOSC_PRI); 								// utilisation du quartz de 8MHz externe 
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_XT); 	// pin OS2 garder pour l'horlohe 
													// oscillateur XT	
_FWDT(FWDTEN_OFF); 									// ne pas activer le watchdog

//--- definition ---// 
#define SWITCH_S4 		PORTDbits.RD13				// entrée digital 
#define SWITCH_S3 		PORTDbits.RD6				// entrée digital
#define LED_3	 		LATAbits.LATA0				// sortie digital 


//--- déclaration de variables de gloables ---// 

//--- déclaration de prototypes ---// 
void init_in_out(void); 				// configuration des entrées - sorties
void init_oscillateur(void); 			// configuration oscillateur  

//--- programme principale ---//
int main(void)
{
	//--- initialisation ---//
	init_in_out();						// entrées / sorties
	init_oscillateur(); 				// oscillateur 

	LED_3 = 0;							// led Eteinte  

	//--- boucle sans fin ---//
	while(1)
	{
		//--- test si pression (impulsion sur le switch S4) ---//
 		if(SWITCH_S4 == 0)
		{
			LED_3 = 1;
		}

		if(SWITCH_S3 == 0)
		{
			LED_3 = 0;
		}				
	}
}


//----------------------------------------------------------------------------------//
//--- nom 				: init_in_out
//--- entrée - sortie 	: - / - 
//--- description 		: initialisation IN/OUT ANAL/NUM --> 
//--- remarque 			: doc = section 10 
//----------------------------------------------------------------------------------//
void init_in_out(void)
{	
	//--- configuration entrée analogique ---// 

	//--- configuration entrée numérique ---// 
	TRISDbits.TRISD13 = 1; 						// S4 --> switch 
	TRISDbits.TRISD6 = 1; 						// S3 --> switch 	

	//--- configuration sortie numérique ---// 
	TRISAbits.TRISA0 = 0; 						// D3 --> led
}


//----------------------------------------------------------------------------------//
//--- nom 				: init_oscillateur
//--- entrée - sortie 	: - / - 
//--- description 		: configuration oscillateur | clock à choix  
//-- remarque 			: doc = section 7
//----------------------------------------------------------------------------------//
void init_oscillateur(void)
{
	//--- configuration de l'oscillateur interne à 8Mhz ---//
	// utilisation formules du datasheet du DSPic33FJ256GP710A --> page 146
	// quartz utilisé sur le carte = 8MHz = Fin
	// Fcy = Fosc / 2 ; Fosc = Fin(M/(N1 N2))
	// Fin = 8Mhz --> Fosc = 16Mhz --> Fcy = 8Mhz 
	OSCCONbits.COSC 	= 3;			// sélection de l'oscillateur XT 
	OSCCONbits.CLKLOCK	= 0;			// l'horloge et la PLL peuvent être modifié 	
	
	CLKDIVbits.ROI	= 0; 				// pas d'effet si il y a interruption 
	CLKDIVbits.DOZE	= 0; 				// pas de réduction sur l'horloge Fcy /1
	CLKDIVbits.DOZEN	= 0; 		
	CLKDIVbits.PLLPRE 	= 0; 			// N1 = 2	
    CLKDIVbits.PLLPOST 	= 0; 			// N2 = 2 	
	
	PLLFBDbits.PLLDIV 	= 6;			// M = 8  

	__builtin_write_OSCCONH(0x03); 		// fonction appelant du code assembleur 
										// configuration du registre NOSC (OSCCON) --> 
										// 011 = Primary Oscillator with PLL (XTPLL, HSPLL, ECPLL)
	__builtin_write_OSCCONL(0x01);		// Active la commutation de la clock
	while(OSCCONbits.COSC != 0b011); 	//
}
