Uart_Init(0,115200);
////////////////////////////////********************************************************************////////////////////////////////////////////////
void Uart_Init(int pclk,int baud)
{
int i;
if(pclk == 0)
pclk = PCLK;
rUFCON0 = 0x0; //UART channel 0 FIFO control register, FIFO disable
rUFCON1 = 0x0; //UART channel 1 FIFO control register, FIFO disable
rUFCON2 = 0x0; //UART channel 2 FIFO control register, FIFO disable
rUMCON0 = 0x0; //UART chaneel 0 MODEM control register, AFC disable
rUMCON1 = 0x0; //UART chaneel 1 MODEM control register, AFC disable
//UART0
rULCON0 = 0x3; //Line control register : Normal,No parity,1 stop,8 bits
注: bit7:保留位
bit6: 通讯模式0--Normal mode 1--Infrared Tx/Rx Mode
bit[5:3]:校验模式000--No parity
bit[2]:停止位 0--one stop bit 1--two stop bit
bit[1:0]:没帧数据位 00-5 bits 01--6 bits 10--7bits 11--8bits
// [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
注: bit[15:12]: all zero
bit[11:10]: 00/10--PCLK 01--UEXTCLK 11-FCLK/n
bit[9]:TX中断设置:0--Pulse 1--Level
bit[8]:RX中断设置:0--Pulse 1--Level
bit[7]:Rx Time Out:Disable
bit[6]:1--Generate Rx err interrupt 0--不生成中断
bit[5]bit[4]:Normal Mode
bit[3:2]bit[1:0]: 01--Interrupt or Polling Mode
rUBRDIV0=( (int)(pclk/16./baud+0.5) -1 ); //Baud rate divisior register 0
注: UBRDIVn = (int)( UART clock / ( buad rate x 16) ) –1
//UART1
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1=( (int)(pclk/16./baud+0.5) -1 );
//UART2
rULCON2 = 0x3;
rUCON2 = 0x245;
rUBRDIV2=( (int)(pclk/16./baud+0.5) -1 );
注:串口1、2与串口0设置一样
for(i=0;i<100;i++);
}
////////////////////////////////********************************************************************////////////////////////////////////////////////
consoleNum = 0;
Uart_Select( consoleNum );//whichUart = consoleNum ;
Uart_SendByte('\n');
////////////////////////////////********************************************************************////////////////////////////////////////////////
void Uart_SendByte(int data)
{
if(whichUart==0)
{
if(data=='\n')
{
while(!(rUTRSTAT0 & 0x2));
注: UTRSTATn为只读寄存器,只有低3位有效,该处为死等到发射buffer为空
bit[0]: Receive buffer data ready: 0--Empty 1--buffer has a received data
bit[1]:Transmit buffer empty:0--The buffer not empty 1--empty
bit[2]:Transmit empty:0--not empty 1--Transmitter (transmit buffer & shifter register) empty
// Delay(1); //because the slow response of hyper_terminal
WrUTXH0('\r');
注: 发送数据'\r'回车
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
// Delay(1);
WrUTXH0(data);
注: 发送数据'\n' next line
}
else if(whichUart==1)
{
if(data=='\n')
{
while(!(rUTRSTAT1 & 0x2));
//Delay(1); //because the slow response of hyper_terminal
rUTXH1 = '\r';
}
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
//Delay(1);
rUTXH1 = data;
}
else if(whichUart==2)
{
if(data=='\n')
{
while(!(rUTRSTAT2 & 0x2));
//Delay(1); //because the slow response of hyper_terminal
rUTXH2 = '\r';
}
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
//Delay(1);
rUTXH2 = data;
}
}
////////////////////////////////********************************************************************////////////////////////////////////////////////
上一篇:S3C2440 测试程序(一)PWM控制蜂鸣器测试
下一篇:S3C2440中断与异常定义与基本知识
推荐阅读最新更新时间:2024-03-16 15:23