Help with direct port syntax

Your source for all things Atmel.

Moderator: phalanx

Post Reply
sdisselhorst
Posts: 31
Joined: Sat Feb 06, 2010 10:04 pm

Help with direct port syntax

Post by sdisselhorst » Fri Mar 26, 2010 6:32 pm

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.

stevech
Support Volunteer
Posts: 2907
Joined: Sat Jun 05, 2004 8:51 pm
Location: USA, California

Re: Help with direct port syntax

Post by stevech » Fri Mar 26, 2010 6:57 pm

sdisselhorst wrote: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...

Code: Select all

PORTD = 0x10; // hex, equivalent to 00010000 in binary
PORTD = 0;
The ones above alter all 8 bits. To alter just one bit:

Code: Select all

PORTD |= 0x10; // bitwise OR of that bit
PORTD &= ~0x10; // bitwise AND with inverted bits
Here's another commonly used syntax in C:

Code: Select all

#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

sdisselhorst
Posts: 31
Joined: Sat Feb 06, 2010 10:04 pm

Re: Help with direct port syntax

Post by sdisselhorst » Fri Mar 26, 2010 7:28 pm

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

sdisselhorst
Posts: 31
Joined: Sat Feb 06, 2010 10:04 pm

Re: Help with direct port syntax

Post by sdisselhorst » Sat Mar 27, 2010 7:41 am

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!

ruurdjan
Posts: 14
Joined: Tue Feb 23, 2010 1:53 am

Re: Help with direct port syntax

Post by ruurdjan » Thu Apr 01, 2010 5:17 am

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

thanks,

RJ

Liencouer
Posts: 155
Joined: Fri Mar 28, 2008 7:36 am

Re: Help with direct port syntax

Post by Liencouer » Fri Apr 02, 2010 4:53 am

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

sdisselhorst
Posts: 31
Joined: Sat Feb 06, 2010 10:04 pm

Re: Help with direct port syntax

Post by sdisselhorst » Sun Apr 04, 2010 9:32 am

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

sdisselhorst
Posts: 31
Joined: Sat Feb 06, 2010 10:04 pm

Re: Help with direct port syntax

Post by sdisselhorst » Sun Apr 04, 2010 7:17 pm

is it 0x20?

Liencouer
Posts: 155
Joined: Fri Mar 28, 2008 7:36 am

Re: Help with direct port syntax

Post by Liencouer » Tue Apr 06, 2010 4:51 am

sdisselhorst wrote: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.

sdisselhorst
Posts: 31
Joined: Sat Feb 06, 2010 10:04 pm

Re: Help with direct port syntax

Post by sdisselhorst » Thu Apr 08, 2010 1:41 pm

Duemilanove

Liencouer
Posts: 155
Joined: Fri Mar 28, 2008 7:36 am

Re: Help with direct port syntax

Post by Liencouer » Thu Apr 08, 2010 3:20 pm

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

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

n1ist
Support Volunteer
Posts: 1043
Joined: Wed Mar 22, 2006 11:02 am

Re: Help with direct port syntax

Post by n1ist » Thu Apr 08, 2010 4:53 pm

Since it is PB5, you can use

Code: Select all

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).

Code: Select all

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

Code: Select all

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

/mike

OrlandoArias
Posts: 23
Joined: Sun Jan 10, 2010 1:22 pm
Location: The Wired 2.0

Re: Help with direct port syntax

Post by OrlandoArias » Thu Apr 08, 2010 8:44 pm

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
C code. C code run. Run, code run... Please?

Post Reply