📄 console.c
字号:
/****************************************************************************/
/*
文件名 : console.c
说明 : UART控制台程序,包含了控制UART的所有功能函数
作者 : 天乐
biggeorge@126.com
修改历史:
02/02/2005 创建
*/
/*****************************************************************************/
#include <stdarg.h>
#include "..\inc\44b0x.h"
#include "..\inc\uTypes.h"
#include "..\inc\SysUtils.h"
#include "..\inc\console.h"
extern U32 g_dwMCLK;
/*
*/
void UartInit(int ch, int baud)//串口初始化函数 通道 波特率115200
{
U8 a;
if(!ch)//如果 CH 取反等于 1 则向下执行否则执行ELSE
{
rUFCON0 = 0x0; //禁止使用FIFO
rUMCON0 = 0x0; //禁止使用MODEM 控制寄存器
rULCON0 = 0x3; //线控寄存器正常无奇偶校验,一个停止位,8个数据位
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;//UART接收保持寄存器
}
else
{
rUFCON1 = 0x0; //禁止使用FIFO
rUMCON1 = 0x0; //禁止使用FIFO
rULCON1 = 0x3; //正常无奇偶校验,一个停止位,8个数据位
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;
}
}
void UartSend(int ch, char data)//发送函数
{
if(!ch)//UART0通道
{
if(data=='\n')//如果没有遇到回车换行则向下执行
{
while(!(rUTRSTAT0&0x2));//判断O通道接收状态寄存器是否为空, 不为空就一直再次等待数据传送,为空的话向下执行
//改动延时时间1--10
sysUtilsDelay (10); //由于超级终端反应较慢,有一个微小延迟
WrUTXH0('\r');//发送换行符
}
while(!(rUTRSTAT0&0x2)); //等待知道THR变空,如果不为空则继续向下执行
//改动延时时间1--10
sysUtilsDelay (10);
rUTXH0 = data; //把数据传送到RS232口输出
}
else
{
if(data=='\n')
{
while(!(rUTRSTAT1&0x2));
//改动延时时间1--10
sysUtilsDelay (10); //由于超级终端反应较慢,有一个微小延迟
rUTXH1 = '\r';
}
while(!(rUTRSTAT1&0x2)); //等待知道THR变空
//改动延时时间1--10
sysUtilsDelay (10);
rUTXH1 = data;
}
}
int UartReceive(int ch)//接收函数
{
if(!ch)
{
while(!(rUTRSTAT0&0x1)); //等待直到接受到一个数据
return rURXH0;
}
else
{
while(!(rUTRSTAT1&0x1)); //等待直到接受到一个数据
return rURXH1;
}
}
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; //如果没有收到字符就返回0
// }
// else
// {
// if(rUTRSTAT1&0x1) //如果收到字符就返回该字符
// return rURXH1;
// else
// return -1; //如果没有收到字符就返回0
// }
//}
int UartGetkey(int ch)
{
return ch?rURXH1:rURXH0;
}
void UartSendString(int ch, char *pt)//发送字符串函数,发送的字符数逐渐减少一个
{
while(*pt)//如果有字符串过来,就调用UARTSEND函数,没有则返回
UartSend(ch, *pt++);//调用一次指针自动加1下一个字符
}
/************************************************/
void console_init(int baud)
{
UartInit(CONSOLE_UART, baud);
}
void putch(char data)
{
UartSend(CONSOLE_UART, data);
}
void puts(char *str)
{
while(*str)
UartSend(CONSOLE_UART, *str++);
}
int getch()
{
return UartReceive(CONSOLE_UART);
}
int getkey()
{
return UartGetkey(CONSOLE_UART);
}
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*/);//vfprintf()会根据参数format字符串来转换并格式化数据,然后将结果输出到参数stream指定的文件中,直到出现字符串结束(’\0’)为止
void printf(char *fmt, ...)
{
va_list ap;//可变参数的调整
char string[256];
va_start(ap, fmt);//* 将可变长参数转换为va_list */
vsprintf(string, fmt, ap);
UartSendString(CONSOLE_UART, string);//调用发送字符串函数
va_end(ap);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -