data on pins changes when interrupt is used

Discussion in 'General Electronics Chat' started by aamirali, Dec 18, 2012.

  1. aamirali

    Thread Starter Member

    Feb 2, 2012
    I have written code which changes port1 bits (bit 19 to 26) & some other statements, & an other interrupt which changes port1 bits (bit 27 & 28).
    Problem is when compiler comes to first part & meantime interrupt comes, data on pins got changes other than as expected.

    But on checking individually, both parts worked correctly.I also did sigle steeping for both & they worked fine.

    Does that mean data goes lost/corrupted. If yes how to avoid this.
  2. GopherT

    AAC Fanatic!

    Nov 23, 2012
    There are two possible issues.

    First, make sure your interrupt service routine correctly captures the status of all bits (first), makes appropriate interupt adjustments that you want, then restores the status bits to the pre-interupt condition, then return to your code. Make sure any variables and subroutines run during the interrupt are not overwriting variables from the main code (i.e, be carefull with "junk" variable names like: temp, counter, etc, that some people reuse in multiple areas of code). Un-expected changes during the interrupt can change the result of the main code.

    Any changes to the input pins during the interupt service may not be noticed unless you specifically monitor changes.

    Finally, Capacitance in the circuit attached to each pin may cause a read command (or mov command, to read current data (still in the process of updating) instead of the intended data (what you were expecting the data to be). This is especially true if you manually change bit values the read the port immediately (within 1 to 4 program steps) after the change.
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    Also, be sure that your bitmasks are set up and you are using them properly(OR to set a bit AND to clear a bit).
  4. RS-232D

    New Member

    Dec 1, 2012
    I'm thinking gopher gave the best answer probably without any specifics gave. I am presuming you meant single stepping, instead of sigle steeping? If so, then I am really thinking GopherT is right with a timing issue, and fluctuation in a possible read or write. you mentioned the data wasn't as expected. but was it consistently the same?
  5. aamirali

    Thread Starter Member

    Feb 2, 2012
    Code is for LPC1317-64P. Mask is used to write specific bits only.Problem occur when ISR occur at same time as data changes on pins.

    Edit: These three statements gets converted into 8 assembly instructions.

    Code ( (Unknown Language)):
    3. void change()
    4. {
    5.   ........statemets
    6.  .............
    7.  LPC_GPIO->MASK[1] = ~(0x00f0);            //mask specific bits to write on port 1
    8.  LPC_GPIO->MPIN[1] = 0xa0;                     //write the pins
    9.  LPC_GPIO->MASK[1] = 0x00;                 //remove the mask
    10.  ..................
    11.  .................
    12. }
    14. void interrupt_any()
    15. {
    16.  LPC_GPIO->MASK[1] = ~(0x000f);
    17.  LPC_GPIO->MPIN[1] = 0x0a;        
    18.  LPC_GPIO->MASK[1] = 0x00;
    19. }
    Last edited: Dec 19, 2012
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    You originally stated that the data is 8 bits and it affects two higher bits when an interrupt occurs. That isn't what this code is doing.

    Perhaps you can explain what you see and what you expect to see.

    Also, a schematic helps us from guessing too much...
  7. aamirali

    Thread Starter Member

    Feb 2, 2012
    that just for reference I put. This is how bits are changed.I have a tft connected to port & this is how I change data.