Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #28629

Re: Getting started with AVR and C

From Ivan Shmakov <oneingray@gmail.com>
Newsgroups comp.arch.embedded, comp.lang.c
Subject Re: Getting started with AVR and C
Date 2012-11-25 04:18 +0700
Organization Aioe.org NNTP Server
Message-ID <86boem66on.fsf@gray.siamics.net> (permalink)
References <22t1b8pt63pcb3fks5ic03tsnlnhlm6ef1@4ax.com>

Cross-posted to 2 groups.

Show all headers | View raw


>>>>> Robert Roland <fake@ddress.no> writes:

	[Cross-posting to news:comp.lang.c, in the hope that someone
	could provide more suggestions, or correct me on C.]

[...]

 > 2. Where do I start learning C?  Is there a good online tutorial
 > somewhere?  I'd also be willing to buy a book.  Is there one that
 > stands out as the best?

	Frankly, I don't quite understand how did I learn C myself.
	FWIW, there were hardly any good book on that that I've read.

	Two aspects of C are probably to be highlighted:

	* first of all, unlike some other, and higher-level, languages
	  (like Pascal, BASIC, etc.), C has a very concise set of
	  syntactic constructs; most of the power lies in libraries, and
	  should you end up using AVR Libc, be sure to check its
	  reference manual [1] (it isn't as good as the GNU C Library
	  manual I'm using for the most time I need information on C,
	  but it's still useful);

	* the C constructs tend to be translated into assembly in a
	  rather straightforward manner (unless advanced optimization is
	  involved, that is); consider, e. g.:

   int8_t i;                    /* not necessarily translated; may force
                                   the compiler to "allocate" a register
                                   (say, r7), or a memory cell */
   i = 3;                       /* ldi r7, 3 */
   i += 2;                      /* adi r7, 2 */
   i++;                         /* inc r7    */
   if (i < 9) {                 /* cpi r7, 9 ;  brge else_a */
     int8_t j = 5;              /* ldi r8, 5 */
     while (j >= 3) {           /* while_a:
                                   cpi r8, 3
                                   brlt end_while_a */
       PORTB ^=  1;             /* in  r9, PORTB
                                   eoi r9, 1
                                   out PORTB, r9 */
     }                          /* jmp while_a
                                   end_while_a: */
   }                            /* else_a:   */

	Not to undermine its value, but as could be seen (I hope) from
	this example, for the most part, C only manages registers and
	memory (including function calling conventions) while the rest
	of its /syntax/ is comparable to that of a library of assembly
	language macros of some sort.

	A cheat sheet for most of the C operators would probably be
	something like the following (where a, b, c, ... are either
	numeric literals, variable identifiers, or expressions.)  Note
	that whenever all the operands are integer, an integer operation
	is performed.  (So, 7 / 3 is 2.)  The result is as wide as the
	widest of the operands.  (So, i + j is 0 if i is 255, j is 1,
	and both are declared to be of the 8-bit unsigned integer
	uint8_t type.)

    Operation                   Value                   Side-effect

                      Operations free of side-effects

    + a                         a
    - a                         - a
    * a                         the value of the memory cell at address
                                a
    & a                         the address of a (must be an "l-value")
    ~ a                         bitwise negated a
    ! a                         1 if a is 0,
                                0 otherwise
    a, b                        b
                NB: a is evaluated first, its result discarded.
    a + b                       a + b
    a - b                       a - b
    a * b                       a b
    a / b                       a / b (quotient of)
    a % b                       remainder of a / b
    a & b                       a (bitwise and) b
    a | b                       a (bitwise or)  b
    a ^ b                       a (bitwise exclusive or) b
    a << b                      a times 2 ^b (shift left)
    a >> b                      a times 2 ^(-b) (shift right)
    a < b                       1 if a is less than b,
                                0 otherwise
    a > b                       1 if a is greater than b,
                                0 otherwise
    a == b                      1 if a is equal to b,
                                0 otherwise
    a <= b                      1 if a is less than or equal to b,
                                0 otherwise
    a >= b                      1 if a is greater than or equal to b,
                                0 otherwise

                           Conditional operations

    a && b                      a is evaluated;
                                if a is non-zero, the value is b;
                                otherwise, the value is 0, while b is
                                not evaluated at all
    a || b                      a is evaluated;
                                if a is zero, the value is b;
                                otherwise, the value is a, while b is
                                not evaluated at all
    a ? b : c                   a is evaluated first;
                                if a is non-zero, the value is b;
                                otherwise, the value is c;
                                the other ("unused") expression is not
                                evaluated

                        Operations with side-effects

                NB: a must be an "l-value"
    a++                         a                       a set to a + 1
    a--                         a                       a set to a - 1
    ++a                         a + 1                   a set to value
    --a                         a - 1                   a set to value
    a = b                       b                       a set to value
    a += b                      a + b                   a set to value
    a -= b                      a - b                   a set to value
    a *= b                      a b                     a set to value
    a /= b                      a / b                   a set to value
    a &= b                      a (bitwise and) b       a set to value
    a |= b                      a (bitwise or)  b       a set to value
    a ^= b                      a (bitwise xor) b       a set to value
    a <<= b                     a times 2 ^b            a set to value
    a >>= b                     a times 2 ^(-b)         a set to value

	Naturally, both "=" and "," can be "nested", thus:

   for (a = b = 0, c = 5; c > 0; a++, b++, c--) {
     /* ... */
   }

	To note is that the for () <statement> form is just a short-hand
	for a specific while () loop.  For instance, the for ()
	statement above can be rewritten as follows:

   a = b = 0, c = 5;
   while (c > 0) {
     /* ... */
     a++, b++, c--;
   }

	Thus, the only convenience of for () is that it allows for the
	"at-the-end-of-the-loop" part to be written above the loop body
	itself (i. e., together with the loop condition.)

	One more thing to note is that there're two basic contexts: the
	statement context, and the expression context.  The switch from
	the former to the latter usually takes place in obvious places,
	while it isn't possible (in standard C; AFAIK) to switch from
	the latter to the former.  E. g.:

   /* statement context */
   while (a < 5 /* expression context */) {
     /* statement context */
     b = 4 /* expression context */ ;
     /* NB: cannot switch back to the statement context, like: */
     /* c = while (b > 0) { /* ... */ } ; */
   }

	As one may need a conditional operator in either context, C has
	both the ?:-operator (see above), and (perhaps a more
	conventional) if ():

   if (a) {
     /* the code here will be executed iff a is non-zero */
   } else {
     /* the code here will be executed otherwise */
   }

	The { }-grouping is only necessary if more than one statement is
	needed as the body; otherwise, it may be elided, like:

   if (a) b = c;

	This allows for convenient nesting, like:

   if (a) {
     /* ... */
   } else if (b) {
     /* ... */
   } else {
     /* ... */
   }

	A similar idiom is possible for the ?:-operator just as well.
	Consider, e. g.:

   a = (b   ? c
        : d ? e
        : f);

	which is not dissimilar to more verbose (and error-prone):

   if (b) {
     a = c;
   } else if (d) {
     a = e;
   } else {
     a = f;
   }

	An example program for an AVR could be as follows.

