I have written code but It doesn't work as it suppose to do. so when I run below code LED is turning ON for forever. I am not sure 20 counts will give 20ms delay timeNow try to write simple code for this flowchart using while() loops and delay() functions.
#include<reg51.h>
sbit Button = P3^7;
sbit LED = P2^0;
#define Button_Pressed 1
#define FOREVER 1
void Wait (unsigned long int DelayTime) /* Function to generate delay time */
{
unsigned long int Count = 0;
for (Count = 0; Count < DelayTime; Count++)
{
/* Do nothing */
}
}
void main() /* Program start from this line */
{
P2 = 0x00; /* P2.0 as output Pin */
P3 = 0x80; /* P3.7 as input Pin */
while (FOREVER) // Run for forever
{
if (Button == Button_Pressed ) /* Check if Push Button Pressed */
{
LED =~ LED; /* Toggle LED */
Wait(20); /* Wait for 20 ms but I am not sure that will give 20 ms */
if (!Button == Button_Pressed ) /* Check if Push Button still Pressed */
{
Wait(20); /* Wait for 20 ms but I am not sure this will give 20 ms */
}
}
}
}
No timers? No simulation?. You have regressed all the way back to post #3.I am not sure 20 counts will give 20ms delay time
@trebla1. Is input for button tied to pull-up or pull-down resistor? From this depends is button input in pushed state 0 or 1 level.
Multi-line comments are dangerous if you are deleting or copy-paste code fragments.
@JohnInTX why timers, I think hardware timer to set debounce time, I am doing simulation but its difficult to simulate behavior of push button. OK I will use hardware timer to get 20msNo timers? No simulation?. You have regressed all the way back to post #3.
I am using timer o mode 1 to get 20 ms delay but I am having trouble in simulationNo timers? No simulation?. You have regressed all the way back to post #3.
#include<reg51.h>
sbit Button = P3^7;
sbit LED = P2^0;
#define Button_Pressed 0
#define FOREVER 1
//20 ms delay, 65536-18433 = 47103
void Delay ()
{
TMOD = 0x01; // Set timer0 in mode 1
TH0 = 0xB7;
TL0 = 0xFF;
TR0 = 1; // Start timer0
TF0 = 1; // Enable Timer0 overflow flag TF0
if(TF0 == 1)
{
TF0 = 0;
}
}
void main(void)
{
P2 = 0x00; // P2.0 as output Pin
P3 = 0x80; //P3.7 as input Pin
while (FOREVER) // Run for forever
{
if (Button == Button_Pressed ) // Check if Push Button Pressed
{
LED =~ LED; // Toggle LED
Delay(); // Wait for 20 ms
if (Button != Button_Pressed ) // Check if Push Button still Pressed
{
Delay(); // Wait for 20 ms
}
}
}
}
my mistake, after correction code doesn't work as it suppose to do. so when I run below code LED is turning ON for foreverLine 18
#include<reg51.h>
sbit Button = P3^7;
sbit LED = P2^0;
#define Button_Pressed 0
#define FOREVER 1
//20 ms delay, 65536-18433 = 47103
void Delay ()
{
TMOD = 0x01; // Set timer0 in mode 1
TH0 = 0xB7;
TL0 = 0xFF;
TR0 = 1; // Start timer0
TF0 = 0; // clear Timer0 overflow flag TF0
if(TF0 == 1)
{
TF0 = 0;
}
}
int main(void)
{
P2 = 0x00; // P2.0 as output Pin
P3 = 0x80; //P3.7 as input Pin
while (FOREVER) // Run for forever
{
if (Button == Button_Pressed ) // Check if Push Button Pressed
{
LED =~ LED; // Toggle LED
Delay(); // Wait for 20 ms
if (Button != Button_Pressed ) // Check if Push Button still Pressed
{
Delay(); // Wait for 20 ms
}
}
}
}
How did you debug code? I am trying to simulate code in last postYou should step through 'Delay' with the debugger. It is about 10 microseconds long.
It still can not get to line 42 under most circumstances.
All of that took less than 5 minutes to figure out using the debug tool.
