Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations IDS on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help to make a programmable timer... 1

Status
Not open for further replies.

Marton79

Electrical
Oct 30, 2006
6
Basically, I'm bothering because I want to make a programmable timer...
Here is the figure:

The timer must have three buttons: Two of them add a different, programmable number of minutes to the count on each press. The third one resets the counter to 0. As long as the count is not 0, the output is 1.
It should have a 3 digit display to see the remaining minutes.
The number of minutes to add can be programmed with jumpers.

Do I need to use a pic to make this? I saw the 4510 is a nice candidate to do it, the problem is that the counter is not cumulative, it just resets to preset value...

Any ideas?

Thanks!!
 
Replies continue below

Recommended for you

Well, you don't have to use a PIC to make it, but some type of microcontroller will certainly make it an easy task. It depends on your needs (size, power source, quantity, environment, budget, etc).
 
I can use a 555 for the timing, a 4510 for counting, and a 4511 for the display... that's perfect, the thing is that it doesn't "add" the preset minutes to the countdown... it just sets the counter to the preset minutes and starts over the countdown...
It is possible to do this "adding" thing without a PIC?
 
Why are you trying to avoid the PIC? Or any microcontroller? Get yourself a Basic Stamp. They are child's play to work with and you will have all functionality you need.

They are costly for a product. So if you are doing 100+ units, a PIC or other micro is what you should use. Nothing else is possible if you look at functionality vs cost. The situation where you need to add a twist to the basic function - or add a new function - will be much nicer if you do it in a micro. And it is not at all difficult to do the job. Give yourself a couple of weeks to get the fundamentals. And you will have your functions quicker than you can say "PCB with CMOS ICs"

Gunnar Englund
--------------------------------------
100 % recycled posting: Electrons, ideas, finger-tips have been used over and over again...
 
The thing is that I know 0 of PIC programming, that's why I prefer discrete components...
 
As I said: Just start using them. That's how everyone else have started. It is not something you are born with. And your timer is a very good project for a beginner. Not too complex. Very straight forward.

Gunnar Englund
--------------------------------------
100 % recycled posting: Electrons, ideas, finger-tips have been used over and over again...
 
Marton, I completely agree with Skogsgurra. I suspect that you can learn enough BASIC to provide the functionality that you're looking for in a very short time. Your resultant circuit will have fewer parts, be easier to debug, and much easier to add or change features.
 
You will be disappointed with the 555 etc etc route. Especially when you start talking about displays and adjustability. No engineer would go that way.. the Low Road.

Keith Cress
Flamin Systems, Inc.-
 
Thanks for all the suggestions. I program a little of C so I think PIC-C is the way. Will I have to use interrupts or anything like that? I see that far too complex... And I've been Googling for hours and could not find any timer examples in PIC-C... And delayms(1000) at the end of the loop to wait one second is not the way because I will need to read the switches all the time... It will be a loooong trip I believe...
 
I don't think anyone bothers to put PIC example code up on the Web because MicroChip will bury you in it for the asking.



Mike Halloran
Pembroke Pines, FL, USA
 
Hiya-

As the other gentlemen have suggested you might start with a PIC or maybe an ATMEL microcontroller.

I've added comments to one of your latter posts:

>Thanks for all the suggestions. I program a little of C so
>I think PIC-C is the way.

You might also look at cc5x (google for it). There is no cost for your first professional project, free if you just keep it in house. I have used this product and found it easy to use and I believe, there are examples of interrupt programming in the documentation. If you keep your object modules to less than 1K, there is no problem. Linking 1K object modules together is a little trickier and the documentation is not that clear, but I'm pretty sure that you can complete your enitre project within 1K.


>Will I have to use interrupts or anything like that? I see
>that far too complex... And I've been Googling for hours
>and could not find any timer examples in PIC-C... And

You could do it without interrupts. If your loop timing is
within the range of the timer resolution you set, you can just sit there and poll the timer expiration. When that happens, go and update your counter for timing. Now, if you want to multiplex your display, you don't want to do that.
Just check your timer expiration, then branch off and check your display update timer, check your pushbuttons etc.

