Reference.PortManipulation History

Hide minor edits - Show changes to output

January 17, 2010, at 04:24 PM by Paul Badger -
Changed lines 8-12 from:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[http://www.arduino.cc/en/Hacking/PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[http://www.arduino.cc/en/Hacking/PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports. The newer Atmega328p chip follows the pinout of the Atmega168 exactly.

January 17, 2010, at 04:22 PM by Paul Badger -
Changed lines 37-38 from:
Referring to the pin map above, the PortD registers control Arduino digital pins 0 � 7.
to:
Referring to the pin map above, the PortD registers control Arduino digital pins 0 to 7.
Changed lines 43-44 from:
[@DDRD = B11111110; // sets Arduino pins 1 � 7 as outputs, pin 0 as input
DDRD = DDRD | B11111100; // this is safer � it sets pins 2 to 7 as outputs
to:
[@DDRD = B11111110; // sets Arduino pins 1 to 7 as outputs, pin 0 as input
DDRD = DDRD | B11111100; // this is safer as it sets pins 2 to 7 as outputs
January 17, 2010, at 04:18 PM by Paul Badger -
January 17, 2010, at 04:17 PM by Paul Badger -
November 01, 2009, at 05:03 PM by Paul Badger -
Changed lines 81-84 from:
to:
!!!See

*[[http://arduino.cc/en/Hacking/Atmega168Hardware| Pin Mapping of Atmega 168/328]]
May 24, 2009, at 03:50 PM by Paul Badger -
Added lines 76-77:
*Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;
Changed line 80 from:
*Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;
to:
May 24, 2009, at 03:50 PM by Paul Badger -
Changed lines 57-59 from:
PINB is the input register variable � it will read all of the digital input pins at the same time.

to:
PINB is the input register variable It will read all of the digital input pins at the same time.

Added lines 74-75:
*You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer clock cycles.
Changed lines 79-80 from:

*You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer clock cycles.
to:
May 24, 2009, at 03:48 PM by Paul Badger -
May 24, 2009, at 03:47 PM by Paul Badger -
Changed lines 16-19 from:
to:
-->DDRD - The Port D Data Direction Register - read/write
-->PORTD - The Port D Data Register - read/write
-->PIND - The Port D Input Pins Register - read only
Changed lines 22-25 from:
to:
-->DDRB - The Port B Data Direction Register - read/write
-->PORTB - The Port B Data Register - read/write
-->PINB - The Port B Input Pins Register - read only
Changed lines 27-31 from:
to:
-->DDRC - The Port C Data Direction Register - read/write
-->PORTC - The Port C Data Register - read/write
-->PINC - The Port C Input Pins Register - read only

Changed lines 37-38 from:
to:
Referring to the pin map above, the PortD registers control Arduino digital pins 0 � 7.
Changed lines 43-44 from:
to:
[@DDRD = B11111110; // sets Arduino pins 1 � 7 as outputs, pin 0 as input
DDRD = DDRD | B11111100; // this is safer � it sets pins 2 to 7 as outputs
Changed lines 57-59 from:
to:
PINB is the input register variable � it will read all of the digital input pins at the same time.

April 29, 2008, at 02:14 PM by Paul Badger -
Changed lines 70-71 from:
*It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111110; above mentions that it must leave pin 0 as an input pin. Pin 0 is the receive line (RX) on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
to:
*It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111110; above mentions that it must leave pin 0 as an input pin. Pin 0 is the receive line (RX) on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 0 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
April 29, 2008, at 02:13 PM by Paul Badger -
Changed lines 70-71 from:
*It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111101; above mentions that it must leave pin 1 as an input pin. Pin 1 is the receive line on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
to:
*It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111110; above mentions that it must leave pin 0 as an input pin. Pin 0 is the receive line (RX) on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
October 15, 2007, at 11:52 AM by Paul Badger -
Changed lines 8-12 from:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[http://www.arduino.cc/en/Hacking/PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



October 15, 2007, at 11:51 AM by Paul Badger -
Changed lines 8-12 from:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping| ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



October 15, 2007, at 11:49 AM by Paul Badger -
Changed lines 8-12 from:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping| ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



October 15, 2007, at 11:48 AM by Paul Badger -
Changed lines 8-12 from:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping | ATmega8]]and[[Atmega168Hardware | ATmega168]] chips show the ports.



to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping | ATmega8]] and [[Atmega168Hardware | ATmega168]] chips show the ports.



October 15, 2007, at 11:47 AM by Paul Badger -
Changed lines 8-9 from:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins.
to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins set to input with pinMode(). The maps of the [[PinMapping | ATmega8]]and[[Atmega168Hardware | ATmega168]] chips show the ports.



October 11, 2007, at 02:51 PM by Paul Badger -
Changed lines 8-9 from:
Each port is controlled by three registers, which are defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins.
to:
Each port is controlled by three registers, which are also defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins.
October 11, 2007, at 02:49 PM by Paul Badger -
Changed lines 8-9 from:
Each port is controlled by three registers, which are defined variables in the arduino language. The DDR registers, and PORT registers may be both written to, and read. PIN registers correspond to the state of inputs and may only be read.
to:
Each port is controlled by three registers, which are defined variables in the arduino language. The DDR register, determines whether the pin is an INPUT or OUTPUT. The PORT register controls whether the pin is HIGH or LOW, and the PIN register reads the state of INPUT pins.

DDR and PORT registers may be both written to, and read. PIN registers correspond to the state of inputs and may only be read.
Changed lines 15-16 from:
to:
October 11, 2007, at 02:45 PM by Paul Badger -
Changed line 21 from:
'''PORTC''' maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini\\
to:
'''PORTC''' maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini
October 11, 2007, at 02:44 PM by Paul Badger -
Changed lines 8-9 from:
Each port is controlled by three registers, which are defined variables in the arduino language
to:
Each port is controlled by three registers, which are defined variables in the arduino language. The DDR registers, and PORT registers may be both written to, and read. PIN registers correspond to the state of inputs and may only be read.
Changed lines 13-14 from:
to:
Changed lines 19-20 from:
to:
Changed lines 24-28 from:
to:
October 11, 2007, at 02:40 PM by Paul Badger -
Changed lines 21-22 from:
'''PORTC''' maps to Arduino analog pins 0 to 5.\\
->Pins 6 & 7 are only accessible on the Arduino Mini\\
to:
'''PORTC''' maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini\\
October 11, 2007, at 02:40 PM by Paul Badger -
Changed lines 21-22 from:
'''PORTC''' maps to Arduino analog pins 0 to 5 pins 6 & 7 are only accessible on the Arduino Mini\\
to:
'''PORTC''' maps to Arduino analog pins 0 to 5.\\
->Pins 6 & 7 are only accessible on the Arduino Mini\\
October 11, 2007, at 02:38 PM by Paul Badger -
Changed lines 11-29 from:
to:
October 11, 2007, at 02:36 PM by Paul Badger -
Changed lines 11-14 from:
to:
October 11, 2007, at 02:35 PM by Paul Badger -
Changed lines 12-14 from:
to:
October 11, 2007, at 02:33 PM by Paul Badger -
Changed lines 11-14 from:
to:
October 11, 2007, at 02:29 PM by Paul Badger -
Changed lines 10-14 from:
to:
October 11, 2007, at 02:26 PM by Paul Badger -
Changed lines 11-14 from:
to:
October 11, 2007, at 02:25 PM by Paul Badger -
Deleted line 10:
Changed lines 12-14 from:
to:
October 11, 2007, at 02:24 PM by Paul Badger -
Changed lines 12-15 from:
to:
October 11, 2007, at 02:22 PM by Paul Badger -
October 11, 2007, at 02:21 PM by Paul Badger -
Changed lines 8-12 from:
to:
October 11, 2007, at 02:11 PM by Paul Badger -
Changed lines 8-12 from:
Each port is controlled by three registers (substitute the letter of the port for the "x"):
* DDRx sets the direction of each pin (input or output):
* PORTx sets the output of the pin (high or low)
* PINx reads the input on each pin
to:
October 11, 2007, at 02:06 PM by Paul Badger -
Changed lines 16-17 from:
Register use details
to:
!!!! Examples
Changed line 41 from:
!! Why use port manipulation?
to:
!!!! Why use port manipulation?
September 01, 2007, at 05:10 AM by David A. Mellis -
Changed lines 13-39 from:
to:
Each bit of these registers corresponds to a single pin; e.g. the low bit of DDRB, PORTB, and PINB refers to pin PB0 (digital pin 8). For a complete mapping of Arduino pin numbers to ports and bits, see the diagram for your chip: [[Hacking/PinMapping | ATmega8]], [[Hacking/PinMapping168 | ATmega168]]. (Note that some bits of a port may be used for things other than i/o; be careful not to change the values of the register bits corresponding to them.)

September 01, 2007, at 05:08 AM by David A. Mellis -
Changed lines 3-7 from:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports: B (digital pin 8 to 13), C (analog input pins), and D (digital pins 0 to 7). Each port is controlled by three registers: DDRx sets the direction of each pin (input or output), PORTx sets the output of the pin (high or low), and PINx reads the input on each pin (substitute the letter of the port for the "x"). Each bit of these registers corresponds to a single pin; e.g. the low bit of DDRB, PORTB, and PINB refers to pin PB0 (digital pin 8). For a complete mapping of Arduino pin numbers to ports and bits, see the diagram for your chip: [[Hacking/PinMapping | ATmega8]], [[Hacking/PinMapping168 | ATmega168]].



to:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports:
* B (digital pin 8 to 13)
* C (analog input pins)
* D (digital pins 0 to 7)

Each port is controlled by three registers (substitute the letter of the port for the "x"):
* DDRx sets the direction of each pin (input or output):
* PORTx sets the output of the pin (high or low)
* PINx reads the input on each pin

Each bit of these registers corresponds to a single pin; e.g. the low bit of DDRB, PORTB, and PINB refers to pin PB0 (digital pin 8). For a complete mapping of Arduino pin numbers to ports and bits, see the diagram for your chip: [[Hacking/PinMapping | ATmega8]], [[Hacking/PinMapping168 | ATmega168]].



September 01, 2007, at 05:06 AM by David A. Mellis -
Changed lines 3-7 from:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports: B (digital pin 8 to 13), C (analog input pins), and D (digital pins 0 to 7). Each port is controlled by three registers: DDRx sets the direction of each pin (input or output), PORTx sets the output of the pin (high or low), and PINx reads the input on each pin (substitute the letter of the port for the "x").



to:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports: B (digital pin 8 to 13), C (analog input pins), and D (digital pins 0 to 7). Each port is controlled by three registers: DDRx sets the direction of each pin (input or output), PORTx sets the output of the pin (high or low), and PINx reads the input on each pin (substitute the letter of the port for the "x"). Each bit of these registers corresponds to a single pin; e.g. the low bit of DDRB, PORTB, and PINB refers to pin PB0 (digital pin 8). For a complete mapping of Arduino pin numbers to ports and bits, see the diagram for your chip: [[Hacking/PinMapping | ATmega8]], [[Hacking/PinMapping168 | ATmega168]].



September 01, 2007, at 05:02 AM by David A. Mellis -
Changed lines 3-5 from:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. Each port contains up to 8 pins and is controlled by three registers: one to set the direction of each pin (input or output), one to set the output of the pin (high or low), and one to read the input on the pin (high or low).

to:
Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. The chips used on the Arduino board (the ATmega8 and ATmega168) have three ports: B (digital pin 8 to 13), C (analog input pins), and D (digital pins 0 to 7). Each port is controlled by three registers: DDRx sets the direction of each pin (input or output), PORTx sets the output of the pin (high or low), and PINx reads the input on each pin (substitute the letter of the port for the "x").



September 01, 2007, at 04:58 AM by David A. Mellis -
Changed lines 1-4 from:
!! Atmega Port Manipulation

Atmega168 Register Descriptions
to:
!!Port Registers

Port registers allow for lower-level and faster manipulation of the i/o pins of the microcontroller on an Arduino board. Each port contains up to 8 pins and is controlled by three registers: one to set the direction of each pin (input or output), one to set the output of the pin (high or low), and one to read the input on the pin (high or low).

June 28, 2007, at 03:24 AM by Paul Badger -
Changed lines 37-38 from:
// without changing the value of pins 0 & 1 @]
to:
// without changing the value of pins 0 & 1, which are RX & TX @]
June 28, 2007, at 03:23 AM by Paul Badger -
Changed lines 33-34 from:
DDRB is the direction register for Port D. The bits in this register control whether the pins in PORTD are configured as inputs or outputs so, for example:
to:
DDRD is the direction register for Port D (Arduino digital pins 0-7). The bits in this register control whether the pins in PORTD are configured as inputs or outputs so, for example:
April 24, 2007, at 05:36 PM by Paul Badger -
April 24, 2007, at 05:35 PM by Paul Badger -
Changed lines 40-42 from:
[[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]]

to:
[[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]] in the Playground

Changed lines 53-55 from:
From [[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]] in the Playground

to:
From [[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]]

April 24, 2007, at 05:34 PM by Paul Badger -
Changed lines 53-55 from:
From [[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]]

to:
From [[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]] in the Playground

April 24, 2007, at 05:33 PM by Paul Badger -
Changed lines 33-34 from:
DDRB is the direction register for Port B. The bits in this register control whether the pins in PORTB are configured as inputs or outputs so, for example:
to:
DDRB is the direction register for Port D. The bits in this register control whether the pins in PORTD are configured as inputs or outputs so, for example:
April 24, 2007, at 05:29 PM by Paul Badger -
April 24, 2007, at 02:09 PM by Paul Badger -
Changed lines 49-51 from:
to:
April 24, 2007, at 02:07 PM by Paul Badger -
Changed line 39 from:
See the bitwise operators reference pages and
to:
@@//See the bitwise operators reference pages and@@
Changed lines 45-46 from:
PORTD = B10101000; // sets digital pins 7,5,3 HIGH.
to:
@@PORTD = B10101000; // sets digital pins 7,5,3 HIGH@@
April 24, 2007, at 07:56 AM by Paul Badger -
April 24, 2007, at 07:55 AM by Paul Badger -
Changed lines 56-57 from:
Generally speaking, doing this sort of thing is not a good idea. Why not? Here are a few reasons:
to:
Generally speaking, doing this sort of thing is '''not''' a good idea. Why not? Here are a few reasons:
April 24, 2007, at 07:54 AM by Paul Badger -
Changed lines 58-63 from:
* The code is much more difficult for you to debug and maintain, and is a lot harder for other people to understand. It only takes a few microseconds for the processor to execute code, but it might take hours for you to figure out why it isn't working right and fix it! Your time is valuable, right? But the computer's time is very cheap, measured in the cost of the electricity you feed it. Usually it is much better to write code the most obvious way.

* The code is less portable. If you use digitalRead() and digitalWrite(), it is much easier to write code that will run on all of the Atmel microcontrollers, whereas the control and port registers can be different on each kind of microcontroller.

* It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111101; above mentions that it must leave pin 1 as an input pin. Pin 1 is the receive line on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
to:
*The code is much more difficult for you to debug and maintain, and is a lot harder for other people to understand. It only takes a few microseconds for the processor to execute code, but it might take hours for you to figure out why it isn't working right and fix it! Your time is valuable, right? But the computer's time is very cheap, measured in the cost of the electricity you feed it. Usually it is much better to write code the most obvious way.

*The code is less portable. If you use digitalRead() and digitalWrite(), it is much easier to write code that will run on all of the Atmel microcontrollers, whereas the control and port registers can be different on each kind of microcontroller.

*It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111101; above mentions that it must leave pin 1 as an input pin. Pin 1 is the receive line on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
Changed lines 66-67 from:
* If you are running low on program memory, you can use these tricks to make your code smaller. It requires a lot fewer bytes of compiled code to simultaneously write a bunch of hardware pins simultaneously via the port registers than it would using a for loop to set each pin separately. In some cases, this might make the difference between your program fitting in flash memory or not!
to:
*If you are running low on program memory, you can use these tricks to make your code smaller. It requires a lot fewer bytes of compiled code to simultaneously write a bunch of hardware pins simultaneously via the port registers than it would using a for loop to set each pin separately. In some cases, this might make the difference between your program fitting in flash memory or not!
April 24, 2007, at 07:54 AM by Paul Badger -
Changed lines 37-38 from:
// without changing the value of pins 0 & 1@]
to:
// without changing the value of pins 0 & 1 @]
Changed lines 62-63 from:
* It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111101; above mentions that it must leave pin 1 as an input pin. Pin 1 is the receive line on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
to:
* It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111101; above mentions that it must leave pin 1 as an input pin. Pin 1 is the receive line on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?
Changed lines 66-70 from:
* If you are running low on program memory, you can use these tricks to make your code smaller. It requires a lot fewer bytes of compiled code to simultaneously write a bunch of hardware pins simultaneously via the port registers than it would using a for loop to set each pin separately. In some cases, this might make the difference between your program fitting in flash memory or not!

* Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;

* You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer clock cycles.
to:
* If you are running low on program memory, you can use these tricks to make your code smaller. It requires a lot fewer bytes of compiled code to simultaneously write a bunch of hardware pins simultaneously via the port registers than it would using a for loop to set each pin separately. In some cases, this might make the difference between your program fitting in flash memory or not!

*Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;

*You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer clock cycles.
April 24, 2007, at 07:50 AM by Paul Badger -
Changed lines 51-52 from:
Why use port manipulation?
Arduino functions such as digitalWrite also ultimately end up manipulating these Port Variables, because they are really the only way to control the state of the pins on the Atemga chip.
to:

!! Why use port manipulation?
From [[http://www.arduino.cc/playground/Code/BitMath | The Bitmath Tutorial]]


Generally speaking, doing this sort of thing is not a good idea. Why not? Here are a few reasons:

* The code is much more difficult for you to debug and maintain, and is a lot harder for other people to understand. It only takes a few microseconds for the processor to execute code, but it might take hours for you to figure out why it isn't working right and fix it! Your time is valuable, right? But the computer's time is very cheap, measured in the cost of the electricity you feed it. Usually it is much better to write code the most obvious way.

* The code is less portable. If you use digitalRead() and digitalWrite(), it is much easier to write code that will run on all of the Atmel microcontrollers, whereas the control and port registers can be different on each kind of microcontroller.

* It is a lot easier to cause unintentional malfunctions with direct port access. Notice how the line DDRD = B11111101; above mentions that it must leave pin 1 as an input pin. Pin 1 is the receive line on the serial port. It would be very easy to accidentally cause your serial port to stop working by changing pin 1 into an output pin! Now that would be very confusing when you suddenly are unable to receive serial data, wouldn't it?

So you might be saying to yourself, great, why would I ever want to use this stuff then? Here are some of the positive aspects of direct port access:

* If you are running low on program memory, you can use these tricks to make your code smaller. It requires a lot fewer bytes of compiled code to simultaneously write a bunch of hardware pins simultaneously via the port registers than it would using a for loop to set each pin separately. In some cases, this might make the difference between your program fitting in flash memory or not!

* Sometimes you might need to set multiple output pins at exactly the same time. Calling digitalWrite(10,HIGH); followed by digitalWrite(11,HIGH); will cause pin 10 to go HIGH several microseconds before pin 11, which may confuse certain time-sensitive external digital circuits you have hooked up. Alternatively, you could set both pins high at exactly the same moment in time using PORTB |= B1100;

* You may need to be able to turn pins on and off very quickly, meaning within fractions of a microsecond. If you look at the source code in lib/targets/arduino/wiring.c, you will see that digitalRead() and digitalWrite() are each about a dozen or so lines of code, which get compiled into quite a few machine instructions. Each machine instruction requires one clock cycle at 16MHz, which can add up in time-sensitive applications. Direct port access can do the same job in a lot fewer clock cycles.
April 24, 2007, at 07:47 AM by Paul Badger -
Changed lines 29-30 from:
to:
Changed lines 35-39 from:
to:
Changed lines 45-48 from:
PORTB = B10101000; // sets digital pins 7,5,3 HIGH.

You will only see 5 volts on these pins however if the pins have been set as outputs using the DDRB register or with pinMode().
to:
PORTD = B10101000; // sets digital pins 7,5,3 HIGH.

You will only see 5 volts on these pins however if the pins have been set as outputs using the DDRD register or with pinMode().
April 24, 2007, at 07:40 AM by Paul Badger -
Added lines 5-6:
'''PORTD maps to Arduino digital pins 0 to 7'''
Changed lines 11-14 from:
'''PORTD maps to Arduino digital pins 0 to 7'''


to:


'''PORTB maps to Arduino digital pins 8 to 13'''
The two high bits (6 & 7) map to the crystal pins and are not usable
Changed lines 20-21 from:
'''PORTD maps to Arduino digital pins 0 to 7'''
to:
'''PORTC maps to Arduino analog pins 0 to 5'''
pins 6 & 7 are only accessible on the Arduino Mini
April 24, 2007, at 07:35 AM by Paul Badger -
Changed lines 9-12 from:
PORTD maps to Arduino digital pins 0 to 7


to:
'''PORTD maps to Arduino digital pins 0 to 7'''


Changed lines 17-18 from:

to:
'''PORTD maps to Arduino digital pins 0 to 7'''
April 24, 2007, at 07:34 AM by Paul Badger -
Deleted lines 4-7:
Added lines 9-18:
April 24, 2007, at 07:33 AM by Paul Badger -
Changed lines 9-10 from:
to:
Changed lines 13-14 from:
to:
Changed line 28 from:
// the the bitwise operators reference
to:
// see the bitwise operators reference
April 24, 2007, at 07:31 AM by Paul Badger -
Changed lines 27-28 from:
// without changing the value of pins 0 & 1
// the the bitwise operators reference
to:
// without changing the value of pins 0 & 1
// the the bitwise operators reference
April 24, 2007, at 07:30 AM by Paul Badger -
Added lines 1-39:

Share