Search code examples
cputtyavr

AVR C : Serial Communication Clear Screen


I am using AVR C with Atmega micro controller. I am using serial communication, I want to transmit a char "A" to screen and then erase it and display "B". I am having troubling with clearing the screen.

I read that ESC ASCII can work so I tried.

#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;
}

But its not working, I am fairly new to micro-controller, it just prints

AAAA..

How do I clear screen and then print a new character on the serial communication screen, I am using putty.

Update

transmitByte('A');



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

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

transmitByte('B');

Output

enter image description here

How do I get the cursor back to its orignal position?

Final Working

transmitByte('A');

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

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

transmitByte('B');

Above code works, erase and the moves the cursor back.


Solution

  • I am not really sure you want to transmit an "A" and then clear the screen, but... if you want to clear the screen you must send the escape sequence your terminal emulator understands. Often, ESC[2J does that and it is understood by many terminal emulators. So you could write:

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

    Look at putty "terminal emulation" characteristics (it has a few options) and, in general, to "escape sequences". Also consider that ^[[2J (^[ means Escape) clears the screen, but should not send the cursor to home. To do that, you need ^[[H.

    Some link for basic info: http://www.isthe.com/chongo/tech/comp/ansi_escapes.html

    also: http://ascii-table.com/ansi-escape-sequences.php

    That said, it can also be interesting to print backspaces to go back one position on line (character 8 in decimal), or a carriage return to go back to the start of the current line (character 13 in decimal), and finally print something, then clear the rest of the line with ESC[K.