📄 uart.c
字号:
#include <stdarg.h> //提供va_start()函数定义等
#include <stdio.h> //提供文件操作等
#include "..\system_cdefBF561.h" //提供CPU寄存器映射定义
#include "uart.h"
//asm(".section L1_code");
unsigned char Uart_GetByte(void)
{
unsigned char ch;
while(!(*pUART_LSR & DR))
;
ch = *pUART_RBR;
return ch;
}
unsigned char Uart_RecvClear(void)
{
unsigned char ch;
ch = *pUART_RBR;
return ch;
}
void Uart_SendByte(unsigned char data)
{
while(!(*pUART_LSR & THRE))
;
*pUART_THR = data;
}
void Uart_SendData(char *pt , int len)
{
int i;
for(i=0; i<len;i++)
{
Uart_SendByte(*pt++);
}
}
void Uart_SendString(char *pt)
{
while(*pt)
Uart_SendByte(*pt++);
}
void Uart_Printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}
int fprintf(FILE *stream, const char *fmt, ...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
return 0;
}
int printf(const char *fmt, ...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
return 0;
}
void Uart_Init(int bps)
{
unsigned int divisor;
// First of all, enable UART clock.
*pUART_GCTL = UCEN;
// Read period value and apply formula: divisor = period/16/8
// Write result to the two 8-bit DL registers (DLH:DLL).
*pUART_LCR = DLAB;
divisor = SYSCLK / bps / 16 ;
*pUART_DLL = divisor;
*pUART_DLH = divisor>>8;
// Clear DLAB again and set UART frame to 8 bits, no parity, 1 stop bit.
// This may differ in other scenarious.
*pUART_LCR = WLS(8);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -