wylee
Full Member level 1
tmr0 interrupt
Hi people,
I currently facing a strange PicBasic interrupt timer problem,
I wanted to create a light sensor using CDS (refer to picture attached) which will take light intensity reading every 3 seconds and send out the result through RS232. PIC used here is PIC16F84A
But whenever the program reaches instruction set call "readCDS" routine, it just hung there, not executing the subsequent "serout" command and rebooted!
I noticed the 'hanging' because when I commented out "readCDS", replacing with "pause 100", it will pause for 100ms and then execute "serout" command
I can't understand why this is happening...why can't it execute "readCDS" which uses RCTIME command. I tested & realized that RCTIME command run prefectly well if no interrupt was used.
The TMR0 prescalar is set to 1:256, TMR0 interrupt is generated every 256us x ( 255 - 38 ) = 55.552ms. A "cntint" is use to count 54 ticks for approx. 3 seconds
Another IMPORTANT thing need to point out, PicBasic Pro in MPLab does not show any error on my codes!
The attached is my PicBaisc code:
'****************************
'Light sensor for PIC16F84A
include "modedefs.bas"
symbol CDS = PORTB.3
CDS_raw var word
CDS_val var byte
symbol TX = PORTB.2
output TX
symbol LED = PORTB.4
output LED
DEFINE OSC 4
DEFINE CHAR_PACING 1000
'Interrupt counter
intcnt var word
int_tick con 54 '3 seconds
Wsave VAR BYTE $20 SYSTEM ' Save-location for the W register
Ssave VAR BYTE bank0 SYSTEM ' Save-location for the STATUS register
Psave VAR BYTE bank0 SYSTEM ' Save-location for the PCLATH register
Fsave VAR BYTE bank0 SYSTEM
DEFINE INTHAND isr 'Tell PBP what code to execute on an interrupt
goto start
ASM
isr
Movwf Wsave
Swapf STATUS,w
Clrf STATUS
Movwf Ssave
Movf PCLATH,w
Movwf Psave
Movf FSR,w
Movwf Fsave
Endasm
TMR0 = 38
intcnt=intcnt+1
if intcnt=int_tick then '3 seconds
intcnt=0
toggle LED
' pause 100
call readCDS 'read light intensity
Serout TX,T2400,["CDS=",#CDS_val,10,13]
endif
ASM
Exit
bcf INTCON,2 ;reset TMR0 overflow
Movf Fsave,w
Movwf FSR
Movf Psave,w
Movwf PCLATH
Swapf Ssave,w
Movwf STATUS
Swapf Wsave,f
Swapf Wsave,w
Retfie
Endasm
start:
intcnt=0
TMR0 = 38
OPTION_REG = %00000111 'prescaler 256us
INTCON = %10100000 'TMR0 overflow timer on
start1:
goto start1
readCDS:
high CDS ' Make CDS pin high
PAUSE 5 ' Let cap charge
RCTIME CDS,1,CDS_raw ' Time & record discharge time
CDS_val = NCD CDS_raw
return
end
'***************************
Hi people,
I currently facing a strange PicBasic interrupt timer problem,
I wanted to create a light sensor using CDS (refer to picture attached) which will take light intensity reading every 3 seconds and send out the result through RS232. PIC used here is PIC16F84A
But whenever the program reaches instruction set call "readCDS" routine, it just hung there, not executing the subsequent "serout" command and rebooted!
I noticed the 'hanging' because when I commented out "readCDS", replacing with "pause 100", it will pause for 100ms and then execute "serout" command
I can't understand why this is happening...why can't it execute "readCDS" which uses RCTIME command. I tested & realized that RCTIME command run prefectly well if no interrupt was used.
The TMR0 prescalar is set to 1:256, TMR0 interrupt is generated every 256us x ( 255 - 38 ) = 55.552ms. A "cntint" is use to count 54 ticks for approx. 3 seconds
Another IMPORTANT thing need to point out, PicBasic Pro in MPLab does not show any error on my codes!
The attached is my PicBaisc code:
'****************************
'Light sensor for PIC16F84A
include "modedefs.bas"
symbol CDS = PORTB.3
CDS_raw var word
CDS_val var byte
symbol TX = PORTB.2
output TX
symbol LED = PORTB.4
output LED
DEFINE OSC 4
DEFINE CHAR_PACING 1000
'Interrupt counter
intcnt var word
int_tick con 54 '3 seconds
Wsave VAR BYTE $20 SYSTEM ' Save-location for the W register
Ssave VAR BYTE bank0 SYSTEM ' Save-location for the STATUS register
Psave VAR BYTE bank0 SYSTEM ' Save-location for the PCLATH register
Fsave VAR BYTE bank0 SYSTEM
DEFINE INTHAND isr 'Tell PBP what code to execute on an interrupt
goto start
ASM
isr
Movwf Wsave
Swapf STATUS,w
Clrf STATUS
Movwf Ssave
Movf PCLATH,w
Movwf Psave
Movf FSR,w
Movwf Fsave
Endasm
TMR0 = 38
intcnt=intcnt+1
if intcnt=int_tick then '3 seconds
intcnt=0
toggle LED
' pause 100
call readCDS 'read light intensity
Serout TX,T2400,["CDS=",#CDS_val,10,13]
endif
ASM
Exit
bcf INTCON,2 ;reset TMR0 overflow
Movf Fsave,w
Movwf FSR
Movf Psave,w
Movwf PCLATH
Swapf Ssave,w
Movwf STATUS
Swapf Wsave,f
Swapf Wsave,w
Retfie
Endasm
start:
intcnt=0
TMR0 = 38
OPTION_REG = %00000111 'prescaler 256us
INTCON = %10100000 'TMR0 overflow timer on
start1:
goto start1
readCDS:
high CDS ' Make CDS pin high
PAUSE 5 ' Let cap charge
RCTIME CDS,1,CDS_raw ' Time & record discharge time
CDS_val = NCD CDS_raw
return
end
'***************************