>delayms(1000) at the end of the loop to wait one second is
>not the way because I will need to read the switches all
>the time... It will be a loooong trip I believe...

As mentioned, just poll the timer. If "popped" then service it, otherwise, go on and check the pushbuttons.

Or you can mix and match. You can poll for the pushbuttons, poll for the timer, and interrupt on another timer for the digit display.

As an aside, I did my FIRST pic project as an enlarger timer with two digits and a couple of up/down pushbuttons. Also had stored the timer value in EEPROM on the chip. I have only a two digit display and it only does seconds from 0 to 99. Did it in assembly.

I might suggest starting with small little portions of the project and stitching them together. Like start with the display and see how that goes. Then maybe add a 1 second timer. Then output the incrementing time to the display. Then work on the pushbuttons. I suggest this as an approach as the pushbutton debounce can be a bit tricky for someone that hasn't done a lot of real time programming.

Of course, you could do this all with an ATMEL brand of microcontroller. I just started out with the PIC and haven't had a need to change.

Last week, I wanted/needed an RS232 to 4 digit LED display. I started the project last week and its finished as of Sunday.

The nice thing about doing the learning curve is that after you have gone through the experience, you have gained such a usefull tool!

I did not go through the basic stamp route due to cost and prior experience with real time programming on other micros. The cost of my "middle of the road" pics is around $2.00 in quantity 25. Yep. I buy them in that price break. I've got them on hand for little tasks like the one mentioned above. You don't have to do that of course. In fact, I think that Microchip will still send you some small number of them for FREE! I did it but, it took awhile for them to show up. I had ordered some from Digikey at the same time and the Digikey came in much quicker. A couple of them at first will get you by.

The other things that I suggest you have are:

1. Programmer. I built my own first one. Google for them. Even if this is a home project you might want to save yourself the hassle and buy a kit rather than trying to wire up a protoboard. Just my experience.

2. One of those little push in prototype boards. I find this useful for whomping up little portions of the project and starting the coding effort quickly. They have a tendancy to be a little unreliable, but it's very quick for making changes. I use solid copper wire from phone company cables (the 25 pair type) as interconnect wire.

3. Logic probe. I debugged the enlarger timer with just that. An oscilloscope would be much nicer sometimes. And a logic analyzer too. But we're talking some serious bucks here. Oh, before the logic analyzer, I found a PC parallel port logic analyzer. It was horrible, but it did give some semblance to the order of things.

4. A colletion of 1/4W resistors in the following values:
330ohms, 470, 1K, 5K, 10K. These usually will get you by for the digital side of things. Some 0.01 microfarad bypass caps. A collection of LEDs. These can be used as debugging aids. If you don't have a fancy single stepping emulator/debugger, you can have the LEDs act as "you are here" printf type debugging aids.

5. Power supply of some sort. Sometimes you can just find a 5V DC wall wart and you are set! Other times you might have to put a small linear voltage regulator (with an optional full wave bridge rectifier and filter cap) on a wall wart. You typically can get by with 100ma or so for the digital portion of it. Maybe a 1A if you want to drive some relays, etc. Look around the house for one that went to a discarded electronic appliance. Honest, I *STILL* use the little linear 1A supply that I made in college 30+ years ago! It's a little "bud" box with a 7805 in a TO3 package. A Radio shack transformer and a fuse that if memory serves me well, was sleeved with a plastic soda straw.

6. A digital multimeter or VOM, makes life easier and they can be had from Harbor Freight for $3.99 on special.

That's about all that you need to get started. I've prattled on long enough.

Hope this helps to get you started!

Cheers,

Rich S.
 
Hear! Hear! Just what I wanted to say.

A few quotes:

"You could do it without interrupts. If your loop timing is within the range of the timer resolution you set, you can just sit there and poll the timer expiration."

"The nice thing about doing the learning curve is that after you have gone through the experience, you have gained such a usefull tool!"

And. By setting up a simple Finite State Machine, you can do lots and lots of things seemingly simultaneously. Even with a slow and simple processor. The idea is very simple (and the implementation even more so). Draw a stae transition diagram (sounds more compliceted that it is). Number the states and write conditions for changing states. Then just code on. A beautiful and powerful technique. You can even have several FSMs running in parallel in one processor.

