Hello,
I've written some code for an educational project which tries to capture input from the user, I've noticed that when using the library function _getkey() alone it turns each typed Enter or Intro pressed key into CR ('\r', ASCII encoding = 0x0D), but when using the function getchar(), which is supposed to use _getkey() internally, it turns each typed Enter or Intro pressed key into LF ('\n', ASCII encoding = 0x0A), following I show two small snippets I've used in order to verify this behavior:
1.- Using _getkey() and putchar() to get the job done:
#include <reg515c.h> #include <stdio.h> void test_getkey (void) { char c; while ((c = _getkey ()) != 0x1B) { putchar(c); printf ("\ncharacter = %bu %bx\n", c, c, c); } } void main(void) { SCON = 0x52; BD = 1; /* 9600 Bd @ 10 MHz => SMOD = 1, SREL = 0x3BF */ PCON |= 0x80; SRELL = 0xBF; while(1) test_getkey(); }
Output when pressing the Enter or Intro key:
character = 13 d
1.- Using getchar() to get the job done:
#include <reg515c.h> #include <stdio.h> void test_getchar(void) { char c; while ((c = getchar ()) != 0x1B) { putchar(c); printf ("\ncharacter = %bu %bx\n", c, c, c); } } void main(void) { SCON = 0x52; BD = 1; /* 9600 Bd @ 10 MHz => SMOD = 1, SREL = 0x3BF */ PCON |= 0x80; SRELL = 0xBF; while(1) test_getchar(); }
character = 10 a
In the following Support Knowledge Base article:
http://www.keil.com/support/docs/1791.htm
it is stated that by defaul getchar() is implemented as follows:
char getchar (void) { char c; c = _getkey (); putchar (c); return (c); }
So I don't understand why the characters read vary when pressing the Enter/Intro key, could somebody please shed some light on this issue?.
Thanks in advance, Juan
Just an amendment to the code I posted, actually the correct programs are:
#include <reg515c.h> #include <stdio.h> void test_getkey (void) { char c; while ((c = _getkey ()) != 0x1B) { putchar(c); printf ("\ncharacter = %bu %bx\n", c, c); } } void main(void) { SCON = 0x52; BD = 1; /* 9600 Bd @ 10 MHz => SMOD = 1, SREL = 0x3BF */ PCON |= 0x80; SRELL = 0xBF; while(1) test_getkey(); }
2.- Using getchar() to get the job done:
#include <reg515c.h> #include <stdio.h> void test_getchar(void) { char c; while ((c = getchar ()) != 0x1B) { printf ("\ncharacter = %bu %bx\n", c, c); } } void main(void) { SCON = 0x52; BD = 1; /* 9600 Bd @ 10 MHz => SMOD = 1, SREL = 0x3BF */ PCON |= 0x80; SRELL = 0xBF; while(1) test_getchar(); }
I hope somebody can help me :)
Juan
The getchar() implementation shown in Keil documentation doesn't seem to match the real implementation.
Different computer environments uses different characters to represent line breaks in text files. Some uses \r\n. Some uses \n\r. Some just uses \r and some just uses \n.
When processing a binary stream, you would want characters to be transparently processed. When processing text data, a number of C functions are defined to normalize line breaks, only returning a single \n whatever character combination the native system uses. In the same way, a number of C functions are defined to normalize output, converting a single \n character into the native line break combination.
It seems as if Keils default implementation of getchar() performs conversions of \r into \n.