#ifndef SWUART_H
#define SWUART_H
#ifndef _XC_H_
#include <xc.h>
#endif
/*******************************************************************************
* Parametros configurables por el usuario *
*******************************************************************************/
/*
* El valor de _XTAL_FREQ debe coincidir con el valor definido
* en el programa principal
*/
/*
* En el caso que solo se desee transmitir se debe definir TXONLY
* En el caso que solo se desee recibir se debe definir RXONLY
* En el caso de querer transmitir y recibir se debe definir TXRX
*/
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
#define TXPIN PORTBbits.RB0
#define RXPIN PORTBbits.RB1
#define TXDIR TRISBbits.TRISB0
#define RXDIR TRISBbits.TRISB1
#define SWUARTBAUD 19200
#define TXRX
/*******************************************************************************
* Parametros internos para los tiempos de cada bit *
*******************************************************************************/
#define _BitOutDelay (1000000UL/SWUARTBAUD)
#define StartDelay (1500000UL/SWUARTBAUD)
#if (_XTAL_FREQ <= 4000000)
#define BitOutDelay (_BitOutDelay - 24)
#define BitInDelay (_BitOutDelay - 24)
#elif (_XTAL_FREQ <= 6000000) && (_XTAL_FREQ > 4000000)
#define BitOutDelay (_BitOutDelay - 16)
#define BitInDelay (_BitOutDelay - 16)
#elif (_XTAL_FREQ <= 8000000) && (_XTAL_FREQ > 6000000)
#define BitOutDelay (_BitOutDelay - 12)
#define BitInDelay (_BitOutDelay -12)
#elif (_XTAL_FREQ <= 10000000) && (_XTAL_FREQ > 8000000)
#define BitOutDelay (_BitOutDelay - 10)
#define BitInDelay (_BitOutDelay -9)
#elif (_XTAL_FREQ <= 12000000) && (_XTAL_FREQ > 10000000)
#define BitOutDelay (_BitOutDelay - 8)
#define BitInDelay (_BitOutDelay - 7)
#elif (_XTAL_FREQ <= 16000000) && (_XTAL_FREQ > 12000000)
#define BitOutDelay (_BitOutDelay - 6)
#define BitInDelay (_BitOutDelay - 5)
#elif (_XTAL_FREQ <= 20000000) && (_XTAL_FREQ > 16000000)
#define BitOutDelay (_BitOutDelay - 5)
#define BitInDelay (_BitOutDelay - 4)
#elif (_XTAL_FREQ <= 26000000) && (_XTAL_FREQ > 20000000)
#define BitOutDelay (_BitOutDelay - 3)
#define BitInDelay (_BitOutDelay - 3)
#elif (_XTAL_FREQ <= 48000000) && (_XTAL_FREQ > 26000000)
#define BitOutDelay (_BitOutDelay - 2)
#define BitInDelay (_BitOutDelay - 2)
#endif
/*******************************************************************************
* Definicion de funciones *
*******************************************************************************/
/*
* La funcion swgets recibe una cadena que termine con '\r' (decimal 13)
* la cadena es devuelta a travez del puntero que se le pasa como parametro
*/
void swinit_uart(void);
void swputch(unsigned char);
void swputs(unsigned char *);
unsigned char swgetch(void);
void swgets(unsigned char *);
#endif /* SWUART_H */