News:

Let's find out together what makes a PIC Tick!

Main Menu

Timer1 and Timer2 interrupt on PIC18F47K40

Started by evoortman, May 29, 2024, 01:24 PM

Previous topic - Next topic

evoortman

Hi,
In the past I always worked with software interrupts.
Now trying hardware interrupt but can't get it to work.
Does anyone know what could be the cause?


;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F47K40

Config_Start
  FEXTOSC = OFF   ;Oscillator not enabled
  RSTOSC = HFINTOSC_64MHZ   ;HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
  CLKOUTEN = OFF   ;CLKOUT function is disabled
  CSWEN = On   ;Writing to NOSC and NDIV is allowed
  FCMEN = OFF   ;Fail-Safe Clock Monitor disabled
  MCLRE = INTMCLR   ;If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCLR
  PWRTE = On   ;Power up timer enabled
  LPBOREN = OFF   ;ULPBOR disabled
  BOREN = SBORDIS   ;Brown-out Reset enabled , SBOREN bit is ignored
  BORV = VBOR_245   ;Brown-out Reset Voltage (VBOR) set to 2.45V
  ZCD = OFF   ;ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
  PPS1WAY = OFF   ;PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
  STVREN = On   ;Stack full/underflow will cause Reset
  Debug = OFF   ;Background debugger disabled
  XINST = OFF   ;Extended Instruction Set and Indexed Addressing Mode disabled
  WDTCPS = WDTCPS_10   ;Divider ratio 1:32768
  WDTE = On   ;WDT enabled regardless of sleep
  WDTCWS = WDTCWS_7   ;window always open (100%); software control; keyed access not required
  WDTCCS = HFINTOSC   ;WDT reference clock is the 31.2kHz HFINTOSC output
  WRT0 = OFF   ;Block 0 (000800-003FFFh) not write-protected
  WRT1 = OFF   ;Block 1 (004000-007FFFh) not write-protected
  WRT2 = OFF   ;Block 2 (008000-00BFFFh) not write-protected
  WRT3 = OFF   ;Block 3 (00C000-00FFFFh) not write-protected
  WRTC = OFF   ;Configuration registers (300000-30000Bh) not write-protected
  WRTB = OFF   ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF   ;Data EEPROM not write-protected
  SCANE = OFF   ;Scanner module is NOT available for use, SCANMD bit is ignored
  LVP = OFF   ;HV on MCLR/VPP must be used for programming
  Cp = On   ;UserNVM code protection enabled
  CPD = OFF   ;DataNVM code protection disabled
  EBTR0 = OFF   ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF   ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF   ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF   ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
  EBTRB = OFF   ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****

Xtal = 4

Dim ON_OFF As Bit
Dim FLASHLED_RED As Bit
Dim FLASHLED_COUNT As Byte
Dim TIMEOUT_COUNT As Word
Dim IN_1 As Bit
Dim OUT_1 As Bit
Dim OUT_2 As Bit
Dim OUT_3 As Bit
Dim OUT_4 As Bit

Symbol IPEN = INTCON.5                   ' Interrupt Priority Enable
Symbol PEIE_GIEL = INTCON.6                ' Peripheral Interrupt Enable
Symbol GIE_GIEH = INTCON.7                 ' Global Interrupt Enable

Symbol TMR1IF = PIR4.0                  ' Timer 1 interrupt flag
Symbol TMR1IE = PIE4.0                  ' Timer 1 interrupt enable
Symbol TMR1ON = T1CON.0                  ' Timer 1 on bit
Symbol T1SYNC = T1CON.2                  ' Ext. inp. sync bit
Symbol T1CKPS0 = T1CON.4               ' Timer 1 prescaler bit 0
Symbol T1CKPS1 = T1CON.5               ' Timer 1 prescaler bit 1

Symbol TMR2IF = PIR4.1                   ' TMR2 to PR2 Match Interrupt Flag
Symbol TMR2IE = PIE4.1                   ' TMR2 to PR2 Match Interrupt Enable

T1CKPS0 = 1                          ' Timer1
T1CKPS1 = 1                     ' prescaler 1:8
T1SYNC = 1                     ' No sync
TMR1ON = 1                     ' Timer1 on
TMR1IF = 0                     ' Reset timer 1 flag
TMR1IE = 1                     ' Enable timer 1 interrupt
TMR1L = %00000000                  ' Timer1L preset value
TMR1H = %00000000                  ' Timer1H preset value

T2CON = %11001111                  ' Timer2 on, 1:16 postscaler, 1:16 prescaler
TMR2IF = 0                     ' Reset timer 2 flag
TMR2IE = 1                     ' Enable timer 2 interrupt
PR2 = %11111111                     ' Timer2 period register

PEIE_GIEL = 1                       ' Enable periferal interrupts
GIE_GIEH = 1                      ' Enable global interrupts

On_HARDWARE_Interrupt GoTo INTERRUPT_ROUTINE

'============ INTERRUPT ROUTINES ===================================================           
INTERRUPT_ROUTINE:

              CONTEXT SAVE

           If ON_OFF = 0 Then FLASHLED_RED = 1             ' Turn flash-led off if on/off flag is 0

'------------ TOGGLE FLASH_LED -----------------------------------------------------
           If TMR2IF = 1 And ON_OFF = 1 Then
               Inc FLASHLED_COUNT                  ' To slow down flash_led on timer2         
               If IN_1 = 1 Then FLASHLED_RED = 0          ' If axels aligned then flash led on
               If IN_1 = 0 And ON_OFF = 1 And FLASHLED_COUNT > 5 Then
                   Toggle FLASHLED_RED
                   FLASHLED_COUNT = 0
               EndIf
               TMR2IF = 0                  ' Clear timer2 int. flag
           EndIf   

'------------ SLEEP TIMER ----------------------------------------------------------          
           If TMR1IF = 1 Then
               Inc TIMEOUT_COUNT
             If TIMEOUT_COUNT > 400 Then            ' 500 X 520ms = ± 3.6 min.
                ON_OFF = 0
               OUT_1 = 0
                OUT_2 = 0
                OUT_3 = 0
                OUT_4 = 0
               TIMEOUT_COUNT = 0            ' Reset sleeptimer
             EndIf
               TMR1IF = 0                      ' Clear timer1 int. flag
           EndIf

              CONTEXT RESTORE


charliecoutas

The code you gave doesn't compile.

Charlie

evoortman

Defined some variables. It compiles now.

Dompie

Global interrupt are not enabled as far as I can see.

Johan

evoortman

#4
Quote from: Dompie on May 29, 2024, 04:35 PMGlobal interrupt are not enabled as far as I can see.

Johan

Also enabled global interrupts but still not working. Do not get interrupt on timer1 or timer2

Stephen Moss

Is there some code missing there, such as jumping over the ISR to a main loop or having a main loop before the ISR?
If not won't it just start executing the ISR immediately and with no Return address pushed onto the stack when it exits it will jump some random address that would hang the program?

evoortman

Quote from: Stephen Moss on May 30, 2024, 08:02 AMIs there some code missing there, such as jumping over the ISR to a main loop or having a main loop before the ISR?
If not won't it just start executing the ISR immediately and with no Return address pushed onto the stack when it exits it will jump some random address that would hang the program?
This is not the complete code but only the code blocks that deal with the interrupt and the timers.

evoortman

Solved it!
I forgot to define the clock source for timer1 and timer2. So these weren't running

TMR1CLK = 00000001
T2CLKCON = 00000001