Clock Project Journal Entry #1: What is it, and what’s the point?

One of the things my desk is missing is a clock. Yes, I have a computer, watch, and phone that can all tell time, but when I’m in a full-screen game, I don’t want to have to go looking for the time. I’d rather have a clock large enough that I can simply glance at. First world problems, I know, but in any case, I thought that this would make for an interesting electronics project, despite how simple it may sound.

I will be making two custom circuit boards for this project. One will be the control board and the other will be the actual LED clock display. The project will be controlled by at Atmega328p microcontroller and the time will be kept by the DS3231 real-time clock chip. The LEDs will be driven with the help of some 74HC595 shift registers since it should make programming easier as opposed to multiplexing… I always gravitate toward using shift registers, I guess.
20150926_150931

One of the things I wanted to make sure of was the LED configuration. As it sits now, each segment will have 4 red LEDs. At first, I tried driving them at their “full” 20mA each (so 80mA per segment) but I felt that it was too bright. Bumping up the current-limiting resistor brought it to a more comfortable brightness. With the higher resistor, the current drawn for the segment was about 4mA, which is low enough for a pin on the shift register to handle directly. With that, I don’t need any transistors, which makes me happy…

I was planning to use a single resistor for the four LEDs on each segment, but it’s bad form and I don’t want to go that route for the whole project. I already feel “guilty” about using shift registers instead of just multiplexing. I will be putting a resistor for each LED. The main reason why I wanted to go with the one resistor per four LEDs is because it saves space without having to go with surface-mount components. I’ll see how the layout goes with the 1/4 watt resistors and hope that I don’t have to go and get some surface-mount resistors. I’d like to use whatever I already have.
ds3231rtc_sch_1Here’s the schematic for the control board. The board layout is done, or at least the first draft of it is. The only thing left to add is a switch to either cut the power to the project or at least turn of the display. I haven’t decided yet. I’m planning on using some mini-slide switches I bought a long time ago but I need to create an Eagle part for it which may take a little time. I’m waiting on some digital calipers from Amazon so that I can measure the pin thickness.

The layout for the display is coming along slowly. There’s only enough space for two digits per board, which is not a bad thing since I could use them for other projects. The problem that does arise from not making them specific for the project is that I have to make a separate circuit board for the colon and AM/PM indicator (I don’t really want a 24-hour formatted clock).

That’s all I have to share for now. I’ll be back with more when more details get finalized. Stay tuned!

PS. I started working on this project about a week or so before Ahmed Mohamed’s clock incident happened. I found that to be a funny coincidence.

Advertisements

See you later, Active Surplus.

Active Surplus is one of those stores that you could spend so much time in just to browse. They have a stock of electronics gadgets and parts, as well as an assortment of just… things. Recently, they announced that both their Steeles and Queen stores would be closing. They’ve been around for decades so it’s a tragic announcement for those who visited for all these years.

IMG_20130521_112947

For me, I only discovered the store a couple of years ago and have only visited the Queen location a handful of times and the Steeles one once. Each time, I wandered around in awe of all of these neat parts, while reading all of the quirky product labels. I never actually purchased anything from Active Surplus since I was strapped for cash those times I visited (ie. unemployed) and didn’t want to end up with random things I wouldn’t use. I was planning to go today but I scrapped those plans because I didn’t have the energy to take the trip downtown, and because I’m trying to get rolling on a couple of projects, in between playing Cities Skylines…

Anyways, it sucks to see them go. From what I gather, I take it that they will reappear again in the future so there’s still hope. In the mean time, I have Sayal, a chain electronics store in the Greater Toronto Area. There’s also the online stores like Dipmicro and Chinese eBay sellers.

Do you have any cool stores in your area like Active Surplus? Share in the comments!

Thanks for reading! Stay tuned for some upcoming projects!

This battery was excited to see me

20150904_212131

I have a small area on my desk for batteries of various sizes, including these 9-volt batteries. I came home one day last week to see one of them popped its lid… I’m not sure why. There was nothing across the terminals or nearby that would short it out. I’m not sure if it slowly rose out of its casing like a growing flower but I’m glad that if it was violent at all that it wasn’t harmful to anything around it.

I had to share because it’s not something you see every day!

No Arduino Uno. No Arduino IDE.

Ditching the Arduino IDE
20150906_181919

Some time ago, I started playing around with the Atmega328p microcontroller without the Arduino Uno board. Now it’s time to try doing the same on the software side, even as just an experiment. I’ve been getting started with AtmelStudio which allows you to program Atmel microcontrollers in C. I’m still not quite clear on what to call the Arduino programming language other than C, if not a simpler form of C because the Arduino language takes care of a lot of things for you to make it as easy as it is to program an Arduino.

I learned about microcontroller programming in college. I believe we used a PIC microcontroller for the labs so, while I recognize certain things, there seems to be a little variation in abbreviations. In the labs, the microcontrollers were part of a board my professor designed that had a bunch of peripherals like buttons, switches, LEDs, and an LCD. It was a very thorough course with loads of information to take in. I hope I can remember it all!

Anyways, I’d really like to continue playing around with it and see if I can make a full project with one of my Atmega328p boards programmed using AtmelStudio. I’m taking baby steps to get up to a good speed in AtmelStudio, and I hope that writing small tutorials on the blog will help me firm up my knowledge. If you spot anything that needs correcting, please leave a comment!

The rest of this post will introduce you to the little work I’ve done so far, and may be helpful for anyone else who may be interested in trying it out too.


Hello World!

I made two Hello World programs, with the help of a bunch of guides which I will link to throughout the rest of this post. Both programs blink a bunch of LEDs but using different methods. One uses a delay, while the other uses an interrupt routine.

USBtinyISP with AtmelStudio

AtmelStudio does not work right away with the USBtinyISP, which is what I used to program the Atmega328p. You must add it as an external tool. I followed this guide to add it to AtmelStudio. You’ll need to download WinAVR to complete the guide.

As the guide says, to upload code with the USBtinyISP, you must go to Tools>USBtinyISP (or whatever you named it). Just remember to build your code first before uploading it.

Code

Using a Delay (and Beginner’s Introduction)

As I mentioned, I made two programs that blink LEDs. One uses a delay while the other uses a timer interrupt. Let’s first take a look at the delay version.

Screenshot (3)

The first line defines the clock speed, which is 16MHz. The next line includes io.h header file, which actually just includes the appropriate file for the project’s microcontroller (which you select when you create the project, in this case the Atmega328p) that contains definitions for register addresses and things like that. The third line includes delay.h header file which enables us to use _delay_ms(); to cause the delay.

Now, into the main program! But first, let’s talk about ports.

atmega328p_pinout

This is the pinout of the Atmega328p-pu. Some of the pins are labelled PBx, PCx, and PDx, where x is a number. These stand for PORTB, PORTC, and PORTD, which are 8-bit data registers where each bit is a pin. The pins on the uC aren’t exactly laid out as cleanly as an Arduino Uno when looking up and down the rows of pins, but you can always order them in a way that works best for you when you’re actually wiring up your project.

Anyways, back to the code. In the first two lines of code in our main function, we set all of the pins in PORTB and PORT D to outputs. This is equivalent to the pinMode() function of the Arduino which is used to set certain pins as inputs or outputs. DDR stands for Data Direction Register where setting a bit/pin in the DDR to 1 makes it an output and setting it to 0 makes it an input. To make things easier, I put it in hexadecimal format (“0xFF”). You could also write “0b11111111” which is the binary equivalent and is more helpful when pins in a port are a mix of inputs and outputs. The third line in the main function sets all of the bits in PORTD to 1.

Everything in them main function up to this point is like the setup() function in the Arduino IDE. This code only runs once when the uC is powered on. Everything in the while loop will loop forever, like loop() in the Arduino IDE.

We set all of the bits in PORTD to 1 earlier so that we can toggle all of the bits on PORTB and PORTD at the same time and they will alternate. If we didn’t set them before we started the loop, all the bits in both ports would be the same. In another case, we could put in some logic that would toggle or set all of the bits in PORTD to 1 on the first loop, but that’s unnecessary since we could do it this way.

The exclusive OR (abbreviated as XOR, symbolized as ^) operation can be used to toggle bits high and low whenever the line is executed. In this code, the bits in PORTB and PORTD are toggled, and then there is a delay set for 250ms on the last line of the while loop. It loops around and toggles all of the bits again, and so on.

Using an Interrupt

A lot of the core stuff was covered in the previous code section so I’m not going to go into as much detail for any repeating lines in this section. Also, I’m rusty when it comes interrupts especially so don’t quote me on anything. Check out this and this for the information that I used to help me get this working. Also see the Atmega328p datasheet.

This program sets up Timer1 interrupt so that the ISR, or Interrupt Service Routine, will execute every 250ms. Let’s go line by line where we initialize Timer1.

cli(); disables interrupts. This is used so that important code that shouldn’t be interrupted isn’t.

The next two lines set all of the bits in the TCCR1A and TCCR1B, or Timer/Counter Control Registers for Timer1, to 0.

See the large comment block for information on calculating the Output Compare Register (OCR1A) (“Compare Match Register” in the comment block).

The next line enables the Clear Timer on Compare Match mode, which clears the timer once a match is made.

The next two lines are to set the prescaler to 1024, which is a value that was used to calculate the value for the Output Compare Register. The following table, found in the Atmega328p datasheet, shows how to set the bits in the TCCR1B register to set the prescaler.

Screenshot (6)

The next line enables the Output Compare A Match interrupt.

The last line enables interrupts.

There’s nothing in the while loop since all of our code is in the interrupt routine.

The bottom of the code is the Interrupt Service Routine (ISR) which is the code that runs when there is an output compare match with Timer1. In the ISR, we toggle the bits on PORTB and PORTD like we did in the other Hello World example. The ISR code is run every time there is an output compare match, which is set to 250ms.


So, uh, that’s it.

Trying to write this post was difficult. I need some more reading up and experimenting to do. I hope you got something out of this, at least a link to somewhere that’s more helpful.

Thanks for reading!

 

No more floor boards!

That is, no more boxes of circuit boards laying all over the floor!

I visited Dollarama today and picked up a couple of 3-drawer organizers. They are stackable so I now had 6 drawers to stick all of my circuit boards in.20150906_140626

Even better, I put a piece of cardboard across each drawer to divide each one so I now have 12 compartments to work with. The new set of drawers fit in well with my other Dollarama organizers to the left.

20150906_143224

Once I fitted all of the drawers with cardboard dividers and labels, I put a piece of tape down both sides of the drawer to keep them together. I found that they don’t snap together quite as snug as I would have liked. With the drawers in place, I put beads of hot glue to glue them down. Normally I’d tie the drawers down with a string (because I think that sort of thing looks kind of neat) but I don’t expect to move these drawers around and I think that this solution would be the best.

20150906_150257

This is what that area looks like now. I’d like to keep building up the drawers since they’re relatively cheap ($3) and I could always use more compartments to store things.

Time to throw out my old organizing boxes… 🙂