Binary clock
My new hobby: Microcontrollers.
I just finished my first project: a binary clock. It displays the time in three columns with six LEDs each, which show (from left to right) seconds, minutes and hours. The time can be set using two buttons.
I wrote the code all by myself, with occasional help from HK. The microcontroller I used is an ATmega8.
Pics:
![]()
Might not be pretty, but at least it looks better than its backside - multiplexing LEDs is hell.
If you ask nicely I might just publish the code
Edit: Nicely asked, code delivered into the public domain. ![]()
This entry was posted on Tuesday, April 15th, 2008 at 1:05 am and is filed under Random. Find similar posts by selecting any of the following tags: Code, Images, Microcontroller, Open Source, Random. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
on June 2, 2008 at 12:26 pm joe wrote:
ich hab in meinem praktikum das selbe projekt laufen, auch mit atmega 8… nur den code zu programmieren stellt sich momentan als schwierigkeit heraus. wenn du so nett wärst, deinen code zu veröffentlichen?
[Reply]
on June 2, 2008 at 1:29 pm Valentin wrote:
Immer doch.
[Reply]
on July 26, 2008 at 1:47 pm Aditya wrote:
Hey,
Nice code and nice project. I am also starting up on ATmegA8. Can you please mail me (or post here) the schematic of this circuit? (I hope I am asking nicely
)
[Reply]
on July 26th, 2008 at 4:04 pm Valentin replied:
Hi,
sorry, but I don’t really have any schematic for this, but if you look at the code, there’s a description of the wiring that should be enough to build it… if you’re not sure how to multiplex the LEDs, take a look at “http://www.fpga4fun.com/Opto4.html”. Don’t forget to add pullup resistors to the buttons… if you have any questions, just ask here.
[Reply]
on August 17, 2008 at 7:37 am Aditya wrote:
Hey,
I have started making the project (got the controllers 2 days ago). I read the connections in the source file. Port D has all ‘+’ pins of LEDs connected to it right? Seconds 0, Hour 0, Minute 0, to D0 right? And the negative terminals go to respective pins as described in the diagram?
Aditya
[Reply]
on August 17, 2008 at 12:33 pm Aditya wrote:
I tried the code and it works… Problem is however, I am using ATmega32L and hence the max clock frequency is 8MHz… Will you please tell me the modifications needed to work this on 8MHz… It is working something like 30 seconds for 1 second….
Aditya
[Reply]
on August 17th, 2008 at 12:59 pm Valentin replied:
You’ll simply have to set F_CPU, it’s at the top of the source code. As you can see, it’s set to 16MHz at the moment - if one second takes about 30 seconds, your microcontroller is probably set to use 500KHz.
I also recommend getting an external oscillator (I’m using a 16MHz crystal oscillator since they’re the easiest to use, albeit a bit more expensive). The internal oscillator of your ATmega is good enough for normal use, but it’s too imprecise for a clock - if you have it run with that, you’ll probably get a time difference of a few minutes when you let it run for a day.
Cheers, Apo
[Reply]
on August 17, 2008 at 2:59 pm Aditya wrote:
So, none of the other parameters are linked with it? I mean, I saw that timer overflow is F_CPU/128/1 I thought as F_CPU is now halved, I might have to set the overflow as 2F_CPU/128/1…. I will be buying a crystal soon…
[Reply]
on August 17th, 2008 at 3:19 pm Valentin replied:
No, don’t change any of the other constants - I’m calculating these through F_CPU. So if you set the overflow variable to 2*F_CPU/128 after halving F_CPU, the two changes negate each other. You do not want that
If the frequency of the CPU is halved, the overflow value gets reached half as fast - but by also halving F_CPU, the overflow value gets halved too, so it doesn’t take any longer after all. Sorry about the crappy explanation, but I hope that you understand it anyway.
Also, you can use the reply link at the bottom of a post - that way, all the replies are in one thread.
[Reply]
on August 17th, 2008 at 4:21 pm Aditya replied:
Okay… thanks. But I think something is missing here… I changed the CPU_F to 4MHz and then the overflow to CPU_F/1024/1 and the clock is working great.
One last thing (sorry to bother you again and again… and again) but, how do I connect the interrupt switches? PortB1 - Switch - Ground OR PortB1 - Switch - VCC?
Aditya
[Reply]
on August 17th, 2008 at 5:04 pm Aditya replied:
[Reply]
on August 17th, 2008 at 5:11 pm Valentin replied:
Uh, 20 minutes in real time or when the minute LEDs are at 20 minutes?
[Reply]
on August 17th, 2008 at 5:11 pm Valentin replied:
The buttons are connected to VCC.
What is your frequency? You should really only change that - if setting it to the correct frequency doesn’t work, try setting it to others until the seconds are fine.
[Reply]
on August 17th, 2008 at 5:15 pm Aditya replied:
I am using the internal clock for now. 4MHz… I will be buying a crystal later. I changed the frequency to 4MHz and retained the rest of the code as it is. Now, I am connecting the buttons to VCC and then I will test it.
Thanks…
[Reply]
on August 17th, 2008 at 5:49 pm Aditya replied:
And it does not work. Some problem. Whenever I run the code as it is, just changing the frequency, then the clock runs very slow. And when I changed the overflow denominator to 1024, then it ran well, but the hours were changing every 20 minutes (20 minutes real time AND when the minute LEDs were 20, 40 and 60)
Any ideas? I can’t figure out anything.
[Reply]
on August 17th, 2008 at 5:54 pm Valentin replied:
Try looking at your data sheet to see what you need to set to get your prescaler to 128, then you probably won’t have to change the 128 to 1024. As for the hour thing - no idea, sorry.
[Reply]
on August 18th, 2008 at 7:53 am Aditya replied:
Thanks a lot for all your help. I tried changing the prescaler but that did not help so I changed the timer function a bit and now it works. Thanks a lot.
[Reply]