20 Replies Latest reply on Feb 15, 2018 3:10 AM by michaelkellett

    Code for small diy clock kits.


      I've been looking for a project that would be suitable for young Boy Scouts, and have decided that one of these DIY clock kits would be something that even the youngest could put together.

      Most of the kits appear to come with an AT89C2051 (they all seem to come with the same pcb as well), so I've also searched out code that the older scouts could program additional AT89C2051s with. I'll reach out to the local university to see if they've got a programmer that we can use, but the boys won't have access to one after we're done making these. I think having a couple extra chips to swap around with different programs would be cool to them.


      The clock comes with a standard clock program, but I'm still looking for a simple countdown timer and a simple stopwatch. By "simple" I mean something that I can show the scouts, and explain commented snippets of the code.

      The code that I've found has been on forums where the members haven't been active for years, so I can't ask them followup questions.


      If anyone could help me out with simplified code for two projects, I would greatly appreciate it. I can reassign pins in the code if the boards end up being pinned slightly different than what is pictured (you never know with these things).


      Countdown Timer:

      - button1 == cancel timer / cancel buzzer

      - button2 == add time to countdown

      - button1 held >2sec == enter/exit setup mode.

      - button1 in setup mode == toggle buzzer duration between: 60sec / 300sec / 600sec / 9999sec

      - button2 in setup mode == toggle button2 "value" between: 30sec / 60sec / 180sec / 240sec / 300sec

      Timer counts down in seconds from 9999 - 0 (in base10)

      Digits blink at 500ms intervals while <=10sec

      Button2 push adds value to current countdown at any time

      Buzzer sounds at 500ms interval.



      - button1 == clear

      - button2 == start/pause

      - button1 + button2 == switches between hh:mm / mm:ss / ss:ms ("Hr:  " / " :S " / " S:  ")

      Time counts up (in base 60)

      Time stops at 99:59 (i.e. 99hr:59min / 99min:59sec / 99sec:59ms)


      I think there are some good learning opportunities between the two programs. Showing them the code and having them customize values in their code (like the available "values" for button1&2) will help them feel ownership in the project.



      TheCustomGeek shared some code for one of his projects, and is what I used as the base for my attempts to make the Countdown Timer and Stopwatch described above. Here's a link Multiplexing for a 7 year old | The Custom Geek

      I've attached the code, too, mainly so that the multiplexing can be similar in both projects (easier to explain to the scouts). Using a better method is fine too.  I've removed this, and attached the assembly code that I had initially started with -prior to finding TheCustomGeek's project. We do have access to a C compiler and linker for the 8051s (AT89C2051 included), though.


      I'm still muddling through this on my own, but I'm hoping that one (or more) of you would be able to put together some code much faster/better/cleaner than I.

        • Re: Code for small diy clock kits.



          The attached code is for an Arduino, which contains a different microcontroller compared to AT89C2051. The code isn't compatible.

          You could rewrite the code for AT89C2051, but you'd almost have to start from scratch, since even the LED display is different, yours appears to have 12 pins, and the one that the Arduino code is for has 16 pins, so yours is multiplexed differently, and that would need to be deciphered too. So, a non-trivial exercise to do this, but possible.

          The AT89C2051 is a really ancient chip, so although you'll likely find information on the Internet on how to program it and perhaps some example programs to get you started, personally I don't have any example source code for this microcontroller - I think I maybe briefly used it >10 years ago. Personally I wouldn't advise it, because you'll need to source a programmer and compiler, and most of this is really ancient as mentioned. Better to just have a construction project with it using the pre-programmed chip supplied with the kit perhaps, and have a different project if you want to demonstrate different code.

          2 of 2 people found this helpful
            • Re: Code for small diy clock kits.

              Dangit. I had started this project using code from one of those other groups that I mentioned, but then started looking for other code after a while. TheCustomGeek code was more 'legible' to me, so that's when I pivoted. Here is what I was working with before (see attached txt).

                • Re: Code for small diy clock kits.

                  That's assembler code, which is likely what you'll need to do for the AT89C2051, unless you can find a C compiler and linker (I have no idea if that is practical for that chip, and it will require some setup concerning the 'memory map').

                  Very few people program in assembler any more unless they really have to, and the more modern microcontrollers are geared up for C programming. It will take at least half a days effort to decipher the assember listing and code up what you want, and the risk is high that it wouldn't work first time (easier to make mistakes in assembler code than in C code).

                  1 of 1 people found this helpful
                    • Re: Code for small diy clock kits.

                      Fortunately (or maybe Unfortunately) our schools in Alaska still have all this old stuff around, so compiling/programming for the AT89C2051 didn't seem like a bad idea.

                      Maybe getting help with the code will be harder than I thought, though.


                      Any help (even the kind that points me in a different direction) is greatly appreciated.



                      EDIT:   We do have access to a C compiler and linker for the 8051s (AT89C2051 included). Does that make using TheCustomGeek's code easier?

                        • Re: Code for small diy clock kits.

                          It will definitely make your life easier to use the compiler, because as you've noticed too, the assembler listing is very hard to follow (and difficult for young children to follow too).

                          Regarding re-using the C code: this will still be difficult because the Arduino code has the benefit of using some libraries, that make the code so much easier to write and follow.

                          With the AT89C2051, those libraries do not exist, so you'll need to go through the AT89C2051 datasheets and any reference manuals, and see how to (say) use the timer hardware module inside the chip. The Arduino code abstracts all that away from the hardware, so that things like time measurement are handled by a high-level function called millis() in that code.

                          If you wish to use the AT89C2051, there is no choice but to dig into the datasheets, and also to see if you can find any code in C for the AT89C2051 online, so you can reuse bits of it. You can inspect existing code to see how to set up inputs/outputs, and how to enable the timer, and so on.

                          If you google for "Microcontroller Projects in C for the 8051" by Dogan Ibrahim, it looks like that book will help you (it was written specifically with the AT89C2051 in mind), these were some snippets from the online book preview:


                          If you're serious about getting up to speed with this microcontroller and developing the code, then I think that book will help, plus of course reading the datasheet to get fanmiliar with the chip.

                          3 of 3 people found this helpful