This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using putchar

I am testing simple 8051 program that polls for a character from serial port and output it out to the serial port again.

In the main function, I do the following continously

  u8_t xdata c;

  while(slipdev_char_poll(&c)) {

    slipdev_char_put(c);
    break;

  }
where the functions are written as
void slipdev_char_put(u8_t chr){

	int delaycount;
	putchar(chr);
	for (delaycount=0;delaycount<1000;delaycount++){}
	return;
}

u8_t slipdev_char_poll(u8_t xdata *chr){


	if (RI==1){			//Character is available
		*chr = SBUF;
		RI = 0;
		return 1;
	}else{				//Character not available
		return 0;
	}
}


However, when I test the program, I can only get the correct output only twice. Any subsequent characters that is sent to the 8051 will not be output back, and only the same first two characters are output to the serial port.

Incredibly, if I replace my own "put character" function with putchar(c) ie
  u8_t xdata c;

  while(slipdev_char_poll(&c)) {

    putchar(c);
    break;

  }
Everything works fine!

I can't find what has gone wrong in my method. Anyone have any advice?

Parents
  • Why is there a break in the main while loop? It will only execute once. Might as well have an 'if' statement.

    Your delay loop could easily be optimized away by the compiler, especially as it has no side effects and changes only a local variable. See past threads for various ways to implement a delay.

Reply
  • Why is there a break in the main while loop? It will only execute once. Might as well have an 'if' statement.

    Your delay loop could easily be optimized away by the compiler, especially as it has no side effects and changes only a local variable. See past threads for various ways to implement a delay.

Children
No data