element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Atmega 8bit port read and write
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 6 replies
  • Subscribers 391 subscribers
  • Views 1334 views
  • Users 0 members are here
  • ports
  • programming
  • bytes
  • mnemonics
Related

Atmega 8bit port read and write

billpenner
billpenner over 10 years ago

All I have been able to read (except datasheet) seem to use individual pins on the MPU as serial ports only. Is it possible to store the data on a port as a byte.

The data sheet appears to have a register attached to each 8bit port. if the port could be read, then moved to a register for processing,  ie. add, mult, divide etc.

It would help me with my current project. Mnemonic instructions in the data sheet seem to do just that but I can find no reference how to use them in the Arduino referen

any help will be most appreciated.

Bill

  • Sign in to reply
  • Cancel
Parents
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago

    first thing first

    Arduino ports are arrange like so

     

    PORTD = digital pins 0 through 7

    PORTB = digital 8 through 13, you cant use the last two as there on the crystal

    PORTC = analog 0 through 5 if your using them as digital pins pins 6 and 7 are not available on the UNO


    You need to avoid normally digital pins 0 and 1 if you want to do USB serial to your computer (AKA Serial.Print(xxxx) type instructions)

    so to read from a port all in one go you can do stuff like this

     

    byte PortDataD = PORTD;

    or

    int myPortData = PORTD; //dont forget only the lower 8 bits are valid and you should ignore bits 0 and 1 as there the serial port

     

     

    you can also write to a port like this

    PORTD = B10101000; // sets digital pins 7,5,3 HIGH

    but you need to remember your writing 0 to bits 0 and 1 and this could mess up any attempts to use the serial port, you may be better doing this

    PORTD != B01010100; // this will set only the pins you want and leave the rest alone at their current value (0 or 1), be careful setting an input pin though as you may turn on or off the pullup resistor

     

    you can get all the instruction on this and also setting up the ports for input and output here http://www.arduino.cc/en/Reference/PortManipulation

     

    going this route can save masses of flash memory and increase speed almost a magnitude but you have to be more careful of what you do as your no longer protected by the "Wiring abstraction" of the hardware

     

    Definitely a good place to go once you have mastered wiring and or running out of space in flash but still only need an ATMEGA328 CPU.

     

    Hope this helps

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billpenner
    billpenner over 10 years ago in reply to Robert Peter Oakes

    I have two more questions;

     

    I am revisiting this method of inputing data into a Arduino from a keyboard. I want to input 4 bits at a time via a port and save the data until I have 3 bytes.

    I will then send this data stream via a serial port.

     

    You have helped me understand the port addressing but I now can't seem to read data on a port.

    I can write data to the port. I can then read that data and send it to Serial.print(). as in the sketch below.

    However when I connect 4 switches to the PORTD (pins 2,3,4,5), I cannot read them.

     

    Yet when I use the procedure in Arduino example "button" I can read one of the the pin states.

     

    What am I doing wrong?

     

     

     

    This is the basic sketch I am using for testing.

     

    void setup()
    {
    Serial.begin(9600);
    DDRA = B11110000;   // allows data to be read (If I am correct in my thinking)
    }
    void loop()
    {
    Serial.print(PORTA, HEX) ; // prints to monitor
    PORTA = 0xF0;   //sets PORTA to HEX F0)
    Serial.print(" - ");
    Serial.println(PORTA,HEX);  // prints to monitor
    //delay(1);
    PORTA= 0xEF;  //changes data on PORTA 
    //delay(1);
    }

    2nd question:how do I change this post to a question so I can give credit to you guys.. You are really helpful.

     

    Thanks

     

    Bill

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • billpenner
    billpenner over 10 years ago in reply to Robert Peter Oakes

    I have two more questions;

     

    I am revisiting this method of inputing data into a Arduino from a keyboard. I want to input 4 bits at a time via a port and save the data until I have 3 bytes.

    I will then send this data stream via a serial port.

     

    You have helped me understand the port addressing but I now can't seem to read data on a port.

    I can write data to the port. I can then read that data and send it to Serial.print(). as in the sketch below.

    However when I connect 4 switches to the PORTD (pins 2,3,4,5), I cannot read them.

     

    Yet when I use the procedure in Arduino example "button" I can read one of the the pin states.

     

    What am I doing wrong?

     

     

     

    This is the basic sketch I am using for testing.

     

    void setup()
    {
    Serial.begin(9600);
    DDRA = B11110000;   // allows data to be read (If I am correct in my thinking)
    }
    void loop()
    {
    Serial.print(PORTA, HEX) ; // prints to monitor
    PORTA = 0xF0;   //sets PORTA to HEX F0)
    Serial.print(" - ");
    Serial.println(PORTA,HEX);  // prints to monitor
    //delay(1);
    PORTA= 0xEF;  //changes data on PORTA 
    //delay(1);
    }

    2nd question:how do I change this post to a question so I can give credit to you guys.. You are really helpful.

     

    Thanks

     

    Bill

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube