I am seeing something strange.....It seems I can not read my ADC's or run anything in main when my interrupt is enabled. What my code is supposed to do is output a 1.5ms pulse every 16ms for "home" position on 4 servo's in the interrupt. At any time an ADC value on 5 ADC's is above a threshold I want to stop the interrupt and run the scaled up ADC values to run my servo's in real time. What am I missing?
Code:
ASM
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF
__config _CONFIG2, _PLLEN_OFF & _LVP_OFF & _LVP_OFF & _STVREN_OFF
ENDASM
DEFINE OSC 8
;Define ADCIN parameters
DEFINE ADC_BITS 8 ' Set number of bits in result
DEFINE ADC_CLOCK 4 ' Set clock source, look on data sheet for chart
DEFINE ADC_SAMPLEUS 500 ' Set sampling time in uS
DEFINE INTHAND myint ' Setup interrupt handler
;*******************************************************************************
;*******************************************************************************
; System hardware configuration
;*******************************************************************************
;*******************************************************************************
OSCCON = %01110000 ' bit.7 =32MHz internal
ANSELA = %00000001 ' all digital. A/D disabled
ANSELB = %00000000
TRISB = %00000000 ' set port directions 0 = output, 1 = input
TRISA = %00000001
ADCON0 = %00000111 ' Set PORTA digital 1, analog 0
ADCON1 = %01000000
;*******************************************************************************
; Program variables
;*******************************************************************************
ADCVAL var byte bank0
ADCVAL2 var byte bank0
ADCVAL3 var byte bank0
ADCVAL4 var byte bank0
ADCVAL5 var byte bank0
;*******************************************************************************
; Program constants
;*******************************************************************************
centM con 1500 ;motor stop
forM con 2000 ;motor forward
revM con 1000 ;motor reverse
;*******************************************************************************
; Power on initialization to known port states
;*******************************************************************************
PowerOn_init:
PortA = 0 ' force low to avoid glitches
POrtB = 0
PIR1 = 0 ' clear TMR1 int flag
PIE1 = %00000001 ' TMR1 int enabled
INTCON = %11000000 ' global & peripheral ints enabled
T1CON = %00010001 ' TMR1 1:2 prescale, timer1 on
;*******************************************************************************
;*******************************************************************************
goto mainloop
asm
; Save W, STATUS and PCLATH registers, if not done previously
myint
; retfie auto-restores w, status, bsr, fsr and pclath
BANKSEL T1CON
clrf BSR
bcf T1CON,TMR1ON ; stop timer 1
bcf PIR1,TMR1IF ; clear over flow flag
movlw 0xBF ; load timer for 16,535 * 2 prescaler interrupt on 16 bit timer
movwf TMR1H ; load high byte
movlw 0xFF
movwf TMR1L ; load low byte
call _home
bsf T1CON,TMR1ON ; re-enable timer 1
; movlw 0x02
; xorwf PORTB,f ; toggle RB1
retfie ; Return from interrupt
endasm
home:
if adcval < 4 and adcval2 < 4 and adcval3 <4 and adcval4 < 4 and adcval5 < 4 then
PORTB = %00001111
pauseus 1500
PORTB = %00000000
endif
return
mainloop:
call readADC
call spin
' call forward
' call back
' call left
' call right
pause 15
goto mainloop
readADC:
adcin 0, adcval
ADCIN 1, adcval2
adcin 2, adcval3
adcin 3, adcval4
adcin 4, adcval5
return
spin:
if adcval > 4 then
LATB = %00001111
pauseus 6*(adcval) + 900
LATB = %00000000
endif
return
forward:
if adcval2 > 4 then
LATB = %00001100
pauseus 6*(adcval) + 900
LATB = %00000000
LATB = %00000011
pauseus 6*(adcval) + 900
LATB = %00000000
endif
return
back:
if adcval3 > 4 then
LATB = %00000011
pauseus 6*(adcval) + 900
LATB = %00000000
LATB = %00001100
pauseus 6*(adcval) + 900
LATB = %00000000
endif
return
left:
if adcval4 > 4 then
LATB = %00000101
pauseus 6*(adcval) + 900
LATB = %00000000
LATB = %00001100
pauseus 6*(adcval) + 900
LATB = %00001010
endif
return
right:
if adcval5 > 4 then
LATB = %00001010
pauseus 6*(adcval) + 900
LATB = %00000000
LATB = %00000101
pauseus 6*(adcval) + 900
LATB = %00001010
endif
return
Bookmarks