2013-04-16 104 views
0

我正在使用MPlab从PIC微控制器读取数据。我正在使用pic18F87J11。 我想要读取的数据在RS232的DB9的引脚3上,而我的RS232连接到PIC微控制器。 任何人都可以帮助我或给我一个简单的示例代码来做到这一点?MPLAB中的RS232通信

谢谢

回答

0
// 
// Designed by www.MCUExamples.com 
// [email protected] 
// Serial communications example. Echo data comes to serial port 
// using serial receive interrupt. 
// 

#include <p18f4520.h> 

#pragma config OSC   = HS  // 20MHz Crystal, (HS oscillator) 
#pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on Reset) 
#pragma config WDT   = OFF // watch dog timer off 
#pragma config LVP   = OFF // Low voltage program off 

unsigned char cUART_char; 
unsigned char cUART_data_flg; 

void init_uart(void); 
void UART_putc(unsigned char c); 
void InterruptHandlerLow(); 

void main() 
{ 

    init_uart(); // init UART module 
    while (1) // infinite loop which handles ncoming data as they arrive 
    { 
     if (cUART_data_flg==1)// if new data available, send it back through USART tx line (echo it) 
     { 
      UART_putc(cUART_char); 
      cUART_data_flg=0; // clear new data flag so one charactor will echoed once 
     } 
    } 
} 

//---------------------------------------------------------------------------- 
//---------------------------------------------------------------------------- 

#pragma code InterruptVectorLow = 0x18 
void InterruptVectorLow (void) 
{ 
    _asm 
    goto InterruptHandlerLow //jump to interrupt routine 
    _endasm 
} 

//---------------------------------------------------------------------------- 
// Low priority interrupt routine 

#pragma code 
#pragma interrupt InterruptHandlerLow 

void InterruptHandlerLow() 
{ 
    if (PIR1bits.RCIF==1)//is interrupt occured by EUSART receive?, 
         //then RCREG is full we have new data (cleared when RCREG is read) 
    { 
    if(RCSTA&0x06) //more efficient way than following commented method to check for reception error 
    //if(RCSTAbits.FERR==1 || RCSTAbits.OERR==1) 
    { 
     RCSTAbits.CREN=0; //Overrun error (can be cleared by clearing bit CREN) 
     cUART_char=RCREG; //clear Framing error 
     RCSTAbits.CREN=1; 
    } 
    else 
    { 
     cUART_char = RCREG; // read new data into variable 
     cUART_data_flg = 1; // new data received. so enable flg 
    } 
    } 
} 

//---------------------------------------------------------------------------- 
//---------------------------------------------------------------------------- 

void init_uart(void) // init UART module for 9600bps boud, start bit 1, stopbit 1, parity NONE 
{ 
    cUART_data_flg=0; // init data receive flag to zero (no data) 
    TRISCbits.TRISC7=1; //Make UART RX pin input 
    TRISCbits.TRISC6=0; //Make UART TX pin output 
    SPBRGH = 0x02;  //9600bps 20MHz Osc 
    SPBRG = 0x08;   

    RCSTAbits.CREN=1; //1 = Enables receiver 
    RCSTAbits.SPEN=1; //1 = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins) 
    BAUDCONbits.BRG16=1;//1 = 16-bit Baud Rate Generator – SPBRGH and SPBRG 

    TXSTAbits.SYNC=0; //0 = Asynchronous mode 
    TXSTAbits.BRGH=1; //1 = High speed 
    TXSTAbits.TXEN=1; //1 = Transmit enabled 

    RCONbits.IPEN = 1; //enable Interrupt priority levels 
    IPR1bits.RCIP=0;  // EUSART Receive Interrupt Priority 0 = Low priority 
    PIE1bits.RCIE=1;  // 1 = Enables the EUSART receive interrupt 
    INTCONbits.GIEL = 1;//enable interrupts 
    INTCONbits.GIEH = 1;  
} 

//---------------------------------------------------------------------------- 
//---------------------------------------------------------------------------- 

void UART_putc(unsigned char c) 
{ 
    TXSTAbits.TXEN=0;// disable transmission 
    TXREG=c;   // load txreg with data 
    TXSTAbits.TXEN=1; // enable transmission 
    while(TXSTAbits.TRMT==0) // wait here till transmit complete 
    { 
    Nop(); 
    } 
}