外加1K上拉电阻不行啊
贴出初始化程序。
//#include "math.h"
/***********************************************************************
Name:uart_init(void)
Function:initialize UART
Input:none
Output:none
************************************************************************/
void uart_init (void)
{
TRISC7 = 1;//config RX1 to input
TRISC6 = 0;//config TX1 to output
TRISC1 = 1;//config RX2 to input
TRISC0 = 0;//config TX2 to output
EECON2 = 0x55;
EECON2 = 0xaa;
IOLOCK = 0;
RPINR16 = 0x0c; //assign RP12 as rx2
RPOR11 = 0x05; //assign RP11 as tx2
//RPINR1 = 2;//assign RA5/RP2 as INT1
EECON2 = 0x55;
EECON2 = 0xaa;
IOLOCK = 1;
//enable_rs485_rx();
BRGH1 = 1;//high baud rate select bit,high speed in asynchronous mode
//BRG16 = 0;// 0=8-bit baud rate generator is used
BRG161 = 1;// 1=16-bit baud rate generator is used
//SPBRG1 = 16;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 8MHz
//SPBRG1 = 68;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 32MHz
SPBRG1 = 23;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 11.0592MHz
//SPBRG1 = 143;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 11.0592MHz
SPBRGH1 = 0;
BRGH2 = 1;//highh baud rate select bit,high speed in asynchronous mode
//BRG16 = 0;// 0=8-bit baud rate generator is used
BRG162 = 1;// 1=16-bit baud rate generator is used
//SPBRG2 = 16;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 8MHz
//SPBRG2 = 68;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 32MHz
SPBRG2 = 23;//n = FOSC/[16* (Baud Rate)] -1,baud rate = 115.2Kbps,FOSC = 11.0592MHz
SPBRGH2 = 0;
//BRGH = 0;//High baud rate select bit,low speed in asynchronous mode
//BRG16 = 0;//0=8-bit baud rate generator is used
//SPBRG1 = 12;//baud rate = 9600bps
//SPBRG2 = 12;//baud rate = 9600bps
//TX9 = 0;//Selects 8-bit transmission
//RX9 = 0;
//CSRC1 = 1;//master mode:clock generated internally from BRG
SYNC1 = 0;//EUART mode select bit,0=Asynchrounous mode
SPEN1 = 1;//serial port enable bit,1=serial port enabled
CREN1 = 1;//continues receive enable bit,1=enable recrive in asychronous mode
TXEN1 = 1;//Transmit enable bit,1=transmit enabled'
RC1IE = 1;//enable receive interrupt
//CSRC2 = 1;//master mode:clock generated internally from BRG
SYNC2 = 0;//EUART mode select bit,0=Asynchrounous mode
SPEN2 = 0;//serial port enable bit,1=serial port enabled
CREN2 = 1;//continues receive enable bit,1=enable recrive in asychronous mode
TXEN2 = 1;//Transmit enable bit,1=transmit enabled
RC2IE = 1;//enable receive interrupt
//ABDEN1 = 0;
//ABDEN2 = 0;
} |