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 Leon Heller

----- Original Message ----- 
From: "Stefan Trethan" <stefan_trethan@gmx.at>
To: <AVR-Chat@yahoogroups.com>
Sent: Monday, January 31, 2005 3:13 PM
Subject: Re: [AVR-Chat] accessing bits (e.g. of ports)


>
> On Mon, 31 Jan 2005 09:47:49 -0500 (EST), <ethan@bufbotics.org> wrote:
>
>>
>> <code>
>> if (turn_motor_on) {
>>    setbit(MOTOR_ENABLE_PORT, MOTOR_ENABLE_PIN);
>> } else {
>>    clrbit(MOTOR_ENABLE_PORT, MOTOR_ENABLE_PIN);
>> }
>> </code>
>> Personally, I would prefer something like.....
>> <code>
>> write_bit(turn_motor_on, MOTOR_ENABLE_PORT, MOTOR_ENABLE_PIN);
>> </code>
>
> You could make that with a define i think. I guess even i could figure out
> how eventually.
>
> But well, wouldn't it be great if one could just write
>
> MOTOR_ENABLE_PIN = turn motor on;
>
> or, if need be
>
> MOTOR_ENABLE_PORT.MOTOR_ENABLE_BIT = turn motor on;
>
>
> I have the same situation here.
> I don't understand why that doesn't work. I'm no programmer as everyone
> sees easily, but when reading various stuff i get the impression "well, if
> you don't use bit operations you aren't a proper programmer, get lost.".
> That might well be true, and i'm not a proper programmer, but i still
> don't understand why i must fight with &|!<< to change or read a single
> bit.
> I mean, life is hard enough, why make it any harder?
>
>
> Why exactly is't a bit just a variable like for example PORTB?
> I can write PORTB=0xFF; but can't do the same with bits.

In most languages, including C, you can't manipulate individual bits 
directly in the same way as other data types like ints, chars, etc. High 
level languages don't usually know about anything smaller than a byte. You 
can manipulate individual bits in assembler. For instance:

    sbi    portb,PB2

sets bit 2 of port B

and

    cbi    portb,PB2

clears bit 2 of port B

and many embedded C compilers have language extensions that let you use 
in-line assembler by writing something like:

    asm("sbi    portb,PB2");

Leon 



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.8.2 - Release Date: 28/01/2005

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.