PDA

View Full Version : Need little help with maths - Timer based application



financecatalyst
- 19th June 2010, 11:32
Hi, I am trying to create a timer application which tracks 30minutes, 1Hr, 1h30min, 2H, 2H30min. It does not need to be very precise, variation of upto a 2 seconds is fine.
I am using 16f676 & using 16bit timer1 for this.
PIC-timer calculator says interrupts are generated 524.295mSec @4Mhz & 1:8 prescaler.
I have a word variable which works as a counter for interrupts & tells me that certain time has elapsed.
My problem is that I am not sure if I have calculated the delayset values right as calculator disagrees with me.
Code looks like this:


delay var word
delaystop var word
redled var porta.0
;---------INTERRUPTS DECLARED HERE------------------------------
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _delaystop, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM

T1CON=$31

;-------------INTERRUPTS ENABLED HERE-----------------------------
@ INT_ENABLE TMR1_INT ; Enable external (INT) interrupts
while 1
if button1 then
delay=0 : delayset=2060 ; 30 minutes
endif

if button2 then
delay=0 : delayset=4120 ; 1Hour
endif

if button3 then
delay=0 : delayset=6180 ; 1Hr 30 minutes
endif

if button4 then
delay=0 : delayset=8240 ; 2 Hours
endif

if button5 then
delay=0 : delayset=10300 ; 2Hrs 30 minutes
endif
wend

delaystop:
delay=delay+1
if delay=delaystop then
redled=1
endif
@ INT_RETURN

Darrel Taylor
- 19th June 2010, 17:36
30 minutes = 1,800 seconds:
1800 / 0.524295 = 3433

1 hour = 3,600 seconds:
3600 / 0.524295 = 6866

1.5 hours = 10299
2 hours = 13732

And maybe it's somewhere else in the program that wasn't posted, but ..
It looks like you are adjusting the value in delayset according to the buttons.
Then comparing the delay time against delaystop in the handler.

Oh, and delaystop is both a variable and the Label to the Interrupt handler.
<br>

hth,

financecatalyst
- 19th June 2010, 19:05
Got that!
It is my typing mistake to put delay=delaystop in ISR, though it actually is delay=delayset
Thanks