Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
323 views
11 pages
Arduino Port Manipulation
direct port accest
Uploaded by
uqink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Download
Save
Save Arduino Port Manipulation For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
0 ratings
0% found this document useful (0 votes)
323 views
11 pages
Arduino Port Manipulation
direct port accest
Uploaded by
uqink
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here
.
Available Formats
Download as PDF or read online on Scribd
Carousel Previous
Carousel Next
Download
Save
Save Arduino Port Manipulation For Later
Share
0%
0% found this document useful, undefined
0%
, undefined
Print
Embed
Report
Download
Save Arduino Port Manipulation For Later
You are on page 1
/ 11
Search
Fullscreen
1052015 “Tora: Arcino Port Manipulation tronixstuff fun and learning with electronics ETN na Ua ee Home Tronixlabs Arduino Tutorials + KitReviews Categorized | arduino, education, lesson, microcontrollers, port manipulation, tronixstuff, tutorial Tutorial: Arduino Port Manipulation Posted on 22 October 2011. Tags: arduino, DDRB, DDRC, example, lesson, lessons, manipulation, PIND, port, PORTB, PORTC, PORTD, registers, tronixstuff, tutorial, tutorials Control Arduino /O pins faster and with less code in chapter forty-three of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall —a series of articles on the Arduino universe. [Updated 19/01/13] In this article we are going to revisit the I/O pins, and use what is called “Port Manipulation” to control them in a much faster manner than using digitalWrite()/digitalRead(). Why? Speed! Using this method allows for much faster I/O control, and we can control or read groups of /O pins simultaneously, not one at a time; Memory! Using this method reduces the amount of memory your sketch will use. Once again I will try and keep things as simple as possible. This article is written for Arduino boards that use the ATmegal68 or ATmega328 microcontrollers (used in Arduino Duemilanove/Uno, Ereetronies Eleven/EtherTen, etc). prone tut com 2011/1022"uera-erdkine-port- manipulation | 20ro192015 “Tutorial Arkin Port Manipulation First, we'll use the I/O as outputs. There are three port registers that we can alter to set the status of the digital and analogue I/O pins. A port register can be thought of as a special byte variable that we can change which is read by the microcontroller, therefore controlling the state of various I/O ports. We have three port registers to work with: + D~ for digital pins seven to zero (bank D) + B= for digital pins thirteen to eight (bank B) * C- for analogue pins five to zero (bank ... C!) Register C can control analogue pins seven to zero if using an Arduino with the TQEP style of ATmega328, such as the Nano or Freetronics EtherTen). For example: a - It is very simple to do so. In void setup(, we use 1 DDRy = Brooowex where y is the register type (B/C/D) and xxxxxxxx are eight bits that determine if a pin is to be an input or output, Use 0 for input, and 1 for output. The LSB (least-significant bit [the one on the right!]) is the lowest pin number for that register. Next, to control a bank of pins, use 1 PORTY = Bxxxxxxxx where y is the register type (B/C/D) and xxxxxxxx are eight status bits — 1 for HIGH, 0 for LOW. This is demonstrated in the following example: // Example 43.1 2 // tronixstuff.con/tutorials > chapter 43 3. // John Boxall - October 2011 4 // Digital 0-7 set to outputs, then on/off using port manipulation 6 void setupQ 7 8 DDRD = B11111111; // set PORTD (digital 7-2) to outputs 9 } 10 11 void Loop 2 13. PORTD = B11110000; // digital 4~7 HIGH, digital 3-0 LOW 14 delay(1000); 15 PORTD = 80001111; // digital 4-7 LON, digital 3-0 HIGH 16 — delay(100@); peas tu com 2011/1022tuera-erdine-port-manipustion | 220102015 “Tora: Arcino Port Manipulation wy It sets digital pins 7~0 to output in void setup(). Then it alternates turning on and off alternating halves of digital pins 0~7. At the start I mentioned that using port manipulation was a Jot faster than using regular Arduino /O functions. How fast? To test the speed of port manipulation vs. using digitalWrite(), we will use the following circuit: Arduino and analyse the output at digital pins zero and seven using a digital storage oscilloscope. Our first test sketch turns on and off digital pins 0~7 without any delay between PORTD commands — in other words, as fast as possible, The sketch: // Example 43.1.1 // tronixstuff.com/tutorials > chapter 43 // John Boxall - October 2011 // Digital @-7 set to outputs, then on/off using port manipulation void setup() £ DDRD = B11111111; // set PORTD (digital 7-0) to outputs 3 void Loop) { PORTD = 811111111; PORTD = 800000000; } In the image below, digital zero is channel one, and digital seven is channel three: prone tut com 2011/1022"uera-erdkine-port- manipulation | 201052015 “Tutorial: Arno Port Mariplation Dg ada MOTE Fi 2121 ret oon) Desemart rt rere S Wow —check the frequency measurements — 1.1432 MHz! Interesting to note the longer duration of time when the pins are low vs. high. [Update] Well it turns out that the extra time in LOW includes the time for the Arduino to go back to the top of void loop(). This can be demonstrated in the following sketch. We turn the pins on and off five times instead of once: // Example 43.1.2 2 // tronixstuff.com/tutorials > chapter 43 3 // John Boxall - October 2011 void setup) { DDRD = B11111111; // set PORTD (digital 7-0) to outputs 8} 10 void Loop) nf 12 pORTD = 811111111; 13 PORTD = 800000000; 14 PORTD = 811111111; 15 PORTD = 800000000; 16 PORTD = 811111111; 1? PORTD = 800000000; 18 PORTD = 811111111; 19 PORTD = 800000000; 9 PORTD = 811111111; PORTD ~ 80000000; 2} And the results from the MSO. You can see the duty cycle is much closer to 50% until the end of the sketch, at which point around 660 nanoseconds is the time used between the end of the last LOW period and the start of the next HIGH: prone tut com 2011/1022"uera-erdkine-port- manipulation | 4201052015 “Tutorial: Ardino Port Marvpltion rere) oo) ee Next we do it the normal way, using this sketch 77 Example 43.2 2 // tronixstuff.con/tutorials > chapter 43 3. // John Boxall - October 2011 // Digital 07 set to outputs, then on/off using digitalWritec) 6 void setupC) i tor Girt a0; a<8; a++) o | pinModeCa, OUTPUT); By? B 14 void loop © sor Cint 4-0; a
chapter 43 3. // John Boxall - October 2011 4 // Digital 0-7 set to outputs, then on/off using individual digitalWriteC) 6 void setupc) t 8 for Cint a-@; a<8; a+) 9 10 pinModeCa, OUTPUT); 1} 12 } 14 void Loop) { 16 — digitalWrite(o, HIGH); digitalWrite(1, HIGH); 18 digitalWrite(2, HIGH); 19 digitalWrite(3, HIGH); digitalWritec4, HIGH); digitalWrite(s, HIGH); digitalWritecé, HIGH); digitalWrite(7, HIGH; 24 —digitalWriteCo, Low); 5 digitalWritec1, Lon); 6 digitalWrite(2, Lon); digitalWriteC3, LON); 8 digitalWriteC4, Lon); 2 digitalWrite(s, LON); 30 digitalWrite(6, LOW); 31 digitalWrite(7, LOW); and the results: prone tut com 2011/1022"uera-erdkine-port- manipulation | 201052015 “Tutorial: Arno Port Mariplation MEO Fe 21 211523. rey oral ee coc Perera rt ‘A small speed boost, the frequency has increased to 14,983 kHz. Hopefully you can now understand the benefits of using port manipulation, However there are a few things to take note of: * You can’t control digital pins 0 and 1 (in bank D) and use the serial monitor/port. For example if you set pin zero to output, it can’t receive datal + Always document your sketch — take pity on others who may need to review it later on and become puzzled about wehich bits are controlling or reading what! ‘Now to waste some electron flows by blinking LEDs. Using the circuit described earlier, the following sketch will create various effects for someone’s enjoyment. Arduino 2 // tronixstutt.con/tutorials > chapter 43 // John Boxall - October 2011 // Fun with & LEDs on digital 7-0 void setup() 7 8 DDRD = B11111111; // set PORTD (digital 7-0) 9 // to output 10 } ces 12 byte a = B11111111; 13 byte b = Bo0000001; 14 byte c = 810000000; 15 byte e = B10101010; 16 1? void krider 18 ft 19 for Cint k=O; ke5; k++) ef for Cint 2-0; z
> z; 30 delay(10e); 31 + 32} 33 3 34 35 void ondFFC 36 37 for Cint kaO; kel0; k++) 38 39 PORTD = a; 40 deLay(100) ; 41 PORTD = @) 42 deLay100); 3} 44} 45 46 void invalinkO a7 48 for Cint 2-0; z<10; z++) 4 50 PORTD = e; Si delay(10@); 52 PORTD = ~e; 53 _ delay(100); 54} 5S } 56 57 void binaryCount() 58 { 59 for Cint 2-0; 2<256; 2+) 60 { 61 PORTD = z; 62 deLay(100); 63 } 64 PORTD=0; 65 } 66 67 void Loop) 68 ft 69 invBLinkQ; 70 deLay(500); 7. binaryCount(); 72 deLay(500); 73 kriderO3 74 deLay(500); 75 ondFFO; 76 } And here it is in real life: hpstronestutcom2011/1022%ueral-ardine-port- manipulation |1052015 “Tora: Arcino Port Manipulation Example 43.4 Now to use the 1/0 pins as inputs. Again, it is very simple to do so. In void setup(), we use 1 DDRy = Bxxxxxxxx where y is the register type (B/C/D) and xxxxxxxx are eight bits that determine if a pin is to be an input or output. Use 0 for input. The LSB (least-significant bit [the one on the right!]) is the lowest pin number for that register. Next, to read the status of the pins we simply read the byte: 1 PINy where y is the register type (B/C/D). So if you were using port B as inputs, and digital pins 8~10 were high, and 11~13 were low, PINB would be equal to BO000011. Really, that’s it! ‘Now for another demonstration using both inputs and outputs. We will use a push-wheel switch from Chapter 40 on our inputs (digital pins 8~11), and a seven segment LED display for output (on digtal pins 7-0 ~ segments dp then a~f). The following sketch reads the input from the switch, which returns 0~9 in binary-coded decimal. This value is then used in the function void disp() to retrieve the matching byte from the array “segments”, which contains the appropriate outputs to drive the seven segment LED display unit, Here is the sketch: // Example 43.5 // tronixstuff.com/tutorials > chapter 43 7/ John Boxall - October 2011 4 // inputs and outputs 6 byte segmentst] = 7 BO1111110, 80110000, B@1101101, BO1111001, 84110011, BO1011011, 801011111, Be1110000, 8 // digital pins 7~@ connected to display pins dp,a~g 9 void setupQ) of 11 DDRB = BO@000000; // set PORTB (digital 13~8) to inputs 12 DDRD = B11111111; // set PORTD (digital 7~®) to outputs 13) 14 15 void dispCint z) peas tu com 2011/1022tuera-erdine-port-manipustion | 920s0s20:5 “Tutorial: Arun Port Mariplaton 16 { 17 PORTD = segnents[z]; 18 } 19 20 void Loop() a. { 22 disp(PINB); 23 deLay(100); 24} And the ubiquitous demonstration video Example 43.5 By now I hope you have an understanding of using port manipulation for your benefit. With a little effort your sketches can be more efficient in terms of speed and memory space, and also allow nifiy simultaneous reading of input pins. Have fun and keep checking into tronixstuff.com. Why not follow things on twitter, Google+, subscribe for email updates or RSS using the links on the right-hand column, or join our Google Group — dedicated to the projects and related items on this website. Sign up — it’s free, helpful to each other — and we can all learn something Bio Latest Posts John Boxall Founder, owner and managing editor of tronixstuff.com. pstronestutcom2011/1022%ueral-arduine-port- manipulation | 10201082015 “Tora: Arcino Port Manipulation Please shure with others: = 6 S Like this: ke Like Bo the frst to Ike this October 2011 Competition Review ~ Agilent Infi ion M: 3024A Mixed Signal Oscilloscope — 15 Responses to “Tutorial: Arduino Port Manipulation” les 1 Fritz Charleston (@fritzcharleston) says: October 22, 2011 at 4:45 pm Ifyou fine people can wrap your heads around this, then congratulations, your 100% closer to standalone AVR development! Be wary of using PC6, this also controls the " Also, here is wrong with i Binary | Hex 0000/0 0001 |1 0010 |2 0011 [3 0100 |4 o101|5 0110 [6 Ol |7 1000 |8 1001 |9 1010 1011 |B 1100|C 1101 |D peas tut com 2011/1022"uera-erdine-port- manipulation | set button.” It can be used, you take note. me haxadecimal shorthand to take care of stuff like “00000011.” (Not that anything just a preference thing.) W209
You might also like
Arduino - Controllers
PDF
No ratings yet
Arduino - Controllers
158 pages
Ch07 AVR Programming in C
PDF
100% (4)
Ch07 AVR Programming in C
54 pages
EXPERIMENT 2 - GPIO Programming in AVR - Dhyana
PDF
No ratings yet
EXPERIMENT 2 - GPIO Programming in AVR - Dhyana
12 pages
Programming PIC Microcontrollers in C
PDF
100% (4)
Programming PIC Microcontrollers in C
126 pages
Lec3 - Digital Inputs, Outputs, PWM
PDF
No ratings yet
Lec3 - Digital Inputs, Outputs, PWM
53 pages
I/O Ports in AVR Microcontrollers: Sepehr Naimi
PDF
No ratings yet
I/O Ports in AVR Microcontrollers: Sepehr Naimi
49 pages
Arduino Microcontroller
PDF
No ratings yet
Arduino Microcontroller
55 pages
AVR Programming in C
PDF
100% (2)
AVR Programming in C
79 pages
Architecture of Arduino Microcontroller
PDF
100% (1)
Architecture of Arduino Microcontroller
40 pages
ATmega GPIO
PDF
100% (1)
ATmega GPIO
17 pages
Ch07 AVR Programming in C
PDF
100% (2)
Ch07 AVR Programming in C
49 pages
Ch07 AVR Programming in C
PDF
No ratings yet
Ch07 AVR Programming in C
76 pages
MC Internship
PDF
No ratings yet
MC Internship
32 pages
Arduino
PDF
100% (1)
Arduino
22 pages
4 IOports v21
PDF
No ratings yet
4 IOports v21
21 pages
STM32F407 Parallel I - O Ports. Reference - STM32F407 User Manual
PDF
100% (1)
STM32F407 Parallel I - O Ports. Reference - STM32F407 User Manual
23 pages
Lecture Notes 4
PDF
No ratings yet
Lecture Notes 4
43 pages
Lec 1 DIO Basics
PDF
No ratings yet
Lec 1 DIO Basics
41 pages
Embedded System Design Using Arduino 18ECO108J: Unit Iv
PDF
100% (1)
Embedded System Design Using Arduino 18ECO108J: Unit Iv
53 pages
Micro Peme3
PDF
No ratings yet
Micro Peme3
23 pages
Virtual Hands On Training On Arduino Programming
PDF
No ratings yet
Virtual Hands On Training On Arduino Programming
30 pages
Lecture 4
PDF
No ratings yet
Lecture 4
16 pages
Arduino Training - Day 2
PDF
No ratings yet
Arduino Training - Day 2
46 pages
2 - ARM Parallel IO 123
PDF
No ratings yet
2 - ARM Parallel IO 123
42 pages
2 Gpio - Sar
PDF
No ratings yet
2 Gpio - Sar
21 pages
Embedded Systems - Lec 2 - GPIO
PDF
No ratings yet
Embedded Systems - Lec 2 - GPIO
28 pages
Microcontroller Fundamentals: B. Furman 23FEB2016
PDF
No ratings yet
Microcontroller Fundamentals: B. Furman 23FEB2016
56 pages
CISE414 - Unit 04 AVR Atmega Ports
PDF
No ratings yet
CISE414 - Unit 04 AVR Atmega Ports
30 pages
04 - IoT - MicroPython
PDF
No ratings yet
04 - IoT - MicroPython
24 pages
Manipulare Porturi
PDF
No ratings yet
Manipulare Porturi
33 pages
Thyristor As A Switch
PDF
67% (3)
Thyristor As A Switch
4 pages
2016lab2 Guide A1-2 e
PDF
No ratings yet
2016lab2 Guide A1-2 e
27 pages
Interfacing With AVR GPIO
PDF
No ratings yet
Interfacing With AVR GPIO
27 pages
Tutorial - Arduino and The I2C Bus - Part Two
PDF
100% (1)
Tutorial - Arduino and The I2C Bus - Part Two
16 pages
Lecture Microcontroller Overview
PDF
No ratings yet
Lecture Microcontroller Overview
56 pages
Arduino Clinic 2
PDF
100% (1)
Arduino Clinic 2
24 pages
Lecture Microcontroller Overview
PDF
No ratings yet
Lecture Microcontroller Overview
55 pages
Introduction To AVR Programming: Applied Electronics (28846) Fall 2014
PDF
No ratings yet
Introduction To AVR Programming: Applied Electronics (28846) Fall 2014
25 pages
I-O Port Programming in AVR
PDF
No ratings yet
I-O Port Programming in AVR
7 pages
Interfacing With The ISA Bus
PDF
No ratings yet
Interfacing With The ISA Bus
12 pages
Presentation ON: To Embedded System and Embedded C
PDF
No ratings yet
Presentation ON: To Embedded System and Embedded C
37 pages
Week7 IOs
PDF
No ratings yet
Week7 IOs
23 pages
Tutorial 2 "Special Function Registers (SFR) and Bitwise Operations"
PDF
No ratings yet
Tutorial 2 "Special Function Registers (SFR) and Bitwise Operations"
11 pages
Atmega328 Features: Atmega48A-48Pa-88A-88Pa-168A-168Pa-328-328P - Datasheet PDF
PDF
No ratings yet
Atmega328 Features: Atmega48A-48Pa-88A-88Pa-168A-168Pa-328-328P - Datasheet PDF
31 pages
Iarjset-Ncdmete 13
PDF
No ratings yet
Iarjset-Ncdmete 13
48 pages
Proggrm
PDF
No ratings yet
Proggrm
9 pages
Arduino Lab Report
PDF
No ratings yet
Arduino Lab Report
11 pages
OrCAD Layout Plus Tutorial
PDF
No ratings yet
OrCAD Layout Plus Tutorial
14 pages
03 Program Structure and IO
PDF
No ratings yet
03 Program Structure and IO
10 pages
Objecttves: Upon Completion Chapte4 You To
PDF
No ratings yet
Objecttves: Upon Completion Chapte4 You To
22 pages
Comsats University Microprocessor Systems & Interfacing EEE-342
PDF
No ratings yet
Comsats University Microprocessor Systems & Interfacing EEE-342
8 pages
Parallel Port
PDF
100% (7)
Parallel Port
14 pages
Arduino Tutorial "Communication"
PDF
No ratings yet
Arduino Tutorial "Communication"
4 pages
Exp2 ParallelIO
PDF
No ratings yet
Exp2 ParallelIO
3 pages
Spark V Exp 2
PDF
No ratings yet
Spark V Exp 2
7 pages
Friday, April 20, 2012
PDF
100% (1)
Friday, April 20, 2012
39 pages
Atinny 44 Ports
PDF
No ratings yet
Atinny 44 Ports
2 pages
Introduction To Arduino Uno
PDF
No ratings yet
Introduction To Arduino Uno
6 pages
Datasheet 74LS564
PDF
No ratings yet
Datasheet 74LS564
4 pages
Introduction To Arduino
PDF
No ratings yet
Introduction To Arduino
22 pages
HX711 Weighing Sensor Module
PDF
No ratings yet
HX711 Weighing Sensor Module
2 pages