coin timer. pulses pics

Status
Not open for further replies.

RMIM

Member
HI All,

I need some help on building an all-in-one coin timer/electric/gas meter.

The first thing I have to learn is how to use/read pulse outputs using a pic.

----------------------------------------------------------------------------------------------------

Background:
Currently I have 3 coin operated meters.
1. An old mechanical pound operated meter. Like this **broken link removed**

2. A new digital £1/20p timer that is hooked up to the central heating thermostat to charge for heating.

3. An old mechanical 10p timer that got jammed so I rigged some electronics to it making it digital (4060 14 bit binary counter) which is hooked to a solenoid to charge for hot water.

I also have plans to add to that a washing machine timer and perhaps a gas meter.

For the electric and gas I will use units that give out pulses (1000 pulse per kilowatt) but will deal with this at a later date as I believe it to be more complicated than the other parts I have to tackle. With the electric I have to record how much money has been deposited (say £2) then when 1000pulses has been received I have to deduct £0.15.

So that's a grand total of 3 timers (that may have to work simultaneously) and 2 meters (count pulses and add/deduct credit)

Is it possible to do all that with a single pic?

------------------------------------------------------------------------------------------------------------------

The coin verifier I have uses a pulse output when it recognises a coin. You can set the output from 1 to 50 pulses over 20ms, 50ms or 100ms. (Im guessing slower is better for accuracy?)
**broken link removed**


Coins used and pulses I have assigned to them

£1= pulses 1
5p = pulses 5
10p = pulses 10
20p = pulses 20


I don’t really know much about pics and programming (beginner) but I can hook one and up and flash it.

I want my system to be accurate - when a £1 goes it registers as that and nothing else, so how should I set my pulses to output?

How good are pics at reading pulses? - for example if I set my pulses to output 1p = 1 pulse, 2p = 2 pulses, 5p = 3 pluses 10p= 4 pulses etc. So there is only 1 pulse difference between each coin, could the system not make a mistake and only say read 3 pulses instead of 4 giving an incorrect reading?

Would it be sensible to space out pulses for example 10 pulses, 20 pulses etc and get the software to detect 9 to 11 pulses = £1.

What I need help with to start off is getting the 2 timers to work with my coin verifier working on a pic16F628A. The rest of the project is for much later.

Thanks.

edit im not sure about the pulse output if it means over 100ms or 100ms is the length of the pulse - will hook it up to scope later.
 
Last edited:
Pics can recognise pulses very well. I would say perfectly. The only problem you could have is glitches, where very short pulses from something turning on or off could be registered as a pulse, so I suggest debounce code. That would basically enforce a minimum on time and a minimum off time.

For instance if the coin meter generates pulses that are 10 ms long, with 10 ms gaps, I would suggest ignoring any pulses shorter than 5 ms or any gaps shorter than 5 ms.

You also need to make sure you have good power supply decoupling, and maybe a capacitor that would keep the PIC running for 1/2 second if the power fails. That way a short break in power won't be noticed. A startup timer of 5 seconds or so is a good idea, where the processor does nothing for that time to be sure to get over all start-up funnies.

There is no problem at all having several timers running in a PIC. The hardware timers only have a short maximum duration (if you are running the PIC at MHz speeds) so to get more than 1 second, you need to do the timing in software. If you are doing that, you can just have several software timers.

I usually have a hardware timer to give fast time base, maybe 10 - 100 times a second. You can use an interrupt for that, although I use a routine that looks for the timer overflowing. Each time the hardware timer overflows, each of the software timers is incremented and tested to see if it has timed out etc.
 

Hi Diver300,

Thanks for the reply. So does that mean with the pic I have I can have 3 simultaneous timers or more as it just software? (As I was thinking I could only have 3 timers as I read the spec as having Timer0, Timer1 and Timer2). Are these what you refer to as hardware timers?

My hot water timer needs to be able to time 5min to 15min (depending on amount deposited)
My central heating timer needs to be able to time from 1hr to 12hrs.
My washing machine timer needs to be able to time to 2hrs. (depending on amount deposited)

Attached is the output of a 20p coin (20 pulses).

So is all of the above possible with my 16F628A?

Thanks.
 

Attachments

  • 20p.jpeg
    126.1 KB · Views: 157
Yes, Timer0, Timer1 and Timer2 are the hardware timers. Timer1 is a 16 bit timer, and has a prescale of up to 8. So it's maximum count is 524288. If you are running with an 8 MHz clock, executing 2 million cycles per second, that timer will overflow around 4 times a second.

Now thousands of applications use PICs for far longer timers than that, so the software has to count how many times the hardware timer overflows. You'll need several registers to count to 2 hours.

2 hours is 28800 times more than 1/4 of a second, so you need two registers for a that. Even 5 minutes is too much to go from 1/4 second with a single 8 bit register.

So each timer can easily be done with two registers, so 6 registers are needed, which isn't a big dent in the 224 that you get in a 16F628.

The biggest limitation with the 16F628 is that the 16 bit timer can only divide by 65536. The simplest scheme is to run the processor at a frequency that can be divided by 4 x 65536 and still be a whole-number frequency. If you run at 19.6608 MHz, with one of these:-

https://uk.farnell.com/9712542 then you can run timer1 from that with no prescale, (except the 4 that you always have as pic16s take 4 clock cycles for one instruction). That will overflow 75 times a second. Then you can either have an interrupt from timer1, or check the T1IF bit regularly.

Either way you have some code that runs 75 times a second. That code increments a register. When that gets to 75, reset it to zero, and go and increment the three timers you want.

(If you have a 20 MHz clock, all the divides get rather messy)

As far as I can tell from your picture, the pulses are very slow. It seems to be about 4 pulses per second. You could easily count that with the 16F628. I would run the pulse counting code as 75 Hz as well. I suggest that you use that to time how long the pulse is seen as high, and the how long that it is seen as low. It should be low for at least 50 ms (3 times round the code at 75 Hz) before it is considered low, and similarly for high. When it has been high for more than 250 ms (19 times round the code at 75 Hz) then the pulse stream has stopped, and you can decide what to do with the count.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…