PDA

View Full Version : truyền nhận bằng UART trong PIC24


hnam
16-10-2009, 05:50 PM
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

quangdk
15-07-2011, 11:37 AM
không biết các bạn đã làm chưa, đã lâu mình khong vào diễn đàn, mình post thử code của mình về truyền nhận UART. K biết có lạc hậu quá không!Mong các bạn góp ý

/************************************************** ***************************
*
* UART Driver for PIC24.
*
************************************************** ***************************
* FileName: uart2.c
* Dependencies: system.h
* Processor: PIC24
* Compiler: MPLAB C30
* Linker: MPLAB LINK30
* Company: Microchip Technology Incorporated
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the "Company") is intended and supplied to you, the Company's
* customer, for use solely and exclusively with products manufactured
* by the Company.
*
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
* A simple UART polled driver
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Anton Alkhimenok 10/18/05 ...
* Brant Ivey 3/14/06 Added support for PIC24FJ64004 PIM
************************************************** ***************************/
#include "system.h"

/************************************************** ***************************
* U2BRG register value and baudrate mistake calculation
************************************************** ***************************/
#define BAUDRATEREG2 SYSCLK/32/BAUDRATE2-1

#if BAUDRATEREG2 > 255
#error Cannot set up UART2 for the SYSCLK and BAUDRATE.\
Correct values in main.h and uart2.h files.
#endif

#define BAUDRATE_MISTAKE 1000*(BAUDRATE2-SYSCLK/32/(BAUDRATEREG2+1))/BAUDRATE2
#if (BAUDRATE_MISTAKE > 2)||(BAUDRATE_MISTAKE < -2)
#error UART2 baudrate mistake is too big for the SYSCLK\
and BAUDRATE2. Correct values in uart2.c file.
#endif

/************************************************** ***************************
* Function: UART2Init
*
* Precondition: None.
*
* Overview: Setup UART2 module.
*
* Input: None.
*
* Output: None.
*
************************************************** ***************************/
void UART2Init()
{
// Set directions of UART IOs
UART2_TX_TRIS = 0;
UART2_RX_TRIS = 1;
U2BRG = BAUDRATEREG2;
U2MODE = 0;
U2STA = 0;
U2MODEbits.UARTEN = 1;
U2STAbits.UTXEN = 1;
// reset RX flag
IFS1bits.U2RXIF = 0;
}

/************************************************** ***************************
* Function: UART2PutChar
*
* Precondition: UART2Init must be called before.
*
* Overview: Wait for free UART transmission buffer and send a byte.
*
* Input: Byte to be sent.
*
* Output: None.
*
************************************************** ***************************/
void UART2PutChar(char Ch){
// wait for empty buffer
while(U2STAbits.UTXBF == 1);
U2TXREG = Ch;
}

/************************************************** ***************************
* Function: UART2IsPressed
*
* Precondition: UART2Init must be called before.
*
* Overview: Check if there's a new byte in UART reception buffer.
*
* Input: None.
*
* Output: Zero if there's no new data received.
*
************************************************** ***************************/
char UART2IsPressed()
{
if(IFS1bits.U2RXIF == 1)
return 1;
return 0;
}

/************************************************** ***************************
* Function: UART2GetChar
*
* Precondition: UART2Init must be called before.
*
* Overview: Wait for a byte.
*
* Input: None.
*
* Output: Byte received.
*
************************************************** ***************************/
char UART2GetChar(){
char Temp;
while(IFS1bits.U2RXIF == 0);
Temp = U2RXREG;
IFS1bits.U2RXIF = 0;
return Temp;
}

/************************************************** ***************************
* Function: UART2PutDec
*
* Precondition: UART2Init must be called before.
*
* Overview: This function converts decimal data into a string
* and outputs it into UART.
*
* Input: Binary data.
*
* Output: None.
*
************************************************** ***************************/
void UART2PutDec(unsigned char Dec){
unsigned char Res;
Res = Dec;

if(Res/100)
UART2PutChar(Res/100+'0');
Res = Res - (Res/100)*100;

if(Res/10)
UART2PutChar(Res/10+'0');
Res = Res - (Res/10)*10;

UART2PutChar(Res+'0');
}

/************************************************** ***************************

/************************************************** ***************************
*
* UART Driver for PIC24.
* Modified for PIC24FJ64GA004 family with PPS.
*
************************************************** ***************************
* FileName: uart2.c
* Dependencies: system.h
* Processor: PIC24
* Compiler: MPLAB C30
* Linker: MPLAB LINK30
* Company: Microchip Technology Incorporated
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the "Company") is intended and supplied to you, the Company's
* customer, for use solely and exclusively with products manufactured
* by the Company.
*
* The software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
* A simple UART polled driver
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Anton Alkhimenok 10/18/05 ...
* Brant Ivey 3/14/06 Modified for PIC24FJ64GA004 family with PPS.
************************************************** ***************************/

#include "iomapping.h"

/************************************************** ***************************
* DEFINITIONS
************************************************** ***************************/
// Baudrate
#define BAUDRATE2 19200

// UART IOs
#ifdef PPS_UART2_TX_TRIS
#define UART2_TX_TRIS PPS_UART2_TX_TRIS
#define UART2_RX_TRIS PPS_UART2_RX_TRIS
#else
#define UART2_TX_TRIS TRISFbits.TRISF12
#define UART2_RX_TRIS TRISFbits.TRISF13
#endif


/************************************************** ***************************
* Function: UART2Init
*
* Precondition: None.
*
* Overview: Setup UART2 module.
*
* Input: None.
*
* Output: None.
*
************************************************** ***************************/
extern void UART2Init();

/************************************************** ***************************
* Function: UART2PutChar
*
* Precondition: UART2Init must be called before.
*
* Overview: Wait for free UART transmission buffer and send a byte.
*
* Input: Byte to be sent.
*
* Output: None.
*
************************************************** ***************************/
extern void UART2PutChar(char Ch);

/************************************************** ***************************
* Function: UART2IsPressed
*
* Precondition: UART2Init must be called before.
*
* Overview: Check if there's a new byte in UART reception buffer.
*
* Input: None.
*
* Output: Zero if there's no new data received.
*
************************************************** ***************************/
extern char UART2IsPressed();

/************************************************** ***************************
* Function: UART2GetChar
*
* Precondition: UART2Init must be called before.
*
* Overview: Wait for a byte.
*
* Input: None.
*
* Output: Byte received.
*
************************************************** ***************************/
extern char UART2GetChar();

/************************************************** ***************************
* Function: UART2PutDec
*
* Precondition: UART2Init must be called before.
*
* Overview: This function converts decimal data into a string
* and outputs it into UART.
*
* Input: Binary data.
*
* Output: None.
*
************************************************** ***************************/
extern void UART2PutDec(unsigned char Dec);

/************************************************** ***************************
* EOF
************************************************** ***************************/

#include "system.h"
//#include "LCD.h"

_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & BKBUG_ON & ICS_PGx2 & FWDTEN_OFF)
_CONFIG2(IESO_OFF & FCKSM_CSDCMD & OSCIOFNC_ON & FNOSC_PRI & POSCMOD_HS)


// useful macros for VT100 terminal emulation
#define clrscr() UART2PutChar( "\x1b[2J")
#define home() UART2PutChar( "\x1b[1,1H")

#define COL 40
#define ROW 23

//#define DELAY 1000

main()
{
char c;
//initLCD(); // initializations
UART2Init();
UART2PutChar('s');

while (1)
{
c=UART2GetChar();
UART2PutChar(c);
//UART2PutChar( "Learn to fly with the PIC24!");
//UART2PutChar('\n');
}
}