Ok, I think I have the loose makings of the necessary code for a rolling 1 second window to check whether the frequency has remained the same...
(the signal to be monitored is fed into comparator1)
Code:
INCLUDE "DT_INTS-14.bas"
INCLUDE "ReEnterPBP.bas"
INCLUDE "Elapsed_INT.bas" ; Elapsed Timer Routines
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _timer1_interrupt, PBP, yes
INT_Handler CMP1_INT, _comparator_interrupt, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
@ INT_ENABLE TMR1_INT ; Enable Timer 1 Interrupts
@ INT_ENABLE CMP1_INT ; Enable comparator1 Interrupts
nochange_count var word 'count number of 'timer overflows' where frequency hasn't changed
comparator_count var word 'a variable to count number of comparator interrupts (frequency detection)
loop:
if nochange_count > 3 then 'ie frequency has remained the same for 1 second
nochange_count = 0
gosub dostuff
endif
goto loop
'---[Timer1 - interrupt handler]--------------------------------------------------
timer1_interrupt: 'prescaler set for circa 250ms
if comparator_count = previous_comparator_count then 'needs a 5% 'deviation' allowable error to go here!!
nochange_count = nochange_count+1 'count how many times frequency has NOT changed
else
nochange_count = 0 ' if frequency has changed, then zero counter and start over
endif
previous_comparator_count = comparator_count
comparator_count = 0
@ INT_RETURN
'---[Comparator1 - interrupt handler]--------------------------------------------------
comparator_interrupt:
comparator_count = comparator_count +1
@ INT_RETURN
There may be some syntax errors in there (I'm at work!), but that's the basic framework - what I don't know how to do is the 'deviation' part (bolded in red above).
How can I have an 'if' condition that checks to establish if present count is 'within 5%' of previous count' (plus or minus), therefore....
if comparator_count = previous_comparator_count '+ or -' 5%
Two 'AND'ed ifs?
Many thanks!
Bookmarks