PIC18F6520 delay error

Discussion in 'Embedded Systems and Microcontrollers' started by Stackington21, Apr 16, 2016.

  1. Stackington21

    Thread Starter New Member

    Oct 16, 2015
    11
    0
    Hi all

    I am doing my coding subject for uni and i am struggling as to why one of my delay routines works correctly but the other does not. My first sub routine simply holds a high logic for 400ms and turns it off like so
    Code (C):
    1. void blank()                        //SUBROUTINE PART 7
    2.  
    3.     {
    4.         {
    5.             Blankrelease = 1;
    6.             Delay10KTCYx(40);
    7.             Blankrelease = 0;
    8.         }
    9. }
    10.  
    My second subroutine is about giving an integer and sending a 6 bit logic to the machine. Basically my pulses all work and everything is fine but the delay does not seem to be the required 200ms that I need:
    Code (C):
    1. int wind(int n)                  //SUBROUTINE PART 10
    2. {
    3.     int i;
    4.  
    5.     i = n/100;
    6.     if(i > 63) return -1;
    7.     if (i <= 0) return -1;
    8.     {
    9.     WindInputPulse = 1;
    10.     Delay10KTCYx(20);
    11.     WindInputPulse = 0;
    12.     }
    13.     if(LATD = i)
    14.         BUSY = 1;
    15.  
    16.         BUSY = 0;
    17.  
    18.     return 0;
    19. }
    20.  
    What i am trying to understand is how are these delays acting differently even though they are the exact same? The highest value i can put in the delay for the second subroutine is 11 which gives a 110ms delay, why is that though?

    These codes are both called from the main the same way

    Thanks

    Mod edit: added code tags
     
    Last edited by a moderator: Apr 17, 2016
  2. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,058
    3,819
    I'm not completely sure if what is happening in your program but
    Look at

    if(LATD = i)

    Don't you think it should be,
    if(LATD == i)
     
  3. Stackington21

    Thread Starter New Member

    Oct 16, 2015
    11
    0
    valid point i will change that up, thank you!

    Probably should have mentioned that i am running this at 20MHz
     
  4. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    "but the delay does not seem to be the required 200msms"

    How do you know that?
     
  5. Stackington21

    Thread Starter New Member

    Oct 16, 2015
    11
    0
    Because the send delay i do gets stuck in a constant loop. If i take one of these subroutines out of my main code, they work fine, just when both are called through the main code that the loop occurs
     
  6. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    What does that mean?

    How do you know if one or more of "these subroutines" isn't working?

    It doesn't seem to me that you have done substantive work to establish that the issues are what you said they are.
     
  7. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Nothing in these routines that could cause a constant loop. Look elsewhere.

    Code (C):
    1. if(LATD == i) // changed to "==" from original
    2. BUSY = 1;
    3.  
    4. BUSY = 0;
    What are you trying to accomplish with these lines?
     
  8. Stackington21

    Thread Starter New Member

    Oct 16, 2015
    11
    0
    apologies, ive been leaving out info i shouldnt

    the BUSY is a define for a different port that is required to show a logic 1 when the 6 bits is on the machine and it is winding.

    Regardless of the actual subroutines, they both work when only 1 of them is called in the main function, once they are both called one after another the second one has a continuous loop for the delay
     
  9. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,058
    3,819
    You do realize that, without curly brackets, only the first line after the IF statement is executed (or ignored). The second line after the "IF", where you have, BUSY = 0; is executed every time.
     
  10. Stackington21

    Thread Starter New Member

    Oct 16, 2015
    11
    0
    Not to be rude, but it is a uni assignment and has no practical applications. For what i have it does everything required for the task at hand.
    My original question is about the delays, not the sub routine itself.
     
  11. NorthGuy

    Active Member

    Jun 28, 2014
    604
    121
    Let's see if I understand this correctly. You have a program with no interrupts and the main loop which somehow call the subroutines 7 and 10. If you remove calls to sub7, then you see 200ms pulses on WindInputPulse. If you remove calls to sub10, you see 400ms pulses on BlankRelease, but with both routines on, you see BlankRelease constantly low and WindInputPulse constantly high. Correct?
     
  12. dannyf

    Well-Known Member

    Sep 13, 2015
    1,811
    362
    What about the delays? You never demonstrated how the code behaved contrary to their documented behaviors.

    In those kind of cases, it is best that YOU produce a minimum piece of code that demonstrates your issues.

    And I can tell you with 100% confidence that the delay routines behaved as they should.
     
Loading...