

#include <p33FJ64MC802.h>
#include <stdio.h>
#include <timer.h>
#include <uart.h>

#define FCY (7370000/2)
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16) - 1


// oscillateur interne a 7.37MHz 
/*
_FOSCSEL(FNOSC_FRC);
_FOSC( POSCMD_NONE &OSCIOFNC_OFF&FCKSM_CSDCMD);		// Primary disabled Oscillator & OSC2 is clock O/P &  Both disabled (pas de changement d'oscillateur)
_FWDT(FWDTEN_OFF); 
*/


_FOSCSEL(FNOSC_FRC);			
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF  & POSCMD_XT);	// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
													// OSC2 Pin Function: OSC2 is Clock Output
													// Primary Oscillator Mode: XT Crystal
_FWDT(FWDTEN_OFF); 




void InitUART1() {
	U1MODEbits.UARTEN=0;             // desactive l'UART1
	U1BRG=23;                       //9600 

	
	U1MODEbits.PDSEL=0;             //8bit sans parité
	U1MODEbits.STSEL=0;             //un bit de stop;
	U1MODEbits.UEN=0;               //Juste UX et RX (pas de controles...)
	U1MODEbits.ABAUD=0;             // pas de mesure de vitesse
	U1MODEbits.BRGH=0;              //vitesse lente
	IPC2bits.U1RXIP = 4;	        // Mid Range Interrupt Priority level, no urgent reason
	IEC0bits.U1RXIE = 1;	        // Enable Recieve Interrupts
	U1MODEbits.UARTEN = 1;          //Active l'uart1
	U1STAbits.UTXEN=1;              //Active la transmision
}

void InitPORT_AND_PIN() {
	_U1RXR=3;	                    //RX sur RP3 autre ecriture RPINR0bits.INT1R=0
	_RP4R=3;                       //TX sur RP4
}



void UART_puts(char * BUFF)
{
	unsigned int i=0;
	while (BUFF[i]!='\0')
	{
		while(U1STAbits.UTXBF); //attend buffer vide
		U1TXREG = BUFF[i];
		i++;
	}
}

//unsigned char BUFF[4]={'t','e','f','\0'};
	unsigned  int u=0;
	unsigned  int i=0;

/*
int main (void)
{

    unsigned char BUFF[]="azerty";
	unsigned  int i;

	TRISBbits.TRISB0=0;			// On peut visualiser sur la broche 4 du dspic (RB0)
	AD1PCFGLbits.PCFG2=1;

    InitPORT_AND_PIN();
	InitUART1();
    sprintf(BUFF,"booba");
	UART_puts(BUFF);
	

	for ( u=0;u<5000;u++) {i++;};


	while(1)
	{
	while(U1STAbits.TRMT==0); //attend buffer vide
		U1TXREG = 'g';
		
	while(U1STAbits.TRMT==0); //attend buffer vide
		U1TXREG = 'r';
	}	


}
*/

int main(void) {
	unsigned char BUFF[50]="test";

//	InitClock();	// This is the PLL settings	
	InitPORT_AND_PIN();
	InitUART1();
//	sprintf(BUFF,"Un entier: %d\nUn float: %d",10,10);
	UART_puts(BUFF);

	while(1)
	{
	
UART_puts(BUFF);
	}
	
}





