AVR C:串行通信清屏
AVR C : Serial Communication Clear Screen
我正在使用带有 Atmega 微控制器的 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;
}
但它不起作用,我对微控制器还很陌生,它只是打印
AAAA..
如何清除屏幕,然后在串口通信屏幕上打印一个新字符,我使用的是putty。
更新
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 "terminal emulation" 特性(它有几个选项),一般来说 "escape sequences"。还要考虑 ^[[2J
(^[ 表示转义)清除屏幕,但不应将光标发送到主页。为此,您需要 ^[[H
.
一些 link 的基本信息:http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
还有:http://ascii-table.com/ansi-escape-sequences.php
也就是说,打印退格键返回行中的一个位置(十进制字符 8)或回车符 return 返回到当前行的开头(字符13 十进制),最后打印一些东西,然后用 ESC[K
.
清除该行的其余部分
我正在使用带有 Atmega 微控制器的 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;
}
但它不起作用,我对微控制器还很陌生,它只是打印
AAAA..
如何清除屏幕,然后在串口通信屏幕上打印一个新字符,我使用的是putty。
更新
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 "terminal emulation" 特性(它有几个选项),一般来说 "escape sequences"。还要考虑 ^[[2J
(^[ 表示转义)清除屏幕,但不应将光标发送到主页。为此,您需要 ^[[H
.
一些 link 的基本信息:http://www.isthe.com/chongo/tech/comp/ansi_escapes.html
还有:http://ascii-table.com/ansi-escape-sequences.php
也就是说,打印退格键返回行中的一个位置(十进制字符 8)或回车符 return 返回到当前行的开头(字符13 十进制),最后打印一些东西,然后用 ESC[K
.