如何通过uart0 LPC24xx接收时发送一个字

时间:2014-12-15 10:03:56

标签: c interrupt uart lpc

KEIL有一个代码示例。 它工作正常。 但我的问题是我需要在接收到LPC24xx时发送一个字而不是字符。

这里是例子:

void sio_irq (void) __irq {
  volatile char dummy;
  volatile char IIR;
  struct buf_st *p;

  /*------------------------------------------------
  Repeat while there is at least one interrupt source.
  ------------------------------------------------*/
  while (((IIR = U0IIR) & 0x01) == 0) {
     switch (IIR & 0x0E) {
       case 0x06: /* Receive Line Status */
         dummy = U0LSR;  /* Just clear the interrupt source */
         break;

       case 0x04: /* Receive Data Available */
       case 0x0C: /* Character Time-Out */
         p = &rbuf;

         if (((p->in - p->out) & ~(RBUF_SIZE-1)) == 0) {
           p->buf [p->in & (RBUF_SIZE-1)] = U0RBR;
           p->in++;
         }
         break;

       case 0x02: /* THRE Interrupt */
         p = &tbuf;

         if (p->in != p->out) {
           U0THR = p->buf [p->out & (TBUF_SIZE-1)];
           p->out++;
           tx_restart = 0;
         }
         else {
           tx_restart = 1;
         }
         break;

       case 0x00: /* Modem Interrupt */
//         dummy = U1MSR;  /* Just clear the interrupt source */
         break;

       default:
         break;
     }
  }

  VICVectAddr = 0; /* Acknowledge Interrupt */
}

void com_initialize (void) {
  volatile char dummy;

  /*------------------------------------------------
  Clear com buffer indexes.
  ------------------------------------------------*/
  tbuf.in = 0;
  tbuf.out = 0;
  tx_restart = 1;

  rbuf.in = 0;
  rbuf.out = 0;

  /*------------------------------------------------
  Setup serial port registers.
  ------------------------------------------------*/
//  PINSEL0 |= 0x40000000;               /* Enable TxD1 */
//  PINSEL1 |= 0x00000001;               /* Enable RxD1 */

    PINSEL0 |= 0x00000050;               /* Enable TxD0 and RxD0              */


  U0LCR = 0x03;            /* 8 bits, no Parity, 1 Stop bit */
  U0IER = 0;               /* Disable UART1 Interrupts */

  VICVectAddr6 = (unsigned long)sio_irq;
  VICVectCntl6 = 15;       /* use it for UART1 Interrupt */
  VICIntEnable = 1 << 6;   /* Enable UART1 Interrupt */

  com_baudrate (115200);   /* setup for 115200 baud (14,4MHz) */
  /*** Note that with the PLL and VPB setup the pclk is 14,4MHz. ***/
  /*** This does NOT generate nice baudrates!!! ***/
  /*** Most connection problems are baudrate mismatches. ***/
  /*** Symptoms we've seen include reception on PC but no ***/
  /*** reception on the target system. ***/

  dummy = U0IIR;   /* Read IrqID - Required to Get Interrupts Started */
  U0IER = 7;       /* Enable UART1 RX and THRE Interrupts */
}

int com_putchar (int c) {
  struct buf_st *p = &tbuf;

  /*------------------------------------------------
  If the buffer is full, return an error value.
  ------------------------------------------------*/
  if (SIO_TBUFLEN >= TBUF_SIZE)
    return (-1);

  /*------------------------------------------------
  Add the data to the transmit buffer.  If the
  transmit interrupt is disabled, then enable it.
  ------------------------------------------------*/
  if (tx_restart)   {
    tx_restart = 0;



            U0THR = c;



  }
  else {
    p->buf [p->in & (TBUF_SIZE - 1)] = c;
    p->in++;
  }

  return (0);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/
int com_getchar (void) {
  struct buf_st *p = &rbuf;

  if (SIO_RBUFLEN == 0)
    return (-1);

  return (p->buf [(p->out++) & (RBUF_SIZE - 1)]);
}

/*------------------------------------------------------------------------------
------------------------------------------------------------------------------*/

在终端显示:

[RX] PRESS a key:

[TX] Hello World!   ( i send this string )

[RX] HPRESS a key:
[RX] ePRESS a key:
[RX] lPRESS a key:
[RX] lPRESS a key:
[RX] oPRESS a key:
[RX]  PRESS a key:
[RX] WPRESS a key:
[RX] oPRESS a key:
[RX] rPRESS a key:
[RX] lPRESS a key:
[RX] dPRESS a key:
[RX] !PRESS a key:

发送字符串需要更改什么?不仅是角色!

#include <LPC23XX.H>  
#include <stdio.h>
#include <string.h>
#include "sio.h"
#include "rtl.h"



int main (void) {

  FIO2DIR  = 0x00000004;                 /* LEDs, port 2, bit 0~7 output only */
  FIO2MASK = 0x00000000;


  com_initialize ();                      /* init interrupt driven serial I/O */

  printf ("Interrupt-driven Serial I/O Example\r\n\r\n");

  while (1)
{
    unsigned char c;

    FIO2SET = 0x04;

    printf ("Press a key. ");
    c = getchar ();

    printf ("\r\n");
    com_putchar(c);
  }
}

0 个答案:

没有答案