Sign in

username:

password:



Not a member?

Search avrclub



Search tips

Subscribe to avrclub



avrclub by Keywords

AT90S2313 | AT90S8515 | ATMega | ATmega128 | ECL | FETS | IAR | Keyboard | LCD | STK50 | TMOS | UART

Ads

Discussion Groups

See Also

DSPFPGAElectronics

Discussion Groups | AVRclub | [AVR club] Is there an example showing how to input strings ?


Advertise Here

Atmel AVR Microcontroller discussion group.

[AVR club] Is there an example showing how to input strings ? - Dan Roganti - Oct 14 22:23:49 2009


I'm using WinAVR and trying to learn how to input strings. I'm using a
ATmega32 controller with the serial port connected to my PC where I have
a terminal program that I use to communicate with the micro. I already
have several tests written for this which I send data and text to the
terminal. But I like to be able to enter a string for a command- much
like a command shell- and parse this so it can determine what action to
take next.

Is there an example showing how to input strings ?

thanks
=Dan
------------------------------------

______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.


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


Re: [AVR club] Is there an example showing how to input strings ? - STEVEN HOLDER - Oct 15 10:40:24 2009

Dan,
=A0
I have a standard code set for parsing string commands, it uses a buffered =
char set as there is no native "string" variable in c, rather an array of c=
haracters. I tend to use a serial protocol packet based system, will send y=
ou the files , see what you think.
=A0
Regards
--- On Thu, 15/10/09, Dan Roganti wrote:
From: Dan Roganti
Subject: [AVR club] Is there an example showing how to input strings ?
To: a...@yahoogroups.com
Date: Thursday, 15 October, 2009, 2:55 AM
=A0=20

I'm using WinAVR and trying to learn how to input strings. I'm using a=20
ATmega32 controller with the serial port connected to my PC where I have=20
a terminal program that I use to communicate with the micro. I already=20
have several tests written for this which I send data and text to the=20
terminal. But I like to be able to enter a string for a command- much=20
like a command shell- and parse this so it can determine what action to=20
take next.

Is there an example showing how to input strings ?

thanks
=3DDan

[Non-text portions of this message have been removed]

------------------------------------



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

Re: [AVR club] Is there an example showing how to input strings ? - Dan Roganti - Oct 16 1:25:13 2009


I took a look some more at the avr_libc, and I see there are several
string functions available. I'm just not entirely sure how to use them
yet. There aren't that many examples in avr-libc to show how to use
these. The one in particular is scanf for input strings and strncmp for
comparing strings.

I found this demo program elsewhere online when I googled for tutorial
and scanf , I plan to try this
//------------------------------------------------------
#include

void main (void) {

/* We will use a floating-point and an integer variable. */

double x;
int n;
/* Read in an integer. */

printf("Please enter an integer: ");
scanf("%d", &n);
printf("The integer was %d\n\n", n);
/* Read in a double. */

printf("Please enter a double: ");
scanf("%lf", &x);
printf("The double was %g\n\n", x);
/* Read in an integer and a double. */

printf("Please enter an integer and a floating-point number: ");
scanf("%d%lf", &n, &x);
printf("The numbers were %d %g\n", n, x);

}
//------------------------------------------------------
Is this what your using ?

=Dan

STEVEN HOLDER wrote:
>
>
> Dan,
>
> I have a standard code set for parsing string commands, it uses a
> buffered char set as there is no native "string" variable in c, rather
> an array of characters. I tend to use a serial protocol packet based
> system, will send you the files , see what you think.
>

------------------------------------

______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.


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

Re: [AVR club] Is there an example showing how to input strings ? - Dave Hylands - Oct 16 2:27:43 2009

Hi Dan,

> I found this demo program elsewhere online when I googled for tutorial
> and scanf , I plan to try this

Personally, I have a strong dislike for scanf. It doesn't do error
recovery very well.

I find using fgets to read in a line at a time, and then using sscanf
on the line tht was read in works much better for reporting errors and
recovering from errors.

--
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/
------------------------------------

______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.


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

Re: [AVR club] Is there an example showing how to input strings ? - David Kelly - Oct 16 2:31:13 2009


On Oct 15, 2009, at 7:15 PM, Dan Roganti wrote:

> I took a look some more at the avr_libc, and I see there are several
> string functions available. I'm just not entirely sure how to use them
> yet. There aren't that many examples in avr-libc to show how to use
> these. The one in particular is scanf for input strings and strncmp
> for
> comparing strings.
>
> I found this demo program elsewhere online when I googled for tutorial
> and scanf , I plan to try this
> //------------------------------------------------------
> #include void main (void) {
>
> /* We will use a floating-point and an integer variable. */
>
> double x;
> int n;

[...]

A wonderful example of how NOT to write an AVR-class embedded
application.

scanf() and family are terrible resource hogs. One does NOT casually
use floating point.

Is my understanding that avr-libc does a respectable job of
implementing stdin, stdout, and stderr, but as a general rule I find
it best to avoid those as well.

Start by writing your own getchar() routine to read whatever it is you
expect input. Maybe its a keypad, maybe its a UART.

Then make your own gets() which reads getchar() in a loop. I strongly
suggest you don't pattern it after the C library gets() due to lack of
bounds checking. Perhaps something like this:

