COM_UART.txt

download COM_UART.txt

of 2

Transcript of COM_UART.txt

/*** COMM2** UART2 RS232 asynchronous communication demonstration code*/#include #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_CSDCMD & 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 portvoid initU2 ( void){U2BRG = BRATE;U2MODE = U_ENABLE;U2STA = U_TX;TRTS = 0; // make RTS outputRTS = 1; // set RTS default status} // initU2// send a character to the UART2 serial portint putU2 ( int c){while ( CTS); // wait for !CTS, clear to sendwhile ( U2STAbits.UTXBF); // wait while Tx buffer fullU2TXREG = c;return c;} // putU2// wait for a new character to arrive to the UART2 serial portchar getU2 ( void){RTS = 0; // assert Request To Send !RTSwhile ( !U2STAbits.URXDA); // wait for a new character to arriveRTS = 1;return U2RXREG; // read the character from receive buffer}// getU2main(){char c;// 0. init the PPS configuration// init_PPS (0);// 1. init the UART2 serial portinitU2();// 2. promptputU2( 'b');// 3. main loopwhile ( 1){// 3.1 wait for a characterc = getU2();// 3.2 echo the characterputU2( c);} // main loop}// main