INTCON, TOIF problem

Thread Starter

pancikok

Joined Oct 31, 2009
26
hey ,everybody !!
*my problem is about the TOIF bit in INCON register :(

*it is supposed to wait until the 7-th bit of TMR0 is set, and then the LED on the RP(1) should emit, so then it should wait until the TMR0 is 0xFF and the TOIF bit of intcon in set, so when it is, the LED must turn off on RP(1).

*but when the timer is 0xFF , the "Stack over flow error occurred from instruction at 0x000006" is seen on the output, and the LED is still emits, because it cant reach to CLR PORTB,1 instruction.

*help me please with it, i can paste it. and alsoo, im using MPLAB IDE,v8.36 and this program is for PIC16F84 microcontroller . any responce appreciated
 

Markd77

Joined Sep 7, 2009
2,806
Without seeing the code, I am guessing that a stack overflow means that you are enabling interrupts in the interrupt routine. RETFIE turns interrupts back on anyway so there is no need to.
If this is the case then you are probably not clearing the TOIF bit, which the PIC does not do automatically.
If this doesen't help please post the relevant code.
 

BMorse

Joined Sep 26, 2009
2,675
post your code....... so we can see what the problem is, a stack overflow can happen if you used a call statement without a return to the calling function......

and having something to look at to figure out your problem would be a lot easier....
 

Thread Starter

pancikok

Joined Oct 31, 2009
26
yeah sure!
the cleaning TOIF can be a choise,but anyway im posting the code, and gonno try it
Rich (BB code):
list p=16f84;
include p16f84.inc

org 0x00
     goto main

main
BCF STATUS,RP0; goto bank 0
CLRF TMR0
CLRW
BSF STATUS,RP0; goto bank 1
CLRF TRISB;portb out
BSF INTCON,7;GIE interrupt enabled
BSF INTCON,5;TOIF interrupt enabled

MOVLW 0x51 ;RBPU=0,INTED=1,TOCS=0,TOSE=1,PSA=0,PS(0-2)=001
MOVWF OPTION_REG
BCF STATUS,RP0
CLRF PORTB

ledoff
CLRF TMR0
BCF PORTB,0

test
BTFSS TMR0,7
goto test

BSF PORTB,1

test1
BTFSS INTCON,2
goto test1

goto ledoff

end
 
Last edited:

BMorse

Joined Sep 26, 2009
2,675
where is your ISR????

Rich (BB code):
cblock   0x0C
        safe_w        ;not really temp, used by interrupt svc
        safe_s        ;not really temp, used by interrupt svc
        endc
;--------
; Start of ROM
;--------
        org    0x00        ;Start of code space
        goto    Start
;--------
; INTERRUPT SERVICE ROUTINE
;--------
        org    0x04        ;interrupt vector
Intsvc: 
       movwf    safe_w        ;save w
        swapf    STATUS,w    ;swap status, w
        movwf    safe_s        ;save status(nibble swap, remember)
;--------
; done saving, now start working
;--------

;DO INTERRUPT CODE HERE
       bsf     PORTB,0         ;Turn On LED
;--------
; done working, start restoring
;--------
        swapf    safe_s,w     ;fetch status, reswap nibbles
        movwf    STATUS     ;restore status
        swapf    safe_w,f     ;swap nibbles in preparation
        swapf    safe_w,w    ;for the swap restoration of w
        bcf    INTCON,2       ;clear interrupt flag before return
        retfie                     ;return from interrupt
;--------

main
       BCF STATUS,RP0; goto bank 0
       CLRF TMR0
       CLRW
       BSF STATUS,RP0; goto bank 1
       CLRF TRISB;portb out
       BSF INTCON,7;GIE interrupt enabled
       BSF INTCON,5;TOIF interrupt enabled

       MOVLW 0x51 ;RBPU=0,INTED=1,TOCS=0,TOSE=1,PSA=0,PS(0-2)=001
       MOVWF OPTION_REG
       BCF STATUS,RP0
       CLRF PORTB

       end
 
Last edited:

BMorse

Joined Sep 26, 2009
2,675
actually, im not in, what u r asking me about, i dont understand :((

The code I posted is an ISR (Interrupt Service Routine) this is where your code should go so when your Interrupt fires you can do what you want with the LED.....


The way you are doing it is called "polling" which makes your uc slow, if you are going to keep the uc busy checking for an interrupt.

having an ISR keeps your UC free to do other things until an interrupt occurs.
 

BMorse

Joined Sep 26, 2009
2,675
I edited my code in post #5 to add your code to it, so when the Interrupt fires it will turn on the LED....
 

Thread Starter

pancikok

Joined Oct 31, 2009
26
i guess it's ok now, ! but why cont i see the TMR0 change, while the program runs, except for my checking it's 7-th bit, ! and i knew that ISR is for gaining some time, after interrupt, but actually its for just implementing some instruction when that interrupt occurs
so this is my final code i guess ,together with yours :))

Rich (BB code):
list p=16f84
include p16f84.inc

cblock   0x0C
        safe_w        ;not really temp, used by interrupt svc
        safe_s        ;not really temp, used by interrupt svc
        endc
;--------
; Start of ROM
;--------
        org    0x00        ;Start of code space
        goto    main
;--------
; INTERRUPT SERVICE ROUTINE
;--------
        org    0x04        ;interrupt vector
Intsvc: 
       movwf    safe_w        ;save w
        swapf    STATUS,w    ;swap status, w
        movwf    safe_s        ;save status(nibble swap, remember)
;--------
; done saving, now start working
;--------

;DO INTERRUPT CODE HERE
       bcf     PORTB,1         ;Turn Off LED
;--------
; done working, start restoring
;--------

        swapf    safe_s,w     ;fetch status, reswap nibbles
        movwf    STATUS     ;restore status
        swapf    safe_w,f     ;swap nibbles in preparation
        swapf    safe_w,w    ;for the swap restoration of w
        bcf    INTCON,2       ;clear interrupt flag before return
        retfie                     ;return from interrupt
;--------

main
       BCF STATUS,RP0; goto bank 0
       CLRF TMR0
       CLRW
       BSF STATUS,RP0; goto bank 1
       CLRF TRISB;portb out
       BSF INTCON,7;GIE interrupt enabled
       BSF INTCON,5;TOIF interrupt enabled

       MOVLW 0x51 ;RBPU=0,INTED=1,TOCS=0,TOSE=1,PSA=0,PS(0-2)=001
       MOVWF OPTION_REG
       BCF STATUS,RP0
       CLRF PORTB
ledoff
CLRF TMR0
BCF PORTB,0

test
BTFSS TMR0,7
goto test

BSF PORTB,1


       end
 
Top