Looking for simple maths system for handling interrupt flag timing

Started by TimB, Mar 17, 2023, 11:22 AM

Previous topic - Next topic

JonW

I do something similar but not in an interrupt (it could be), code runs in a very fast loop (<< 10uS)

Run a timer at a base interval, in my case it times out in 8mS and sets the INT Flag, I monitor the Int Flag in the fast loop (could be an interrupt) and once it times out, individual counters are incremented and the flag reset.

I then monitor each counter and do an event once the individual counter equals a multiple of the base timeout x constant and then reset that counter. 
I can therefore schedule events at timed intervals and also monitor other pins at very high speed




TimB

Hello JONW

What would your code look like to solve my issue (original post)

"
What I want is to offset them
If I were counting to 1000 then
9,19,29,39 etc set the 100hz flag
98,198,298 etc the 10hz flag
and also have special events at say when the counter is 657 to run code"

JonW

This is what I have in my current code. Timer0 runs in the background and the loop runs doing fast tasks, my timer times out in 8ms, and I then increment sub counters and exit.  The sub-timers tick up in 8mS intervals, but the individual counters are checked in the fast loop and code is executed and counters are cleared once a specific time has elapsed.  I also have HW interrupts doing the UARTS.. Its simple but works very well. 

Timer is setup and running..

'------------------------------------------------------------------------------------------------
'                                          MAIN CODE
'------------------------------------------------------------------------------------------------
' THIS LOOP RUNS EXTREMELY FAST (TYP < 10us), SO WE USE TIMER0 INTERRUPT TO INCREMENT A SERIES OF
' EVENT COUNTERS, EACH FUNCTION TIMER CAN BE INDEPENDENTLY SET. EVENTS CAN BE SCHEDULED AT DIFFERENT
' INTERVALS WITHOUT SIGNIFICANTLY DELAYING THE LOOP
' ADC IS 16 X OVERSAMPLED AND RUNS AT APPROX 2us/CONVERSION THUS < 50us
' UARTS ARE INTERRUPT DRIVEN, ANY OTHER STATE CHANGE CAN BE DETECTED IN < 10uS THUS ANY INPUT HAS A
' FAST RESPONSE TIME
' TIMER INT IS 8ms AND TICKS IN 125nS

MAINLOOP:
        CLRWDT

        IF  TIMER0INT = 1 then               ' EVERY 8mS WE ENTER HERE
            TIMER0INT = 0:INC UART1_TMR:INC UART2_TMR:INC ADCINTTEMP_TMR:INC SYSTICK_TMR
        EndIf

        CHECKUARTERRORS()                  ' CHECKS FOR UART ERRORS
        PARSE_CONTROLS()                   ' CHECKS VOLTS, LOCK, TONE AND POL PINS & SET FLAGS
        PARSE_SERIAL()                     ' CHECK IF MESSAGE IN BUFFER

        IF ADCINTTEMP_TMR >= 610 then      ' EVERY  5 SECONDS MEASURE THE INTERNAL TEMP
            PICTEMP = GETPICTEMP()
            'TXWORD1(PICTEMP)
            ADCINTTEMP_TMR = 0
        ENDIF

        IF SYSTICK_TMR >= 12 then          ' 0.1 SEC
            SYSTICK_TMR = 0
            IF PLL_LOCKBIT = 0 then
               LED1 = 0
               LED2 = 0
               PULSELED(5)
            EndIf
        ENDIF

        IF UART1_TMR = 15 THEN      ' IF WE GET CLOSE TO THE LIMIT OR GET NO EOM, CLEAR BUFFERS
           IF RX1_PNTR > RXBUFFSIZE - 2 then  RX1_PNTR = 0: UART1_TMR = 0
        EndIF

        IF UART2_TMR = 15 THEN      ' IF WE GET CLOSE TO THE LIMIT OR GET NO EOM, CLEAR BUFFERS
           IF RX2_PNTR > RXBUFFSIZE - 2 then  RX2_PNTR = 0: UART2_TMR = 0
        EndIF

        GoTo MAINLOOP



In my case, it's not mega accurate scheduling as the events slow the execution time within the loop.