Timer at 16F1509

Thread Starter

FroceMaster

Joined Jan 28, 2012
556
Hi,
Have to make a little MCU with some small things.
Need to have a indication when ex 15 min is reached.
Have planed this.,
Use a 16F1509 ( Spare one i have :)
MCU Running at FOSC 8 mhz.
use TIMER2
Input 2 mhz (FOSC/4)
PRESCALE 64 = 2.000.000 / 64 = 31.250 hz,
Set Compare (PR2) to 31.250
Rollover at 1 sec. approx.
Set POSTSCALE to 15 = 4 times pr minut.
Set Interrupt and add one to variable "numbers_of_runs"
"number_of_runs" up to 60 = 15 minutes. give or take some seconds.

Is this an easy way to do it, ??

Here is from datasheet.
16f1509 timer2.PNG

And in main code just a loop that check if "numbers_of_runs" = 60 then do something

Know it's overkill to use that MCU, but all i have.
 

jpanhalt

Joined Jan 18, 2008
8,511
Have you considered adding a very inexpensive tuning fork oscillator to the T1OSI/T1OSO pins to give you a 2^n clock that is probably more accurate than the MCU's internal oscillator and can be time much longer intervals?
 

jpanhalt

Joined Jan 18, 2008
8,511
So, what's your question?

Counting rollovers seems pretty straight forward. It's what I do to time long periods, and I don't find adding the crystal (a few pennies) complicated.
 

Ian Rogers

Joined Dec 12, 2012
689
Seeing that the MCU is doing nothing else... The internal OSC is precise enough... Just count.. No timers no interrupt.. just count.

set a mS delay then count 1000 for a second delay, yada yada yada....
 

AlbertHall

Joined Jun 4, 2014
9,047
2,000,000 / 64 = 31.25kHz (not Hz)
PR2 can be set to only an integer value between 0 and 255 (not 31.25)

If you do as you describe except set PR2 to 200 then use a 16 bit variable for 'number of runs' and stop at 600,000 then it will work.
 

AlbertHall

Joined Jun 4, 2014
9,047
Seeing that the MCU is doing nothing else... The internal OSC is precise enough... Just count.. No timers no interrupt.. just count.

set a mS delay then count 1000 for a second delay, yada yada yada....
This would be easy to do in XC8 code as there is the __delay_ms(xxx) function which will give a correct delay. There is a maximum limit for xxx but it would certainly handle 1000 to give a 1 second delay.
 

AlbertHall

Joined Jun 4, 2014
9,047
2,000,000 / 64 = 31.25kHz (not Hz)
PR2 can be set to only an integer value between 0 and 255 (not 31.25)

If you do as you describe except set PR2 to 200 then use a 16 bit variable for 'number of runs' and stop at 600,000 then it will work.
You won't get 600,000 in 16 bits either. It will need 24 bits!
 

Ian Rogers

Joined Dec 12, 2012
689
This would be easy to do in XC8 code as there is the __delay_ms(xxx) function which will give a correct delay. There is a maximum limit for xxx but it would certainly handle 1000 to give a 1 second delay.
This was only an issue with pic18... The pic16's and 12's were fine with 16 bit values..
 

AlbertHall

Joined Jun 4, 2014
9,047
15 minutes is 900,000ms so the dunction would need to be able to handle 24 bit values which I don't think it will.

[EDIT] Reply to #11
 
Last edited:

Thread Starter

FroceMaster

Joined Jan 28, 2012
556
Delay 100 ms
Add one to variablesecund
If value==600 (1 minut gone)
Add 1 to minutcount
Variablesecund=0
If minutcount ==15 do my thing
Repeat

Not a chance of getting over 16 bit
 

djsfantasi

Joined Apr 11, 2010
5,970
Delay 100 ms
Add one to variablesecund
If value==600 (1 minut gone)
Add 1 to minutcount
Variablesecund=0
If minutcount ==15 do my thing
Repeat

Not a chance of getting over 16 bit
Works if “do my thing” resets minutcount. Otherwise once the count reached 15 minutes, it won’t trigger until the variable overflows to a value less than 15.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
556
Have now the programming part ok, BUT
will this setup not damage the MCU. ?
Get 12V into the MCU when switch (1513) not is activated ?
Skærmbillede 2020-01-03 05.37.47.png
 

AlbertHall

Joined Jun 4, 2014
9,047
The pins of the mcu should always be between 0V and the supply voltage (except MCLR during high voltage programming) otherwise the chip will be damaged.
 

peterdeco

Joined Oct 8, 2019
76
I program in basic using PicBasic Pro. To activate something after 15 minutes I would use this. I have done this with a 16f819 with 4MHZ internal oscillator and found the accuracy to be within 2 minutes per hour.

x var byte 'byte variable used to count
clear 'all variables = 0
trisa=%00000001 'all porta outputs except porta.0 with pushbutton and 47k pullup
trisb=%00000000 'all outputs
porta=0 'all porta outputs low
start:
nap 0 'greatly reduces battery drain
if porta.0 = 1 then start 'stay here until button pushed
timercount:
let x = x + 1
pause 60000 'wait 1 minute
if x >= 15 then high porta.1 : goto waithere 'activate device and remain idle after 15 minutes
goto timercount
waithere:
nap 0
goto waithere 'stay here idle
 
Top