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><strong>How To Make LED Cube 8x8x8 </strong></p><p><a href="http://www.tricksandhacks.com/make-led-cube-8x8x8/" rel="nofollow">http://www.tricksandhacks.com/make-led-cube-8x8x8/</a></p>
Hi I want to buy these components, any body guide me
<p>This is the best kit that I have come across for the LED's</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><iframe allowfullscreen="" frameborder="0" height="400" src="https://www.youtube.com/embed/4gNiDprbUPc" width="600"></iframe></p>
Circuit diagrams<br>Or kits details me <br>Also thes programmes code
<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>I sorry you, because I do not use Arduino, so I did not go through the difficulties you face, but you can read more online material on its use. I can only help you in terms of hardware. Good luck. I believe you will succeed! : D</p>
<p>Thnx for replying!<br>I got a solution for it already!</p>
Your led cube <br>Whitch kits using<br>
<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>
You don't need changes in code
<p>Sorry for answers will tell you now, when using ULN2803 then your program absolutely nothing changes, we simply optimized hardware only. Good luck! soon see your results here. : D</p>
<p>Cho e xin sơ đồ mạch in đc ko ạ</p>
<p>You can use even less space using SMT 2N2222 type transistors. My board will take either.</p>
<p>Can you please kindly send me the circuit and diagram.</p><p>MAy mail add<br>isurutharanga019@gmail.com</p>
Can you please kindly send me the circuit and diagram.
<p>The circuit diagrams for this project are in this instructable.</p>
My email address is sisirroy777@gmail.com
<p>Hi DongP1, I am so like the Cube and I want to make it and give to my girl friend as a big surprise. Can you send all schematics ,programs and other files to me . Thans so much.</p><p>My e-mail address: dongliang.ma@matrixopto.com </p>
<p>Hey guy, if i use STK500 device to set up for atmega32 then plug it to the board , so, can i remove RS232 and AVR Isp?</p>
<p>sir, i have successfully made my cube, now the problem is with the code... im using arduino uno and the code given in this article i.e for arduino user is working fine for me... but other codes which are used for AVR is not compatible with aduino ? does coding for both arduino uno and AVR are different ?</p><p>i got a error as &quot;compilation terminated </p><p> exist status 1</p><p> error compiling &quot;</p><p>if there is solution for this plz post . </p><p>and the code in the link which u gave is compatible with arduino uno ? </p><p> Thanks :)</p>
<p>You likely need an older, or possibly a newer copy of the IDE.</p>
<p>sir presently im using latest version of IDE i.e 1.6.7</p><p>can i know the the exact version of IDE ??</p><p>and sir how can i make others codes compatible with IDE?</p><p>Thanks :)</p>
mail me.. 233himanshu@gmail.com
This is here. https://drive.google.com/file/d/0BzXbfPpoHKV5eWQxU1VQODZhTTA/view?usp=sharing and don't need chager in program .
<p>Hi Phamd4 , thank you so much. I have another question, I can not open the CUBE schematic and pcb files with Protues Desing Suite 8.0 , my computer is Win 7 system. whether the version of the Protues is not matching? Can you help me .</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>can you please kindly send the code for programming </p><p>my emai id chiranjitkarmakar838@gmail.com</p>
<p>can you please kindly send the code for programming</p>
<p>look the video led not working and bord file in pdf can u people help me fix my problem</p>
<p>look my video led was not working on 2 row actually working but working different way I don what mistake I have done I attached board file in pdf please anybody can help I am really appreciate. I put full effort for this project but finally animation make me upset </p>
<p>hai I have problem in cube I done well but some animation not coming properly 1 and 2 row I mean 1and 2 ic I checked very well circuit is correct can u help me I upload pdf of my schematic and pics</p>
<p>Hello,</p><p>I read these sentences in this post:</p><p>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.</p><p>Can anybody help me to explain how to calculate to 10472.7 times per second. I am new to AVR timer.</p><p>Thanks,</p>
Thanks for sharing the awesome project.? . The instructibles are very elaborative. Atfirst i was sceptical if i could make this project. But atlast after backbraking hours of soldering and debugging , it was a succes. And the credit goes to chr. Thanks chr . U rock☺.<br>I used arduino uno as the controller. But the code is not complete. I mean many effects are missing. I dont know anything about programming. I request u to please mail me the code with every effect. It will be a great help. Thank u.<br>dbnjnds@gmail.com
<p>Is there some other way other than gray coding in software to avoid 74HC138 glitches </p>
<p>If the latch order is incorrect from the 138, usually it's the binary inputs are either miswired (wrong order, or a line not connected) or 2 inputs are shorted together.</p><p>Another issue you may run into is switching the data too fast, not latching long enough etc. which has to be fixed in the code.</p>
<p>hey can you please help me.</p><p>IC 74hc138 is misbehaving. I suspect it to be race hazard.</p><p>Wrong latches are being selected by the address selecter.</p><p>the following sequence seems to fix the proplem.</p><p>void draw(char *buffer)</p><p>{</p><p> PORTB=0b00001000;</p><p> PORTA=buffer[0]; </p><p> PORTB=0b00001001;</p><p> PORTA=buffer[1];</p><p> PORTB=0b00001101;</p><p> PORTA=buffer[5];</p><p> PORTB=0b00001100;</p><p> PORTA=buffer[4];</p><p> PORTB=0b00001110;</p><p> PORTA=buffer[6];</p><p> PORTB=0b00001010;</p><p> PORTA=buffer[2];</p><p> PORTB=0b00001011;</p><p> PORTA=buffer[3];</p><p> PORTB=0b00001111;</p><p> PORTA=buffer[7];</p><p> PORTB=0b00001000;</p><p> PORTB=0b00000000;</p><p>}</p><p>What I did was to change one logical state in the input to decoder at a time.</p><p>some sites recommend temporary disabling of output to prevent race hazards.</p><p>how did you deal with this issue&gt;</p><p>pls help </p>
<p>WOW - 5 million views.</p><p>I think it's some kind of record! LOL!</p>
Hi, Where do i buy all that stuff for 69 dlls? Thanks
<p>thats projects its great! Im looking for all the stuff and making time for doing! please continue doing amazing thing. :)</p>
<p>hi, really great instructable, kind that inspires people.</p><p>This code, cube[5][3] |= (0x01 &lt;&lt; 4);</p><p>Hex01 bit wise move 4, or with a 2d array, I can't get my head around it.</p><p>Plz, a more detailed description will save me from more tablets, cheers</p>

About This Instructable




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: