usart.c

来自「基于中断实现的串口通信程序」· C语言 代码 · 共 107 行

C
107
字号
/*                ****ROBOCON 2009 | BUPT TEAM*******
 * ------------------------------------------------------------------------
 * FileName   : usart.c
 * Version    : 0.1
 * Biref      : USART Driver(use queue)
 * Code by    : Leaf
 * Date       : Dec.08 2008
 * Dependence : Queue
 * Note       :
 *
 *
 * ------------------------------------------------------------------------
 */


#include "config.h"

Queue u0send;
char u0sendbuf[U0_SENDBUF_SIZE];

void inline uart0_tx_isr(void)
{
  if(!q_empty(&u0send))
    UDR0 = q_popfront(&u0send);
}
void inline uart0_init(UINT16 baud)
{
   UCSR0B = 0x00;
   UCSR0A = 0x00;
   UCSR0C = 0x06;
   UBRR0L = baud & 0x00ff;
   UBRR0H = baud >> 8;
   UCSR0B = 0x48;
   q_init(&u0send, u0sendbuf, U0_SENDBUF_SIZE);
}

void inline u0_sendstart(void)
{
   if((UCSR0A&(1<<UDRE0)))
      UDR0 = q_popfront(&u0send);
}

void u0_putc(char c)
{
   while(q_full(&u0send));
   q_pushback(&u0send, c);
   u0_sendstart();
}

void u0_puts(char *buf)
{
   while(*buf)
   {
      while(q_full(&u0send));
      q_pushback(&u0send, *buf);
	  ++ buf;
   }
   u0_putc(0x0d);
}


Queue u1send;
char u1sendbuf[U1_SENDBUF_SIZE];

void inline uart1_tx_isr(void)
{
  if(!q_empty(&u1send))
    UDR1 = q_popfront(&u1send);
}
void inline uart1_init(UINT16 baud)
{
   UCSR1B = 0x00;
   UCSR1A = 0x00;
   UCSR1C = 0x06;
   UBRR1L = baud & 0x00ff;
   UBRR1H = baud >> 8;
   UCSR1B = 0x48;
   q_init(&u1send, u1sendbuf, U1_SENDBUF_SIZE);
}

void inline u1_sendstart(void)
{
   if((UCSR1A&(1<<UDRE1)))
      UDR1 = q_popfront(&u1send);
}

void u1_putc(char c)
{
   while(q_full(&u1send));
   q_pushback(&u1send, c);
   u1_sendstart();
}

void u1_puts(char *buf)
{
   while(*buf)
   {
      while(q_full(&u1send));
      q_pushback(&u1send, *buf);
	  ++ buf;
   }
   u1_putc(0x0d);
}



⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?