#include "drvUART.h"

void InitUART1()
{
	U1MODEbits.UARTEN=0;             // desactive l'UART1  
	
	U1MODEbits.USIDL = 0;	// Bit13 Continue in Idle
	U1MODEbits.IREN = 0;	// Bit12 No IR translation
	U1MODEbits.RTSMD = 0;	// Bit11 Simplex Mode
	U1MODEbits.UEN = 0;		// Bits8,9 TX,RX enabled, CTS,RTS not
	U1MODEbits.WAKE = 0;	// Bit7 No Wake up (since we don't sleep here)
	U1MODEbits.LPBACK = 0;	// Bit6 No Loop Back
	U1MODEbits.ABAUD = 0;	// Bit5 No Autobaud (would require sending '55')
	U1MODEbits.URXINV = 0;	// Bit4 IdleState = 1  (for dsPIC)
	U1MODEbits.BRGH = 0;	// Bit3 16 clocks per bit period
	U1MODEbits.PDSEL = 0;	// Bits1,2 8bit, No Parity
	U1MODEbits.STSEL = 0;	// Bit0 One Stop Bit


	U1BRG = BRGVAL;

	IPC2bits.U1RXIP = 4;	        // Mid Range Interrupt Priority level, no urgent reason
	IEC0bits.U1RXIE = 0;	        // Disable Recieve Interrupts : Validation IT RX
	
	_U1TXIF = 0;	// Clear the Transmit Interrupt Flag
	_U1TXIE = 0;	// Disable Transmit Interrupts
	_U1RXIF = 0;	// Clear the Recieve Interrupt Flag
	_U1RXIE = 0;	// Disable Recieve Interrupts

	U1MODEbits.UARTEN = 1;          //Active l'uart1
	U1STAbits.UTXEN=1;              //Active la transmision
 	Delay_Us(110); // tempo d'au moins 1 bit avant 1er envoi

}

void UART_puts(char * BUFF)
{
	unsigned int i=0;
	while (BUFF[i]!='\0') // tant que fin tableau non atteinte
	{
		while(U1STAbits.UTXBF); //attend buffer vide
		U1TXREG = BUFF[i]; // envoi sur TX
		i++;
	}
}