Hello....
I have been looking at this code all the day and cant find my mistake
i am using a 420Hz frequency as input to the Comparator in 16f628, the signal is correctly biased to 2.5v and Vref is set to Vcc/2.
So now... i am trying to sample this signal into a Bits array and then sending through serial port.
Code:
<font color="#000080"><i>;----[ Change these to match your LCD ]---------------------------------------
</i></font>LCD_DB4 <b>VAR </b>PORTA.6
LCD_DB5 <b>VAR </b>PORTB.6
LCD_DB6 <b>VAR </b>PORTB.5
LCD_DB7 <b>VAR </b>PORTB.4
LCD_RS <b>VAR </b>PORTA.0
LCD_E <b>VAR </b>PORTA.7
LCD_Lines <b>CON </b>2 <font color="#000080"><i>' # of Lines on LCD, 1 or 2 (Note: use 2 for 4 lines)
</i></font>LCD_DATAUS <b>CON </b>50 <font color="#000080"><i>' Data delay time in us
</i></font>LCD_COMMANDUS <b>CON </b>2000 <font color="#000080"><i>' Command delay time in us
</i></font><b>INCLUDE </b><font color="#FF0000">"LCD_AnyPin.pbp" </font><font color="#000080"><i>; *** Include MUST be AFTER LCD Pin assignments ****
</i></font><b>INCLUDE </b><font color="#FF0000">"DT_INTS-14628.bas"
</font><b>INCLUDE </b><font color="#FF0000">"ReEnterPBP.bas"
</font><b>INCLUDE </b><font color="#FF0000">"modedefs.bas"
</font><b>INCLUDE </b><font color="#FF0000">"Elapsed_INT628b2.bas"
</font><b>DEFINE </b>OSC 4
<b>DEFINE </b>HSER_RCSTA 90h <font color="#000080"><i>' Set transmit register to transmitter enabled
</i></font><b>DEFINE </b>HSER_TXSTA 24h <font color="#000080"><i>' Set baud rate
</i></font><b>DEFINE </b>HSER_BAUD 9600
<b>DEFINE </b>HSER_CLROERR 1
PinActivar <b>VAR </b>portb.0
PinDescuelgue <b>VAR </b>porta.5
PinPolaridad <b>VAR </b>porta.4
InZCD <b>VAR </b>porta.2
PinDTMF <b>VAR </b>portb.7
PinOffHook <b>VAR </b>portb.3
TMR2_clock <b>VAR WORD
</b>FlagSample <b>VAR BIT
</b>NumSamples <b>VAR BYTE
</b>Samples <b>VAR BIT </b>[124]
x <b>VAR BYTE
ASM
</b><font color="#008000">INT_LIST macro </font><font color="#000080"><i>; IntSource, Label, Type, ResetFlag?
</i></font><font color="#008000">INT_Handler TMR2_INT, _Sample, PBP, yes
endm
INT_CREATE </font><font color="#000080"><i>; Creates the interrupt processor
</i></font><b>ENDASM
</b><font color="#008000">@ INT_ENABLE TMR2_INT </font><font color="#000080"><i>; Enable Timer 1 Interrupts
;----[ Your Main program starts here ]----------------------------------------
</i></font>LoopCount <b>VAR WORD
</b>Config:
trisb.0=0
trisa.5=1
trisa.4=1
trisa.2=1
trisb.7=0
trisb.3=0
CMCON = 5
VRCON = %11101100
OPTION_REG = %00000000
T2CON = %00000000 <font color="#000080"><i>' TMR2 prescale 1:1
</i></font><b>PAUSE </b>500 : <b>LCDOUT </b>$FE,1 : <b>PAUSE </b>250
PR2 = 125 <font color="#000080"><i>' load tmr2 PR2 to reset every 1ms
</i></font>MAIN:
<b>HIGH </b>PINACTIVAR
<b>GOSUB </b>GETSAMPLE
<b>PAUSE </b>1000
<b>GOTO </b>MAIN
GetSample:
T2CON.2 = 1
NumSamples = 0
FlagSample = 0
<b>WHILE </b>FlagSample = 0
<b>WEND
HSEROUT </b>[<font color="#FF0000">":"</font>,10,13]
<b>FOR </b>x = 0 <b>TO </b>119
<b>HSEROUT </b>[<b>DEC </b>Samples [x],10,13]
<b>NEXT </b>x
<b>RETURN
</b>Sample:
Samples [NumSamples] = CMCON.7
NumSamples = NumSamples + 1
<b>IF </b>NumSamples = 120 <b>THEN
</b>FlagSample = 1
T2CON.2 = 0
<b>ENDIF
</b><font color="#008000">@ INT_RETURN
</font></code></pre><!--EndFragment--></body>
My problem is that the frequency sample doesnt match, it seems like the interrupt period is not consecuent with my need..
I am using PR= 125, and with internal clock 4Mhz this should interrupt 0.000125s or 8000Hz.
This doesnt seemd to be the case. Please any advice on how to interrupt with this TMR2 each 125uS.
How can i confirm that my program is interrupting every 0.000125 Seconds?, maybe i am wrong somewhere else.
Thanks for any help
Bookmarks