|
Tài trợ cho PIC Vietnam |
Giao tiếp cổng COM và LPT RS232, RS485 và LPT là những giao tiếp cơ bản và kinh điển khi mới học về vi điều khiển... |
|
Ðiều Chỉnh | Xếp Bài |
16-10-2009, 05:50 PM | #1 |
Nhập môn đệ tử
Tham gia ngày: Jun 2009
Bài gửi: 1
: |
truyền nhận bằng UART trong PIC24
hiện tại mình đang tìm hiểu giao thức truyền UART trong PIC24FJ128GA010 mình sử dụng kit phát triển explorer 16 của microchip. mình đã đọc kĩ phần datasheet nhưng mình test thử 3 ngày rồi mà không thành công mong các bạn góp ý cho code mẫu sau. mục tiêu của code mẫu là nhân data từ hyper terminal và trả lại. hệ thống mình sử dụng thạch anh 8 mhz
#include <p24fj128ga010.h> _CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) _CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRI) // I/O definitions for the Explorer16 #define CTS _RF12 // Cleart 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 12 // 19200 baud (BREGH=0) #define U_ENABLE 0x8000 // enable the UART peripheral (BREGH=0) #define U_TX 0x0400 // enable transmission // initialize the UART2 serial port void initU2( void) { TRISFbits.TRISF5 = 0; TRISFbits.TRISF4 = 1; LATFbits.LATF5 = 1; 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 char putU2( char 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 the receive buffer }// getU2 main() { char c; // 1. init the UART2 serial port initU2(); //KHOI_DONG_LCD(); // 2. prompt putU2( 'n'); putU2( 't'); // 3. main loop while ( 1) { // 3.1 wait for a character c = getU2(); // 3.2 echo the character putU2( c); //PUTLCD( c ); } // main loop }// main |
|
|