我正在使用带有 Atmega 微 Controller 的 AVR C。我正在使用串行通信,我想将一个字符“A”传输到屏幕,然后将其删除并显示“B”。我在清除屏幕时遇到了麻烦。
我读到 ESC ASCII 可以工作,所以我尝试了。
#define F_CPU 16000000UL
void initUART(unsigned int baud);
void transmitByte(unsigned char data);
unsigned char receiveByte(void);
int getNumOfDigits(int num);
void printDec(int num);
int main(void) {
DDRB = 0b00000011;
PORTB = 0b00011000;
initUART(9600);
while(1) {
//s1 pressed
if ((PINB & 0b00001000) == 0) {
transmitByte('A');
transmitByte((char) 27);
transmitByte('B');
_delay_ms(1000);
}
}
return 0;
}
void initUART(unsigned int baud) {
/*
Initialize settings for uart functions.
Must be done once at the beginning of the program.
*/
//Normal mode UBRR formula
unsigned int ubrr = F_CPU/16/baud-1;
//shift MSB and store in UBRR0H
UBRR0H = (unsigned char) (ubrr >> 8);
//store LSB in UBRR0L
UBRR0L = (unsigned char) ubrr;
//Enable transmitter/receiver
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//8-Bit Characters, 0 Stop bits, No parity
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01);
}
void transmitByte(unsigned char data) {
/*
Write byte to UART
*/
//Wait for empty transmit buffer
while(!(UCSR0A & (1 << UDRE0)));
//Start transmission by writing to UDR0
UDR0 = data;
}
unsigned char receiveByte(void){
/*
Read byte from UART
*/
//Wait for incoming byte
while(!(UCSR0A & (1 << RXC0)));
//Return the byte
return UDR0;
}
但它不起作用,我对微 Controller 还很陌生,它只是打印
AAAA..
如何清除屏幕然后在串行通信屏幕上打印一个新字符,我正在使用腻子。
更新
transmitByte('A');
transmitByte(27); // this is the escape
transmitByte('[');
transmitByte('2');
transmitByte('J'); // uppercase J
transmitByte('^');
transmitByte('[');
transmitByte('H');
transmitByte('B');
输出
如何让光标回到原来的位置?
最终工作
transmitByte('A');
transmitByte(27); // this is the escape
transmitByte('[');
transmitByte('2');
transmitByte('J'); // uppercase J
transmitByte(27);
transmitByte('[');
transmitByte('H');
transmitByte('B');
以上代码有效,删除并将光标移回。
最佳答案
我不太确定你想要传输一个“A”然后清除屏幕,但是......如果你想清除屏幕你必须发送你的终端模拟器理解的转义序列 .通常,ESC[2J
会执行此操作,并且许多终端仿真器都可以理解它。所以你可以这样写:
transmitByte(27); // this is the escape
transmitByte('[');
transmitByte('2');
transmitByte('J'); // uppercase J
查看 putty 的“终端仿真”特性(它有几个选项),一般来说,“转义序列”。还要考虑 ^[[2J
(^[ 表示 Escape)清除屏幕,但不应将光标发送到主页。为此,您需要 ^[[H
。
基本信息的一些链接:http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
还有:http://ascii-table.com/ansi-escape-sequences.php
也就是说,打印退格键返回行中的一个位置(十进制字符 8)或回车返回到当前行的开头(十进制字符 13)也可能很有趣,并且最后打印一些东西,然后使用 ESC[K
清除该行的其余部分。
关于c - AVR C : Serial Communication Clear Screen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42639909/