INTCON, TOIF problem

Discussion in 'Embedded Systems and Microcontrollers' started by pancikok, Dec 2, 2009.

  1. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.
     
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    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....
     
  4. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    yeah sure!
    the cleaning TOIF can be a choise,but anyway im posting the code, and gonno try it
    Code ( (Unknown Language)):
    1.  
    2. list p=16f84;
    3. include p16f84.inc
    4.  
    5. org 0x00
    6.      goto main
    7.  
    8. main
    9. BCF STATUS,RP0; goto bank 0
    10. CLRF TMR0
    11. CLRW
    12. BSF STATUS,RP0; goto bank 1
    13. CLRF TRISB;portb out
    14. BSF INTCON,7;GIE interrupt enabled
    15. BSF INTCON,5;TOIF interrupt enabled
    16.  
    17. MOVLW 0x51 ;RBPU=0,INTED=1,TOCS=0,TOSE=1,PSA=0,PS(0-2)=001
    18. MOVWF OPTION_REG
    19. BCF STATUS,RP0
    20. CLRF PORTB
    21.  
    22. ledoff
    23. CLRF TMR0
    24. BCF PORTB,0
    25.  
    26. test
    27. BTFSS TMR0,7
    28. goto test
    29.  
    30. BSF PORTB,1
    31.  
    32. test1
    33. BTFSS INTCON,2
    34. goto test1
    35.  
    36. goto ledoff
    37.  
    38. end
    39.  
     
    Last edited: Dec 5, 2009
  5. BMorse

    Senior Member

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

    Code ( (Unknown Language)):
    1.  
    2. cblock   0x0C
    3.         safe_w        ;not really temp, used by interrupt svc
    4.         safe_s        ;not really temp, used by interrupt svc
    5.         endc
    6. ;--------
    7. ; Start of ROM
    8. ;--------
    9.         org    0x00        ;Start of code space
    10.         goto    Start
    11. ;--------
    12. ; INTERRUPT SERVICE ROUTINE
    13. ;--------
    14.         org    0x04        ;interrupt vector
    15. Intsvc:
    16.        movwf    safe_w        ;save w
    17.         swapf    STATUS,w    ;swap status, w
    18.         movwf    safe_s        ;save status(nibble swap, remember)
    19. ;--------
    20. ; done saving, now start working
    21. ;--------
    22.  
    23. ;[B]DO INTERRUPT CODE HERE[/B]
    24.        bsf     PORTB,0         ;Turn On LED
    25. ;--------
    26. ; done working, start restoring
    27. ;--------
    28.         swapf    safe_s,w     ;fetch status, reswap nibbles
    29.         movwf    STATUS     ;restore status
    30.         swapf    safe_w,f     ;swap nibbles in preparation
    31.         swapf    safe_w,w    ;for the swap restoration of w
    32.         bcf    INTCON,2       ;clear interrupt flag before return
    33.         retfie                     ;return from interrupt
    34. ;--------
    35.  
    36. main
    37.        BCF STATUS,RP0; goto bank 0
    38.        CLRF TMR0
    39.        CLRW
    40.        BSF STATUS,RP0; goto bank 1
    41.        CLRF TRISB;portb out
    42.        BSF INTCON,7;GIE interrupt enabled
    43.        BSF INTCON,5;TOIF interrupt enabled
    44.  
    45.        MOVLW 0x51 ;RBPU=0,INTED=1,TOCS=0,TOSE=1,PSA=0,PS(0-2)=001
    46.        MOVWF OPTION_REG
    47.        BCF STATUS,RP0
    48.        CLRF PORTB
    49.  
    50.        end                                                                                                    
    51.  
    52.  
     
    Last edited: Dec 2, 2009
  6. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    actually, im not in, what u r asking me about, i dont understand :((
     
  7. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    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.
     
  8. BMorse

    Senior Member

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

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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 :))

    Code ( (Unknown Language)):
    1. list p=16f84
    2. include p16f84.inc
    3.  
    4. cblock   0x0C
    5.         safe_w        ;not really temp, used by interrupt svc
    6.         safe_s        ;not really temp, used by interrupt svc
    7.         endc
    8. ;--------
    9. ; Start of ROM
    10. ;--------
    11.         org    0x00        ;Start of code space
    12.         goto    main
    13. ;--------
    14. ; INTERRUPT SERVICE ROUTINE
    15. ;--------
    16.         org    0x04        ;interrupt vector
    17. Intsvc:
    18.        movwf    safe_w        ;save w
    19.         swapf    STATUS,w    ;swap status, w
    20.         movwf    safe_s        ;save status(nibble swap, remember)
    21. ;--------
    22. ; done saving, now start working
    23. ;--------
    24.  
    25. ;DO INTERRUPT CODE HERE
    26.        bcf     PORTB,1         ;Turn Off LED
    27. ;--------
    28. ; done working, start restoring
    29. ;--------
    30.  
    31.         swapf    safe_s,w     ;fetch status, reswap nibbles
    32.         movwf    STATUS     ;restore status
    33.         swapf    safe_w,f     ;swap nibbles in preparation
    34.         swapf    safe_w,w    ;for the swap restoration of w
    35.         bcf    INTCON,2       ;clear interrupt flag before return
    36.         retfie                     ;return from interrupt
    37. ;--------
    38.  
    39. main
    40.        BCF STATUS,RP0; goto bank 0
    41.        CLRF TMR0
    42.        CLRW
    43.        BSF STATUS,RP0; goto bank 1
    44.        CLRF TRISB;portb out
    45.        BSF INTCON,7;GIE interrupt enabled
    46.        BSF INTCON,5;TOIF interrupt enabled
    47.  
    48.        MOVLW 0x51 ;RBPU=0,INTED=1,TOCS=0,TOSE=1,PSA=0,PS(0-2)=001
    49.        MOVWF OPTION_REG
    50.        BCF STATUS,RP0
    51.        CLRF PORTB
    52. ledoff
    53. CLRF TMR0
    54. BCF PORTB,0
    55.  
    56. test
    57. BTFSS TMR0,7
    58. goto test
    59.  
    60. BSF PORTB,1
    61.  
    62.  
    63.        end
     
  10. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    anyway, thank you , to everyone , to help me :)))
     
Loading...