#include <htc.h>
#include "rs232.h"

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif 

unsigned char rs232_init()
{
	TRISB = 0x00;
	PORTB = 0x00;
	SPBRG = 25;								//Baud rate 9600, datasheet
	TXSTA = 0b00100110;
	RCSTA = 0b10010000;
	return(0);
}

void rs232_send_byte(unsigned char x)		//Slanje
{
	TXREG = x;
	while(!TRMT);
	return;
}

unsigned char rs232_read_byte()				//Čitnanje
{
	if(OERR)								//Provjera overrun error-a
	{
		CREN=0;
		CREN=1;
	}
	if(FERR)								//Provjera framing error-a
		RCREG;
	if(RCIF)
		return (RCREG);
	else
		return 0;
}

void rs232_send_text(const char string[])
{
	int i=0;
	char c;
	
	while(string[i]!='\0')
	{
		c = string[i];
		rs232_send_byte(c);
		i++;
	}
	return;
}