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>I want to run code on desktop computer, but i dont know how to create a User Interface ( a simple software) to send a binary buffer to micro controller.Someone can teach me how to do this.</p><p>Thanks so much.</p>
Thanks for doing this Instructabe! I actually did this as my first electronic project, and it worked flawlessly on an Arduino UNO. I really apreciate you took the time to do a guide so extensive and complete.<br>Now, i wanna manage the cube via Serial Comunication on Bluetooth. So i need an Arduino MEGA(the Uno doesnt have any Pins lef). Problem is, the code isnt compatible with the Mega and i cant fully understand how the PORT commands work, so i am unable to make it work on am Arduino MEGA. Can you help doing a compatible code. (I dont need to use Analog pins anymore to control the cube)
I have the code for the mega. Will post this evening.
<p>Hi!<br>Can you please post the complete code (with all animations+serial programming)<br>for Arduino MEGA!?</p>
<p>Hello!,<br>I also have same problem. I also now want to make more awesome animations using serial communication. Have you found some way?<br>Or have you found some way to use UNO itself? Or i have to go for Arduino Mega?</p>
<p>Pls .pls help me i can't understand the wiring can someone pls give me easy board design i am going to control it with a arduino uno or an arduino no (most probably nano) pls i need help .pls tell me a easy board design. </p>
<p><a href="http://www.pyroelectro.com/projects/8x8x8_led_cube/img/8x8x8_schematic.png" rel="nofollow">http://www.pyroelectro.com/projects/8x8x8_led_cube...</a><br><br>See this..this uses Arduino UNO..<br>I would recommend you to read some tutorials first..like CHR, SuperTech-IT, and Pyro Electro..<br>Then get your hands dirty! :) </p>
Just about to start this project as mg first. Any tips would be lovely. Thanks!
<p>Just be very sure about not to short VCC and GND anywhere!<br>And don't forget the capacitors anywhere, altough they seem of less use but infact they improve switching.</p>
<p>hello all of you, I have an idea to replace 16 BJT by ULN2803, it would save up for your area, and it works very tot.Toi did 2 cube leds and it all works fine however, I did not design the RS232 connection on my circuit. and here are some pictures about it:</p>
<p>Hello DongP1, <br>Can you share a more clear schematic how did you used the ULN2803 and explain what changes have you done to the code ?</p><p>Thanks</p>
<p>OK you, I'm very fun when help you. You can use Proteus to open file schematic and PCB, and i did fixed code. all in here:<br>https://drive.google.com/file/d/0BzXbfPpoHKV5eWQxU1VQODZhTTA/view?usp=sharing<br>when you use atmega32 you must set fuse for it. <br></p><p>and when you solder PCB solder you should note carefully and did not miss any legs, as this is the second circuit face, you can also book for someone trained to do it to ensure circuit reliability. And my final non-electric circuits become restricts motherboard area, it absolutely does not affect the led. And can not forget that it is a 5V supply. Wish success. I look forward to seeing your circuit here.</p><p></p><p>Inside the downloaded folder: principle diagram, schematic print, inside a simulated 3d opened by Proteus, the revised code with external hex file, inside the code can edit custom and standard you, use WINAVR (recommended recommended). and images to set the fuse for ATmega32.</p>
<p>Hi!!<br>Do you know hoe to flash the EEPROM of Arduino just like we do in normal non-arduino boards..<br>i.e. we flash .eep file directly into the micro controller.. do you know any similar or equivalant thing in Arduino...?</p>
<p>Hello, </p><p>Now I got it. The idea to use the ULN2803 is to avoid using transistors correct ?<br>What do you mean that I need to set the fuse for the ATMEGA32 ?<br>I will be using arduino board, either Nano or mega. </p><p>Thanks for your reply </p><p>Jose </p>
<p>is this, if you use it you must chip atmega fuse set inside the IC, the fuse is to control a number of functions such as: using the calibrated internal or external crystal, and and some rather different features, please reference IC data sheet. so that it operates according to your desired this function. I use the load circuit to the load burn-e program for my IC, please find out more about it, or you can use the load circuit which you are available. <br>ULN2803 help replace the transistor 16 in the layer. you read this ic data sheet to learn more. I had to diagram inside folders principle schematic. you see that in order to better understand how it works. wish success! : D</p>
<p><iframe allowfullscreen="" frameborder="0" height="400" src="https://www.youtube.com/embed/4gNiDprbUPc" width="600"></iframe></p>
<p>hello everyone, I have an idea to replace 16 BJT layers by ULN2803, it will save your area, and it works very well I did 2 LED cube and all activities well however, I did not design the RS232 connection on my circuit.</p>
<p>How Do i flash the EEPROM of arduino!?<br>i.e. i want to store the fonts in the eeprom of arduino UNO ..how to do?<br>for eg. i could easily flash the .eep file when using normal AVR board or some non arduino board..but how to do the equivalent thing in Arduino UNO!?<br>PLZ Help!</p>
<p>Hey thanks for the instructable! I finally finished mine. Here is a link to the arduino mega2560 code that i used for my cube.</p><p><a href="https://forum.arduino.cc/index.php?topic=371012.0" rel="nofollow">https://forum.arduino.cc/index.php?topic=371012.0</a></p><p>Hope that helps someone!</p>
<p>Perfect explanations, I have learnt soldering and reading circuits with this project, which had been my final project for high school.<br>I used 5mm LEDs (I think I should have bought 3mm LEDs instead because the sight of the LED at the opposite corner is not clear) and an Arduino Nano v3.0 to run the software. <br>The best thing of this guide is that as soon as you understand how the functions of the provided code work you can easily make your own ones (if you know C)</p>
<p>great job!!!</p>
<p>Thanks for your great instructions. The structure worked great and all without a mistake. The biggest problem for me is still the programming.<br> But should also be creating.<br> sorry for my bad english I have it translated into google XD<br> But pictures say more than a thousand words.</p>
<p>what a neat project ;D ,great soldering job ! You are my idol now. You must be a soldering master or monster ;)</p>
<p>thank you very much. It was a very elaborate work.</p><p>when the housing is done I take a picture.</p>
<p>i think u have same code used in site or different one if u have different one can u send to this email aravin_fun@icloud.com</p>
<p>I am done everything its work good but I feel animation is not working some of that </p>
<p>your cube was look nice perfect</p>
can any one please tell me how to use PC software? (pl dont tell me about escape characters, i already understand them). I want to know how does the PC software send data through COM PORT via serial line. I am using USB-TTL converter, but i'm unable to run the PC software as i suppose it was written for UNIX. i'm having trouble with the PC software functions for advance animations. Also, the terminos.h file and related Header files are not present. where should i run the PC software? in Atmel studio 4, or atmel studio 6, where? i'm unable to understand. all you people, please guide this beginner :)<br>thnx.
<p>https://www.youtube.com/watch?v=qO5z9IuCnXY </p>
<p>what a nice job! I spent a whole week on soldering and debugging (what a struggling job! )I finally decide to make a PCB multiplexer board. it works well and make whole device look nice and clean. I still have 9 pcs PCB rest . if anyone needs please send message to me . 20usd/pc .</p>
<p>what size drill bit?</p>
<p>I've made the LED lattice and am starting on the electronics now but have a few questions:</p><p>What values should the pull up resistors be?</p><p>Is 2k2, 1k or 100ohm the best for the transistor base resistor?</p>
<p>Do you have something like a link ready to a cart with all of the materials except the LED's? That would be extremely helpful</p><p>Thanks</p>
Hi I'm getting closer to being done with my cube and I just wanted to say thanks for making this possible. I've had to make things work my way but you've made it possible for me to do it. I will post a pic when I'm done
<p>You can have programs teaching it? !</p><p>Send to peterabbey52013@gmail.com </p>
<p>please give me a code and schematic email phanquoctri89@gmail.com</p>
<p>Hello, I used the exact set up with an Arduino Uno. As soon as I connect the the 5v pin to the board its stops. No more lights on the arduino. Dose this mean I have a short circuit somewhere? How do I trouble shoot for it? Thanks I'm new to this and was really excited about building one. </p>
<br> Really<br> great post. I just unearthed your web journal and needed to say that I have<br> truly delighted in perusing your blog entries. Any way I'll be subscribing to<br> your food and I trust you post again soon. Huge a debt of gratitude is in<br> order for the helpful information.<br><br> <a href="http://www.grivy.com/featureddeals/city/bali" rel="nofollow">bali deals</a><br>
<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>

About This Instructable


3,794 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: