/*This header file contains the functions
  necessary to output a string, char, and
  an integer to a terminal*/

#include <stdlib.h>
#include <p24FJ256GB110.h>
#include <libpic30.h>
#include <stdbool.h>

// Define output port #s
#define U1Tx  3;
#define U1RTS  4;


// Initialize USART1 to USB input
void init_usart(unsigned short rate)
{
    RPINR18bits.U1RXR = 38;     //Assign U1Rx to pin RPI38
    RPINR18bits.U1CTSR = 40;    //Assign U1CTS to pin RPI40
    RPOR7bits.RP15R = U1Tx;     //Assign U1Tx to pin RP15
    RPOR15bits.RP30R = U1RTS;   //Assign U1RTS to pin rp21
    U1MODEbits.UARTEN = 1;      //Enable Uart1
    U1MODEbits.UEN = 0b10;      //Tx, Rx, CTS, and RTS enabled & used
    U1MODEbits.PDSEL = 0b00;    //8-bits no parity
    U1STAbits.UTXEN = 1;        //Enable transmit

    U1BRG = rate;               //set the BRG register to rate
}

/***************************************************************************
 * Function to put a char to the terminal
 ***************************************************************************/
void myputc(char value)
{
//    volatile bit TSReg = U1STAbits.TRMT;
    while(!U1STAbits.TRMT);
    U1TXREG = value;
    return;
}

/***************************************************************************
 * Function to put a string to the terminal
 ***************************************************************************/
void myputs(char *strn)
{
    int i = 0;
    while(strn[i] != NULL)
    {
        myputc(strn[i]);
        i++;
    }
}

/***************************************************************************
 * Function to put an integer to the terminal
 ***************************************************************************/
void myputi(int value)
{
    char cValue[10];
    itoa(cValue, value, 10);
    myputs(cValue);
    
}



