| 网站首页 | 新闻 | SOPC | FPGA | DSP | ARM | 嵌入式操作系统 | 下载 | 所有产品 | 留言 | 论坛 | 购买指南 | 网络协议 | 驱动设计 | 
您现在的位置: 21嵌入式控制研究室 >> ARM >> 其它 >> 文章正文 用户登录 新用户注册
UART操作的程序            【字体:
UART操作的程序
作者:佚名    文章来源:21control    点击数:    更新时间:2005-12-12
UART操作的程序
作者:转载

#include "config.h"

/*define UARTs data structure*/
typedef struct UartMode
{
 uint8 datab; // byte length
 uint8 stopb; // stop bit,1/2
 uint8 parity; // parity bit setting
}UARTMODE;

uint8 rcv_buf[8];
uint8 rcv_byte[8];
volatile uint8 add_data_num;
volatile uint8 rcv_new; //flag for rcving new data
volatile uint8 add_data;

void __irq IRQ_UART0(void)
{
 uint8 i;

 if ((U0IIR & 0x0F) == 0x04)
 {
  rcv_new = 1;
  for (i = 0; i<8; i++)
  {
   rcv_buf[i] = U0RBR;
  }
 }
 
 // read all the data in FIFO once. Then there is no too much interruptions
 if ((U0IIR & 0xF) == 0x0C)
 {
  add_data_num = 0;
  while (U0LSR & 0x00000001)// Read UARTs LSR register first,
  {        // and determine whether FIFO is empty
   add_data = 1;    // or not.
   rcv_byte[add_data_num] = U0RBR;
   add_data_num++;
  }
 }
 
 VICVectAddr = 0x00;
}

void UART0_SendByte(uint8 dat)
{
 U0THR = dat;
}

void UART0_SendBuf(void)
{
 uint8 i;
 
 for (i=0; i<8; i++)
  UART0_SendByte(rcv_buf[i]);
 while ((U0LSR & 0x20) == 0) ;
}

void UART0_SendAddByte(uint8 j)
{
 uint8 i;
 for (i = 0; i<j; i++)
  UART0_SendByte(rcv_byte[i]);
 
 while ((U0LSR & 0x20) == 0) ; 
}


int UART0_Init(uint32 baud, UARTMODE set)
{
 uint32 bak;
 
 if ((baud == 0)||(baud > 115200))
  return (0);
 if ((set.datab < 5)||(set.datab > 8))
  return (0);
 if ((set.stopb == 0)||(set.stopb > 2))
  return (0);
 if (set.parity > 4)
  return (0);
 
 /*set UARTs baud rate*/
 U0LCR = 0x80; //DLAB = 1
 bak = (Fpclk >> 4)/baud;
 U0DLM = bak >> 8;
 U0DLL = bak &0xFF;
 
 /*set UARTs mode*/
 bak = set.datab - 5;
 if (set.stopb == 2)
  bak |= 0x04;
 
 if (set.parity != 0)
 {
  set.parity = set.parity - 1;
  bak |= 0x08;
 }
 
 bak |= set.parity << 4;
 
 U0LCR = bak;
 
 return (1);
  
}


int main (void)
{// add user source code
 UARTMODE set;
 
 set.datab = 8;
 set.stopb = 1;
 set.parity = 0;
 
 rcv_new = 0;
 
 PINSEL0 = (PINSEL0 & 0xFFFFFFF0) | 0x5;
 
 UART0_Init(115200, set);
 U0FCR = 0x81;
 U0IER = 0x01;
 
 IRQEnable();
 /*Enable UART0 Interruption*/
 VICIntSelect = 0x00000000;
 VICVectCntl0 = 0x20|0x06;
 VICVectAddr0 = (uint32)IRQ_UART0;
 VICIntEnable = 1<<0x06;    //Enable UART0s Interruption

 while (1)
 {
  if (rcv_new == 1)
  {
   rcv_new = 0;
   UART0_SendBuf();
  }
  if (add_data == 1)
  {
   add_data = 0;
   UART0_SendAddByte(add_data_num);
  }
 }
 
    return 0;
}

文章录入:flyongrass    责任编辑:flyongrass 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
  • ARM入门

  • 一个典型的嵌入式系统设计和…

  • 基于RTCORBA技术的嵌入式代理…

  • 基于ARM的海底大地电磁信号采…

  • 32位ARM嵌入式处理器的调试技…

  • ARM CPU S3C44B0X与C54X DSP…

  • 32位ARM嵌入式处理器的调试技…

  • ARM处理器及物理IP实现突破性…

  • 基于ARM的实时测控系统开发平…

  • 基于ARM核的AT75C220及其在指…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)