0

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.

Saad A
  • 1,135
  • 2
  • 21
  • 46
  • 1
    [This may help](http://stackoverflow.com/a/15559322/1790864). – Rev Mar 07 '17 at 07:37
  • The escape character just introduces a string that performs some special command, in this cae "BAB" that does nothing. Better choices might backspace, carriage return, or form feed. 8, 13, or 12. Try them and see what happens – UncleO Mar 07 '17 at 07:54

1 Answers1

3

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.

  • just to clarify, I want to print "A" on my putty screen and then erase it to print "B". – Saad A Mar 08 '17 at 02:58
  • erasing the character "A" and printing "B" works fine but, now the cursor problem, it moves ahead which I don't want. I tried ^[H but it doesn't seems to fix it. – Saad A Mar 08 '17 at 03:18
  • 1
    @SaadA sorry for "clear the screen", it seemed to me you wanted that. About the cursor problem, I don't understand. The cursor always moves after printing, but a backspace should make it go back. If you send `A BS B BS` you should see a "B" with the cursor beneath it. Can you explain better? – linuxfan says Reinstate Monica Mar 08 '17 at 06:55