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 Arduino trick heard on embedded.fm: use down counters in your while loop
  • 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 4 replies
  • Subscribers 390 subscribers
  • Views 283 views
  • Users 0 members are here
  • loop
  • sketch
  • arduino
  • performance
Related

Arduino trick heard on embedded.fm: use down counters in your while loop

Jan Cumps
Jan Cumps over 10 years ago

When listening to this week's embedded.fm episode ( chriswhite ), I heard this Arduino tip:

If you use downcounters in stead of upcounters in your loop(), you save a clock cycle each time,

because down counting is a core operation and takes 1 cycle. Up takes 2 cycles.

 

For those cases where you have to pinch that last cycle out of your loop().

  • Sign in to reply
  • Cancel

Top Replies

  • Jan Cumps
    Jan Cumps over 10 years ago in reply to Workshopshed +1
    More than 100 episodes. Have fun.
  • Workshopshed
    Workshopshed over 10 years ago

    I've not heard of Embedded.fm before, will definitely be checking that out, thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Jan Cumps
    Jan Cumps over 10 years ago in reply to Workshopshed

    More than 100 episodes. Have fun.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • jaidi_71
    jaidi_71 over 10 years ago

    Yes, I listened to that pod cast. Very interesting. I'm new to the AVR world; so, I was curious to check out the ATmega328 instruction set. My guess is that the advice given is good but for the wrong reason. This puppy can add and subtract in one cycle just fine. The instruction timing diagram shows for such 1 cycle instructions the first third of the clock being taken up with register setup to the ALU. the middle third taken up by the ALU operation, and the final third being taken up with writing the answer back to the register file. This design doesn't really push the ALU. Thus such ALU instructions also have time to figure out appropriate status and write back to the status register.. One of these status flags is the Zero Flag. Thus after decrementing the loop counter and having steered the Zero Flag appropriately, a simple conditional branch if non-zero (BRNE) back to the top of the loop is all that is necessary. This BRNE takes 2 cycles when branching back to the top.  I f one is incrementing the loop variable, then between the ADD of the loop variable and the BRNE back to the top, at the very least one has to insert a CPI instruction testing for the upper bound to get the Zero Flag set appropriately.  Essentially, when counting up, the appropriate setting of the Zero status flag takes at least one extra cycle.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Workshopshed
    Workshopshed over 10 years ago

    Just had a flashback to the days of hand writing for the Z80.

    Decrease, jump non zero was a single instruction. I can't remember if this particular one was a long or short jump.

    The other thing I remember was padding out sound routines with dummy instructions so that each branch took the same number of cycles. Sound on the Spectrum worked by toggling a single bit on one of the output ports.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
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