#include <p24FJ64GA002.h> // This is the header file for the uP you are using
#define OSC_FREQ 32000000 // Frequency of the CPU Oscillator
#define Fcy (OSC_FREQ/2) // Peripheral Clock Frequency
// Initialize UART 1 to the baud_rate passed
void USARTInit(unsigned long baud_rate)
{
unsigned long temp, temp1;
U1MODE = 0x0800; // No Flow Control, Use TX & RX, 8 data bits, no parity, one stop bit, BRGH=0 - start with Uart Disabled
temp = (unsigned long)Fcy >> 4L;
temp1 = (temp / baud_rate);
U1BRG = (unsigned int)(temp1); // Set BRG Register based on Baud Rate passed
RPINR18bits.U1RXR=8; // Assign RX input to RPR8 (pin 17)
RPOR4bits.RP9R=3; // Assign TX output to RPR9 (pin 18)
U1MODEbits.UARTEN=1; // Enable Uart
U1STAbits.UTXEN = 1; // Enable Transmitter
}
// Send 1 character to UART 1
void USARTPut(unsigned char c)
{
while(U1STAbits.UTXBF == 1); // Wait for room in the transmitter FIFO..
U1TXREG = c; // then send the character
}
// Send an array of characters to UART 1
void USARTPutArray(unsigned char *array, unsigned int number_bytes)
{
while (number_bytes--)
USARTPut(*(array++));
}
// Send a zero-terminated string to UART 1
void USARTPutString(char *string)
{
unsigned char c;
while( (c = *(string++)) )
USARTPut(c);
}