News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

Timer1 Gated Clock Source Question?

Started by Craig, Oct 06, 2026, 06:06 PM

Previous topic - Next topic

Craig

Hi I am busy with an Interrupt on Timer1 Gated Running at MFINTOSC @ 500Khz, I see in the data sheet Page 45 that it can run at 31.25Khz. I have looked on Google and the data sheet but, cannot seem to get a straight answer on this. I See that the Default setting on timer2 is set to 31.25Khz on MFINTOSC and Timer1 MFINTOSC Seems to be fixed at 500Khz?
I did read somewhere that you can divide this down by 16 but, I don't think this is correct as a Pre-scaler of 8 is the highest on timer1 that I can Find?

I need to find a way to run this a bit slower as it is monitoring Led Pulse from a peripheral device which comes in at 1 Pulse per 2 Seconds, 2 Pulses per 2 Seconds 3 Pulses per 2 Seconds, 4 Pulses per 2 Seconds then there are intermediate pulses which can either be mixed or slower or more pulses depending on the condition of the peripheral. With the Timer1 Gated interrupt running at MFINTOSC @ 500Khz with a 1:8 Pre-scaler it is catching most of them but, it is also misreading some of them. I think by lowering it to 31.25Khz will work well.

My whole system is running at 64Mhz Internal Cristal on a Pic 18F47Q10 with a separate RX2 Uart Ring Buffer Interrupt running off the 64Mhz Xtal that is why I have used the MFINTOSC running in the Background to monitor the Timer1 Gated Interrupt.

My Question is it possible to set the MFINTOSC to run at 31.25Khz? and is this the best approach to monitor this LED which has long delays?

Proc Setup()
  Osc_64MHz()                                               ' Set the internal oscillator to 64MHz
  UART2_Setup()                                             ' Setup and open USART2
  INTERRUPT_Initialize()                                    ' Enable Global & Peripheral Interrupts         
  TMR1_Initialize()                                         ' Setup the Timer1 Gate Interrupt
EndProc

'--------------------------------------------------------------------
' Set the PIC18F47Q10 device to 64MHz with the internal oscillator
'--------------------------------------------------------------------
Proc Osc_64MHz()
    OSCCON1 = $60
    OSCFREQ = $080
EndProc

' =================================================
' 2. BAUD RATE CONFIGURATION (115200 @ 64MHz)
' =================================================
Proc UART2_Setup()
  TRISBbits_TRISB6 = 0                                      ' Usart2-Tx Output
  TRISBbits_TRISB7 = 1                                      ' Usart2-Rx Input
  ANSELBbits_ANSELB6 = 0                                    ' All_Digital
  ANSELBbits_ANSELB7 = 0                                    ' All_Digital
  TX2STAbits_BRGH = 1                                       ' High-Speed Asynchronous mode
  BAUD2CONbits_BRG16 = 1                                    ' Use 16-bit Baud Rate Generator
  SP2BRGH = 0x00                                            ' High byte - Load calculated divisor value (138 = 0x008A) Set to 115200 Baud
  SP2BRGL = 138                                             ' Low byte Set to 115200 Baud = 138 or (hex 0x8A)
  TX2STAbits_SYNC = 0                                       ' Asynchronous Mode
  RC2STAbits_SPEN = 1                                       ' Enable Serial Port
  RC2STAbits_CREN = 1                                       ' Enable Continuous Receive
EndProc

'--------------------------------------------------------------------------------
Proc INTERRUPT_Initialize()                                 
  PIE3bits_RC2IE = 1                                       ' Enable Eusart2 RX Receive Bit
  INTCONbits_IPEN = 1                                      ' Enable interrupt priority levels
  INTCONbits_GIEH = 1                                      ' Enable all High Priority Global Interrupts (INTCONbits_GIEL = 1 "For Low Priority")
  INTCONbits_PEIE = 1                                      ' Enable the Peripheral Interrupts
EndProc

'================================================================================
Proc TMR1_Initialize()
  T1GCONbits_GE = 1                                         ' Timer Counting is Controlled by the Timer Gate Function - Enable the Gate 1 Timer
  T1GCONbits_GPOL = 1                                       ' Active High - Counts when Pulse is High coming from the Status LED on PORTB.5 (T1GPPS)
  T1GCONbits_GGO_DONE = 1                                   ' Timer acquistion is ready
  T1GCONbits_GSPM = 1                                       ' Timer Gate Single Pulse Mode is Enabled and is Controlling Timer gate = 1)
  TMR1CLK = 0b00000101                                      ' MFINTOSC @ 500KHz (Each Rising Edge High Pulse is 360.000us_2.778Khz)
  IPR5bits_TMR1GIP = 1                                      ' TMR1GIP - TMR1 Gate Interrupt to High Priority
  PIR5bits_TMR1GIF = 0                                      ' Clearing Gate If Interrupt Flag before Enabling the Interrupt
  PIE5bits_TMR1GIE = 1                                      ' Enable TMR1 Gate Interrupt
  T1CON = 0b00110111                                        ' Timer1 prescaler is 1:8, Timer1 enabled, 16-bit read, not synchronized
EndProc     

All input is very much appreciated.

Regards
Craig

RGV250

Hi,
That seems an awful device to set up unless you need it. I wonder if you want it to be slower could TMR1CK be 100 instead of 101 as that is then LFINTOSC.

Bob

Craig

Hi Bob

Thanks Very much for your input Bob,I will run it on the LFINTOSC and see the Accuracy, Microchip's Data Sheet Stinks with the Explanation on the MFINTOSC

Regards
Craig

trastikata

Quote from: Craig on Oct 06, 2026, 06:06 PMMy Question is it possible to set the MFINTOSC to run at 31.25Khz? and is this the best approach to monitor this LED which has long delays?

MFINTOSC is set to 500 KHz and it is dynamically derived from the HFINTOSC, whatever the HFINTOSC value currently is.

The 31.25 is actually the MFINTOSC/16 and it is not available as clock source for Timer1,3,5 (Page 305), only the 500 KHz without post scaller. 

Timer2,4,6 have it available the MFINTOSC/16 i.e. 31.25 KHz as their clock source, it is a 8b timer though.

But if you look closer at the clock sources for Timer 1 you can see you can select the Timer 3,5 overflow as clock, since those timers can be set to use the MFINTOSC i.e. 500KHz as clock, then you can easily have any frequency, lower than 500KHz, as clock source for Timer1.

▲