📄 uart.c
字号:
#include "includes.h"
uchar UartRxBuf[RX_BUFFER_SIZE];
uchar UartRxCount;
uchar UartRxFlag;
uchar UartTxBuf[TX_BUFFER_SIZE]; /* 256 bytes UART transmit buffer */
uchar UartTxRdPtr; /* UART transmit buffer read pointer */
uchar UartTxWrPtr; /* UART transmit buffer write pointer */
uchar UartTxCount; /* Number of characters to send */
// USART Receiver interrupt service routine
#pragma vector=USART_RXC_vect
__interrupt void UART_RXC_ISR(void)
{
uchar status,data;
status = UCSRA;
data = UDR;
if (status & (1<<RXC)) {
if (!UartRxFlag){
UartRxBuf[UartRxCount] = data;
switch (UartRxCount)
{
case 0:
if (data == UART_BEGIN_STX)
UartRxCount = 1;
break;
case 1:
case 2:
case 3:
case 4:
UartRxCount++;
break;
case 5:
UartRxCount = 0;
if (data == UART_END_STX) UartRxFlag = 1;
break;
}
}
} else UartRxCount = 0;
}
#pragma vector=USART_TXC_vect
__interrupt void UART_TXC_ISR (void)
{
UartTxCount--;
if (UartTxCount) {
UDR = UartTxBuf[UartTxRdPtr];
UartTxRdPtr++;
} else {
UCSRA &= ~(1<<TXC);
UartTxRdPtr = 0;
}
}
void PutChar (uchar c)
{
_CLI();
if (UartTxCount) {
UartTxBuf[UartTxWrPtr] = c;
UartTxWrPtr++;
UartTxCount++;
} else {
UDR = c;
UartTxCount = 1;
UartTxWrPtr = 0;
UCSRA |= (1<<TXC);
}
_SEI();
}
/*
void PutChar (uchar c)
{
while (!(UCSRA&(1<<UDRE)));
UDR = c;
}
*/
void PutString (const uchar *s)
{
while (*s != '\0') {
PutChar(*s++);
}
}
void SPrintDec(uchar *s, uint x, uchar n)
{
uchar i;
s[n] = '\0';
for (i = 0; i < n; i++) {
s[n - i - 1] = '0' + (x % 10);
x /= 10;
}
for (i = 0; i < (n - 1); i++) {
if (s[i] == '0') {
s[i] = ' ';
} else {
break;
}
}
}
void PutDec (uchar x2)
{
uchar x0;
uchar x1;
x0 = (x2 % 10);
x2 /= 10;
x1 = (x2 % 10);
x2 /= 10;
if (x2) {
PutChar(x2 + '0');
}
if (x1 || x2) {
PutChar(x1 + '0');
}
PutChar(x0 + '0');
}
void VT102Attribute (uchar fgcolor, uchar bgcolor)
{
PutChar(0x1b);
PutChar('[');
PutDec(30 + fgcolor);
PutChar(';');
PutDec(40 + bgcolor);
PutChar('m');
}
void VT102DispClrScr (void)
{
VT102Attribute(COLOR_WHITE, COLOR_BLACK);
PutString("\x1B[2J");
}
void VT102DispChar (uchar x, uchar y, uchar c, uchar fgcolor, uchar bgcolor)
{
VT102Attribute(fgcolor, bgcolor);
PutChar(0x1B);
PutChar('[');
PutDec(y);
PutChar(';');
PutDec(x);
PutChar('H');
PutChar(c);
}
void VT102DispStr (uchar x, uchar y, uchar *s, uchar fgcolor, uchar bgcolor)
{
VT102Attribute(fgcolor, bgcolor);
PutChar(0x1B);
PutChar('[');
PutDec(y);
PutChar(';');
PutDec(x);
PutChar('H');
PutString(s);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -