Yahoo Groups archive

AVR-Chat

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

Message

Re: [AVR-Chat] accessing bits (e.g. of ports)

2005-01-31 by Svenn Dahlstrøm

I use ICCAVR and do it this way :

#define setbit(ADDRESS,BIT) ((ADDRESS) |= (1<<(BIT)))
#define clrbit(ADDRESS,BIT) ((ADDRESS) &= ~(1<<(BIT)))
#define chkbit(ADDRESS,BIT) ((ADDRESS) & (1<<(BIT)))
#define togglebit(ADDRESS,BIT) ((ADDRESS) ^= (1<<(BIT)))

and write code like this :

setbit(PORTA, 2);
togglebit(PORTB, 6);
clrbit(DDRC, 0);

And this can be used to set, clear, check or toggle one bit on any register 
or variable.

Svenn


>
>
> Hi,
>
> what is the easiest way to access single bits of ports (SFRs)?
>
> i have found <http://lists.gnu.org/archive/html/avr-libc-dev/2003-08/
> msg00020.html> with google.
>
> This:
> PORTC |= _BV(0);.
> is madness ;-). I'm doing this for a hobby and stuff like that causes
> me to loose my interest in it. I need to think 30 minutes until i know
> what i have to write, and then i probably got it wrong.
>
> this:
> bit_set(PORTC, _BV(0));
> is better, but still complicated in a way that boggles my mind.
>
> Why, and if yes how, can i get something like that:
> PORTC.0 = 1;
> or
> PC0 = 1;
> or if it must be
> PORTC.BIT0 =1;
>
> or whatever, just UNDERSTANDABLE for the mind of a non-programmer.
>
> I do not shy the typing work, but even with comments if i don't
> understand the code it is like reading a book in a foreign language
> you don't speak using a dictionary. (But i'm trying to WRITE that
> book!!)
>
> You see my problem, please help, i'm getting so frustrated here....
>
> ST
>
>
>
>
>
>
>
> Yahoo! Groups Links
>
>
>
>

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.