0

我正在使用 MPlab 从 pic 微控制器读取数据。我正在使用 pic18F87J11。我要读取的数据在 RS232 的 DB9 的引脚 3 上,我的 RS232 连接到 pic 微控制器。谁能帮助我或给我一个简单的示例代码来做到这一点?

谢谢,

4

1 回答 1

0
    //
// Designed by www.MCUExamples.com
// rasika0612@gmail.com
// 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();
  }
}
于 2013-04-24T13:26:11.557 回答