Author |
Message |
State Machine
Janitor


Joined: Apr 17, 2006 Posts: 2810 Location: New York
Audio files: 24
|
Posted: Thu Dec 20, 2012 5:57 am Post subject:
Working the Arduino I/O Ports in BYTE chunks |
 |
|
In case you need to work with more than just one PORT bit at a time, here is some good information on I/O PORT manipulation on the Arduino development platform.
http://arduino.cc/en/Reference/PortManipulation
This is also applicable to the read or write of any of the Atmega MCU hardware control registers.
For example, here is a section of code below I needed to write to control TIMER 5 of an Atmega1280 chip on the Arduino MEGA board to output a 2500 Hz, 500 ns pulse on an I/O port bit. You will need to reference the chip data sheet in order to understand what the actual data does:
// timer/counter 5 setup
TCCR5A = B00101001; // Phase and frequency correct PWM change at OCRA
TCCR5B = B00010001; // System clock Select (16 Mhz)
OCR5A = 3200; // Set to 2500 Hz
OCR5C = 4; // Init to .125 % (500 ns PW)
Hope this helps !
Happy Holidays !
Bill |
|
Back to top
|
|
 |
elmegil

Joined: Mar 20, 2012 Posts: 2179 Location: Chicago
Audio files: 16
|
Posted: Thu Dec 20, 2012 6:39 am Post subject:
|
 |
|
Thanks! |
|
Back to top
|
|
 |
State Machine
Janitor


Joined: Apr 17, 2006 Posts: 2810 Location: New York
Audio files: 24
|
Posted: Thu Dec 20, 2012 7:09 am Post subject:
|
 |
|
Your very welcome. I have seen this question come up here and there and though it was useful information. I have an example of a FIFO memory read routine I wrote and will post that as another example.
Sure beats reading in PORT bits one by one in a loop and assembling a byte in software (SLOW !!) when you can just read the FIFO a byte at a time and get on with processing the data.
Bill |
|
Back to top
|
|
 |
cappy2112

Joined: Dec 24, 2004 Posts: 2490 Location: San Jose, California
Audio files: 2
G2 patch files: 1
|
Posted: Mon Jan 07, 2013 9:59 pm Post subject:
|
 |
|
State Machine wrote: | Your very welcome. I have seen this question come up here and there and though it was useful information. I have an example of a FIFO memory read routine I wrote and will post that as another example.
Sure beats reading in PORT bits one by one in a loop and assembling a byte in software (SLOW !!) when you can just read the FIFO a byte at a time and get on with processing the data.
Bill |
In case it's not obvious to others, the direct port I/O is much faster than digitalWrite(). I had posted some scope screenshots in an Arduino forum on another site, but I forget the actual speed differences. _________________ Free Tibet. Release the Panchen Lama from prison. Let the Dalai Lama return to his home. |
|
Back to top
|
|
 |
State Machine
Janitor


Joined: Apr 17, 2006 Posts: 2810 Location: New York
Audio files: 24
|
Posted: Tue Jan 08, 2013 8:17 am Post subject:
|
 |
|
The bottom line here is that if digitalWrite() is "real time" enough and you dont care that the bits being manipulated are NOT time aligned, then use it. If speed is a requirment, then use direct port manipulation so long as the bit masking process involved, if required, while operating on single/multiple port bits does not bog your process down.
Bill |
|
Back to top
|
|
 |
|