Continue to Site

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.

  • 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.

Variable Delay (controlled with push switches)

Status
Not open for further replies.

malsch

New Member
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.
 

Attachments

  • untitled24.JPG
    untitled24.JPG
    62.4 KB · Views: 345
Last edited:
P0^0 and P0^1 refer to pins P0.0 and P0.1 respectively. i didnt exactly understood your reply. thanks
 
unsigned int u,v;
for(u=0;u<BPMtime;u++);
for(v=0;v<190;v++);

The compiler is probably optimizing the variables.
Try declaring them as volatile.
Like so:

volatile unsigned int u,v;
 
Ok! Then P0 = 0xff, not P0 = 0 you need P0 to be an input port!

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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top