You are on page 1of 2

/*

** COMM2
** UART2 RS232 asynchronous communication demonstration code
*/
#include <p24fj128ga010.h>
#if defined( __PIC24FJ256GB110__ ) || defined( __PIC24FJ256GB106__ )
#define PLL_96MHZ_ON 0xF7FF
_CONFIG1(ICS_PGx2 & JTAGEN_OFF & FWDTEN_OFF) // JTAG off, watchdog timer off
_CONFIG2( FNOSC_PRIPLL & PLL_96MHZ_ON & PLLDIV_DIV2 & OSCIOFNC_OFF & FCKSM_CSDCM
D & POSCMOD_XT)
#else // PIC24FJ128GA010 and PIC24FJ256GA110
_CONFIG1( ICS_PGx2 & JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF)
_CONFIG2( FNOSC_PRIPLL & OSCIOFNC_OFF & FCKSM_CSDCMD & POSCMOD_HS)
#endif
//void init_PPS (int use_sdi1);
// I/O definitions for the Explorer16
#define CTS _RF12 // Clear To Send, input, HW handshake
#define RTS _RF13 // Request To Send, output, HW handshake
#define TRTS TRISFbits.TRISF13 // tris control for RTS pin
// timing and baud rate calculations
#define BRATE 34 // 115200 baud (BREGH=1)
#define U_ENABLE 0x8008 // enable the UART peripheral (BREGH=1)
#define U_TX 0x0400 // enable transmission
// initialize the UART2 serial port
void initU2 ( void)
{
U2BRG = BRATE;
U2MODE = U_ENABLE;
U2STA = U_TX;
TRTS = 0; // make RTS output
RTS = 1; // set RTS default status
} // initU2
// send a character to the UART2 serial port
int putU2 ( int c)
{
while ( CTS); // wait for !CTS, clear to send
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
return c;
} // putU2
// wait for a new character to arrive to the UART2 serial port
char getU2 ( void)
{
RTS = 0; // assert Request To Send !RTS
while ( !U2STAbits.URXDA); // wait for a new character to arrive
RTS = 1;
return U2RXREG; // read the character from receive buffer
}// getU2
main()
{
char c;
// 0. init the PPS configuration
// init_PPS (0);
// 1. init the UART2 serial port
initU2();
// 2. prompt
putU2( 'b');
// 3. main loop
while ( 1)
{

// 3.1 wait for a character


c = getU2();
// 3.2 echo the character
putU2( c);
} // main loop
}// main

You might also like