Yahoo Groups archive

AVR-Chat

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

Thread

C Question in AVR GCC and codevision

C Question in AVR GCC and codevision

2007-06-17 by kernels_nz

Hi guys,

I played with AVR GCC over the weekend a bit to try and optimize some
code I had written in Codevision that wasnt executing fast enough to
my liking. As a matter of interest, with just very minor but clearly
important optimizations, I took code that took 57us to execute @ 10MHz
down to 24us in codevision, and then by porting it to AVR GCC I got it
down to approx. 11us, awesome ! 

But . . . Is there a easy way in AVR GCC to turn a port pin on / off
based on the value of a variable.

Example:

CODEVISION:

unsigned char random_value; 

while(1){

  random_value = Get_Random_Value();  //returns 1 or 0 "randomly"
  PORTA.1 = random_value;
}

that compliles and works fine in codevision, but in AVR GCC ive had to
do the following:

unsigned char random_value;

while(1){

  random_value = Get_Random_Value();
  if (random_value) sbi(PORTA, PA1);
  else cbi(PORTA, PA1);
}

was just wondering if there is a cleaner way to accomplish this
without using a if.

Thanks 
Hein B
Auckland, New Zealand.

RE: [AVR-Chat] C Question in AVR GCC and codevision

2007-06-17 by larry barello

PORTA &= _BV(PA1);
If (random value)
	PORTA |= _BV(PA1);

You can define PORTA as a bit-field and have the compiler do the work for
you.

// ----------------- Header info...
Typedef struct
{
	Uint8_t bit0:1;
	Uint8_t bit1:1;
	Uint8_t bit2:1;
	Uint8_t bit3:1;
	Uint8_t bit4:1;
	Uint8_t bit5:1;
	Uint8_t bit6:1;
	Uint8_t bit7:1;
}
Bits_t;

Typedef struct
{
	Bits_t PIN;
	Bits_t DDR;
	Bits_t PORT;
}
Port_t;

#define A = (*(Port_t *)0x39);

// ---------------- Code...

A.PORT.bit1 = (bool)Get_Random_value();

---------------------------------------------

But sometimes that obscures what is going on.  I explicitly manage bit
sets/clears so it is clear what is happening: 

Static inline void SomeHardwareFunction(bool bState)
{
	If (bState)
		PORTA |= _BV(PA1);
	Else
		PORTA &= ~_BV(PA1);
}

The compiler will in-line the above as optimal cbi/sbi instruction (if
possible), or lds/andi/ori/sts sequence.  Very efficient, very explicit and
I make a function for each hardware bit I am twiddling (or data base bit, or
whatever).


Cheers!
Show quoted textHide quoted text
-----Original Message-----
From: AVR-Chat@yahoogroups.com [mailto:AVR-Chat@yahoogroups.com] On Behalf
Of kernels_nz
Sent: Sunday, June 17, 2007 2:43 PM
To: AVR-Chat@yahoogroups.com
Subject: [AVR-Chat] C Question in AVR GCC and codevision

Hi guys,

I played with AVR GCC over the weekend a bit to try and optimize some
code I had written in Codevision that wasnt executing fast enough to
my liking. As a matter of interest, with just very minor but clearly
important optimizations, I took code that took 57us to execute @ 10MHz
down to 24us in codevision, and then by porting it to AVR GCC I got it
down to approx. 11us, awesome ! 

But . . . Is there a easy way in AVR GCC to turn a port pin on / off
based on the value of a variable.

Example:

CODEVISION:

unsigned char random_value; 

while(1){

  random_value = Get_Random_Value();  //returns 1 or 0 "randomly"
  PORTA.1 = random_value;
}

that compliles and works fine in codevision, but in AVR GCC ive had to
do the following:

unsigned char random_value;

while(1){

  random_value = Get_Random_Value();
  if (random_value) sbi(PORTA, PA1);
  else cbi(PORTA, PA1);
}

was just wondering if there is a cleaner way to accomplish this
without using a if.

Thanks 
Hein B
Auckland, New Zealand.



 
Yahoo! Groups Links

Re: [AVR-Chat] C Question in AVR GCC and codevision

2007-06-18 by Dave Hylands

On 6/17/07, larry barello <yahoo@barello.net> wrote:
>
> PORTA &= _BV(PA1);
> If (random value)
>         PORTA |= _BV(PA1);
>
> You can define PORTA as a bit-field and have the compiler do the work for
> you.

It seems to me that if ( random value) is going to be 1 except when
random value is 0.

I expect that what you really want is something like combining a bit
field and using

PORTA.bit0 = randomValue & 1;

-- 
Dave Hylands
Vancouver, BC, Canada
http://www.DaveHylands.com/

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.