PLS for Rich

Gunnar Englund
--------------------------------------
100 % recycled posting: Electrons, ideas, finger-tips have been used over and over again...
 
Rich, many, MANY thanks for your kind explanation!!
I have most of the stuff from your suggestions (the only one I do not own is the o'scope which is @ work).
The one thing that will make me suffer at least when starting to program those tiny things (I think) are the timers... for most of the other stuff, I've been reading an excellent tutorial made in my mother tongue (spanish) and it helped a lot. It has many examples of how to drive a display, and do the 'strobing' for more than one, so that's more than a nice start.
There's even more. I found a project that "almost" does what I want:
Its operation, circuit and code are MORE THAN WELL explained.
I will need to modify it so instead of having it started at the count set by the dip switches, it just ADDS the count set by the dip switches. I can add one more switch to the free leg of the 74HC138, and gain one more input if I need it, by removing one of the displays (I only need to count the minutes, in three digits).
As it is now, it can count to 99 minutes and 59 seconds... so by applying the previous change, I will need to modify the code to allow from 1-999 minutes and make the seconds count internally.
Going back to the timers, if I'm right, each instruction takes 1msec to execute? I see on many examples that there are many NOPs one next to each other, to generate a delay... how do you calculate that stuff? I mean is it THAT critical? I think that if I add too many lines of code, Wouldn't I screw up the 'second' counter?
I guess that's the "tricky" part about the microcontrollers... the instruction set is not 'that' big so it shouldn't be hard to learn how it works...

Many thanks again!!

Marton
 
For PICs, the internal instructions run at 1/4 the clock rate. If you use the internal 4 MHz clock found on most PICs, then yes, one instruction will be 1ms in length.

It can be critical, but not necessarily, it all depends on how you set up your code. For example, let's say you run a tight loop that needs to use up 20ms to time your minutes out (neglecting interrupts for the moment). If you mistakenly use only 18 instructions, your timer will be off by 6 seconds for every minute it tries to count, which can add up. If you use timer interrupts, you set a register value once and forget it, no need to worry (too much, anyway) how much code you write (within reason, of course).

The trickiest part about PICs (or any new micro, for that matter), is getting the part set up correctly. An incorrect setup can make a fully functional part act like a door stop. Starting with a known-working program for that particular chip is a good way to get past that stage, then make changes one at a time to the setup to make sure you're not breaking anything.


Dan - Owner
Footwell%20Animation%20Tiny.gif
 
I think there is a mixup. The 4 MHz PIC needs 1 us (microsecond) not ms (millisecond). So it is one thousand times faster than said.

If you saw NOPs to adjust timing, I can only say: Burn that book. That is very bad programming. Either poll a timer or use interrupts. Polling a timer is less critical, if you worry about interrupts.



Gunnar Englund
--------------------------------------
100 % recycled posting: Electrons, ideas, finger-tips have been used over and over again...
 
The earliest, slowest BASIC Stamp runs at ~2000 instructions per second. PICs are much faster.

Please, do not even try to set program timing with NOPs, loops, and instruction counts; it's a waste of your time, that's what hardware timers are for, and they're not hard to use.

Get a Microchip CD, load the simulator, and work through a few application notes.



Mike Halloran
Pembroke Pines, FL, USA
 
Hiya-

O.K. now for the bad side of things. The 16F84s needs an external timing device to operate. Some of the newer chips
have internal oscillators that can allow the devices to self clock. HOWEVER, I have found that the internal oscillator is somewhat temperature dependant.

I did a "geek clock" (bcd with descrete LEDS) and used the internal oscillator and it lost or gained time depending on the season. Even in the air conditioned office.

This might not be a problem for you, just something to keep in mind if you want to do a time of day clock in the future.

Now, onto your other questions. First, might I suggest that you look at one of the newer PIC chips. The 16F84As have been replaced two generations ago with the counterpart of a 16f648A. IT DOES TAKE A DIFFERENT PROGRAMMER AND YOU HAVE TO ADD TWO MORE INSTRUCTIONS IN THE STARTUP CODE. Yes, I did have to shout that (sorry). The 16F648A has a full 4K of program flash memory, and many more "general purpose" registers (RAM). It also has another timer or two.

There are 4 digit displays out there and they can be used. They are available in both common anode and common cathode versions. I believe from the schematic that they use a common anode device. I might suggest a search for 4 digit (or 3 digit) common anode devices that have only 12 pins on the device. Will save a LOT of wiring if you are going to do this via the protoboard method described in the web page!

Even doing a home built PCB (Like I do. 4 digits times 8 or 9 pins (with the decimal point), is 36 holes to drill versus the 12 the other displays have. Tip. The displays that I am talking about are 0.6 inch width pinouts with 0.1 inch between pins on the same row. Get like a 24 pin wide socket we use for static ram or old Eproms and cut it half for the socket for the display. I suggest using machine pin sockets for this.

Yep, the circuit is a pretty straight forward implementation.

Now, as to using timers. Well, the simple example of doing a counted delay loop is all well and good if all you are doing is waiting. But, for this timer, I don't think that you desire to have the display go blank and show up for a brief spurt every second or so.

You should expect to have to change the display and show each digit about 50 or 60 times each second. Otherwise you might get flicker in the display.

O.K. that being said, we have to be doing something else while we are timing. So, we have to have other code executing. That code (most likely) will have one or two or (many) more branches in it. Each one with its own different execution time as the microcontroller goes through it's code. Therefore, it gets a tad bit complicated trying to figure out the timing variations in each code path.

I strongly suggest that you bite the bullet and learn to use the timers. You don't have to have them interrupt driven. The interrupts on the PIC timers can be disabled and one's code can poll the timer rollover bit to determine a fixed length of time. Please note that these are "up counters" and will "pop" as they roll from FFFF (or FF) to 0000 (or 00). So for a shorter time, put in a higher integer value into the timer/counter register.

You can also partition the problem. Try doing a one second timer example bit of code with a simple LED that turns on,
starts the timer and turns off. Or, better yet, get it to toggle (if it's on, turn it off, if it's off, turn it on) and get the LED to blink at the one hertz rate. That will get you the confidence of getting the timer operation to work more or less as you expect.

Of course, this is AFTER you get the PIC set up correctly as macgyvers2000 has said. Here a little delay loop blink first to get your toolchain and configuration words set up will get you over the first and biggest hurdle. Sorry, I should have had this paragraph much earlier.

After you get both the delay loop blink program and the timer blink working, then might I suggest that you get the multiplexed display working. Just put in fixed values for each of the digits. For example, see if you can get the display to show a 00, then a 01, maybe a 12, then a 34, etc.

After you get the display working the way you want it to, now you have a real good debugging tool in its own right. You can for example, read the BCD switches and display them on the leds.

You can check your BCD arithmetic, take a couple of sums and put that on the display.

Check your timer counting logic and display that. Start off at a preset value and count up (or down) once each second.

Put those all together and you have your program!

Oh, and you can keep all 4 digits, if you want. There is a spare output on th 138 (y3) that you can use like the start button.

And instead of the "point to point" wiring that they used in the project, might I suggest that you look into wire wrap and a resistor netork. You might also find some diodes in a 16 pin pack. Or at least descretes soldered onto a 16 pin header that fits in a wire wrap socket. I think that you will find the assembly much easier. I KNOW that there are some integrated circuits with PNP transistors in a DIP package (I just can't remember them off hand, sorry). Maybe someone else has a part number handy. Indeed, you can get DIP switches and use them in place of the rotary BCD switches that are used there in the web site.

Just some suggestions.

Well, I see that I've prattled on long enough AGAIN!

I hope that this gives you some food for thought. Now, with some thought, we might be able to reduce the parts count somewhat, but we can do that after we get this one done.

Best of luck! Keep us posted!

Cheers,

Rich S.
 
Gawd Rich!! Why don't you just build it and mail it to Marton![lol][lol][lol]


You could've fixed your geek clock. You use one more output to a resistor and an input to control the temperature of the CPU. You just keep it at five degrees more than any expected ambient and Wha-law! No more drift. You could even brag about your "oven controlled oscillator" as found on really high end 'reference' stuff.

Keith Cress
Flamin Systems, Inc.-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor