How to print data from register(8051) to terminal(serial monitor)

8051keil

I have written a program that uses printf statement to write to the TERMINAL program using serial communication. Now, for my actual code, I want to transfer the content of my register and display it on the terminal window. How do I do that?

The test code is:

#include <REG51F.H>
#include <stdio.h>

void main(void){

 SCON = 0x50;   /* SCON: mode 1, 8-bit UART, enable rcvr */
 TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
 TH1 = 253; /* TH1: reload value for 1200 baud @ 16MHz */
 TR1 = 1; /* TR1: timer 1 run */
 TI = 1; /* TI: set TI to send first char of UART */

  while(1){

      printf("Test\n");
    }
   }

Best Answer

//make stdin, stdout and stderr point to the debug usart
static int putChar(char c, FILE *out);
static int getChar(FILE *in);
static FILE fileInOutErr = FDEV_SETUP_STREAM(putChar, getChar, _FDEV_SETUP_RW);

This is how it's done for an AVR. The definitions of the functions are trivial:

//uart
static int putChar(char c, FILE *out){
        USARTtransmit(c);
        return 0;       //SUCCESS
}

static int getChar(FILE *in){
        unsigned char c;
        USARTreceive(&c);
        return (int)c;  //SUCCESS
}

And then you fire up minicom or some other serial console on the PC and communicate.