打印

串口的队列

[复制链接]
4353|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
win2000_li|  楼主 | 2009-4-8 16:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
win2000_li|  楼主 | 2009-4-9 08:57 | 只看该作者

OK

原来我记着这里有一篇关于讲述队列。

使用特权

评论回复
板凳
computer00| | 2009-4-9 10:03 | 只看该作者

俺一般用循环缓冲。

使用特权

评论回复
地板
win2000_li|  楼主 | 2009-4-9 11:07 | 只看该作者

谢谢

我去您的博客去看一看。

原来有一篇很精典的一篇关于串口对队讲述

还有程序例子。

使用特权

评论回复
5
win2000_li|  楼主 | 2009-4-9 11:10 | 只看该作者

现在没有找到。

大至意思是“一个兄弟把它的串口程序拿出来给大家评审一下”

还有很多兄弟发了言的,指出了其中的不足与优点,还有CPU的

利用率等,对于我这初学者还是有很大的指导意义的。

可惜我现在找不到了。

请大家指点一下,或者帮我找一找。

使用特权

评论回复
6
computer00| | 2009-4-9 11:12 | 只看该作者
7
win2000_li|  楼主 | 2009-4-9 17:21 | 只看该作者

谢谢00

虽然不是这个贴,但是还是谢谢了兄弟。

我再找一找。

对了,我在你的博客上没有找到您的程序啊!!!

使用特权

评论回复
8
computer00| | 2009-4-9 17:22 | 只看该作者

可以看第六章那个USB转串口的例子

使用特权

评论回复
9
zae888| | 2011-7-11 07:46 | 只看该作者
不错

使用特权

评论回复
10
phz0008| | 2011-7-11 09:53 | 只看该作者
最简单的,你去看看keil c51 help上就有。
或者看看这个循环缓冲,用队列实现就可以了,俺觉得理解思想,用哪个都成

/*----------------------------------------------------------------------------
  Notes:
  The length of the receive and transmit buffers must be a power of 2.
  Each buffer has a next_in and a next_out index.
  If next_in = next_out, the buffer is empty.
  (next_in - next_out) % buffer_size = the number of characters in the buffer.
*----------------------------------------------------------------------------*/
#define TBUF_SIZE   256             /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/
#define RBUF_SIZE   256      /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/

/*----------------------------------------------------------------------------
*----------------------------------------------------------------------------*/
#if TBUF_SIZE < 2
#error TBUF_SIZE is too small.  It must be larger than 1.
#elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0)
#error TBUF_SIZE must be a power of 2.
#endif

#if RBUF_SIZE < 2
#error RBUF_SIZE is too small.  It must be larger than 1.
#elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0)
#error RBUF_SIZE must be a power of 2.
#endif

/*----------------------------------------------------------------------------
*----------------------------------------------------------------------------*/
struct buf_st {
  unsigned int in;                                // Next In Index
  unsigned int out;                               // Next Out Index
  char buf [RBUF_SIZE];                           // Buffer
};

static struct buf_st rbuf = { 0, 0, };
#define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out))

static struct buf_st tbuf = { 0, 0, };
#define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out))

static unsigned int tx_restart = 1;               // NZ if TX restart is required

/*----------------------------------------------------------------------------
  USART1_IRQHandler
  Handles USART1 global interrupt request.
*----------------------------------------------------------------------------*/
void USART1_IRQHandler (void) {
  volatile unsigned int IIR;
  struct buf_st *p;

    IIR = USART1->SR;
    if (IIR & USART_FLAG_RXNE) {                  // read interrupt
      USART1->SR &= ~USART_FLAG_RXNE;                  // clear interrupt

      p = &rbuf;

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

    if (IIR & USART_FLAG_TXE) {
      USART1->SR &= ~USART_FLAG_TXE;                  // clear interrupt

      p = &tbuf;

      if (p->in != p->out) {
        USART1->DR = (p->buf [p->out & (TBUF_SIZE-1)] & 0x1FF);
        p->out++;
        tx_restart = 0;
      }
      else {
        tx_restart = 1;
                USART1->CR1 &= ~USART_FLAG_TXE;                      // disable TX interrupt if nothing to send

      }
    }
}

/*------------------------------------------------------------------------------
  buffer_Init
  initialize the buffers
*------------------------------------------------------------------------------*/
void buffer_Init (void) {

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

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

/*------------------------------------------------------------------------------
  SenChar
  transmit a character
*------------------------------------------------------------------------------*/
int SendChar (int c) {
  struct buf_st *p = &tbuf;

                                                  // If the buffer is full, return an error value
  if (SIO_TBUFLEN >= TBUF_SIZE)
    return (-1);
                                                  
  p->buf [p->in & (TBUF_SIZE - 1)] = c;           // Add data to the transmit buffer.
  p->in++;

  if (tx_restart) {                               // If transmit interrupt is disabled, enable it
    tx_restart = 0;
        USART1->CR1 |= USART_FLAG_TXE;                          // enable TX interrupt
  }

  return (0);
}

/*------------------------------------------------------------------------------
  GetKey
  receive a character
*------------------------------------------------------------------------------*/
int GetKey (void) {
  struct buf_st *p = &rbuf;

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

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

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

142

主题

718

帖子

1

粉丝