Page 1 of 1

Help with direct port syntax

Posted: Fri Mar 26, 2010 6:32 pm
by 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.

Re: Help with direct port syntax

Posted: Fri Mar 26, 2010 6:57 pm
by stevech
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

Re: Help with direct port syntax

Posted: Fri Mar 26, 2010 7:28 pm
by sdisselhorst
Will this work in the Arduino IDE? If not, is there a way to use the Arduino with C?

Re: Help with direct port syntax

Posted: Sat Mar 27, 2010 7:41 am
by sdisselhorst
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!

Re: Help with direct port syntax

Posted: Thu Apr 01, 2010 5:17 am
by ruurdjan
Is there a reference of all port/register variables somewhere? I tried to find one, to no avail.

thanks,

RJ

Re: Help with direct port syntax

Posted: Fri Apr 02, 2010 4:53 am
by Liencouer
the datasheet? there is also the io.h file reference. iom168.h or something for the mega168 kind of thing.

Re: Help with direct port syntax

Posted: Sun Apr 04, 2010 9:32 am
by sdisselhorst
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

Re: Help with direct port syntax

Posted: Sun Apr 04, 2010 7:17 pm
by sdisselhorst
is it 0x20?

Re: Help with direct port syntax

Posted: Tue Apr 06, 2010 4:51 am
by Liencouer
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.

Re: Help with direct port syntax

Posted: Thu Apr 08, 2010 1:41 pm
by sdisselhorst
Duemilanove

Re: Help with direct port syntax

Posted: Thu Apr 08, 2010 3:20 pm
by Liencouer
http://arduino.cc/en/uploads/Main/ardui ... ematic.pdf

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

Re: Help with direct port syntax

Posted: Thu Apr 08, 2010 4:53 pm
by n1ist
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

Re: Help with direct port syntax

Posted: Thu Apr 08, 2010 8:44 pm
by OrlandoArias
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