Announcement

Collapse
No announcement yet.

Timer 4 in a 1-shot software config on PIC18F67K40 using Instant Interrupts

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Timer 4 in a 1-shot software config on PIC18F67K40 using Instant Interrupts

    Hello All-
    I am trying to do something simple but it is not working at all. I am trying to start Timer4 and have it operate as a 1-shot.
    I monitor with a scope and I do not see my testpoint (F4) go high at all. I do see the trigger (F5) go low which should trigger the 1-shot but it does not.
    I am trying to listen on a 485 line and when not busy, send out a packet based on delays and trying again.
    TxPacket1 is where it doesn't seem to work.
    Instant interrupts have been modified to work with the timers and the serial ports on this chip.
    Any help would be greatly appreciated.

    Pertinent code below:

    T4CON = %00000011 ; Prescaler=1:1, PostScaler=1:4, TMR4OFF. 2 milliSec Timer.
    T4HLT = %00001000 '1-shot, software start
    T4CLKCON = %00000101 ; LFINTOSC Selected for Timer 4 clock source.
    T4TMR = $0F
    T4PR = $0F

    F4 var PORTF.4 'Test point
    F5 var PORTF.5 'Isolated RO of the 'listening' 485 interface

    ;------------ Assembly Interrupts Listed Here ---------------------------------------------------
    ;ASM INTERRUPTS COURTESY OF DARREL TAYLOR.
    define USE_LOWPRIORITY 1
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler RX1_INT, _Get_PC_Char, PBP, YES
    INT_Handler RX2_INT, _Get_Backbone_Char, PBP, YES
    INT_Handler TMR4_INT, _DataBusTimer, PBP, YES ; Low Priority interrupt.
    endm
    INT_CREATE ; Creates the interrupt processor
    ENDASM

    ASM
    INT_LIST_L macro; IntSource, Label, Type, ResetFlag?
    INT_Handler TMR2_INT, _GateTimer, PBP, YES ; Low Priority interrupt.
    ; INT_Handler TMR4_INT, _DataBusTimer, PBP, YES ; Low Priority interrupt.
    ; INT_Handler TMR0_INT, _ClockCount, PBP, YES ; Low Priority interrupt.
    INT_Handler TMR1_INT, _Heart_Beat, PBP, YES ; Low Priority interrupt.
    endm
    INT_CREATE_L ; Creates the interrupt processor
    ENDASM
    '@ INT_ENABLE TMR0_INT ; enable Timer 0 interrupts.
    @ INT_ENABLE TMR1_INT ; enable Timer 1 interrupts.
    '@ INT_ENABLE TMR2_INT ; enable Timer 2 interrupts.
    @ INT_ENABLE TMR4_INT ; enable Timer 4 interrupts.
    @ INT_ENABLE RX1_INT ; enable RECEIVER interrupts.
    @ INT_ENABLE RX2_INT ; enable RECEIVER interrupts.
    ;---------------------------------------------------------------------------------------------------

    ;*******************************************************************************
    DataBusTimer: ; Timer4 Interrupt.
    BusBusyFlag = 0
    ' F4 = 0
    T4CON.7 = 0
    @ INT_RETURN
    ;*******************************************************************************

    TxPacket1:
    if F5 = 0 then
    F4 = 1
    BusBusyFlag = 1
    T4CON.7 = 1 'Start Timer4
    endif
    while BusBusyFlag = 1 : wend
    F4 = 0
    Pauseus (TempNodeNum/3)
    if F5 = 0 then 'One last check to see if someone is talking
    ' F4 = 1 'Transmitting
    BackboneRxTx = 1
    for GPCounter6 = 0 to 5
    hserout [PC_Tx_Buff[GPCounter6]];PC_Tx_Buff[0],PC_Tx_Buff[1],PC_Tx_Buff[2],PC_Tx_Buff[3],PC_Tx_Buff[4],PC_Tx_Buff[5]] 'Send command
    Backbone_Tx_Buff[GPCounter6] = PC_Tx_Buff[GPCounter6] 'Make a copy for Backbone transmission
    while not TX1STA.1 : wend

    hserout2 [PC_Tx_Buff[GPCounter6]]
    while not TX2STA.1 : wend
    next GPCounter6
    else
    goto TxPacket1 'Someone was talking so go back and try again
    endif
    @ INT_ENABLE TMR2_INT ; enable Timer 2 interrupts. 'wait for USART to finish xmitting
    ' F4 = 0 'Not Transmitting
    BackboneRxTx = 0
    BusBusyFlag = 0
    return




  • #2
    Why do you have your Interrupt Declarations broken into 2 groups? They should all be tied together in the same group. I haven't looked any further than that, there may be other issues, so try that first.
    We can crack this cotton PIC'n thang!

    Comment


    • #3
      Thanks for the response Mpgmike, they are in 2 groups due to one group is low priority interrupt and the other is high.

      Comment


      • #4
        I don't see that, but OK... For the K40 PICs, DT's Instant Interrupts has to be modified due to SFR locations. This was covered at http://dt.picbasic.co.uk . If you haven't done that, there is your issue.
        We can crack this cotton PIC'n thang!

        Comment


        • #5
          Apologies for my late reply. Yes MPGMike, the changes need in Instant Interrupts have been done and 'should' be fine.
          Again, my problem is triggering the 1-shot - I just don't see it.
          Am I correct in stating
          T4CON.7 = 1 'Start Timer4
          will start the timer4 1-shot?
          Is this setup accurate?
          T4CON = %00000011 ; Prescaler=1:1, PostScaler=1:4, TMR4OFF. 2 milliSec Timer.
          T4HLT = %00001000 '1-Shot software start
          T4CLKCON = %00000101 ; LFINTOSC Selected for Timer 4 clock source.
          T4TMR = $0F
          T4PR = $0F

          That is where I need assistance initially I believe.
          Thanks again for the interest and your time.
          Regards.

          Comment


          • #6
            I'll assume you are running at 64 MHz INTOSC, since no OSCCON SFRs are listed. T4HLT.7 should be %1 so activity synchronizes with Fosc/4. This is necessary when using any form of Fosc as your T4CLKCON.

            T4CLKCON = %00000101 uses MFINTOSC, not LFINTOSC. For LFINTOSC, T4CLKCON = %00000100.

            Code:
            T4TMR = $0F
            T4PR = $0F
            will trigger a match immediately. T4TMR should be $00, which allows it to increment to $0F before triggering the T4IF and shutting off.

            To get a 2 millisecond timer:
            LFINTOSC, 1:1 Prescaler, 1:1 Postscaler
            - T4CON = 0 ; 1:1 Prescaler, 1:1 Postscaler
            - T4CLKCON = $04 ; LFINTOSC
            - T4PR = $3E ; 62d
            - PIE5.3 = 1 ; Enables Timer 4 Interrupt Flag
            - INTCON = %11000000

            By stating "T4CON.7 = 1" you initiate your 2 mS timer. After 2 mS, T4CON.7 = 0 through a hardware clear, and PIR5.3 = 1 (TMR4IF)
            We can crack this cotton PIC'n thang!

            Comment


            • #7
              Thanks MPGMike. I have made the changes you suggested and the contention is working MUCH better. This was started when I was trying to design a multi-master RS-485 network. I had an extra 2 pins on this chip and wanted to add a 485 interface which would 'listen' to the 485 common buss. Everytime it 'heard' bits/packets it would output a low. I wanted to use this to start the 1-shot which is an approx packet time (6 bytes at 57.6k). If I wanted to put a packet out on this party-line, I would check F5 (the output of the listening 485 interface goes into PORTF.5). Set a flag (BusBusyFlag) and start the 1-shot. I wait for the 1-shot to time out, then wait the address divided by 3 of the board that wants to transmit, then check the party-line once more (F5) and if clear send my packet.I still get some missed packets due to some timing that I don't have nailed down yet but you clearly helped me get further, Thanks!

              Comment


              • #8
                If you review the Timer 2/4/6/8 section of the data sheet you will see you can initiate a Hardware start, where you don't have to start the timer with software. It will start and stop automatically with no software intervention.
                We can crack this cotton PIC'n thang!

                Comment

                Working...
                X