📄 uart.h
字号:
#ifndef __UART_H__
#define __UART_H__
#include"44b.h"
#include"user.h"
static int UartIndex=0;
void Uart_Init(BYTE ComIndex,int baud){
int i;
rUFCON0=0x0; //FIFO disable
rUFCON1=0x0;
rUMCON0=0x0;
rUMCON1=0x0;
//UART0 /********************************************************************************/
//ULCON | 7 | | 6 | | 543 | | 2 | |1 0|
// reserve Infra-red ParityMode StopBitsNum WordLength
/********************************************************************************/
rULCON0=0x3; //Normal,No parity,1 stop,8 bit
//rULCON0=0x7; //Normal,No parity,2 stop,8 bit
/********************************************************************************/
//UCON | 9 | | 8 | | 7 | | 6 |
// TxInterruptType RxInterruptType RxTimeOutEnable RxErrorStatusInterruptEnable
//UCON | 5 | | 4 | |3 2| |1 0|
// Loop-BackMode SendBreakSignal TransmitMode ReceiveMode
/********************************************************************************/
rUCON0=0x245; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
rUBRDIV0=( (int)(MCLK/16./baud + 0.5) -1 );
//UART1
//rULCON1=0x7; //Normal,No parity,2 stop,8 bit
rULCON1=0x3;
rUCON1=0x245;
rUBRDIV1=( (int)(MCLK/16./baud + 0.5) -1 );
UartIndex=ComIndex;
for(i=0;i<100;i++);
}
void Uart_Select(int ch){
UartIndex=ch;
}
/********************************************************************************/
//UTRSTAT | 2 | | 1 | | 0 |
// TransmitShifterEmpty TransmitBufferEmpty ReceiveDataReady
//
//UTXH (UartTransmitHolding-Buffer-)WriteOnly
//URXH (UartReceiveHolding-Buffer-) ReadOnly
/********************************************************************************/
void Uart_TxEmpty(int ch){
if(ch==0){
while(!(rUTRSTAT0 & 0x4)); //wait until tx shifter is empty.
}else{
while(!(rUTRSTAT1 & 0x4)); //wait until tx shifter is empty.
}
}
/*void Uart_SendBYTE(int data){
if(UartIndex==0){
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
//Delay(10);
WrUTXH0(data);
}else{
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
//Delay(10);
rUTXH1=data;
}
}*/
BYTE Uart_GetBYTE(void){
if(UartIndex==0){
while(!(rUTRSTAT0 & 0x1)); //Receive data read
return RdURXH0();
}else{
while(!(rUTRSTAT1 & 0x1)); //Receive data ready
return rURXH1;
}
}
////////////////////////////////////////////////////////////////////////////
void Uart_SendByte(int data)
{
if(UartIndex==0)
{
if(data=='\n')
{
while(!(rUTRSTAT0 & 0x2));
//Delay(10); //because the slow response of hyper_terminal
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
//Delay(10);
WrUTXH0(data);
}
else
{
if(data=='\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);
rUTXH1=data;
}
}
/****************************************************************************************/
/****************************************************************************************/
void Uart_SendString(char *pt)//及其注意,不能用 BYTE *pt ,与 Keil51 区别
{
while(*pt)
Uart_SendByte(*pt++);
}
void Uart_SendNum(int num){
char bf[6],bf2[6];
int N=10000,i,j=0;
int ZeroFlag=TRUE;
if(num<0){
bf[0]='-';
num=-num;
}else{
bf[0]=' ';
}
for(i=1;i<6;i++){
bf[i]='0'+num/N;
num-=(num/N)*N;
N/=10;
if(bf[i]!='0'){
ZeroFlag=FALSE;
}
if(ZeroFlag==TRUE&&i!=5){
bf[i]=' ';
}
}
for(i=0;i<6;i++){
bf2[i]=' ';
if(bf[i]!=' '){
bf2[j]=bf[i];
j++;
}
}
Uart_SendString(" ");
for(i=0;i<6;i++)
Uart_SendByte(bf2[i]);
}
void Uart_Printf(char* Pt,int num){
Uart_SendString(Pt);
Uart_SendNum(num);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -