Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: Some C help please !

2007-03-28 by kernels_nz

SORRY ! Wasnt me, no idea why that message got pasted so many times ! 


--- In AVR-Chat@yahoogroups.com, "kernels_nz" <kernels@...> wrote:
>
> So ... after the big discussion on  bit-shifting and masking I decided
> to rewrite a bit of pretty rough wireless code I use a lot with my new
> found appreciation of making my code more portable and tidy-er. 
> 
> But! To my dismay I found that the codevision header files dont
> contain defines for bits like SPIF in the SPSR (SPI status register)
> do the other compilers like GCC and IAR have header files that have
> the names of each bit of every register defined ?
> 
> I did end up rewriting the code, and to me it is now a thing of
> beauty, but I had to go through the header file and #define all the
> bits I was using for shift and masking.
> 
> Cheers
> Hein B
> Auckland
> New Zealand
> 
> 
> 
> --- In AVR-Chat@yahoogroups.com, Ned Konz <ned@> wrote:
> >
> > I wrote:
> > 
> > > So, using my headers, your example test for tx completed or empty
> could 
> > > be written:
> > > 
> > 
> > [snip]
> > 
> > >        if (UCSR0B & ((1 << bitTXC0) | (1 << bitUDRE0)))
> > >           /* do stuff */
> > > 
> > > 
> > > and the generated code would look like this:
> > 
> > [snip]
> > 
> > > or this (somewhat more efficient):
> > > 
> > > 
> > >    15:test2.c  ***       if (UCSR0B & ((1 << bitTXC0) | (1 <<
> bitUDRE0)))
> > > 
> > >   324 0010 8AB1      		in r24,42-0x20
> > >   325 0012 8076      		andi r24,lo8(96)
> > >   326 0014 31F0      		breq .L5
> > >    *** /* do stuff */
> > >   338                   .L5
> > 
> > 
> > 
> > And here we see the problem with the bit numbers: they're not
connected 
> > with the register.
> > 
> > I made the mistake of referring to UCSR0B, when TXC0 and UDRE0 are 
> > actually in UCSR0A.
> > 
> > This is a not-uncommon error using this idiom. You don't have the 
> > ability to make this error when using bitfields.
> > 
> > The above should be:
> > 
> >          if (UCSR0A & ((1 << bitTXC0) | (1 << bitUDRE0)))
> >             /* do stuff */
> > 
> > 
> > I spent some time making a safer version using C++ templates, but 
> > haven't been satisifed with its ease of use.
> > 
> > -- 
> > Ned Konz
> > ned@
> > http://bike-nomad.com
> >
>

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.