macro pointer?

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

Hi,

Question: if you have a macro like this:

#define	OUT_TRG_LO		PORTD &= ~0x04

And you want to call it with a pointer, how do you do that?

I have 5 lines that I want to turn off after a fixed time, so I am using a struct to hold the parameters and a timer tick to count the time. Then a timer expires I need to call the macro to turn off the output line. Or is there a better way?

Thanks!

Tim Ressel
Portland, OR
timr@earthling.net

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

You can't have a pointer to a macro, nor can you "call" a macro. Macros are simple text substitution. It can sometimes be worthwhile to have code lik:

#define   OUT_TRG_LO      PORTD &= ~0x04 

void out_trg_low_f(void) {
  OUT_TRG_LO;
}

(but it's more likely that you should just have a function defined, instead of a macro.)

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

^ Yeah, if you need a pointer to something, that something needs to have an address. And only functions can be called, via pointers or not.

Could you explain what you are doing in a bit more detail? I'm a bit curious of what it is you're actually trying to achieve.

Just my guess

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

Your macro will end up as a single CBI instruction that takes 2 cycles.

Any attempt to specify port address and bit number will involve a lot of instructions and many more cycles.

You either end up with a function that takes port address and bit number
or a function that looks up these values from a 'pin number'

Arduino code uses the second approach. It is very flexible but is slow.

David.

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

I suspect what you are really asking is this:

http://www.nongnu.org/avr-libc/u...

But as others have noted you do that with a function not a macro. Probably something like:

void out_trg_lo(volatile uint8_t port, uint8_t bit) {
 *port &= ~(1 << bit);
}

int main(void) {
  out_trg_lo(&PORTC, 5);
}

But be warned that when the PORT and bit numbers are not fixed at compile time the compiler has no choice but to do the bit shifting at run time and access the PORT with LD and ST as you can see here:

out_trg_lo:
//==> void out_trg_lo(volatile uint8_t *port, uint8_t bit) {
/* prologue: function */
/* frame size = 0 */
/* stack size = 0 */
.L__stack_usage = 0
//==>  *port &= ~(1 << bit);
	movw r30,r24
	ld r20,Z
	ldi r18,lo8(1)
	ldi r19,0
	mov r0,r22
	rjmp 2f
	1:
	lsl r18
	2:
	dec r0
	brpl 1b
	com r18
	and r18,r20
	st Z,r18
	ret

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

david.prentice wrote:
It is very flexible but is slow.

Rather like ASF, then...

:lol:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Rather like ASF, then...

Rather like Arduino then...

;-)