hi All
i'm quite new at this, but i managed to do the standard tricks with the Uart port
I Can send a character, string or number to the terminal using printf and putchar
echoing the input of the terminal to the output isn't a problem.
But I'm stuck at saving a number or a string inputted by the keyboard to a variable.
sscanf or scanf doesn't seem to work with me
So the question : How do i put a string/number into a variable so I can print it with
printf or do some calculations on it?
this is what I have
int putchar (int ch)
{
if (ch == '\n')
{
while (!(U0LSR & 0x20));
U0THR = CR; // output CR
}
while (!(U0LSR & 0x20));
return (U0THR = ch);
}
int getchar (void) /* Read character from Serial Port */
{
while (!(U0LSR & 0x01));
return (U0RBR);
}
void main()
{
int ch;
initsio0();
printf("write string \n");
...
}
Can anyone help me?
thanks in advance!
------------------------------------

(You need to be a member of lpc2000 -- send a blank email to lpc2000-subscribe@yahoogroups.com )
Stijn,
Here's my $0.01 worth of advice:
- follow Richard's advice on writing your own %$#@_f I/O routines based on K&R
examples
- here's my version for an scanf-like routine:
- low-level routines, one with echo, other without (sendchar is your own routine, all
coming from Keil's samples)
int ler_tecla (void)
{
int tecla;
while (!(U0LSR & 0x01))
;
tecla = U0RBR; /* read character */
sendchar (tecla); /* echo it */
return (tecla); /* return it */
}
int getkey (void) { /* Read character from Serial Port */
while (!(U0LSR & 0x01))
;
return (U0RBR); /* no echoing */
}
- now, call ler_tecla from a loop with as many characters as you wish, here's an
example for reading a date from UART, scanning it and saving it to the RTC registers:
void mostrar_data (void)
{
BYTE dados[8];
int i;
BYTE mes, dia;
WORD ano;
int chr;
printf ("\nData (dd:mm:aa ou ENTER): ");
chr = getkey ();
if (chr == '\r' || chr == '\n')
{
data = CTIME1;
dia = data & MASK_DIA;
mes = (data >> SH_MES) & MASK_MES;
ano = (data >> SH_ANO) & MASK_ANO;
printf ("Data atual: %02d/%02d/%4d\n", dia, mes, ano);
return;
}
sendchar (chr);
dados[0] = chr;
for (i = 1; i < 8; i++)
dados[i] = ler_tecla ();
dia = (dados[0] - '0') * 10 + (dados[1] - '0');
mes = (dados[3] - '0') * 10 + (dados[4] - '0');
ano = (dados[6] - '0') * 10 + (dados[7] - '0') + INIT_ANO;
DOM = dia;
MONTH = mes;
YEAR = ano;
}
I hope you get the point. Not the smartest, slickiest, smallest or most efficient, but you
can work it out to your standards.
Julio de Melo
--- In l...@yahoogroups.com, "rtstofer"
wrote:
>
> --- In l...@yahoogroups.com, "Stijn" wrote:
> >
> > hi All
> >
> > i'm quite new at this, but i managed to do the standard tricks with the Uart port
> >
> > I Can send a character, string or number to the terminal using printf and putchar
> >
> > echoing the input of the terminal to the output isn't a problem.
> >
> > But I'm stuck at saving a number or a string inputted by the keyboard to a
variable.
> > I would tend to avoid library functions that require the use of the heap. In fact, I
would avoid even HAVING a heap.
>
> To that end, I use a lot of code from "The C Programming Language" by Kernighan &
Ritchie (K&R).
>
> For example, if I have a string of input chars that are supposed to contain a number, I
would use the atoi(s) function (and not the one from the library):
> int atoi(char s[])
> {
> int i, n, sign;
>
> for (i = 0; s[i]==' ' || s[i] == '\n' || s[i] == '\t'; i++)
> ; /* skip white space */
>
> sign = 1;
> if (s[i] == '+' || s[i] == '-') /* sign present */
> sign = (s[i++] == '+') ? 1 : -1;
>
> for (n = 0; s[i] >= '0' && s[i] <= '9'; i++)
> n = (10 * n + s[i] - '0';
>
> return (sign * n);
> }
>
> As I don't usually use printf(), I use itoa(int n, char *s) for output. It is also from
K&R.
>
> static void reverse(char *s)
> {
> int c, i, j;
>
> for (i = 0, j = strlen(s)-1; i < j; i++, j--)
> {
> c = s[i];
> s[i] = s[j];
> s[j] = c;
> }
> }
>
> void itoa(int n, char *s)
> {
> int i, sign;
>
> if ((sign = n) < 0)
> n = -n;
> i = 0;
> do {
> s[i++] = n % 10 + '0';
> } while ((n /= 10) > 0);
> if (sign < 0)
> s[i++] = '-';
> s[i] = 0x00;
> reverse(s);
> }
>
> If I want the number fitted to a field, I use my own function:
>
> void putnum(const int n, const int width)
> {
> char s[32];
> int spaces;
>
> itoa(n,s);
> spaces = width - strlen(s);
> if (spaces < 0)
> spaces = 0;
> while (spaces--)
> putch(' ');
> puts(s);
> }
>
> Here I use the library function strlen(s) but there is a version in K&R that I could use
just as well.
>
> In other words, I want to use my own conversion functions and not incur the overhead of
newlib and certainly not printf, scanf or sprintf.
>
> Richard
>
------------------------------------

(You need to be a member of lpc2000 -- send a blank email to lpc2000-subscribe@yahoogroups.com )