/* OK teste le 24/05/2011 sur 64MC802 */


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

#define Fcy (80000000/2)
#define BAUDRATE 9600
#define BRGVAL ((Fcy/BAUDRATE)/16) - 1

//config oscillateur
// demarrage RC puis switch sur quart+pll	
_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 InitPORT_AND_PIN() 
{  // depend du schema de la carte
	_U1RXR=3;	                    //RX sur RP3 autre ecriture RPINR0bits.INT1R=0
	_RP4R=3;                        //TX sur RP4
}

void InitClock2() 
{   /*Quartz 10 Mhz => coef x8 */
	PLLFBD = 30;	//  ,M = 32
	CLKDIVbits.PLLPOST = 0;	// N1 = 2
	CLKDIVbits.PLLPRE = 0;	// N2 = 2
	OSCTUN = 0;
	RCONbits.SWDTEN = 0;
	// Clock switch to incorporate PLL
	__builtin_write_OSCCONH(0x03);		// Initiate Clock Switch to 
													// FRC with PLL (NOSC=0b001)
	__builtin_write_OSCCONL(0x01);		// Start clock switching
	while (OSCCONbits.COSC != 0b011);	// Wait for Clock switch to occur	
	while(OSCCONbits.LOCK != 1) {};
}

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++;
	}
}

unsigned char BUFF[50]={'i','u','t','\r','\n','\0'};

int main (void)

{	InitClock2() ; //init oscillateur
	TRISBbits.TRISB0=0;
	AD1PCFGLbits.PCFG2=1;
	InitPORT_AND_PIN(); // init broche remapable
	InitUART1(); // init broche remapable

	// exemple 1 : appel à la fct  UART_puts
	//passage du tableau à envoyer
	UART_puts(BUFF);
	///////////////////////////

	// exemple 2 : formatage avec sprintf
	// BUFF: tableau de caractere résultat de la conversion 
	sprintf(BUFF,"Un entier: %d\n Un float: %f \n",10,10.5);
	UART_puts(BUFF); // envoi sur RS232 du resultat

	for (;;)
	{
		PORTBbits.RB0=!PORTBbits.RB0; 
		Delay(Delay_15mS_Cnt); // tempo BLOQUANTE: attention pour demo uniquement!!!
		UART_puts(BUFF); // envoi en bouble
	
	}
}
