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 Stefan Trethan

On Mon, 31 Jan 2005 12:13:18 -0800, Larry Barello <yahoo@barello.net>  
wrote:

>
> This is easy to do and produces optimal code on GCC (e.g. sbi/cbi when
> appropriate):
> typedef union// Delclare a union that allows bit & byte access
> {
> unsigned char byte;// This is a byte for 8 bit access
> struct
> {
> unsigned char P0:1;// These are bit-fields
> unsigned char P1:1;
> unsigned char P2:1;
> unsigned char P3:1;
> unsigned char P4:1;
> unsigned char P5:1;
> unsigned char P6:1;
> unsigned char P7:1;// We can use an anonomous struct
> };// because the bit names are unique.
> } sfr_struct;
> #define PORTB (*(sfr_struct *)0x38)// Cast the sram address to the
> structure
> ...
> #define MOTOR_ENABLE_PIN PORTB.P2// Now make an alias for your bit
> ...
> MOTOR_ENABLE_PIN = TRUE;// Now, just use it.
> If you want to access the port as a byte value, then use:
> PORTB.byte = ...


Now that one i like.
Thanks, i will certainly use it.

ST

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.