uint16_t my_gets( char *dest, uint16_t size, char eol )
{
int16_t c;
uint16_t count;

for( count = 0 ; count < size ; count++ ) {
dest[count] = 0;
c = my_getchar();
if( c == EOT )
break;
if( eol && ( c == eol ) )
break;
dest[count] = c;
}
return count;
}

--
David Kelly N4HHE, d...@HiWAAY.net
========================================================================
Whom computers would destroy, they must first drive mad.

------------------------------------

______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.


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

Re: [AVR club] Is there an example showing how to input strings ? - STEVEN HOLDER - Oct 16 8:15:36 2009

Dan,
=A0
As mentioned by others , scanf and printf are not a good way of dealling wi=
th this, they resource hogs, and given the use below can use a lot of ram i=
f not carefully used. The functions are relatively slow as well.
=A0
I try to avoid floating point stuff like the plague on 8 bit systems like t=
he avr mega range, the floating point libraries again are resource hogs and=
cumbersome, you need to be a bit more creative and use integer type maths =
, that way you won't need to the floating point libraries, functions will r=
un quicker and if interrupts are around you will have less chance of corrup=
ting data.
=A0
I tend to design systems that need to communicate while running, so they ne=
ed to be quick in response, and modest in their resource requirements. The =
example below is exactly how i wouldn't do it.
=A0
I tried to send an example to your robot site e-mail address pghrobot@comca=
st.net but it has bounced back. Let me know an e-mail add i can get you on =
(i'm sure i had it before but i then i have lost it sorry) and i'll send yo=
u a method i regulary employ. Remember if you need to do any complex proces=
sing, let the pc do it, just get the "raw data" back from the microcontroll=
er as quickly and efficiently as possible. Try not to send large "intrepret=
ed strings " from the controller, like i say let the pc do the grunt work, =
thats what its for.
=A0
Have a look at the method employed in the files, like i say it is buffered =
data in a char array, the receiver is interrupt driven, but the transmitter=
is not (although you could do it in an interrupt manner and i generally do=
with RTOS/SCHEDULER TYPE SYSTEMS.
=A0
See what you think.
=A0
Regards
--- On Fri, 16/10/09, Dan Roganti wrote:
From: Dan Roganti
Subject: Re: [AVR club] Is there an example showing how to input strings ?
To: a...@yahoogroups.com
Date: Friday, 16 October, 2009, 1:15 AM
=A0=20

I took a look some more at the avr_libc, and I see there are several=20
string functions available. I'm just not entirely sure how to use them=20
yet. There aren't that many examples in avr-libc to show how to use=20
these. The one in particular is scanf for input strings and strncmp for=20
comparing strings.

I found this demo program elsewhere online when I googled for tutorial=20
and scanf , I plan to try this
//---------- --------- --------- --------- --------- --------
#include

void main (void) {

/* We will use a floating-point and an integer variable. */

double x;
int n;

/* Read in an integer. */

printf("Please enter an integer: ");
scanf("%d", &n);
printf("The integer was %d\n\n", n);

/* Read in a double. */

printf("Please enter a double: ");
scanf("%lf", &x);
printf("The double was %g\n\n", x);

/* Read in an integer and a double. */

printf("Please enter an integer and a floating-point number: ");
scanf("%d%lf" , &n, &x);
printf("The numbers were %d %g\n", n, x);

}
//---------- --------- --------- --------- --------- --------

Is this what your using ?

=3DDan

STEVEN HOLDER wrote:
>=20
>
> Dan,
>=20
> I have a standard code set for parsing string commands, it uses a=20
> buffered char set as there is no native "string" variable in c, rather=20
> an array of characters. I tend to use a serial protocol packet based=20
> system, will send you the files , see what you think.
>=20

[Non-text portions of this message have been removed]

------------------------------------



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

Re: [AVR club] Is there an example showing how to input strings ? - Dave Hylands - Oct 16 13:38:10 2009

Check out the string functions available

On Thu, Oct 15, 2009 at 11:30 PM, David Kelly wrote:
>
> On Oct 15, 2009, at 7:15 PM, Dan Roganti wrote:
>
>> I took a look some more at the avr_libc, and I see there are several
>> string functions available. I'm just not entirely sure how to use them
>> yet. There aren't that many examples in avr-libc to show how to use
>> these. The one in particular is scanf for input strings and strncmp
>> for
>> comparing strings.
>>

...snip...

> A wonderful example of how NOT to write an AVR-class embedded
> application.
>
> scanf() and family are terrible resource hogs. One does NOT casually
> use floating point.

...snip...

Check out here for the string functions provided by the C runtime library


strtok is good for tokenizing a string (i.e. extract keywords)

I like strtol and strtoul for parsing numbers.

To get more detailed information about any of the runtime functions, I
like to use www.die.net and search using the function name. This gives
the glibc documentation, which avr-libc may not replicate 100%, but it
will mostly be right, and there are usually samples.

--
Dave Hylands
Shuswap, BC, Canada
http://www.DaveHylands.com/
------------------------------------

______________________________
Stellaris® MCU Family: New Parts, New Package, New Price.


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