#include <string.h> #include <stdio.h> #include "Target\44b.h" #include "Target\44blib.h" /********************************************/ // ARMSYS实验七:异步串行口(UART)应用测试 // 描述:UART数据收发程序 /********************************************/ static int UartNum=0; void myUart_Init(int whichuart, int baud)//对Uart进行初始化,以所需要的波特率为输入参数 { if(whichuart==0) { UartNum=0; rUFCON0=0x0; //不使用FIFO rUMCON0=0x0; //不使用自动流控制 rULCON0=0x3; //不采用红外线传输模式,无奇偶校验位,1个停止位,8个数据位 rUCON0=0x245; //发送中断为电平方式,接收中断为边沿方式,禁止超时中断,允许产生错误状态中断,禁止回送模式,禁止中止信号,传输模式为中断请求模式,接收模式也为中断请求模式。 rUBRDIV0 = ( (int)(MCLK/16./baud + 0.5) -1 ); //根据波特率计算UBRDIV0的值 } else if(whichuart==1) { UartNum=1; rUFCON1=0x0; rUMCON1=0x0; rULCON1=0x3; rUCON1=0x245; rUBRDIV1 = ( (int)(MCLK/16./baud + 0.5) -1 ); } } void myUart_SendByte(char ch) { if (UartNum ==0) { if(ch=='\n') { while(!(rUTRSTAT0 & 0x2));//等待,直到发送缓冲区为空 //Delay(10); //超级中断的响应速度较慢 WrUTXH0('\r');//发送回车符 } while(!(rUTRSTAT0 & 0x2)); //等待,直到发送缓冲区为空 Delay(10); WrUTXH0(ch);//发送字符 } else { if(ch=='\n') { while(!(rUTRSTAT1 & 0x2)); Delay(10); //because the slow response of hyper_terminal rUTXH1='\r'; } while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty. Delay(10); WrUTXH1(ch); } } void myUart_Send (char *str) { myUart_Init(0,115200); while (*str) myUart_SendByte(*str++); } void Main(void) { char aa; Port_Init(); Led_Display(0xf); Beep(0x1); myUart_Send("\n\n*************************************************************************"); myUart_Send("\n* -UART test- *"); myUart_Send("\n* Version 1.10 *"); myUart_Send("\n* Email:rao_dali@263.net *"); myUart_Send("\n* UART0 Config--COM:115.2kbps,8Bit,NP,UART0 *"); myUart_Send("\n*-----------------------------------------------------------------------*"); Beep(0x0); Led_Display(0x0); while(1); }
UART的传输过程主要是通过寄存器的各个引脚的设置来达到,详细原理参见博文:http://blog.csdn.net/woshixiongge/article/details/8977414
#include "2410lib.h" /********************************************************************************************* * name: uart0_test * func: uart test function * para: none * ret: none * modify: * comment: *********************************************************************************************/ void uart0_test() { char cInput[256]; UINT8T ucInNo=0; char c; uart_init(0,115200,0); //define the baud rate uart_printf("\n UART0 Communication Test Example\n"); uart_printf(" Please input words, then press Enter:\n"); #ifdef BOARDTEST sprintf(&cInput, "Type via UART0 to test."); print_lcd(195,170,0x1c,&cInput); #endif uart_printf(" "); g_nKeyPress = 1; while(g_nKeyPress==1) // only for board test to exit { c=uart_getch(); //uart_sendbyte(c); uart_printf("%c",c); if(c!='\r') cInput[ucInNo++]=c; else { cInput[ucInNo]='\0'; break; } } delay(1000); uart_printf(" \nThe words that you input are: %s\n",cInput); uart_printf(" end.\n"); }
uart_init()的系统定义如下:
void uart_init(int nMainClk, int nBaud, int nChannel) { int i; if(nMainClk == 0) nMainClk = PCLK; switch (nChannel) { case UART0: rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits // [10] [9] [8] [7] [6] [5] [4] [3:2] [1:0] // Clock Sel, Tx Int, Rx Int, Rx Time Out, Rx err, Loop-back, Send break, Transmit Mode, Receive Mode // 0 1 0, 0 1 0 0, 01 01 // PCLK Level Pulse Disable Generate Normal Normal Interrupt or Polling rUCON0 = 0x245; // Control register //rUBRDIV0=( (int)(nMainClk/16./nBaud) -1 ); // Baud rate divisior register 0 rUBRDIV0=( (int)(nMainClk/16./nBaud+0.5) -1 ); // Baud rate divisior register 0 break; case UART1: rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable rULCON1 = 0x3; rUCON1 = 0x245; rUBRDIV1=( (int)(nMainClk/16./nBaud) -1 ); break; case UART2: rULCON2 = 0x3; rUCON2 = 0x245; rUBRDIV2=( (int)(nMainClk/16./nBaud) -1 ); rUFCON2 = 0x0; //UART channel 2 FIFO control register, FIFO disable break; default: break; } for(i=0;i<100;i++); delay(0); }
换汤不换药,UART通信协议的实现都是通过寄存器的各个引脚实现传输。更多原理参见博文:http://blog.csdn.net/woshixiongge/article/details/8977414
嵌入式学习笔记之UART编程练习
最新推荐文章于 2022-09-30 08:53:07 发布