Introduction: 24hr Timer: Coding for and Applying to a Circuit
This is a school project we did to get to grips with programming chips, working with different boards and coding.
In this project I will use Flowcode (the software we were given to make our program on) to programme a PIC and make an LCD display a clock that accurately counts seconds, minutes and hours, whilst a Relay acts as a 'ticker' to mark the passing of each second.
It's not as neat as it could be, due to me being fresh to using Flowcode, but the project was an amazing to help me understand the main principles of both programming and constructing a circuit. I've tried to make this as easy to follow as possible and to talk you through how to use Flowcode so even if you're new to it (like I was) you get a good understanding of what is where and what goes where.
You can download Flowcode for free from their website and have a play about yourself. I hope this Instructable isn't too hard to follow and if you have any questions ask away in the comments and I will endeavour to answer.
Now without further ado lets get stuck in :)
Step 1: Starting Up Your Flowcode
1.When you first load up Flowcode select New Project, this will then give you a list of options for which chip you want to program for, the chip in my PIC is a 16F1937 but you select whichever chip you have
2.This will start a new project for you, but whilst you’re here you want to make sure you have your chip visible (just for reference) as well as your 2D Dashboard panel as this is where you will be able to interact with all the simulated inputs and outputs you choose to put on your PIC
•Bring up your 2D Dashboard panel by clicking on view and enabling it
Step 2: Inserting Our LCD
3.Select an LCD output for our timer to print on to by going to the outputs toggle on the top bar and clicking LCD. This will put an LCD in to our 2D Dashboard panel
4.Right click on the LCD to bring up the properties list and connect our LCD to PORTB
Step 3: Programming Our LCD
5.To make our program less messy I put my code in a macro, but for simplicity sake you can just put the LCD setup straight in to our program line by doing the following;
6.Insert a component Macro (on the side bar and drag it in to place between our ‘Begin’ and ‘End’ sequence. Double click on it and it should pop up a ‘Properties Macro’ box, on the list of components click the small plus sign next to LCD to expand the list, then select the ‘start’ command. Press okay and this is the component command that will start our LCD
7.Insert another component macro underneath and select the component this time as LCD ‘clear’, this erases anything that was previously on our LCD
8.Insert component macro underneath the previous one and select the component as LCD ‘cursor’, this will show parameters in the bottom that you can change, we want to set the x to 0 and the y to 0. This outlines where our numericals and letters will print on the LCD
9.Insert component macro underneath once again and select the component as LCD ‘print string’. In the parameters section we want to change our expression to ‘”Hrs Mins Secs”’, putting quotation marks (i.e. “ “) around the expression as seen in step nines image. This is our LCD top line set up and ready to use
Step 4: Setting Our Clock at 0
10.On the left hand side, drag a calculation underneath your LCD setup and double click to bring up the calculation properties. This is where we set it up so that the clock starts from zero.
11.We do this by making new ‘variables’ for each of Hrs, Mins and Secs. The initial value for each should be 0, click OK and set up the variables as illustrated in the red box in step 12
12.In the calculations box select your variable ‘Hrs’ and write ‘Hrs=0’, then press enter to get the line below and select your ‘Mins’ variable and write ‘Mins=0’, press enter to get the line below and then enter ‘Secs=0’ you want it as shown in the blue outlined box
Step 5: Making Your Clock Count
13.From the left side bar drag a loop in to your circuit underneath your previous work, this will make the clock continue clocking
14.Insert a calculation in to the loop that will count up our seconds by inserting in to the calculations box ‘Secs=Secs+1’
15.Drag an output box from the left side bar underneath your calculation, we’re going to set the output up to the relays we’re going to have attached to PORTC of our PIC
16.Clicking the output brings up the ‘Properties output’ and here we went to set out variable as 2 (this will turn on the second relay on our relay board, the click of the relay coming on and off will simulate the ticking of a clock every second)
Step 6: Making Your Counter 'tick' Courtesy of the Relays
17. To simulate the relay board in our programme, insert an LED array output, making sure to go in to the properties menu to connect it to PORTC - where our relays will be connected to our PIC
Step 7: Creating Your Seconds Timer
18.The next step is to display your counting seconds on the bottom line of your LCD display. You do this by inserting a component macro and setting the ‘cursor’ to 10 on the x axis and 1 on the y axis, this will display your counting seconds in the red boxes shown. My LCD display is 16 across and 2 down which is read from the top left hand corner as 0,0 and the bottom right hand corner as 15,1
19.Insert a delay from the left side bar underneath our LCD cursor, and double click it to bring up the properties. Set the delay value as 1 second. This will be our second timer.
Step 8: Printing on to the LCD
20.The next stage is to print our seconds on to the LCD, this is done by inserting a component macro, selecting ‘Print Number’ from the menu and setting our expression as ‘secs’
21.To turn the relay off in time ready for it to tick again at the next second, we put a 0 output to PORTC by inserting an output and setting the variable as 0
22.Now put in a delay of 10milliseconds to allow the relay time to turn on and off in the circuit. Without a delay the off signal doesn’t have enough time to register in the circuit to turn off the relay. Do this by inserting a delay and setting the time properties as 10ms
Step 9: Transitioning Between Seconds to Minutes
23.Time to make a decision so that when your seconds counter hits 60 it adds a minute to your clock and resets back to zero, this is done by inserting a decision and setting it for if ‘secs=60’
24.And then inserting the following within this decision;
•Inserting a calculation for secs=0
•Inserting a component macro on the LCD cursor in position (x:10, y:1)
•Inserting a component macro on the LCD that prints a string code of “ “ (so that it doesn’t print anything as it adds a minute)
•Insert a calculation now so that the minutes start counting: Mins=Mins+1
•Insert a component macro on the LCD cursor in position (x:6, y:1) this will count the minutes below where you set your Minutes read out on the LCD
•Insert a component macro on the LCD that prints the Minute number
If you’re struggling to remember how to do the previous steps, go back and take a look at the previous stages which break it down a bit more
Step 10: Transitioning Between Minutes and Hours
25.This stage is essentially the same as the last but modified to count minutes instead of seconds. Time to make a decision so that when your minutes counter hits 60 it adds an hour to your clock and resets back to zero, this is done by inserting a decision and setting it for if ‘mins=60’
26.Starting the counting of the hours can be done by
•Inserting a calculation for mins=0
•Inserting a component macro on the LCD cursor in position (x:6, y:1)
•Inserting a component macro on the LCD that prints a string code of “ “ (so that it doesn’t print anything as it adds an hour)
•Insert a calculation now so that the minutes start counting: Hrs=Hrs+1
•Insert a component macro on the LCD cursor in position (x:1, y:1) this will count the hours below where you set your Hours read out on the LCD
•Insert a component macro on the LCD that prints the Hours number
•If you’re struggling to remember how to do the previous steps, go back and take a look at the previous stages which break it down a bit more
Step 11: 24hrs Has Been Exceeded?!
27.Now what happens once you’ve hit 24hrs? Well, I want my clock to return to zero and stay at zero (but you can easily modify this with a bit of ingenuity) as it is more of a 24hr timer than an actual clock. Insert a decision with the parameters that if ‘Hrs=24’
28.If you want a 24hr timer like mine then:
•Insert a loop that will make the circuit remain at 00:00:00 once it has passes 24 hours
•Within the loop put a calculation of Hrs=0, Mins=0 and Secs=0
•Insert a call component macro on the LCD that will clear the bottom line (Y:1) à
•Insert a call component macro on the LCD that prints a string of “00 00 00 “
•(That’s 00 space space space 00 space space 00 space space space)
Step 12: Testing Your Simulation
29. Now the best way to make sure your program works is to run your simulation at every stage to see how it works. You can do this by pressing the play button at the top of the taskbar and then pressing stop once you're happy. Here I’ve run it all the way through and after watching it count through 24hours it has gone to 0 as I have requested of it
Step 13: Compiling Your Program on to Your Chip and PIC
30.To get your written program on to your chip plug your PIC via USB cable in to your computer, and turn on your PIC power supply . Click the button ‘Compile to chip’ which will pop up another window in Flowcode ‘compiler messages’, wait for it to display that it has finished and it will have sent your program to your chip and made your board fully functioning
But that's the programming over, easy right? Time to crack out the hardware.
Step 14: Components
•USB cable (from PIC to USB, mine was a standard printer USB cable)
•Power Supply Unit (to plug in to your PIC and give it power)
The components I’m using are all part of a Matrix set we were given to use at work and the PIC I’m using contains the 16F1937 microchip. This is a picture of the tray we were given along with our PIC, I've boxed the components we need so you know what you're looking at throughout the rest of this tutorial.
Step 15: Assembling Our PIC
31.Start off by inserting your Relay Board in to PORTC
32.Now insert your LCD Board in to PORTB
33.Plug your PIC to USB cable in at both the computer end and the PIC end
34.Plug in your power supply unit
Step 16: Wiring Our PIC
Wiring up the PIC using a screw driver to loosen and tighten the screws in the tops of the pins, it doesn’t really matter too much which type of electrical wire you use as long as it’s copper and is the right length (to illustrate where the wires connect I’ve gone over my picture with corresponding colour lines
35.Connect a wire from the +14V on your PIC to the +V on your Relay Board, this will supply power to your relays (see brown wire)
36.Connect a wire from GND (ground) on your PIC to GND on your Relay Board (see red wire)
37.Connect a wire from +V on your PIC to +V on your LCD Board (see white wire)
Step 17: And It Is Done!
Once your PIC has been assembled and your program put on to your chip have a play about, it should count seconds, minutes and hours perfectly as a 24hour timer and then set to 0 once it’s maxed out. Because of the relays mine now also ticks every second like a real clock! Fun project and a lot easier to do than I’d anticipated.
Step 18: To Take-away
I've attached the Flowcode document that I used to make my 24hr timer, so you can mod it, take a look at it or play about for yourself. It works perfectly with my PIC so fingers crossed it transitions the same for yours. I've also attached above a Powerpoint of all the steps in this tutorial so that you can have leftovers for breakfast should you need them.