Two functions in the high priority interrupt.

Discussion in 'Embedded Systems and Microcontrollers' started by ahgan84, Oct 12, 2014.

  1. ahgan84

    Thread Starter Member

    Dec 19, 2011
    55
    0
    Hi guys, I'm using the PIC18F46k22 mcu and I'm using two function in my high priority interrupt routine:

    Code ( (Unknown Language)):
    1.  
    2. #pragma code
    3. #pragma code My_HiPrio_Int=0x0008
    4. #pragma code
    5. #pragma interrupt chk_isr
    6.  
    7. #pragma code
    8. void My_HiPrio_Int(void)
    9. {
    10. _asm
    11. GOTO chk_isr
    12. _endasm
    13. }
    14.  
    15. void chk_isr(void) /*Serial Interrupt*/
    16. {
    17. INTCONbits.GIE = 0;
    18.  
    19. if(INTCONbits.TMR0IF==1) //Timer routine
    20. Timer0_ISR();
    21.  
    22. if(PIR1bits.RC1IF) //RS485 receiver
    23. RC_ISR();
    24.  
    25. INTCONbits.GIE = 1;
    26.  
    27.  
    28. }
    29.  
    30.  
    31. void Timer0_ISR(void)
    32. {
    33. nTick0++;
    34.  
    35. if(pSet == 0) nTickSetPress++;
    36. else nTickSetPress = 0;
    37.  
    38. if(pPlus == 0) nTickPlusPress++;
    39. else nTickPlusPress = 0;
    40.  
    41. if(pMinus == 0) nTickMinusPress++;
    42. else nTickMinusPress = 0;
    43.  
    44. if(pShift == 0) nTickShfPress++;
    45. else nTickShfPress = 0;
    46.  
    47. if(pCountPlus == 0) nTickCount++;
    48. else nTickCount = 0;
    49.  
    50. if(pReset == 0) nTickResetPress++;
    51. else nTickResetPress = 0;
    52.  
    53. if(bCdlyStart == 1) nCdlyCount++;
    54. if(nCdlyCount >= nTickCdly) bCdlyStart = 0;
    55.  
    56. if(bDisplayTime == 1) nDisplayTimeCount++;
    57.  
    58. if(bBlinkDigitFast == 1) nTickBlinkFast++;
    59.  
    60. TMR0H = TMR0HValue;
    61. TMR0L = TMR0LValue;
    62. INTCONbits.TMR0IF = 0;
    63. }
    64.  
    65.  
    66. void RC_ISR(void)
    67. {
    68. rxbuf485 = RCREG1;
    69.  
    70. if (rxbuf485 == 0)
    71. {
    72. return;
    73. }
    74.  
    75. if (rxbuf485 == TOKEN)
    76. {
    77. b485RxToken = 1;
    78. return;
    79. }
    80.  
    81. if (b485RxComplete) return;
    82.  
    83. if (!b485SOH)
    84. {
    85. if (rxbuf485 != SOH) return;
    86. b485SOH = 1;
    87. n485RxDataPos = 0;
    88. b485RxComplete = 0;
    89. memset (RS485RXDATA, 0, sizeof(RS485RXDATA));
    90. return;
    91. }
    92. else if (rxbuf485 == EOT)
    93. {
    94. b485SOH = 0;
    95. b485RxComplete = 1;
    96. return;
    97. }
    98. if (n485RxDataPos == 50)
    99. n485RxDataPos = 50;
    100.  
    101. if (n485RxDataPos>=RS485RXSIZE)
    102. n485RxDataPos--;
    103.  
    104. RS485RXDATA[n485RxDataPos++] = rxbuf485;
    105. return;
    106.  
    107. }
    108.  
    109. void Timer0Init(void)
    110. {
    111. T0CON = 0x07;
    112. TMR0H = TMR0HValue;
    113. TMR0L = TMR0LValue;
    114. T0CONbits.TMR0ON = 1;
    115. INTCONbits.TMR0IE = 1;
    116. nTick0 = 0;
    117. nTickSetPress = 0;
    118. nTickResetPress = 0;
    119. nTickCdly = 0;
    120. nTickBlinkFast = 0;
    121. }
    122.  
    123. void RS485Init(void)
    124. {
    125. TRISCbits.TRISC7=1; // RX
    126. TRISCbits.TRISC6=0; // TX
    127.  
    128. TXSTA1 = 0x00;
    129. RCSTA1 = 0x90;
    130. SPBRG1 = 30;
    131. BAUDCON1 = 0x00;
    132. PIE1bits.RC1IE = 1;
    133. TRISCbits.TRISC3 = 0;
    134. p485 = 0;
    135.  
    136. memset (RS485RXDATA, 0, sizeof(RS485RXDATA));
    137. }
    138.  
    Can two routine be done in a high priority interrupt? Will there be any problem in long term?
    This is because I'm trying to find out why my device always hang after running a few days...
     
    Last edited by a moderator: Oct 13, 2014
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    No. There should not be any routines called in the ISR since once you make even a single call then C assumes you need many more resources then you really do, and the routine isno longer fast and tight.

    You can service many sources of interrupts, but don't use a call, use another structure such as if...else.

    And don't touch the global interrupt flag inside the routine. The compiler will take care of that for you without fail.
     
  3. ahgan84

    Thread Starter Member

    Dec 19, 2011
    55
    0
    Don't understand what you mean. Any examples?
    I already use 'if' in the chk_isr routine...
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    I can't show an example of NOT doing something.

    You have three functions here: My_HiPrio_Int, Timer0_Irs, andRs485Init. Merge the code from the last two functions into the first.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,447
    3,363
    Remove chk_isr(void).

    There is no need to check which interrupt occurred. The two interrupts are independent and are handled by their own ISR.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    chk_isr(void) is certainly essential so leave that.

    I also notice the memset function being called... write the code to remove that call too.
     
Loading...