'18F4620 Intr Demo 1
''' +++++++++++++++++++++++++
Dim i As Byte
Dim intrcnt As Byte
T1CON = %01001001 ''' prescaler set for 1:8 so 20MHz/4 = 5MHZ internal, div by 8
'' =625KHz TMR1 clock, divided by 65536 gives a TMR1 int every 104,857uSec
'' say 0.1sec, so have your intr counter max set to approx 100
TMR1H = 0x00
TMR1L = 0x00
INTCON.GIE = 1
INTCON.PEIE = 1
PIE1.TMR1IE = 1
PIR1.TMR1IF = 0
IPR1.TMR1IP = 0
''++++++++++++++++++++++++++++
''interrupts - disabled / enabled in int routine by compiler
On High Interrupt 'different syntax to 16F877A
Save System 'save system vars
If PIR1.RCIF = 1 Then 'it was a serial interrupt
Hserin rxchar 'get one character from UART
rxbuf(rxcount) = rxchar 'write to array
rxcount = rxcount + 1 'prepare for next char
If rxcount > 38 Then 'we are expecting 38 chars
rxcount = 0 'reset array on overflow of > 38 Chars
dataflag = False 'bad freq data
Goto toolong 'string too long, bail out
Endif
If rxchar = ";" Then 'string terminator ?
If rxcount = 38 Then 'and 38 chars ?
rxcount = 0 'reset array
For i = 5 To 8 'save 4 wanted freq chars in RxBufCopy
rxbufcopy(i) = rxbuf(i)
Next i
dataflag = True 'good freq data
'Read L ADC and calculate an average reading
adctemp = 0
For i = 0 To 7 'take 8 ADC readings
Adcin 2, rawadcl 'get ADC value for L from 2nd channel
adctemp = adctemp + rawadcl
Next i
avgadcl = ShiftRight(adctemp, 3) 'shift right 3 places to div by 8
'and store avg L ADC value
'Read C ADC and calculate an average reading
adctemp = 0
For i = 0 To 7 'take 8 ADC readings
Adcin 3, rawadcc 'get ADC value for C from 3nd channel
adctemp = adctemp + rawadcc
Next i
avgadcc = ShiftRight(adctemp, 3) 'shift right 3 places to div by 8
'and store the avg C ADC value
Else
rxcount = 0 'reset array
dataflag = False 'bad freq data
Endif
Endif
'+++++++++++++++++++++++++++++++++++++++++++++++
TMR1H = 0x00 '######' recv oK so restart TMR1 at 0000
TMR1L = 0x00 '######
'++++++++++++++++++++++++++++++++++++++++++++++
Else 'not our interrupt, ignore it, test for TMR1 intr'++++
'+++++++++++++++++++++++++++++++++++++++++++
If PIR1.TMR1IF = 1 Then 'its a TMR1 intr
PIR1.TMR1IF = 0 'clr intr flag
intrcnt = intrcnt + 1 'bump intr cnt
TMR1H = 0x00 'reload TMR1 with 0000
TMR1L = 0x00
Endif
If intrcnt > 100 Then 'if max time elapsed then do msg
'''do reset msg
''' intr=0
PIR1.TMR1IF = 0 'clr intr flag
Endif
'+++++++++++++++++++++++++++++++++++++++++++++++
Endif
toolong:
PIR1.RCIF = 0 'reset interrupt flag
Resume