#include <avr/io.h>             /* for PORTB, DDRB, etc. */
#include <util/delay.h>         /* for _delay_ms () */

/* global variable declarations; not necessary in this example */

static void
blink_led (void)
{
  PORTB ^= (1 << PB0);  /* toggle PB0 */
  _delay_ms (500);      /* wait for 0.5 s */
  PORTB ^= (1 << PB0);  /* toggle PB0 again */
  _delay_ms (500);      /* wait for 0.5 s more */
}

/* the main () function is the conventional program's entry point */
int
main ()
{
  /* set up PB0 for output, all the other tri-stated */
  DDRB = (DDRB0 << 1);

  /* enter infinite loop */
  while (1) {
    /* call our function */
    blink_led ();
  }

  /* never reached */
  return 0;
}

	For sure, there's over than a dozen of individual syntactic
	constructs more (and then there's a handful or so of the
	preprocessor #-directives, too), but I hope that with the above,
	reading the sources would become a bit easier task.

[1] http://www.nongnu.org/avr-libc/user-manual/

-- 
FSF associate member #7257

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: Getting started with AVR and C Ivan Shmakov <oneingray@gmail.com> - 2012-11-25 04:18 +0700
  Re: Getting started with AVR and C Richard Damon <news.x.richarddamon@xoxy.net> - 2012-11-24 17:09 -0500
  Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-24 22:12 +0000
    statement / expression contexts in C Ivan Shmakov <oneingray@gmail.com> - 2012-11-28 12:54 +0700
    Re: Getting started with AVR and C Ivan Shmakov <oneingray@gmail.com> - 2012-11-28 13:37 +0700
      Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-28 14:49 +0000
      Re: Getting started with AVR and C Frank Miles <fpm@u.washington.edu> - 2012-11-28 17:53 +0000
        Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-28 18:05 +0000
          Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 12:56 -0600
            Re: Getting started with AVR and C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-28 21:45 +0000
              Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 15:55 -0600
              Re: Getting started with AVR and C David Brown <david.brown@removethis.hesbynett.no> - 2012-11-29 01:35 +0100
                Re: Getting started with AVR and C Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-11-29 01:47 +0100
                Re: Getting started with AVR and C David Brown <david@westcontrol.removethisbit.com> - 2012-11-29 09:24 +0100
                Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-29 01:18 +0000
                Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 20:07 -0600
                Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 15:23 +0000
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 11:01 -0500
                Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 19:19 +0000
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 14:32 -0500
                Re: Getting started with AVR and C Jon Kirwan <jonk@infinitefactors.org> - 2012-11-29 11:44 -0800
                Re: Getting started with AVR and C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-29 21:23 +0000
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-11-29 18:15 -0800
                Re: Getting started with AVR and C upsidedown@downunder.com - 2012-11-29 22:17 +0200
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-11-29 12:36 -0800
                Re: Getting started with AVR and C Stephen Sprunk <stephen@sprunk.org> - 2012-11-29 16:22 -0600
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-11-29 18:36 -0800
                Re: Getting started with AVR and C "Boudewijn Dijkstra" <sp4mtr4p.boudewijn@indes.com> - 2012-12-11 10:53 +0100
                Re: Getting started with AVR and C John Devereux <john@devereux.me.uk> - 2012-12-11 14:53 +0000
                Unicode Ivan Shmakov <oneingray@gmail.com> - 2012-12-14 23:25 +0700
                Re: Getting started with AVR and C Richard Damon <news.x.richarddamon@xoxy.net> - 2012-12-15 14:30 -0500
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-12-15 12:24 -0800
                Re: Getting started with AVR and C Richard Damon <news.x.richarddamon@xoxy.net> - 2012-12-15 18:02 -0500
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-12-15 15:45 -0800
                Re: Getting started with AVR and C Richard Damon <news.x.richarddamon@xoxy.net> - 2012-12-16 22:55 -0500
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-12-16 23:20 -0800
                Re: Getting started with AVR and C Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-12-17 12:27 +0200
                Re: Getting started with AVR and C John Devereux <john@devereux.me.uk> - 2012-12-17 11:17 +0000
                Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-12-17 14:07 +0000
                Re: Getting started with AVR and C Nobody <nobody@nowhere.com> - 2012-12-16 03:28 +0000
                Re: Getting started with AVR and C upsidedown@downunder.com - 2012-12-16 10:40 +0200
                Re: Getting started with AVR and C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-29 02:31 +0000
                Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-29 03:36 +0000
                Re: Getting started with AVR and C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-29 03:55 +0000
                Re: Getting started with AVR and C David Brown <david@westcontrol.removethisbit.com> - 2012-11-29 09:37 +0100
                Re: Getting started with AVR and C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-29 11:31 +0000
                Re: Getting started with AVR and C David Brown <david@westcontrol.removethisbit.com> - 2012-11-29 13:44 +0100
                Re: Getting started with AVR and C Hans-Bernhard Bröker <HBBroeker@t-online.de> - 2012-11-29 13:22 +0100
                Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-29 13:32 +0000
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-11-29 11:28 -0800
                Re: Getting started with AVR and C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-11-29 13:20 +0000
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 09:07 -0500
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 09:17 -0500
                Re: Getting started with AVR and C pete <pfiland@mindspring.com> - 2012-11-29 18:25 -0500
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 22:46 -0500
                Re: Getting started with AVR and C lawrence.jones@siemens.com - 2012-11-30 11:47 -0500
                Re: Getting started with AVR and C Tim Rentsch <txr@alumni.caltech.edu> - 2012-12-17 03:53 -0800
                Re: Getting started with AVR and C Keith Thompson <kst-u@mib.org> - 2012-11-29 11:38 -0800
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 15:21 -0500
            Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-28 22:26 +0000
              Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 17:32 -0600
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-28 19:02 -0500
                Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 18:29 -0600
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-28 23:39 -0500
                Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.please> - 2012-11-29 00:59 -0600
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 07:57 -0500
                Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-29 13:02 -0600
                Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 15:19 +0000
                Re: Getting started with AVR and C Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2012-11-29 12:03 +0200
                Re: Getting started with AVR and C glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2012-11-29 11:39 +0000
                Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-29 08:04 -0500
                Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 15:17 +0000
            Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 19:19 -0600
              Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-28 23:44 -0500
        Re: Getting started with AVR and C James Kuyper <jameskuyper@verizon.net> - 2012-11-28 13:15 -0500
          Re: Getting started with AVR and C Arlet Ottens <usenet+5@c-scape.nl> - 2012-11-28 19:17 +0100
            Re: Getting started with AVR and C Tim Wescott <tim@seemywebsite.com> - 2012-11-28 19:30 -0600
              Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 15:29 +0000
                Re: Getting started with AVR and C John Devereux <john@devereux.me.uk> - 2012-11-29 16:36 +0000
                Re: Getting started with AVR and C upsidedown@downunder.com - 2012-11-29 22:40 +0200
                Re: Getting started with AVR and C Jon Kirwan <jonk@infinitefactors.org> - 2012-11-29 13:55 -0800
                Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 22:06 +0000
                Re: Getting started with AVR and C Jon Kirwan <jonk@infinitefactors.org> - 2012-11-29 14:22 -0800
                Re: Getting started with AVR and C Grant Edwards <invalid@invalid.invalid> - 2012-11-29 21:59 +0000
                Re: Getting started with AVR and C John Devereux <john@devereux.me.uk> - 2012-11-30 08:29 +0000
  Re: Getting started with AVR and C Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-11-25 05:41 -0800
  Re: Getting started with AVR and C Rosario1903 <Rosario@invalid.invalid> - 2012-11-27 10:21 +0100

csiph-web