Help with direct port syntax

I am trying to get this pulser program to be fast enough for me. I am really close using digitalWrite(4,HIGH) followed by digitalWrite(4,LOW). I can’t quite get the syntax down for using PORTD commands to speed this up. I have tried PORTD = 00010000 followed by PORTD = 00000000. What is the correct way to put pin 4 high , then low using direct port manipulation? Thanks in advance.

sdisselhorst:
I am trying to get this pulser program to be fast enough for me. I am really close using digitalWrite(4,HIGH) followed by digitalWrite(4,LOW). I can’t quite get the syntax down for using PORTD commands to speed this up. I have tried PORTD = 00010000 followed by PORTD = 00000000. What is the correct way to put pin 4 high , then low using direct port manipulation? Thanks in advance.

Are we talking about standard C language? If so…

PORTD = 0x10; // hex, equivalent to 00010000 in binary
PORTD = 0;

The ones above alter all 8 bits. To alter just one bit:

PORTD |= 0x10; // bitwise OR of that bit
PORTD &= ~0x10; // bitwise AND with inverted bits

Here’s another commonly used syntax in C:

#define MyBitNo 4 // early in the code, then later...
PORTD |= (1<<MyBitNo); // bitwise OR of that bit
PORTD &= ~(1<<MyBitNo); // bitwise AND with inverted bits

Google for a primer in C

Will this work in the Arduino IDE? If not, is there a way to use the Arduino with C?

I wanted to thank you for the guidance. This is working and solved my speed issue big time. I actually had to put a slight delay (7 microseconds) to slow it down a bit because it got too fast. So again, thanks!

Is there a reference of all port/register variables somewhere? I tried to find one, to no avail.

thanks,

RJ

the datasheet? there is also the io.h file reference. iom168.h or something for the mega168 kind of thing.

OK, now I need to pull pin 13 high and low quickly. I can’t seem to find a way to figure out this coversion from bin to hex or whatever.

I know for pin 4 it was PORTD and 0x10. What the heck is it for pin 13? I can figure out it is PORTB, but what is the other part? What would be great is if thewre was a list that showed the hex addresses for all of the pins. Any volunteers to post this?

-Thanks

is it 0x20?

sdisselhorst:
What would be great is if thewre was a list that showed the hex addresses for all of the pins.

which board are you using? here is the schematic of the diecimila - pretty good chance you're using it or a clone.

http://www.arduino.cc/en/uploads/Main/A … ematic.pdf

pin13 on the board corresponds to PB5 on the part.

Duemilanove

http://arduino.cc/en/uploads/Main/ardui … ematic.pdf

same pin layout/mapping as the diecimila. Pin 13 is still PB5.

Since it is PB5, you can use

PORTB |= (1 << 5);

to set it (yes, it’s the same as PORTB |= 0x20 but may be a bit easier to understand - it should compile to the same code).

PORTB &= ~(1<<5);

to clear it. Change the 5 to any number between 0 and 7 to affect bit 0 to 7 of the port. To set multiple bits, you can use

PORTB |= (1 << 5) | (1 << 6) | (1 << 7); 
```for example. 

/mike

There’s an avr-libc macro that does this for you.

The process of saying stuff like PORTB |= (1 << 5) is fine, however, avr-libc defines the _BV() macro for that. Your statement then becomes PORTB |= _BV(5). Reason why this is available is because it makes the code easier to read.

http://www.nongnu.org/avr-libc/user-man … faq_use_bv