SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By sdisselhorst
#97106
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.
By stevech
#97108
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
By sdisselhorst
#97131
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!
By sdisselhorst
#97737
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
By n1ist
#98032
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
By OrlandoArias
#98051
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