Simple Interrupt on Change Problem

Discussion in 'Embedded Systems and Microcontrollers' started by awmt102, Jan 2, 2010.

  1. awmt102

    Thread Starter New Member

    Jan 2, 2010
    3
    0
    Hi all,

    I am having some trouble getting a simple program using the Interrupt on change function.

    Here is my hardware setup:

    PIC18F2550, 4MHz Crystal. LEDs attached on PORTA<0:3>, push button on PORTB.4. I am using the EasyPIC6 dev board from Mikro Electronika so all of the connections are definately correct.

    The software is written in Picbasic Pro using Microcode Studio and includes the use of Darrell Taylors Instant Interrupts.

    The program is ultimately very simple:

    Turn on LED on A.1
    Initialise the USB connection
    Turn on LED on A.0 (and off on A.1)
    Enter a loop in which the USB connecton is serviced approximately every 5ms and set LED on A.3 to status of B.4 (this is to confirm to me that the program is still running)

    When the interrupt on change on PORTB is detected it sets A.2 LED to the status of B.4.

    However the program for some reason does not function correctly. One of two things happens when the button is pressed: Either A.2 stays off (but A.3 follows the button press so the program is running correctly but the interrupt does not seem to be firing.) or all the LEDs turn off and don't come back at all until the device is reset.

    I just cannot figure this out at all! If anyone can help it could be greatly appreciated.

    Since im certain this is a code issue I have attached it below:


    Code ( (Unknown Language)):
    1.  ==============================================================================
    2. ' PIC Configuration Bits
    3. ' ==============================================================================
    4.  
    5.  
    6.   asm
    7.     __CONFIG    _CONFIG1L, _PLLDIV_1_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L  
    8.                             ;              ;                      ; USB clock source comes from the 96 MHz PLL divided by 2
    9.                             ;              ; [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
    10.                             ; No prescale (4 MHz oscillator input drives PLL directly)
    11.  
    12.     __CONFIG    _CONFIG1H, _FOSC_XTPLL_XT_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    13.                             ;                  ;               ; Oscillator Switchover mode disabled
    14.                             ;                  ; Fail-Safe Clock Monitor disabled
    15.                             ; XT oscillator, PLL enabled, XT used by USB
    16.     __CONFIG    _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L  & _BORV_2_2L  & _VREGEN_ON_2L  
    17.     __CONFIG    _CONFIG2H, _WDT_OFF_2H
    18.     __CONFIG    _CONFIG3H, _MCLRE_ON_3H & _LPT1OSC_OFF_3H & _PBADEN_OFF_3H & _CCP2MX_OFF_3H
    19.     __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L ;& _ICPRT_OFF_4L  & _XINST_OFF_4L & _DEBUG_OFF_4L
    20.     endasm
    21.  
    22. DEFINE OSC 48
    23. INCLUDE "DT_INTS-18.bas"        ' Base Interrupt System
    24. INCLUDE "ReEnterPBP-18.bas"     ' Include if using PBP interrupts
    25.  
    26. ' ==============================================================================
    27. ' Constants and Variables Definition
    28. ' ==============================================================================
    29. USBBufferSizeMax   con 3  ' maximum buffer size
    30. USBBufferSizeTX    con 3  ' input
    31. USBBufferSizeRX    con 3  ' output
    32.  
    33. ' the USB buffer...
    34. USBBuffer        Var Byte[USBBufferSizeMax]
    35. USBBufferCount   Var Byte
    36.  
    37.  
    38. ' Used by USB initialisation:
    39. usb_device_state        var byte EXT
    40. CONFIGURED_STATE        CON EXT
    41.  
    42. ' Used in USB Comms
    43. IDLEIF                  VAR UIR.4
    44. SOFIF                   VAR UIR.6
    45. Plugged                 VAR BIT
    46.  
    47.  
    48. InputPin VAR PORTB.4                ' the Input on which edges are detected
    49. InputStatusLED var PORTA.2        ' the status of InputPin is mirrored here
    50. StatusLED_Pin1 var PORTA.0          ' Bicolour Device Status LED pin1
    51. StatusLED_Pin2 var PORTA.1          ' Bicolour Device Status LED pin2
    52. tempbyte var byte
    53. temp var bit
    54.  
    55. ==============================================================================
    56. ' Hardware configuration
    57. ' ==============================================================================
    58.     TRISA = 0                   ' all outputs
    59.     TRISB = %00010000' only RB4 as input so others are excluded from IOC
    60.     TRISC = 0                   ' all outputs
    61.     PORTA = 0                   ' set all LEDs off at startup
    62.     PORTB = 0
    63. ' ==============================================================================
    64. ' A/D converter
    65. ' ==============================================================================
    66. '        
    67.     ADCON0  =   %00000000       ' A/D converter module disabled
    68.     ADCON1  =   %00001111       ' PORTA all digital signals
    69. ' ==============================================================================
    70. ' USB module
    71. ' ==============================================================================
    72.     ucfg    =   %00010100       ' enable internal USB pull-up, Full speed USB
    73. ' ==============================================================================
    74. ' ASSEMBLER MACROS
    75. ' ==============================================================================
    76. '
    77. ' HIGH Priority Interrupt processor macro
    78. '
    79. ' Change on PORTB interrupt to transmit input status only on change
    80. ASM
    81. INT_LIST  macro    ; IntSource,          Label,  Type, ResetFlag?
    82.         INT_Handler    RBC_INT,  _ChangeOnPortB,   PBP,  yes
    83.     endm
    84.     INT_CREATE               ; Creates the interrupt processor
    85.  
    86. endasm
    87.  
    88. ==============================================================================
    89. ' MAIN PROGRAM
    90. ' ==============================================================================
    91.  
    92.     StatusLED_Pin2 = 1
    93.     StatusLED_Pin1 = 0  
    94.  
    95. gosub InitialiseUSB                ' Initialise the USB
    96.  
    97.     StatusLED_Pin2 = 0
    98.     StatusLED_Pin1 = 1  
    99.  
    100.  
    101.  
    102. @   INT_ENABLE  RBC_INT         ; Enable the RBC Interrupt
    103.  
    104.  
    105. ProgramStart:
    106.                     ' Sit in a loop waiting for interrupts to occur
    107.     USBSERVICE
    108.     pause 5
    109.     PORTA.3 = InputPin  
    110. goto ProgramStart
    111.  
    112.  
    113. ' ==============================================================================
    114. ' ROUTINES ==========================================================
    115. ' ==============================================================================
    116.  
    117. ChangeOnPortB:
    118. @   INT_DISABLE RBC_INT    
    119.     tempbyte = PORTB                ' read portb to update the change latch
    120.     InputStatusLED = InputPin
    121. @   INT_ENABLE RBC_INT
    122. @   INT_RETURN
    123.  
    124.  
    125. InitialiseUSB:
    126.    
    127.     pause 500
    128.    
    129.     usbinit ' initialise USB...
    130.     repeat  ' kick-start it by performing a tight loop of servicing until the USB
    131.             ' state is CONFIGURED (Darrell Taylors method)
    132.         usbservice
    133.     until usb_device_state = CONFIGURED_STATE
    134.  
    135. return
    136.  
    137.  
    138.  
     
    Last edited: Jan 2, 2010
  2. awmt102

    Thread Starter New Member

    Jan 2, 2010
    3
    0
    Thanks for the quick response Alberto but if only it were that simple! The interrupt is enabled just after the USB Initialisation routine so I'm afraid that is not the solution.

    Cheers

    Andy
     
  3. awmt102

    Thread Starter New Member

    Jan 2, 2010
    3
    0
    I did not think you had to enable them directly after the declaration since in DTs USB interrupts examples he does not enable the USB Service interrupt until after USB initialisation.

    Anyway I tried it just in case and also stripped out the USB stuff to see if this would help too. It did work, but as soon as I put the USB stuff back in it just turns all LEDs off when the button is pressed. I attempted to remove the USB stuff to get back to a known working state but it continues to just turn the LEDs off - I truly dont understand this!
     
Loading...