#include "usart1.h"
#include "stdio.h"
#include "p18f46j50.h"
#include "delays.h"
#include "adc.h"
#include "string.h"
#include "usart.h"

//config bits
#pragma config WDTEN = OFF          //WDT disabled (enabled by SWDTEN bit)
//#pragma config PLLDIV = 3           //Divide by 3 (12 MHz oscillator input)
#pragma config STVREN = ON            //stack overflow/underflow reset enabled
#pragma config XINST = OFF          //Extended instruction set disabled
#pragma config CPUDIV = OSC1        //No CPU system clock divide
#pragma config CP0 = OFF            //Program memory is not code-protected
#pragma config OSC = HS//PLL          //HS oscillator, PLL enabled, HSPLL used by USB
#pragma config T1DIG = ON           //Sec Osc clock source may be selected
#pragma config LPT1OSC = OFF        //high power Timer1 mode
#pragma config FCMEN = OFF          //Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF           //Two-Speed Start-up disabled
//#pragma config WDTEN = OFF
//#pragma config WDTPS = 32768        //1:32768
#pragma config DSWDTOSC = INTOSCREF //DSWDT uses INTOSC/INTRC as clock
#pragma config RTCOSC = T1OSCREF    //RTCC uses T1OSC/T1CKI as clock
#pragma config DSBOREN = OFF        //Zero-Power BOR disabled in Deep Sleep
#pragma config DSWDTEN = OFF        //Disabled
#pragma config DSWDTPS = 8192       //1:8,192 (8.5 seconds)
#pragma config IOL1WAY = OFF        //IOLOCK bit can be set and cleared
#pragma config MSSP7B_EN = MSK7     //7 Bit address masking
#pragma config WPFP = PAGE_1        //Write Protect Program Flash Page 0
#pragma config WPEND = PAGE_0       //Start protection at page 0
#pragma config WPCFG = OFF          //Write/Erase last page protect Disabled
#pragma config WPDIS = OFF          //WPFP[5:0], WPEND, and WPCFG bits ignored  

void main(void)
{
	char pszText[64];
	char oCount;
	int d1=1, d2=2;

	InitPic();
	SetupUSART();

	while(1)
	{
 		putrs1USART("Szoveg");
		//sprintf("pszText, %c", "Homerseklet");
		//sprintf(pszText,"Homerseklet: %d.%0.3d \r\n",d1,d2);
	}
}

void InitPic(void)
{
	// Globális megszakítás tiltása
	INTCONbits.GIEH = 0;

//	OSCCONbits.SCS = 1;
//	RCSTA1bits.SPEN=1;
//	TRISCbits.TRISC7=1;
//	TRISCbits.TRISC6=1;

	// ADC funkciók kikapcsolása
	CloseADC();
	// PORTA bitjeinek digitális módba állítása
	// ADCON1 = 0x7F;

	// PORTC kezdőértékének és adatirányának beállítása
	LATC = INIT_PORTC;
	TRISC = INIT_TRISC; 	// 
}

void SetupUSART(void)
{
	// Setup USART
	Open1USART(	USART_TX_INT_OFF &
				USART_RX_INT_ON &
				USART_ASYNCH_MODE &
				USART_EIGHT_BIT &
				USART_CONT_RX &
				USART_BRGH,
				USART_SPEED	);

	baud1USART (	BAUD_IDLE_CLK_HIGH &
				BAUD_8_BIT_RATE &
				BAUD_WAKEUP_OFF &
				BAUD_IDLE_TX_PIN_STATE_HIGH &
				BAUD_IDLE_RX_PIN_STATE_HIGH &
				BAUD_AUTO_OFF);

//	IPR1bits.TXIP = 0;
//	IPR1bits.RCIP = 0;
}
