1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Variable Delay (controlled with push switches)

Discussion in '8051/8951' started by malsch, Oct 18, 2011.

  1. malsch

    malsch New Member

    Joined:
    Apr 20, 2011
    Messages:
    24
    Likes:
    0
    Hi, I am using the 8051 to create a delay function with a variable (in this case BPMtime). At first the BPMtime variable is set to 100 and when i push and release the switch (on P0.0), the BPMtime becomes 101 hence the delay is increased. The same goes for the switch on P0.1 but instead of incrementing by one, i would like it to be decremented by one.

    To know if it is working correctly, I outputted BPMtime to port 3 to see if the value is changed whenever i press one of the buttons. So far for some reason the program isn't working correctly. When one of the switches is pushed, the BPMtime stays 100. P0 doesn't have internal pull ups. The program is compiling successfully.

    The program so far:

    #include <reg51.h>

    void BPMDelay();
    unsigned char BPMtime;

    void main(void)
    {
    P0=0xFF;
    BPMtime=100;

    while(1)
    {
    P2 = 0x00;
    BPMDelay();
    P2 = 0xFF;
    BPMDelay();
    }
    }

    void BPMDelay()
    {
    if (P0^0==0)
    {
    BPMtime=BPMtime ++;
    P3=BPMtime;
    }

    if(P0^1==0)
    {
    BPMtime=BPMtime --;
    P3=BPMtime;
    }

    else
    {
    unsigned int u,v;
    for(u=0;u<BPMtime;u++);
    for(v=0;v<190;v++);
    }
    }


    Thanks for your time.
     

    Attached Files:

    Last edited: Oct 21, 2011
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,165
    Likes:
    910
    Location:
    Rochdale UK
    I wouldn't use XOR in this way.. P0^0 wont do anything write P0 & 1 and P0 & 2 for the key enquiry.

    ie.. if(P0 & 1) blah blah ( you dont even need the equality part)
     
    Last edited: Oct 19, 2011
  3. malsch

    malsch New Member

    Joined:
    Apr 20, 2011
    Messages:
    24
    Likes:
    0
    P0^0 and P0^1 refer to pins P0.0 and P0.1 respectively. i didnt exactly understood your reply. thanks
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,165
    Likes:
    910
    Location:
    Rochdale UK

    P0^0 means P0 xor 0 .. Is this what you want to acheive? I use and.. P0 & 1
     
  6. malsch

    malsch New Member

    Joined:
    Apr 20, 2011
    Messages:
    24
    Likes:
    0
    im using keil as a compiler. in keil, P0^0 means P0.0
     
  7. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,165
    Likes:
    910
    Location:
    Rochdale UK
    Ok! Then P0 = 0xff, not P0 = 0 you need P0 to be an input port!
     
  8. ItsMike

    ItsMike New Member

    Joined:
    Mar 3, 2011
    Messages:
    108
    Likes:
    3
    The compiler is probably optimizing the variables.
    Try declaring them as volatile.
    Like so:

    volatile unsigned int u,v;
     
  9. malsch

    malsch New Member

    Joined:
    Apr 20, 2011
    Messages:
    24
    Likes:
    0
    yes that was a mistake. i edit the program and put it in my first post. but still no luck with it working. Port 3 is still isplaying "100" (in binary of course)

    ItsMike, I have declared them as volatile also but the program worked the same as it was...

    10x
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,165
    Likes:
    910
    Location:
    Rochdale UK
    I've looked at the compiler P0^0 can also be written P0_0! does this make a difference.

    Also try

    if(P0 & 1)

    and

    if(P0 & 2)
     
    Last edited: Oct 21, 2011

Share This Page