1

What is wrong with this code? The interrupt at 0004 never executes! MPLAB X IDE simulator I tried changing all the bits of T1CON, but no results

; TODO INSERT CONFIG CODE HERE USING CONFIG BITS GENERATOR
#include "p12f675.inc"

; CONFIG
; __config 0x31F1
 __CONFIG _FOSC_XT & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _BOREN_ON & _CP_OFF & _CPD_OFF

D1 equ h'20'  ;delay variabile
D2 equ h'21' 
D3 equ h'22' 
 
RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

INT_VECT  CODE    0x0004    
 ; TODO ADD INTERRUPTS HERE IF USED
  ;vine de 10 ori/sec
  ;contor 10 ori:
  movlw d'61' ; ar fi (256-61)*256= cca 50 ms > apoi prescaler 2 => 100 ms
  movwf TMR1L  ;numara 61...255
  clrf TMR1H   ;numara 0...255
  bcf PIR1,0  ;clear timer1 interupt flag/ ca sa porneasca
  nop
  retfie

MAIN_PROG CODE                      ; let linker place main program
START
  ; init timer interupt:
  bsf STATUS, RP0  ;banc 1
  bsf PIE1,0  ;enable timer 1 interupt
  bcf STATUS, RP0   ; banc 0
  movlw d'61' ; ar fi (256-61)*256= cca 50 ms > apoi prescaler 2 => 100 ms
  movwf TMR1L  ;numara 61...255
  clrf TMR1H   ;numara 0...255
  movlw b'00010101' ;prescaler 2, timer1 enabled
  movwf T1CON
  movlw b'10000000' ;enable timer-overflow interrupt. trebe?!
  movwf INTCON  
  bcf PIR1,0  ;clear timer1 interupt flag/ ca sa porneasca
  
loop  
    movlw d'10'
    movwf D1
    GOTO loop                          ; loop forever

    END

Mike
  • 4,041
  • 6
  • 20
  • 37
gikam
  • 15
  • 4

2 Answers2

3

To make it short: To enable timer 1 interrupt you had to set these bits:

  • Timer1 interrupt Enable bit (PIE1<0>)
  • PEIE bit (INTCON<6>)
  • GIE bit (INTCON<7>).
Mike
  • 4,041
  • 6
  • 20
  • 37
2

This is because your code is missing a small step. Let me explain the steps you need to take in order to setup Timer 1 interrupt properly.
Here is a picture that shows the internal circutry of all interrupts of PIC12F675. enter image description here Now, if you have a little knowledge of digital electronics, watch how the Timer 1 interrupt is wired all the way through the CPU interrupt. Especially watch out the AND gates since they act like turn-on/turn-off switches which either connect/enable or disconnect/disable one circuit from another. Not to mention that all those pathways are organized in a such way so that we can control them in the software by setting or clearing the corresponding bits.
That being said, now let's focus the activation path of the Timer 1 in the logical circuit. This path is consist of 3 main logical blocks each of which has to be enabled in order to transport the interrupt signal to the CPU:

  • The first AND gate that controls Timer 1 interrupt is one that has 2 inputs called TMR1IF and TMR1IE. TMR1IF is set by internal circuitry when an overflow occurs so you can only clear it. TMR1IE is controlled completely by the software so we set/enable TMR1IE bit as a first step.
  • The next AND gate that controls the entire peripheral interrupt circuitry is one that has an input coming from the peripheral interrupts that we have no control on it and another input called PEIE that we have total control on it. So we need to set/enable it as well, in order to allow the interrupt signal pass through it and reach to the global interrupt control AND gate input.
  • The next stop, as you guess, is the global interrupt control AND gate which has 2 inputs. One is coming from the entire interrupt control logic, while the other is GIE which we have to set/enable as a last step.

There you have, we've completed the Timer 1 interrupt signal pathway so that it interrupts the CPU every time an overflow occurs. Go ahead and follow the given steps above you will have your Timer 1 interrupt functioning.

Kozmotronik
  • 2,080
  • 3
  • 10
  • 25
  • I Canțt find what @WeatherVane mentioned. Can u help? thanx – gikam Apr 24 '23 at 11:06
  • There was another answer by the time I've posted this answer. He commented below that answer but looks like that answer has been removed. Well my answer explains the steps to take in order to get Timer 1 interrupt functioning correctly. Have you tried implementing those steps so far? – Kozmotronik Apr 24 '23 at 12:19
  • great. now it works. at least in the simulator. thanks. – gikam Apr 25 '23 at 15:06
  • 1
    Great! Feel free to change the accepted answer to the most useful one. – Kozmotronik Apr 26 '23 at 15:25