2017-03-07 79 views
0

我正在使用AVR C與Atmega微控制器。我正在使用串行通信,我想傳送一個字符「A」到屏幕,然後擦除它並顯示「B」。我在清理屏幕時遇到麻煩。AVR C:串行通信清除屏幕

我讀了ESC ESC可以工作,所以我試了一下。

#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.. 

如何清除屏幕,然後串行通信屏幕上打印一個新的角色,我使用膩子。

更新

transmitByte('A'); 



transmitByte(27); // this is the escape 
transmitByte('['); 
transmitByte('2'); 
transmitByte('J'); // uppercase J 

transmitByte('^'); 
transmitByte('['); 
transmitByte('H'); 

transmitByte('B'); 

輸出

enter image description here

如何得到光標回其一部開拓創新的地位?

最終工作

transmitByte('A'); 

transmitByte(27); // this is the escape 
transmitByte('['); 
transmitByte('2'); 
transmitByte('J'); // uppercase J 

transmitByte(27); 
transmitByte('['); 
transmitByte('H'); 

transmitByte('B'); 

上面的代碼工作,擦除和移動光標回。

+1

[This may help](http://stackoverflow.com/a/15559322/1790864)。 –

+0

轉義字符只是引入了一個字符串,它執行一些特殊的命令,在這個cae「BAB」中什麼都不做。更好的選擇可能會退格,回車或換頁。 8,13或12.嘗試一下,看看會發生什麼 – UncleO

回答

3

我不確定你想傳送一個「A」然後清除屏幕,但是......如果你想清除屏幕,你必須發送轉義序列您的終端仿真器能夠理解。通常,ESC[2J可以做到這一點,許多終端仿真器都能理解它。所以你可以寫:

transmitByte(27); // this is the escape 
transmitByte('['); 
transmitByte('2'); 
transmitByte('J'); // uppercase J 

看膩子「終端仿真」的特點(它有幾個選項),一般來說,「轉義序列」。還要考慮^[[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清除其餘行。

+0

只是爲了澄清,我想在我的膩子屏幕上打印「A」,然後抹去它以打印「B」。 –

+0

擦除字符「A」和打印「B」工作正常,但現在光標問題,它向前移動,我不想要。我試過^ [H但它似乎沒有解決它。 –

+1

@SaadA對於「清除屏幕」感到抱歉,在我看來,你想這樣做。關於光標問題,我不明白。打印後光標始終移動,但退格應使其返回。如果您發送「A BS B BS」,您應該看到一個「B」,並且光標在它下面。你能解釋得更好嗎? – linuxfan