/*
*******************************************************************************************************************
*												USART DEMO
* Description:  Example 2 for PIC18-LF4550-STK1 board to :
*				1. Blink LED D1 to D4 connected to PORTD<3:0> like example 1
*				2. Wait for any single character from PC via pin RC7, if any character received. Baud rate = 4800
*				3. Transmit an ASCII string of "Hello World[null]" via the USART port RC6.
*				4. Echo the same character received
* File:			..\src\chp2\src2_1\main.c
* Software:		C18 version 2.40 under MPLAB 7.41 IDE
* Programmer: 	John Leung, TechToys Co. (www.TechToys.com.hk)
* Hardware:		PIC18-LF4550-STK1, with PIC18LF4550, 20.000 MHz crystal
* Date:			14th Sept 2006
* Version:		1.0
* Programmer:	John Leung
* Remarks:		Enable JP2 for LED and JP6&7 for hardware USART (RC6,7)
*				ICD2 employed for linker script 18f4550i.lkr.
*******************************************************************************************************************
*/

#include <p18cxxx.h>
#include <delays.h>
#include <usart.h>

/*
********************************************************************************************************************
*												Pragma Section
*
* Configuration bits under MPLAB v7.41 subject to changes on every little change on project settings.
* So #pragma config statments used for configuration bits instead of using Configuration bits tab in MPLAB
* Note: Refer to pdf document "mcc18\doc\MPLAB-C18-Addendum.pdf" under mcc18 installation directory for config
* 		variables available
********************************************************************************************************************
*/
#pragma config FOSC   = HS			// HS oscillator
#pragma config CPUDIV = OSC4_PLL6	// OSC postscaler of 4 => CPU clock at 5MHz (20MHz/4)
#pragma config PWRT   = ON			// PowerUp timer ON
#pragma config WDT    = OFF			// watchdog timer off
#pragma config LVP    = OFF			// low level voltage OFF
#pragma config BOR    = OFF			// brown out detect OFF
#pragma config DEBUG  = ON			// Debug ON
#pragma config VREGEN = OFF			// Off 3.3V internal regulator
#pragma config PBADEN = OFF 		// PORTB<4:0> as digital IO on reset

/*
********************************************************************************************************************
*													MAIN
********************************************************************************************************************
*/
unsigned char counter, charRd;

void main(void)
{
	ADCON1 = 0x0F;								//PORTA all digital operation
	LATD = 0x00;								//use LED1 - LED4 as a simple counter
	TRISD = 0x00; 								//configure PORTD for output

	OpenUSART(	USART_TX_INT_OFF &
				USART_RX_INT_OFF &
				USART_ASYNCH_MODE &
				USART_EIGHT_BIT &
				USART_CONT_RX &	
				USART_BRGH_HIGH, 64);			//Fosc = 5 MHz, SPBRG = 64 => baud rate=5MHz/(16*(64+1))=4,808 bps
												//% error = (4808-4800)/4800 = 0.16%
	putrsUSART("Communication at 4800 bps:\n");	
										
	while(1)
	{

			for(counter = 0 ; counter <16; counter++){
			LATD = counter;
			Delay100TCYx(200);					//software delay 100*200 machine cycle=>20,000*(0.8us)=16ms
												//dependency: delays.h under ..\mcc18\h
			if(DataRdyUSART()){					//monitors for any character received
				charRd=ReadUSART();
				while(BusyUSART());
				putrsUSART("Hello World Echo:");//Echo the character received with "Hello World Echo" string
				WriteUSART(charRd);
				}//if statement
			}//for loop
					
	}//while(1)
}
