Bit Flipping Tutorial - circuit cellar

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@ eric,

very nice article on the matter, and congrats on your publication!

Bit Flipping Tutorial
An Uncomplicated Guide to Controlling MCU Functional
Eric Weddington
Bit flipping—the process of flipping bits in registers—enables you to control a microcontroller’s functionality. But what is the best method of bit flipping? Eric points you in the right direction with this helpful tutorial. p. 52

Keywords: Programming, bit flipping, masking, logic, C, HAL, hardware abstraction layer, API

http://tinyurl.com/8h9zo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for all the kind comments. :)
And note that it's difficult to present all sides on the subject in such a small space as a magazine article. I understand that there are many different approaches to this subject; after all, they have all been discussed here on AVR Freaks! The article just discusses a single viewpoint.

Thanks again
Eric Weddington

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey,

Yes - an enjoyable article! I'm looking forward to reading Lee's version ;-)

-Colin

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A bit of a tits-on-a-bull situation..
Seems one needs to purchase the article..
May be OK for residents of USA for us poor sods offshore it may be a bit of long ish wait.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If Eric holds the "Copyright" then maybe he can post the work he submitted ... here

/Bingo

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ignoramus wrote:
Seems one needs to purchase the article..

Bummer - most of the articles were available in PDF format at no charge. That said, I've been a Circuit Cellar subscriber since issue #1.

BTW, congratulations Eric, from another Circuit Cellar article author (December 1999). I read the July issue and your article today.

Don

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

donblake wrote:

Bummer - most of the articles were available in PDF format at no charge.

I'm also interest on reading the article without needing a credit card.

I can only see lots of "purchase" buttons, but no download links.

I think, a free forum is a wrong place to discuss not free readable articles.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

donblake wrote:
ignoramus wrote:
Seems one needs to purchase the article..

Bummer - most of the articles were available in PDF format at no charge. That said, I've been a Circuit Cellar subscriber since issue #1.

BTW, congratulations Eric, from another Circuit Cellar article author (December 1999). I read the July issue and your article today.

Don

Thanks for the kind words...

I guess it's pretty much CC's policy to allow some free articles, the others you have to purchase. Of course, I don't have any control over this.

As to copyright, I'll have to check things out... but I think that they may own it "lock, stock, and barrel".

But, for those who can't read the article, realize that the article was inspired by very lively discussions here on AVR Freaks. The article is essentially a tutorial on using C language bit operators (see the "bit flipping tutorial post" in the sticky post on C Language references) and then using macros to create abstraction layers for the hardware and then for the application itself.

Eric

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I did think the HAL http://en.wikipedia.org/wiki/Har... was perhaps one of the most interesting aspects of the article. The use of macros to develop a HAL in C succinctly clarified a pragmatic programming technique often overlooked in code development. However, the API http://en.wikipedia.org/wiki/App... discussion might be easily construed as stretching the HAL discussion too far for the AVR.

These details of bit flipping in C and HAL code development, when understood as they should be, lend a sense of confidence that the reader has reached a fundamental level of understanding microcontroller programming in C. Most books in C programming gloss over these important and powerful techniques; perhaps because most fail to recognize the power of bit flipping and do not devote enough time to the matter. Even K&R devote but a few paragraphs. Eric's perspective brings bit flipping to the forefront of programming - and it was long overdue in mainstream discussions on programming in C. Too often discussions involve using C to emulate higher operating functions; Eric pointed the discussion to a level that challenges ASM on bit manipulation, yet expanded the discussion of C to challenge high end abstracted languages - not an easy accomplishement in just a few pages.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
As to copyright, I'll have to check things out... but I think that they may own it "lock, stock, and barrel". 

Hey - from what I remember from the author agreement CC owns the pubslished version's copyright. However - the work you submitted is all yours still. If you wait a few months after the magazine's mostly gone they might be willing to give up a PDF version for AVRFreaks or something like that...

-Colin

Last Edited: Sun. Jul 17, 2005 - 02:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My half of cents only :-)

====================

for any C compiller:

#define SET_B(x) |= (1<<x)
#define CLR_B(x) &=~(1<<x)
#define INV_B(x) ^=(1<<x)

PORTB SET_B(5);

PORTB CLR_B(2);

PORTB INV_B(6);

=======================

CodeVisionAVR:

PORTB.7=0;

PORTB.5=1;

~PORTB.1;

=======================

ImageCraft: (see help: "Bit Twiddling")

#include

PORTB &= ~BIT(7); // clear bit7

PORTB |= BIT(5); // set bit5

PORTB ^= BIT(1); // invert bit1

=======================

IAR:

PORTB_Bit7 = 0;
PORTB_Bit5 = 1;
~PORTB_Bit1;

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

c_mcu_faq wrote:
My half of cents only :-)

====================

for any C compiller:

#define SET_B(x) |= (1<<x)
#define CLR_B(x) &=~(1<<x)
#define INV_B(x) ^=(1<<x)

PORTB SET_B(5);

PORTB CLR_B(2);

PORTB INV_B(6);

=======================

CodeVisionAVR:

PORTB.7=0;

PORTB.5=1;

~PORTB.1;

You need to read the article to see why CV's non-standard way of flipping bits is less powerful than then the standard C way of manipulating bits.

For example, try doing this using CV's "method":

#define bit_set(v, m) (v) |= (m)
#define bit(x) (1 << x)

bit_set(PORTB, bit(0) | bit(2) | bit(4) | bit(6))

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sonos wrote:
I did think the HAL http://en.wikipedia.org/wiki/Har... was perhaps one of the most interesting aspects of the article. The use of macros to develop a HAL in C succinctly clarified a pragmatic programming technique often overlooked in code development. However, the API http://en.wikipedia.org/wiki/App... discussion might be easily construed as stretching the HAL discussion too far for the AVR.

Not really. As a matter of fact this is usually how various "layers" of software can be constructed, for example in a protocol stack.

I will concede that perhaps the distinction between a "hardware layer" and an "application layer" can be less distinct. But it can be very distinct too depending on, of course, how things are specified and written. The line can be arbitrary.

But, in making a distinction between the two layers, allows for certain benefits. One can test the hardware layer separately and then reuse it in other applications.

Anyways....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi
I’m using WinAvr and it seems a bit complicated to use this:

#define SET_B(x) |= (1<<x)
#define CLR_B(x) &=~(1<<x)
#define INV_B(x) ^=(1<<x)

PORTB SET_B(5);

PORTB CLR_B(2);

PORTB INV_B(6);

Is there no easy way to toggle one pin in WinAvr.

Something like : PORTA1 = 0; or PINA1 = 0;

Please tell !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I'm using this technic since some years...
In AVR GCC, in the header file do the following: (example here...)

#define Enable_Port  PORTD
#define Enable_DDR  DDRD
#define Enable_DIN   PIND
#define Enable_Bit    0x40

#define Enable_Out    Enable_DDR |= Enable_Bit
#define Enable_In       Enable_DDR &= ~Enable_Bit
#define Enable_Hi      Enable_PORT |= Enable_Bit
#define Enable_Lo      Enable_PORT &= ~Enable_Bit

In your Application:

Enable_Out;        // Set Port Out
Enable_Hi;          // Set Enable Hi

Advantage: if you change Pin assignment (or Processor) you only have to change ONE place in the headerfile and everyone can read your code without shematic....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@toti
for example, if you want to set a bit in PORTB:

 PORTB |= ( 1<< PORTBx );
where x is 0,1...7

if you want to clear a bit in PORTB:

PORTB &= ~( 1<< PORTBx );
where x is 0,1...7
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

toti,

If you code:

PORTB &= ~(1 << 3);

then look at the compiler output you'll probably find that it has compiled to a single CBI 0x05, 3 instruction anyway. The compiler (well the GNU one anyway) is very good at spotting what you are trying to achieve and generating optimised code so don't worry about the "wordy" syntax you're using in C or if it bothers you follow baer.ac's advice above and just put it all into an easy to read macro.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

/*******************************************************************
Is there no easy way to set and read one pin in WinAvr.
And label a pin by name ?
********************************************************************/

Like with CodeVision you can do it simply by doing this :

#define	RED	PORTB.0	/* definitions to actual outputs */
#define	YEL	PORTB.1	/* used to control the lights */

#define	SWITCH	PINB.5

int main (void)
{
	//set PORTB for input and output
	DDRB = 0x0F;

	while (1)
	{
		YEL = 1;

		If(SWITCH)		//Read if switch is pressed
		{
			RED = 1;
			YEL = 0
		}
		else
		{
	  		RED = 0;
			YEL = 1
		}

	}
	return 1;
}

How can I do this in WinAvr ?

I now that I can do this as follows in WinAvr and for any C compiler:

#define SET_B(x) |= (1<<x) 
#define CLR_B(x) &=~(1<<x) 
#define INV_B(x) ^=(1<<x) 


PORTB SET_B(5); 		//set PORTB.5
PORTB CLR_B(2); 		//clear PORTB.5
PORTB INV_B(6); 		//invert PORTB.5

But it seems a bit complicated to use this when I need to define many ports by name:

Please tell, what is the easy way to this in WinAvr !

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You've cross posted this in two forum - see my answer in the GCC forum here:

https://www.avrfreaks.net/index.p...

(but I'm sure the mods here will frown on cross posting!)

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[GCC Forum Moderator]
Yes, they do.
Please try and post once in the appropriate forum. It makes it easier on everyone to answer and to follow the discussion.

Thanks
Eric