📄 lpc_lib_uart.c
字号:
/***********************************************************************
Embest Info&Tech Co., Ltd. All rights reserved.
www.embedinfo.com
file :
author : z.x.q. embest
establish: 2006.4
modify :
notes :
***********************************************************************/
/*-------------------------------------------------------------------*/
/* include files */
/*---------------------------------------------------------------- --*/
#include "..\..\com\lpc_lib_uart\lpc_lib_uart.h"
/*-------------------------------------------------------------------*/
/* local function declare */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* extern function/variable declare */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* global variable define */
/*-------------------------------------------------------------------*/
/*-------------------------------------------------------------------*/
/* function code */
/*-------------------------------------------------------------------*/
/*************initialize uart0,set baurate***************************/
void uart0_init(INT32U BaudRate)
{
INT16U udiv;
INT32U i;
i=PINSEL0; //select the pin to uart0
i&=(~P0_UART0_PINSEL0_MASK);
i|=P0_UART0_PINSEL0;
PINSEL0=i;
UART0_IER=0x0; //disable all interrupts
UART0_FCR=0x0; //disable FIFO
UART0_LCR=0x80; // enable DLAB
udiv=(PCLKF>>4)/BaudRate; //compute the baud division
UART0_DLM = udiv>>8;
UART0_DLL = udiv&0xff;
UART0_LCR = 0x03; //set the data format
i=UART0_RBR; //clear the Recive buffer
spi_extend_set(0,module_c_U0_EN); //open the uart0 module latch
}
/*****************send byte to hyperterminal once********************/
void uart0_sendch(INT8U data)
{
if(data=='\n') //if data is '\n',change line and to begin of the line
uart0_sendch('\r');
UART0_THR=data;
while ((UART0_LSR&0x20)==0); //wait the THR for empty
}
/****************send string to hyperterminial *********************/
void uart0_sendstr(INT8U *str)
{
while(*str) //till equal to zero
uart0_sendch(*str++);
}
/*******************get byte from hyperterminal*********************/
INT8U uart0_getch(void)
{
while((UART0_LSR&0x01)==0); // wait recive data ready
return UART0_RBR;
}
INT8U uart0_getkey(void)
{
if((UART0_LSR&0x01)!=0)
return UART0_RBR;
else
return 0;
}
/********************get string from hyperterminal******************/
/*desc: get char from uart0 byte by byte, every time get a byte and
then send out it till 'enter' key input,all the bytes converge
to a string,store to the char point 'str'!
********************************************************************/
void uart0_getstr(INT8U *str)
{
INT8U *string=str;
INT8U c;
c=uart0_getch();
while(c!='\r')
{
if((c=='\b')&&((INT32U)string<(INT32U)str))
{
uart0_sendch('\b');
uart0_sendch(' ');
uart0_sendch('\b');
str--;
}
else
{
uart0_sendch(c);
*str++=c;
}
c=uart0_getch();
}
*str='\0';
uart0_sendch('\n');
}
/************************print main function**************************/
void uart0_printf(INT8U *fmt,...)
{
va_list ap;
INT8U string[128];
va_start(ap,fmt); //correspond to va_end func
vsprintf(string,fmt,ap); //variable function
uart0_sendstr(string);
va_end(ap);
}
void uart0_close(void)
{
spi_extend_set(1,module_c_U0_EN);
}
/*
---------------------------------------------------------------------------
uart1 library
---------------------------------------------------------------------------
*/
void uart1_init(INT32U BaudRate)
{
INT16U udiv;
INT32U i;
i=PINSEL0; //select the pin to uart0
i&=(~P0_UART1_PINSEL0_MASK);
i|=P0_UART1_PINSEL0;
PINSEL0=i;
UART1_IER=0x0; //disable all interrupts
UART1_FCR=0x0; //disable FIFO
UART1_LCR=0x80; // enable DLAB
udiv=(PCLKF>>4)/BaudRate; //compute the baud division
UART1_DLM = udiv>>8;
UART1_DLL = udiv&0xff;
UART1_LCR = 0x03; //set the data format
i=UART1_RBR; //clear the Recive buffer
spi_extend_set(0,module_c_U1_EN); //open the uart0 module latch
}
void uart1_sendch(INT8U data)
{
if(data=='\n') //if data is '\n',change line and to begin of the line
uart1_sendch('\r');
UART1_THR=data;
while ((UART1_LSR&0x20)==0); //wait the THR for empty
}
INT8U uart1_getch(void)
{
while((UART1_LSR&0x01)==0); // wait recive data ready
return UART1_RBR;
}
INT8U uart1_getkey(void)
{
if(UART1_LSR&0x01)
return UART1_RBR;
else
return 0;
}
void uart1_close(void)
{
spi_extend_set(1,module_c_U1_EN);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -