RB0 interrupt and toggle issue


Closed Thread
Results 1 to 36 of 36
  1. #1

    Question RB0 interrupt and toggle issue

    Hello,
    I am running a hardware interrupt on RB0 (rising edge trigger). I use RB0 to interrupt the micro which is (I assume) sitting at a DEBUGIN waiting for serial data.

    The inline assembly code (as listed in the datasheets 16F877A) to take care of house keeping going in and coming out of the interrupt is present. During an interrupt I jump to a "call routine" which jumps out and runs some code in Picbasic, then returns. The problem I have is in this routine called from within the interrupt is supposed to toggle port bits (1 through 8, bitwise) written in Picbasic. The problem is it does not toggle. It seems to only go HIGH but will not go LOW.

    Anyone run into this issue where toggle does not work? I will post the code tomorrow for reference...I have been trying to figure this out for over 1.5 days now.

    Thanks!
    Nick

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Macgman2000 View Post
    Hello,
    I am running a hardware interrupt on RB0 (rising edge trigger). I use RB0 to interrupt the micro which is (I assume) sitting at a DEBUGIN waiting for serial data.

    The inline assembly code (as listed in the datasheets 16F877A) to take care of house keeping going in and coming out of the interrupt is present. During an interrupt I jump to a "call routine" which jumps out and runs some code in Picbasic, then returns. The problem I have is in this routine called from within the interrupt is supposed to toggle port bits (1 through 8, bitwise) written in Picbasic. The problem is it does not toggle. It seems to only go HIGH but will not go LOW.

    Anyone run into this issue where toggle does not work? I will post the code tomorrow for reference...I have been trying to figure this out for over 1.5 days now.

    Thanks!
    Nick
    Hi Nick, yeah I have had toggle not work and never figured out why. BTW Call as I understand it is for use in ASM and gosub for PBP. When you post your code we can all chew on it a while, see what we can learn. I bet the BIG GUNS here CAN answer this problem.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3


    Did you find this post helpful? Yes | No

    Question code

    OK...here it is. The stuff in the mainloop is waiting for serial data to update the port bits which each control the activation of a relay (8 relays). The interrupt port RB0 is tied to 8 button inputs, the idea is to push any of the 8 buttons and interrupt out of the DEBUGIN to decode key presses. It goes through the interrupt, loads payload in the interrupt (it works). Where it is not working is in the CALL outside of the interrupt. It does NOT toggle the port bits. I saw something strange that I could not replicate, when it did toggle it undid the logic of any bits that were set high. Anyway...here it is, I would appreciate the help!

    Nick


    INCLUDE "Modedefs.Bas"
    @ device HS_OSC, LVP_OFF, WDT_OFF
    ; @ Device pic16F877A, HS_OSC, BOD_OFF, PWRT_ON, WDT_ON, PROTECT_OFF

    DEFINE OSC 20



    DEFINE DEBUGIN_REG PORTC ' Set portC as software RX in
    DEFINE DEBUG_BAUD 2400 ' Set bit rate
    DEFINE DEBUGIN_BIT 7 ' Set portC bit 7
    DEFINE DEBUGIN_MODE 1 ' Set Debugin mode: 0 = true, 1 = inverted

    DEFINE INTHAND myint ' Setup interrupt handler button presses




    ; initialize Interrupts
    OPTION_REG.6 = 1 ; option_reg RB0 interrupt rising=1, falling=0 edge
    INTCON = %10010000 ; Enable INTE RB0 interrupt $90


    ;set ports to input
    TRISD = %11111111 ; 8 button press inputs + RB0 interrupt for all off


    ;set ports to output
    TRISA = %00000000 ; set port A to outputs for relays
    TRISE = %00000000 ; set port E to outputs for relays

    ;set port initializations
    PORTA = %00000000
    PORTE = %00000000

    ' Configure internal registers

    wsave VAR BYTE $70 system ' Saves W
    ssave VAR BYTE bank0 system ' Saves STATUS
    psave VAR BYTE bank0 system ' Saves PCLATH
    fsave VAR BYTE bank0 system ' Saves FSR

    payload VAR byte bank0 system ' data location for selecting relays
    payload1 var byte bank0 system ' previous good data
    chksum var byte bank0 system ' check sum calculated at the TX side and sent over RF link


    payload = 0



    goto mainloop


    asm

    ; Save W, STATUS and PCLATH registers, if not done previously
    myint
    ; bsf INTCON, 1
    movwf wsave ; these commands are necessary to preserve registers to prevent
    swapf STATUS, W ; corruption
    clrf STATUS
    movwf ssave
    movf PCLATH, W
    movwf psave


    movf portd, w ; 8 momentary button press value
    movwf payload ; move portd value into payload to handoff to update_keys2
    call _keys2

    ; Restore saved registers
    bsf porte,0
    movf psave, W ; reload system register to previous values before interrupt was called
    movwf PCLATH
    swapf ssave, W
    movwf STATUS
    swapf wsave, F
    swapf wsave, W
    retfie ; Return from interrupt

    endasm




    keys2: ; called by interrupt to update relay
    if payload = 0 then
    portE = 0
    portA = 0
    endif
    if payload = 1 then
    toggle portA.0
    endif
    if payload = 2 then
    toggle portA.1
    endif
    if payload = 4 then
    toggle portA.2
    endif
    if payload = 8 then
    toggle portA.3
    endif
    if payload = 16 then
    toggle portA.4
    endif
    if payload = 32 then
    toggle portA.5
    endif
    if payload = 64 then
    toggle portE.0
    endif
    if payload = 128 then
    toggle portE.1
    endif

    return


    mainloop:
    INTCON = %10010000 ; Enable INTE RB0 interrupt $90
    ; receive data packets and check sum, throwing out sync byte (255)
    debugin [wait (255),payload,chksum]
    if chksum != payload then ; verify good packets by comparing check sums
    goto mainloop ; check sum does not match transmitted data
    endif

    update_keys:

    if payload = 0 then
    portE = 0
    portA = 0
    endif
    if payload = 1 then
    toggle portA.0
    endif
    if payload = 2 then
    toggle portA.1
    endif
    if payload = 4 then
    toggle portA.2
    endif
    if payload = 8 then
    toggle portA.3
    endif
    if payload = 16 then
    toggle portA.4
    endif
    if payload = 32 then
    toggle portA.5
    endif
    if payload = 64 then
    toggle portE.0
    endif
    if payload = 128 then
    toggle portE.1
    endif

    goto mainloop

  4. #4
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Hi Macgman2000,
    I didn't analyze your code too closely, because frankly I am not smart enough to understand all the assembly code, and it compiled with too many errors here for me to sort out, so I did the next best thing, I tossed out the assembly interrupts and installed Darrel's DT_INTS, and recompiled. Please try it as you have the breadboard already set up. MPASM Required.
    Code:
    'http://www.picbasic.co.uk/forum/showthread.php?p=73542#post73542
    'Macgman2000.bas
    '********************************************************************
    
    
    
    
    
    
    
    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
    
    INCLUDE "Modedefs.Bas"
    ;@ device HS_OSC, LVP_OFF, WDT_OFF
    ; @ Device pic16F877A, HS_OSC, BOD_OFF, PWRT_ON, WDT_ON, PROTECT_OFF
    @ __config _HS_OSC & _BODEN_OFF & _PWRTE_ON & _WDT_ON
    DEFINE OSC 20
    
    
    
    DEFINE DEBUGIN_REG PORTC ' Set portC as software RX in
    DEFINE DEBUG_BAUD 2400 ' Set bit rate
    DEFINE DEBUGIN_BIT 7 ' Set portC bit 7
    DEFINE DEBUGIN_MODE 1 ' Set Debugin mode: 0 = true, 1 = inverted
    
    ;DEFINE INTHAND myint ' Setup interrupt handler button presses
    
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
        INT_Handler RBC_INT, _keys2, PBP, yes
      endm
      INT_CREATE ; Creates the interrupt processor
    ENDASM
    
    
    ; initialize Interrupts
    OPTION_REG.6 = 1 ; option_reg RB0 interrupt rising=1, falling=0 edge
    INTCON = %10010000 ; Enable INTE RB0 interrupt $90
    
    
    ;set ports to input
    TRISD = %11111111 ; 8 button press inputs + RB0 interrupt for all off
    
    
    ;set ports to output
    TRISA = %00000000 ; set port A to outputs for relays
    TRISE = %00000000 ; set port E to outputs for relays
    
    ;set port initializations
    PORTA = %00000000
    PORTE = %00000000
    
    ' Configure internal registers
    
    
    
    payload VAR byte bank0 system ' data location for selecting relays
    payload1 var byte bank0 system ' previous good data
    chksum var byte bank0 system ' check sum calculated at the TX side and sent over RF link
    
    
    payload = 0
    
    
    
    goto mainloop
    
    
    
    
    
    keys2: ; called by interrupt to update relay
    if payload = 0 then
    portE = 0
    portA = 0
    endif
    if payload = 1 then
    toggle portA.0
    endif
    if payload = 2 then
    toggle portA.1
    endif
    if payload = 4 then
    toggle portA.2
    endif
    if payload = 8 then
    toggle portA.3
    endif
    if payload = 16 then
    toggle portA.4
    endif
    if payload = 32 then
    toggle portA.5
    endif
    if payload = 64 then
    toggle portE.0
    endif
    if payload = 128 then
    toggle portE.1
    endif
    
    @ INT_RETURN
    
    
    
    mainloop:
    INTCON = %10010000 ; Enable INTE RB0 interrupt $90
    ; receive data packets and check sum, throwing out sync byte (255)
    debugin [wait (255),payload,chksum]
    if chksum != payload then ; verify good packets by comparing check sums
    goto mainloop ; check sum does not match transmitted data
    endif
    
    update_keys:
    
    if payload = 0 then
    portE = 0
    portA = 0
    endif
    if payload = 1 then
    toggle portA.0
    endif
    if payload = 2 then
    toggle portA.1
    endif
    if payload = 4 then
    toggle portA.2
    endif
    if payload = 8 then
    toggle portA.3
    endif
    if payload = 16 then
    toggle portA.4
    endif
    if payload = 32 then
    toggle portA.5
    endif
    if payload = 64 then
    toggle portE.0
    endif
    if payload = 128 then
    toggle portE.1
    endif
    
    goto mainloop
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  5. #5


    Did you find this post helpful? Yes | No

    Question

    Hello Joe,

    Is it possible to send me the include files?

    INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
    INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

    Also, Why is MPASM needed if the files are .bas ? I haven't used this interrupt module before. I need a tad bit hand holding through this. I appreciate your help!!!!!!

    Nick

  6. #6
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    http://darreltaylor.com/DT_INTS-14/intro.html
    http://darreltaylor.com/DT_INTS-14/downloads.htm
    http://darreltaylor.com/DT_INTS-18/home.html

    Darrel's include files Require MPASM to work. Darrel Requires you not to copy paste the code into your code, rather use them as include files as I did. They exist in versions for the 14 bit core and for the 18F series. He has generously made these available to us, and I am grateful to him. THANKS DARREL !

    Quote Originally Posted by Macgman2000 View Post
    Also, Why is MPASM needed if the files are .bas ?
    Nick
    If you need help switching assemblers let me know, it's pretty easy.

    EDIT:
    Nick I think I set your code up with the wrong interrupt, I set it up as RBC_INT and that would be for on change interrupt whereas you want interrupt on portB.0 which should be INT_INT in the assy statement for INT_Handler .
    Last edited by Archangel; - 30th April 2009 at 00:41. Reason: ReEnterPBP-14.bas is in the .zip file
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  7. #7


    Did you find this post helpful? Yes | No

    Question

    I am getting a lot of errors. I unzipped the files in the PBP directory (same as PBP.exe). It shows up in the compiler includes column on the left side of microcode. I am using compiler 2.47.

    Errors in microcode studio:

    1). _config
    2). local directive use only in macro
    3). undefinded symbol 'iflagreg'
    4). numeric constant or symbol name expected
    5). ')' expected
    6). too many errors

    I am clueless as to why it is doing that. The include files from Darrel do not need any special modifications or set up....right?

    Nick

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Like Joe said ... you must be using MPASM as the assembler.
    <br>
    DT

  9. #9


    Did you find this post helpful? Yes | No

    Question

    Switched over to MPASM....now I have these errors:

    @ _config _HS_OSC & _BODEN_OFF & _PWRTE_ON & _WDT_ON

    For whatever reason it does not like the syntax of the above. Once I comment them out and compile, it has no errors. Putting them in produces errors specific to the items after the @. Is this the correct syntax?

    Nick

  10. #10
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Macgman2000 View Post
    Switched over to MPASM....now I have these errors:

    @ _config _HS_OSC & _BODEN_OFF & _PWRTE_ON & _WDT_ON

    For whatever reason it does not like the syntax of the above. Once I comment them out and compile, it has no errors. Putting them in produces errors specific to the items after the @. Is this the correct syntax?

    Nick
    2 things, The config statement requires TWO UNDERSCORES __ without spaces between them,Like this @ __config and you will need to open the 16F877A.inc file (if that is the chip you are using), in the PBP Root directory and put a semicolon ; just prior to the MPASM DEFAULT CONFIG STATEMENT. This will cause PBP to ignore it as a comment, because your program can only use the one.
    Last edited by Archangel; - 30th April 2009 at 02:45.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  11. #11


    Did you find this post helpful? Yes | No

    Question

    OK it compiled but still does not work. Meaning....A key press yeilds no toggling of ports. It gets to the interrupt (I placed a porta.0 = 1 flag) so I know that part is working just like the PBP interrupt handler when any key is pressed. The problem is nothing toggles or the payload value of the keypress is corrupted. I inserted pause 1000 for debounce but that didn't work either. It seems to be blasting right by the conditional statements and exiting the interrupt.

    Man this is frustrating!

  12. #12
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Macgman2000 View Post
    OK it compiled but still does not work. Meaning....A key press yeilds no toggling of ports. It gets to the interrupt (I placed a porta.0 = 1 flag) so I know that part is working just like the PBP interrupt handler when any key is pressed. The problem is nothing toggles or the payload value of the keypress is corrupted. I inserted pause 1000 for debounce but that didn't work either. It seems to be blasting right by the conditional statements and exiting the interrupt.

    Man this is frustrating!
    As I said before I have had toggle not work. Perhaps you can use a bitwise ^ to flip the bit. Try looking at the variables contents using a serout or debugout and see if it contains the value it is supposed to have. I will look at this tonight after I come home from dinner.
    Last edited by Archangel; - 1st May 2009 at 02:14.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  13. #13
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Toggle doesn't work because you have'nt disable the ADCs.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  14. #14
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    Toggle doesn't work because you have'nt disable the ADCs.
    Hahaha I just stumbled across one of Melanies posts mentioning that, http://www.picbasic.co.uk/forum/showthread.php?t=977
    This chip has CCP module on portC. Good call Steve.
    Last edited by Archangel; - 1st May 2009 at 02:29.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  15. #15


    Did you find this post helpful? Yes | No

    Question

    It shouldn't affect portE which is where 2 of my relay pins are. I will try it anyway and let you know. Thanks for the suggestion!

    Nick

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    Where is the PIC populated? Is it on any Microchip board?

    Ioannis

  17. #17
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Macgman2000 View Post
    It shouldn't affect portE which is where 2 of my relay pins are. I will try it anyway and let you know. Thanks for the suggestion!

    Nick
    Sure enough it will as PORTE<2:0> are multiplexed with AN<7:5>
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  18. #18


    Did you find this post helpful? Yes | No

    Default

    I went through and disabled everything and then some on portA and PortE. No dice. portA and PortE change from low to high but does not toggle. In fact it undoes any set pins on portA and E. It does not alter them at the bit level individually. My desired function is to latch individual pins in either 1 or 0 condition to energize a relay or de-energize it, through the decoding of local switch presses. I need to take a step back or something....thanks for your help guys.

    Nick

  19. #19
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    I repeat my question. Do you have any dev. kit or use your own PCB design?

    Ioannis

  20. #20


    Did you find this post helpful? Yes | No

    Question

    The micro is on a circuit board. It uses a 20Mhz resonator and I am using in-circuit-programming. All ports are on header pins and not tied to anything, except for the button switches which are pulled low via 10k resistors. I have physical jumpers to put the hardware into ICP mode and then back to normal mode. I can set ports when the interrupt routine is called on any button press, but it does not decode the content of the variable I place the key press in to. IE, it blasts through conditional statements.

    I can set bit wise port A and Port E within the interrupt. If within the interrupt I set portA (your bit of choice) and then use a conditional statement to reset portA (your chosen bit) if the bit is set...it does nothing....the port bit stays set. Toggle does nothing to alter the port logic level output.

    Very strange.
    Last edited by Macgman2000; - 3rd May 2009 at 21:21. Reason: toggle issue

  21. #21
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    OK i have had an eintesy bit of success with your code.
    To your config statement add _DEBIG_ON and ADD TRISB = %00000001 so the B.0 interrupt will work. I am not saying this will fix it, but it needs those fixes, gotta run, Gotta Barbeque.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  22. #22


    Did you find this post helpful? Yes | No

    Question interrupts

    Ok for my own sanity, I would like confirmation on my understanding of how RB0 interrupt works or any interrupt for that matter. Once an interrupt condition happens, the interrupt flag is set, then the code is vectored to the interrupt start location.

    In my code, I do the customary saving status, pclath..etc. Then I run my code, then repack status and pclath.
    Retfie statement at the end of the interrupt clears the interrupt flag so now it is enabled to recieve interrupts. So during the time I am running my code in the interrupt, it shouldn't be retriggering the interrupt based on switch bounce until it sees Retfie....or does it? Somewhere, somehow the contents of the variable that I am passing to the case statements is getting hosed (I assume). The variable is specfied as bank0 System, so content must be common across banks.....or is it?

    Lots of questions, I guess I am second guessing everything now.

    Nick

  23. #23
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818


    Did you find this post helpful? Yes | No

    Default

    It is still a little buggy but it works after a fashion
    Code:
    DEFINE OSC 20
    
    DEFINE DEBUGIN_REG PORTC ' Set portC as software RX in
    DEFINE DEBUG_BAUD 2400   ' Set bit rate
    DEFINE DEBUG_BIT 6
    DEFINE DEBUGIN_BIT 7     ' Set portC bit 7
    DEFINE DEBUGIN_MODE 0    ' Set Debugin mode: 0 = true, 1 = inverted
    
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
        INT_Handler INT_INT, _keys2, PBP, yes
      endm
      INT_CREATE ; Creates the interrupt processor
    ENDASM
    ADCON0 = 0    ' Kill analog Functions
    ADCON1 = 7
    CMCON = 7
    CVRCON = 0
    CCP1CON = 0
    CCP2CON = 0
    
    ; initialize Interrupts
    OPTION_REG.6 = 1   ; option_reg RB0 interrupt rising=1, falling=0 edge
    INTCON = %11010000 ; Enable INTE RB0 interrupt $90
    
    
    
    
    ;set port initializations
    PORTA = %00000000
    PORTE = %00000000
    
    
    TRISA = %00000000 ; set port A to outputs for relays
    TrisB = %00000001 ' b0 input for interrupt
    TRISC = %10000000 'set 7 as input debugin
    TRISD = %00000000 ; 8 button press inputs + RB0 interrupt for all off  Made outputs as unused
    TRISE = %00000000 ; set port E to outputs for relays
    ' Configure internal registers
    
    
    payload  VAR byte ; bank0 system ' data location for selecting relays
    'payload1 var byte ; bank0 system ' previous good data
    chksum   var byte ; bank0 system ' check sum calculated at the TX side and sent over RF link
    
    
    payload = 0
    CHKSUM  = 0
    
    
    goto mainloop
    
    
    
    
    
    keys2: ; called by interrupt to update relay
    
    if payload = 0 then
    portE = 0
    portA = 0
    endif
    if payload = 1 then
    toggle portA.0
    endif
    if payload = 2 then
    toggle portA.1
    endif
    if payload = 4 then
    toggle portA.2
    endif
    if payload = 8 then
    toggle portA.3
    endif
    if payload = 16 then
    toggle portA.4
    endif
    if payload = 32 then
    toggle portA.5
    endif
    if payload = 64 then
    toggle portE.0
    endif
    if payload = 128 then
    toggle portE.1
    endif
    
    @ INT_RETURN
    
    
    
    mainloop:
    INTCON = %11010000 ; Enable INTE RB0 interrupt $90
    ; receive data packets and check sum, throwing out sync byte (255)
    debugin [payload,chksum]
    debug payload,chksum
    if chksum != payload then ; verify good packets by comparing check sums
    goto mainloop ; check sum does not match transmitted data
    endif
    
    update_keys:
    
    if payload = 0 then
    portE = 0
    portA = 0
    endif
    if payload = 1 then
    toggle portA.0
    endif
    if payload = 2 then
    toggle portA.1
    endif
    if payload = 4 then
    toggle portA.2
    endif
    if payload = 8 then
    toggle portA.3
    endif
    if payload = 16 then
    toggle portA.4
    endif
    if payload = 32 then
    toggle portA.5
    endif
    if payload = 64 then
    toggle portE.0
    endif
    if payload = 128 then
    toggle portE.1
    endif
    PAYLOAD = 0  ' Zero here or loaded variable will turn on port next loop after INT
    CHKSUM = 0
    goto mainloop
    Here is what I don't get . . if yoy turn on portE.0 and PortE.1 toggling either, toggles both. ???? I have in the past written code to do what this code does and had the same problem, only I think it was on portA.
    Last edited by Archangel; - 4th May 2009 at 22:28.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  24. #24


    Did you find this post helpful? Yes | No

    Question

    Great I am not the only one experiencing the weirdness of trying to use toggle! Thanks for verifying I am not going nuts!!!!! I have a version of the code written where the ports work in terms of toggling...but here is the kicker and I don't know why......I add pauses with in the Call routine, this allows the port to change and even toggle. But Only PortA.0......lol...... If rapid press for a few seconds eventually I get portA.1 but if PortA.0 is set, portA.1 change resets PortA.0.....man am I ready to throw this thing against the wall!

    I want to eventually, when I calm down try to run a timer interrupt fast enough to look at the input port status. I don't know if I am just going to see the same problem or not.

  25. #25
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Macgman2000 View Post
    Ok for my own sanity, I would like confirmation on my understanding of how RB0 interrupt works or any interrupt for that matter. Once an interrupt condition happens, the interrupt flag is set, then the code is vectored to the interrupt start location.

    In my code, I do the customary saving status, pclath..etc. Then I run my code, then repack status and pclath.
    This HINT, was also a comment from your first code ...
    Code:
    ; Save W, STATUS and PCLATH registers, if not done previously
    Since a 16F877A has more than 2K of code space,
    It was "done previously", and automatically by PBP.
    Doing it again saves/restores the wrong values, which makes everything go Wacko.

    However, you are also trying to CALL basic language routines from an ASM interrupt handler.

    You can't do that.
    At least, not the way your trying.
    <br>
    DT

  26. #26
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    try this one
    Code:
            @   __CONFIG _HS_OSC & _LVP_OFF
            DEFINE OSC 20
            
            INCLUDE ".\INCLUDE_ROUTINES\DT_INTS-14.bas"
            INCLUDE ".\INCLUDE_ROUTINES\ReEnterPBP.bas"
            
            ;set port initializations
            PORTA = %00000000
            PORTE = %00000000
            
            TRISA = %00000000 ; set port A to outputs for relays
            TrisB = %00000001 ' b0 input for interrupt
            TRISC = %10000000 'set 7 as input debugin
            TRISD = %00000000 ; 8 button press inputs + RB0 interrupt for all off  Made outputs as unused
            TRISE = %00000000 ; set port E to outputs for relays
           
            ' Configure internal registers
            ADCON1 = 7
            CMCON = 7
    
            DEFINE DEBUGIN_REG PORTC ' Set portC as software RX in
            DEFINE DEBUG_BAUD 2400 ' Set bit rate
            DEFINE DEBUGIN_BIT 7 ' Set portC bit 7
            DEFINE DEBUGIN_MODE 1 ' Set Debugin mode: 0 = true, 1 = inverted
            'DEFINE DEBUG_REG PORTC 'Debug pin port 
            'DEFINE DEBUG_BIT 6 'Debug pin bit    
            'DEFINE DEBUG_MODE 1 'Debug mode: 0 = True, 1 = Inverted      
            
            ; initialize Interrupts
            OPTION_REG.6 = 1   ; option_reg RB0 interrupt rising=1, falling=0 edge
            ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
            INT_Handler INT_INT, _keys2, PBP, yes
            endm
        
            INT_CREATE ; Creates the interrupt processor
            ENDASM
    
            NewPayload  var Byte
            payload     VAR byte ' data location for selecting relays
            chksum      var byte ' check sum calculated at the TX side and sent over RF link
            INT0IF      var INTCON.1
            
            CLEAR  
            pause 50 ' settle time         
    @       INT_CLEAR  INT_INT  ; make sure INT0IF is cleared
    @       INT_ENABLE INT_INT  ; Enable Interrupt        
    
    mainloop:
            ; receive data packets and check sum, throwing out sync byte (255)
            DEBUGIN [wait(255),NewPayload, chksum]
            'DEBUG NewPayload, chksum
            if chksum != newpayload then MainLoop
            payload=NewPayload
            goto    updatePort
            
    
    keys2: ; Called by Interrupt update relay
            
            pause 20                ' small debounce delay
            if !PORTB.0 then GetOut ' button up?  
                                    ' If so, getout of here... no need for noise call
    UpdatePort:  
            select case payload
                    case 0
                        PORTE = 0
                        PORTA = 0
                    CASE 1
                        toggle portA.0
                    CASE 2
                        toggle portA.1
                    CASE 4
                        toggle portA.2
                    CASE 8
                        TOGGLE PORTA.3
                    CASE 16
                        toggle portA.4
                    CASE 32
                        toggle portA.5
                    CASE 64
                        toggle portE.0
                    CASE 128
                        toggle portE.1
                    END SELECT 
    GetOut:
            If INT0IF THEN  ' called by Interrupt?
                    @ INT_RETURN    
                            ' - Yes, clear the flag and getout
                    ELSE    '
                        GOTO MAINLOOP
                            ' - No, return to Mainloop
                    ENDIF
    Make sure you transmit the right way from your PC...
    Last edited by mister_e; - 5th May 2009 at 07:39.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  27. #27


    Did you find this post helpful? Yes | No

    Default

    mister_e, I copied and pasted the code, I made one slight modification to the Include statements. I don't have them in the directories you have them in, but instead in the main PBP file. I switched over my assembler to MPASM. I get the following error when I compile.

    "Error [118] c:\pbp\RX_New.asm 103: overwriting previous address contents 2007"

  28. #28
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

  29. #29


    Did you find this post helpful? Yes | No

    Default

    Thanks Ioannis and mister_e, for your help!!!! I appreciate your patience, I feel like a fish out of water on this project.

    The code is not responsive to button presses. I changed the port configuration from output to input on portD. enclosed is my button arrangment, the idea is any button press will also trigger the RB0 interrupt along with the key momentarily held on portD. Also, the code is attached for reference.

    @ __CONFIG _HS_OSC & _LVP_OFF
    DEFINE OSC 20

    INCLUDE ".\DT_INTS-14.bas"
    INCLUDE ".\ReEnterPBP.bas"



    ;set port initializations
    PORTA = %00000000
    PORTE = %00000000

    TRISA = %00000000 ; set port A to outputs for relays
    TrisB = %00000001 ' b0 input for interrupt
    TRISC = %10000000 'set 7 as input debugin
    TRISD = %11111111 ; 8 button press inputs + RB0 interrupt for all off Made outputs as unused
    TRISE = %00000000 ; set port E to outputs for relays

    ' Configure internal registers
    ADCON1 = 7
    CMCON = 7

    DEFINE DEBUGIN_REG PORTC ' Set portC as software RX in
    DEFINE DEBUG_BAUD 2400 ' Set bit rate
    DEFINE DEBUGIN_BIT 7 ' Set portC bit 7
    DEFINE DEBUGIN_MODE 1 ' Set Debugin mode: 0 = true, 1 = inverted
    'DEFINE DEBUG_REG PORTC 'Debug pin port
    'DEFINE DEBUG_BIT 6 'Debug pin bit
    'DEFINE DEBUG_MODE 1 'Debug mode: 0 = True, 1 = Inverted

    ; initialize Interrupts
    OPTION_REG.6 = 1 ; option_reg RB0 interrupt rising=1, falling=0 edge
    ASM
    INT_LIST macro ; IntSource, Label, Type, ResetFlag?
    INT_Handler INT_INT, _keys2, PBP, yes
    endm

    INT_CREATE ; Creates the interrupt processor
    ENDASM

    NewPayload var Byte
    payload VAR byte ' data location for selecting relays
    chksum var byte ' check sum calculated at the TX side and sent over RF link
    INT0IF var INTCON.1

    CLEAR
    pause 50 ' settle time
    @ INT_CLEAR INT_INT ; make sure INT0IF is cleared
    @ INT_ENABLE INT_INT ; Enable Interrupt

    mainloop:
    ; receive data packets and check sum, throwing out sync byte (255)
    DEBUGIN [wait(255),NewPayload, chksum]
    'DEBUG NewPayload, chksum
    if chksum != newpayload then MainLoop
    payload=NewPayload
    goto updatePort


    keys2: ; Called by Interrupt update relay

    pause 20 ' small debounce delay
    if !PORTB.0 then GetOut ' button up?
    ' If so, getout of here... no need for noise call
    UpdatePort:
    select case payload
    case 0
    PORTE = 0
    PORTA = 0
    CASE 1
    toggle portA.0
    CASE 2
    toggle portA.1
    CASE 4
    toggle portA.2
    CASE 8
    TOGGLE PORTA.3
    CASE 16
    toggle portA.4
    CASE 32
    toggle portA.5
    CASE 64
    toggle portE.0
    CASE 128
    toggle portE.1
    END SELECT
    GetOut:
    If INT0IF THEN ' called by Interrupt?
    @ INT_RETURN
    ' - Yes, clear the flag and getout
    ELSE '
    GOTO MAINLOOP
    ' - No, return to Mainloop
    ENDIF ;Make sure you transmit the right way from your PC...
    Attached Images Attached Images

  30. #30
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Well it has to go to the ISR, but probably you want to refresh the Payload value with the PushButton?

    In this case...
    Code:
    keys2: ; Called by Interrupt update relay
    
            pause 20 ' small debounce delay
            if !PORTB.0 then GetOut ' button up?
                                    ' If so, getout of here... no need for noise call
    
            PayLoad = PORTD ' read PushButtons and update Payload Value
    UpdatePort:
    You could also use a timer interrupt and poll PORTD, this will allow you to remove all those diodes, and leave RB0 for something else. As it is user push-button, you'll probably never feel the latency of the Timer interrupt.
    Last edited by mister_e; - 5th May 2009 at 17:42.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  31. #31
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    Also if the button is kept low for a while it will always loop through the ISR.

    Maybe a check at the start or the end of the ISR is needed to assure that button is released.

    Ioannis

  32. #32
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    It shouldn't loop that much, as the ISR is called by a rising edge.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  33. #33
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,795


    Did you find this post helpful? Yes | No

    Default

    Oh yes Steve. I missed that.

    Then I cannot explain this bizzare behaviour.

    Ioannis

  34. #34
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Yesterday I tried my version and it seems to work properly here, the second addition above "should" cover what the OP need...

    Fact is, when you're using push button and interrupts, things can sometimes pan-out in weird way due to the noise on the contact when you push/release them.
    Code:
    keys2: ; Called by Interrupt update relay
    
            pause 20 ' small debounce delay
            if !PORTB.0 then GetOut ' button up?
                                    ' If so, getout of here... no need for noise call
    is one way to avoid problem, even thought, it's not always the best way
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  35. #35


    Did you find this post helpful? Yes | No

    Talking Awesome!

    mister-e you are right on the money! I added PayLoad = PORTD and it works very well, it toggles the individual bits only and not reset the entire port. PortA.4 is not responding though....it stays low.

    Thank you and everyone who helped. I think I will be using this new interrupt handler in the future.

    Nick
    Last edited by Macgman2000; - 5th May 2009 at 20:45.

  36. #36
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    <table><tr><td></td><td>
    PortA.4 is not responding though....it stays low
    for PORTA.4... well... it's an open-collector output type on this one. So you may need to slightly modify your hardware and software.

    http://www.picbasic.co.uk/forum/showthread.php?t=11116

    In brief, connect your LED between Vdd and PORTA.4, and change those
    Code:
    PORTA = 0
    to
    Code:
    PORTA = %00010000
    That should work for LEDs. If you drive a transistor but then it's easier... not software modification are needed, just hardware.
    </td></tr></table>
    Last edited by mister_e; - 5th May 2009 at 21:10.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Instant Interrupts - Revisited
    By Darrel Taylor in forum Code Examples
    Replies: 772
    Last Post: - 17th February 2016, 23:14
  2. TMR0 interrupt and HSERIN
    By boban in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd September 2008, 12:48
  3. I have problem with interrupt RB0
    By chai98a in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 5th July 2008, 10:55
  4. tmr2 interrupt problem
    By ADCON in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 2nd January 2008, 19:49
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 02:07

Members who have read this thread : 2

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts