?? console.c
字號(hào):
/****************************************************************************/
/*
文件名 : console.c
說(shuō)明 : UART控制臺(tái)程序,包含了控制UART的所有功能函數(shù)
作者 : 郭天祥
*/
/*****************************************************************************/
#include <stdarg.h>
#include "..\inc\44b0x.h"
#include "..\inc\uTypes.h"
#include "..\inc\SysUtils.h"
#include "..\inc\console.h"
extern U32 g_dwMCLK;
/***********************************
串口初始化
入口參數(shù):ch 為串口號(hào)
baud 為波特率
************************************/
void UartInit(int ch, int baud)
{
U8 a;
if(!ch)
{
rUFCON0 = 0x0; //禁止使用FIFO
rUMCON0 = 0x0; //禁止使用FIFO
rULCON0 = 0x3; //正常無(wú)奇偶校驗(yàn),一個(gè)停止位,8個(gè)數(shù)據(jù)位
rUCON0 = 0x45; //TX RX 都用PULSE非LEVEL中斷
//rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
//rUBRDIV0 = (int)(MCLK/16./baud+0.5)-1;
rUBRDIV0 = g_dwMCLK/(16*baud);
a = rURXH0;
}
else
{
rUFCON1 = 0x0; //禁止使用FIFO
rUMCON1 = 0x0; //禁止使用FIFO
rULCON1 = 0x3; //正常無(wú)奇偶校驗(yàn),一個(gè)停止位,8個(gè)數(shù)據(jù)位
rUCON1 = 0x45;
//rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
//baud *= 16;
//rUBRDIV1 = MCLK/baud+((MCLK%baud)>=(baud/2))?1:0;
rUBRDIV1 = g_dwMCLK/(baud*16);
a = rURXH1;
}
}
/*串口發(fā)送*/
void UartSend(int ch, char data)
{
if(!ch)
{
if(data=='\n')
{
while(!(rUTRSTAT0&0x2));
//改動(dòng)延時(shí)時(shí)間1--10
sysUtilsDelay (10); //由于超級(jí)終端反應(yīng)較慢,有一個(gè)微小延遲
WrUTXH0('\r');
}
while(!(rUTRSTAT0&0x2)); //等待知道THR變空
//改動(dòng)延時(shí)時(shí)間1--10
sysUtilsDelay (10);
rUTXH0 = data;
}
else
{
if(data=='\n')
{
while(!(rUTRSTAT1&0x2));
//改動(dòng)延時(shí)時(shí)間1--10
sysUtilsDelay (10); //由于超級(jí)終端反應(yīng)較慢,有一個(gè)微小延遲
rUTXH1 = '\r';
}
while(!(rUTRSTAT1&0x2)); //等待知道THR變空
//改動(dòng)延時(shí)時(shí)間1--10
sysUtilsDelay (10);
rUTXH1 = data;
}
}
/*串口接收*/
int UartReceive(int ch)
{
if(!ch)
{
while(!(rUTRSTAT0&0x1)); //等待直到接受到一個(gè)數(shù)據(jù)
return rURXH0;
}
else
{
while(!(rUTRSTAT1&0x1)); //等待直到接受到一個(gè)數(shù)據(jù)
return rURXH1;
}
}
/*串口接收狀態(tài)*/
int UartRxStat(int ch)
{
if(!ch)
return (rUTRSTAT0&0x1);
else
return (rUTRSTAT1&0x1);
}
/*串口接收益而高字符*/
int UartGetch(int ch)
{
if(!ch)
{
if(rUTRSTAT0&0x1) //如果收到字符就返回該字符
return rURXH0;
else
return -1; //如果沒(méi)有收到字符就返回0
}
else
{
if(rUTRSTAT1&0x1) //如果收到字符就返回該字符
return rURXH1;
else
return -1; //如果沒(méi)有收到字符就返回0
}
}
/*接收數(shù)據(jù)
入口參數(shù):ch 為端口號(hào)
*/
int UartGetkey(int ch)
{
return ch?rURXH1:rURXH0;
}
void UartSendString(int ch, char *pt)
{
while(*pt)
UartSend(ch, *pt++);
}
/**串口終端的初始化**/
void console_init(int baud)
{
UartInit(CONSOLE_UART, baud);
}
/*串口發(fā)送*/
void putch(char data)
{
UartSend(CONSOLE_UART, data);
}
/*發(fā)送字符串*/
void puts(char *str)
{
while(*str)
UartSend(CONSOLE_UART, *str++);
}
/*獲取一個(gè)字符*/
int getch()
{
return UartReceive(CONSOLE_UART);
}
/*獲取狀態(tài)*/
int getkey()
{
return UartGetkey(CONSOLE_UART);
}
/*獲取串口的狀態(tài)*/
int kbhit()
{
return UartRxStat(CONSOLE_UART);
}
#ifdef __SDT_COMPILER
typedef char *__va_list[1];
#else
typedef int *__va_list[1];
#endif
int vsprintf(char * /*s*/, const char * /*format*/, __va_list /*arg*/);
/*標(biāo)準(zhǔn)輸出函數(shù)*/
void printf(char *fmt, ...)
{
va_list ap;
char string[256];
va_start(ap, fmt);
vsprintf(string, fmt, ap);
UartSendString(CONSOLE_UART, string);
va_end(ap);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -