Step 53: Software: Interrupt routine

The heart of the LED cube code is the interrupt routine.

Every time this interrupt runs, the cube is cleared, data for the new layer is loaded onto the latch array, and the new layer is switched on. This remains on until the next time the interrupt runs, where the cube is cleared again, data for the next layer is loaded onto the latch array, and the next layer is switched on.

The ATmega32 has 3 timer/counters. These can be set to count continuously and trigger an interrupt routine every time they reach a certain number. The counter is reset when the interrupt routine is called.

We use Timer2 with a prescaler of 128 and an Output Compare value of 10. This means that the counter is incremented by 1 for every 128th cpu cycle. When Timer2 reaches 10, it is reset to 0 and the interrupt routine is called. With a cpu frequency of 14745600 Hz, 128 prescaler and output compare of 10, the interrupt routine is called every 1408th CPU cycle (128*11) or 10472.7 times per second. It displays one layer at a time, so it takes 8 runs of the interrupt to draw the entire cube once. This gives us a refresh rate of 1309 FPS (10472.7/8). At this refresh rate, the LED cube is 100% flicker free. Some might say that 1300 FPS is overkill, but the interrupt routine is quite efficient. At this high refresh rate, it only uses about 21% of the CPU time. We can measure this by attaching an oscilloscope to the output enable line (OE). This is pulled high at the start of each interrupt and low at the end, so it gives a pretty good indication of the time spent inside the interrupt routine.

Before any timed interrupts can start, we have to set up the Timer 2. This is done in the ioinit() function.

TCCR2 (Timer Counter Control Register 2) is an 8 bit register that contains settings for the timer clock source and mode of operation. We select a clock source with a 1/128 prescaler. This means that Timer/counter 2 is incrementet by 1 every 128th CPU cycle.

We set it to CTC mode. (Clear on Timer Compare). In this mode, the counter value TCNT2 is continuously compared to OCR2 (Output Compare Register 2). Every time TCNT2 reaches the value stored in OCR2, it is reset to 0 and starts counting from from 0. At the same time, an interrupt is triggered and the interrupt routine is called.

For every run of the interrupt, the following takes place:

1) All the layer transistors are switched off.

2) Output enable (OE) is pulled high to disable output from the latch array.

3) A loop runs through i = 0-7. For every pass a byte is outputed on the DATA bus and the i+1 is outputed on the address bus. We add the +1 because the 74HC138 has active low outputs and the 74HC574 clock line is triggered on the rising edge (transition from low to high).

4) Output enable is pulled low to enable output fro the latch array again.

5) The transistor for the current layer is switched on.

6) current_layer is incremented or reset to 0 if it moves beyond 7.

That's it. The interrupt routine is quite simple. I'm sure there are some optimizations we could have used, but not without compromising human readability of the code. For the purpose of this instructable, we think readability is a reasonable trade-off for a slight increase in performance.

<p>please give me a code and schematic email asimsheikh780@gmail.com</p>
<p>Great survey, I'm sure you're getting a great response.<br><a href="http://markoservices.pl/pod-lupa-szklo-w-kaszkach-nestle/" rel="nofollow">szklo w nestle</a></p>
<p>Hello, I understand that the use of two transistor for each layer is due to the current limitation of the transistor. Can those 2 be changed for a more powerfull transistor ?<br>Also,its not possible to use some kind of shift register to control the transistors ?Use 3 ports instead of 8 ports ?</p>
<p>Great work..</p>
Hii....i built my cube but its not glowing properly....
<br> The<br> website is looking bit flashy and it catches the visitors eyes. Design is<br> pretty simple and a good user friendly interface.<br><p><a href="http://journalistesportif.tumblr.com" rel="nofollow">http://journalistesportif.tumblr.com</a></p>
<p>Hi, I've added your project to the &quot;<strong>A Collection of WAAAY To Many 8X8X8 RGB LED Cubes!</strong>&quot; Collection</p><p>This is the link If you are interested:</p><p><a href="http://www.instructables.com/id/A-Collection-of-WAAAY-To-Many-8X8X8-RGB-LED-Cubes/">http://www.instructables.com/id/A-Collection-of-WA...</a></p>
<p>Hi guys! ? </p><p>first, thanks to chr for that incredible instructable! it has me inspired, a lot. </p><p>i tried to download the files from the project. but i was not successfull.</p><p>any ideas?</p>
<br> <a href="http://damdaar.com/14-major-incidents-by-global-warming-climate-changes/" rel="nofollow">hardly heat waves in Argentina</a><br>
<p>I tried it, but failed to glow all...</p>
<p>Good work...</p>
<p>hi all</p><p>As like &quot;<a href="http://www.instructables.com/member/power000" rel="nofollow">power000</a>&quot; my led cube also having da same problem, some led column remain on constantly, pls help &amp; suggest the reason and how to rectify the problem.</p>
<p>go to my revisited instructable and get my code or go to <a href="http://www.TheLEDCube.com"> www.TheLEDCube.com </a> and download the RAMP.zip file, and upload my code to your cube.</p><p>Video the POWER ON SELF TEST and send me a message with a link to your video, and I will diagnose your issue.</p>
<p>Hi, I seem to have successfully set the fuse bits, but here's the thing: after uploading the test.hex file to atmega32 and turning the cube on i get some random layers light up and stay on (with some flickering). Here's the video of that: <a href="https://www.dropbox.com/s/qpha7qhs9xkwyb8/VID_20151030_185558.mp4?dl=0" rel="nofollow">https://www.dropbox.com/s/qpha7qhs9xkwyb8/VID_2015...</a></p><p>I have a suspicion, that i may have fried something on the board and i hope that you may have an answer to that. At one point, i have accidentally powered on the cube via the 5V power source while it was still connected to USBasp which was plugged in the usb port. The USBasp didn't work afterwards (lol), so i ordered a new one. I also had trouble connecting to the atmega via the on-board connector, so i put it on a breadboard to see if it makes a difference - still no go. But after adding the crystal and two 22pF capacitors on the XTAL1 and XTAL2 SUCCESS! Avrdude started displaying the correct signature, i managed to set the fuse bits and write the test.hex successfully into the atmega. Do you have any idea if i could tinker with a multimeter to see whether anything went bad? Oh, i had a 1A fuse installed, which went off during my &quot;accident&quot;.</p><p>Thank you in advance!</p><p>P.S. I'm thinking of upgrading my cube in the long run by purchasing one of your premade boards to eliminate the wiring. However, I'm confused on which one i should get and how much that would cost me.</p>
<p>One of the fuses you set initially is switching from the internal 4MHz clock to the external crystal - so no crystal = no running after setting the fuses...that is until you add the crystal. So chances are, you didn't damage anything. In fact it did exactly what one would expect. For a mono cube, you'll want one of the RAMP boards.</p>
<p>So basically what you're saying is that atmega is not &quot;seeing&quot; the external crystal?</p>
<p>No, what I am saying is it never looks for the crystal UNTIL you set the fuse bits.</p><p>Once they are set, it MUST have a crystal.</p><p>No crystal means no clock, and a CPU running at zero clock cycles won't get much done! That's why it sprang to life once you added the crystal.</p>
<p>I finally got test.hex flashed and it works..sort of (video here: https://www.dropbox.com/s/5kophve403vw5cr/VID_2015...</p><p>I was happy nonetheless, because the sequence runs nicely with a few exceptions:</p><p>1. There must be a wiring issue, because the rows go like this: 1, 2, 4, 3, 6, 5, 7, 8. I tried tinkering with the wires to no avail, i don't really understand what could be causing this. I'm thinking maybe transistors?</p><p>2. The flickering and unlit LEDs must be caused by soldering errors or lack of contact, am I right?</p><p>3. There are some LEDs which faintly glows when turned off (itcan be seen when the whole cube goes dark). Does it mean that those LEDs receive some of the current even in the off state?</p>
<p>You may have the last 2 bits of the demultiplexer reversed....</p><p>so instead of bits A0, A1, A2 you have them wired bits A0, A2, A1. (as I recall, these would be pins 2 and 3 of the 74HC138) This might explain the sequence.</p><p>Triple check your wiring to the 138</p><p>Also you might have the data bus wired wrong, or you may simply have wired the cube to the controller incorrectly.</p><p>Your dropbox link isn't working, maybe try to youtube it.</p>
<p>Ok, thanks, I'll try that. </p><p>Here's link to youtube: <iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/nvisYO6vk0E" width="500"></iframe></p>
<p>looks more like a wiring issue from controller to cube than demultiplexer issue.</p><p>try the main.hex from <a href="http://www.instructables.com/id/CHRs-8X8X8-LED-Cube-Revisited-with-improvements/step10/How-do-I-modify-the-software-Questions-Answers-and/">http://www.instructables.com/id/CHRs-8X8X8-LED-Cub...</a> </p><p>I think that has my power on self test, which I would like you to video. It will tell me in better detail what's wrong with your cube.</p>
<p>main.hex video here:</p><p><iframe allowfullscreen="" frameborder="0" height="281" src="//www.youtube.com/embed/las1Agjynrc" width="500"></iframe></p><p>Very nice sequence, can't stop watching the cube!</p>
<p>I tried to transfer the correct code to you by skype, but you never came back online.</p>
Thanks for your time. Sorry I didn't have the time for that, I'm studying in college and working night shifts, so free time is scarce :D after placing the pcb boards with the wiring on a stable surface (the boards were just placed beneath the cube without any extra support) and running the cube for several hours I've noticed an improvement in terms of flickering and missing LEDs. There's still one part that is flickering, but I believe I'll manage that. Also, after flashing this main.hex file I noticed that the sequence runs in order (maybe the test.hex had it's flaws?). So, apart 2 burned LEDs and some minor flickering during certain animation I find the cube in an OK condition, requiring minor tweaks. So I don't really want to bother you with such minor problems. I'd appreciate any .hex files you can throw my way though. Many thanks, I'll keep in touch!
<p>OK, but remember you won't get any text unril you also flash the MAIN.EEP. both the eep and the hex need to be flashed to the chip to get the text, otherwise all the letters will appear as solid blocks.</p>
Alright, but if I remember correctly, I have to use rs232 connector? I have a slight problem with that, because my computer doesn't have the serial port (db9).
<p>nope. You know those squares that go around the outside of the cube right now? Those are actually undefined letters.</p>
<p>crap, that's the old main.hex....I'll have to compile one for you that has the power on self test. However, in the mean time...you do seem to have some short circuits, and you seem to have some of the lines wired wrong. I'll PM you my skype address, and I'll try to have a video session with you. Add me to your skype.</p>
<p>...sorry, can I impement this Code<br>(RAMP.zip) in Arduino UNO??? it&acute;s the same?? I have looked at:<br><a href="http://www.TheLEDCube.com/" rel="nofollow">www.TheLEDCube.com</a> but I<br>haven&acute;t seen it, in which section it is? I can&acute;t see it...Many<br>thanks! </p>
<p>At the top of the main page, click SOURCE CODE then download RAMP.ZIP</p><p>Unzip the file to a folder on your hard disk / desktop.</p><p>The source code for the Arduino is in the Atmega 328 section/folder.</p><p>Because my board conforms 100% to this project on both the Atmega328P (Arduino) and the ATmega32, I include code for both processors.</p><p>Please remember that with the Arduino, you lose serial capabilities (and on my boards, you also lose music response)</p>
<p>...I think got it!, do you think is this file: &quot;8X8X8 Arduino.pde&quot;?</p>
<p>that one or the one WITH POST (Power On Self Test)</p>
<p>...which one, which one? (do you mean this instructable publication you <br>are recomending me?) and where can I see the one with POST (Power On Self Test) you said? Sorry this instructable publication is yours, or you <br>post it? Many thanks!!!</p>
<p>You can use either code. If you downloaded the RAMP mini-CD then the code with POST is in a subdirectory where the regular arduino code is.</p><p>This instructable is not mine, it's one I post in. This was the instructable that got me into microcontrollers and LED cubes. TheLEDCube.com is my site where I offer PC Boards and extended code for this and the RGB LED cube projects.</p><p>Please don't expect people to tell you what to do every step of the way. Try to explore and READ and figure things out on your own. If you have a question that is BEYOND all the documentation, then we are here to help.</p>
<p>Yes sorry!!! Many thanks I will ;D</p>
<p>...mmm wow, I like Arduino because it seems less work to do, but so which circuit is the best or more complete to do? The one of Instructables? &amp; sorry serial capabilities for what? what can I do with that?</p>
<p>To answer all your questions, please finish reading this complete instructable, and then go through my &quot;revisited&quot; instructable.</p>
<br> This is<br> such a great resource that you are providing and you give it away for free. I<br> love seeing blog that understand the value. Im glad to have found this post<br> as its such an interesting one! I am always on the lookout for quality posts<br> and articles so i suppose im lucky to have found this! I hope you will be<br> adding more in the future...<br><br> <a href="https://www.youtube.com/watch?v=ZyF8T0eOYqc" rel="nofollow">Genius marketing pro review </a><br>
<p>Hi, </p><p>Thanks a lot for this instructable, i finished building the cube and it works almost perfecly. The only issue is that when the cube wants to show the first data of the fifth IC(547) it turns all the plane on. i've checked the continuity between all the data lines as well as changing the IC, but none of this worked. I would really aprecciate your help since i don t know what else to do.</p><p>(By the way i&acute;m using an Arduino UNO and it's a 7x7x7 cube).</p>
<br> This was<br> a really great contest and hopefully I can attend the next one. It was alot<br> of fun and I really enjoyed myself..<br><br> <a href="https://www.youtube.com/watch?v=aUuMyaPyby4" rel="nofollow">Build my list 2.0 review</a><br>
<br> Greatpost, you have pointed out somefantasticpoints , Ilikewisethinkthis s a verywonderful<br> website.<br><br> <a href="https://www.youtube.com/watch?v=2OKOGM3JYCc" rel="nofollow">Build my list 2.0 bonus</a><br>
<p>Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. <a href="https://www.youtube.com/watch?v=6XvjFwRywZU" rel="nofollow">Commission Juggernaut Reloaded</a></p>
<br> I felt<br> very happy while reading this site. This was really very informative site for<br> me. I really liked it. This was really a cordial post. Thanks a lot!.<br><br> <a href="http://lagrill.co.ukhttp://lagrill.co.uk" rel="nofollow">restaurants in sutton coldfield</a><br>
<br> howdy,<br> your websites are really good. I appreciate your work.<br><br> <a href="http://vertshocked.net" rel="nofollow">how to improve a vertical jump</a><br>
<br> Hey There. I found<br> your blog using msn. This is a very well written article. I&rsquo;ll be sure to<br> bookmark it and come back to read more of your useful info. Thanks for the<br> post. I&rsquo;ll definitely return.<br><br> <a href="https://www.youtube.com/watch?v=BbRyrYF0NmQ" rel="nofollow">ecom premier academy review</a><br>
<br> If you set out to<br> make me think today; mission accomplished!I really like your writing style and how you express your ideas.Thank you.<br><br> <a href="https://www.youtube.com/watch?v=BbRyrYF0NmQ" rel="nofollow">ecom premier academy review</a><br>
<br> I have been<br> checking out a few of your stories and i can state pretty good stuff. I will<br> definitely bookmark your blog<br><br> <a href="https://www.youtube.com/watch?v=YRB6IfTZkGc" rel="nofollow">The Venus Factor Review</a><br>
<p>I am not using rs232 ic just using atmega32 and 74hc574and 74hc138ic just using basic module I don want confuse I want program the atmega working alone </p>
<p>check my &quot;revisited&quot; instructable.</p>

About This Instructable


3,647 favorites


Bio: I like microcontrollers and LEDs :D
More by chr: Reverse Engineering: RGB LED Bulb with IR remote Reverse engineering: USB controlled home automation hack LED Cube 8x8x8
Add instructable to: