Write 8051 C Program to Monitor Door Sensor & Sound Buzzer

  • Thread starter Altairs
  • Start date
  • Tags
    Program
In summary, the door sensor will beep when it is high voltage, and will not beep when it is low voltage.
  • #1
Altairs
127
0
This is example 7-14 from "The 8051 Microcontroller and Embedded Systems" by Mazidis and McKinlay.

A door sensor is connected to P1.1 pin, and a buzzer is connected to P1.7. Write an 8051 C program to monitor the door sensor, and when it opens, sound the buzzer.

Code:
#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor = P1^1;
sbit Buzzer = P1^7;

void main(void)
{
Dsensor = 1;

while (Dsensor ==1)
{
buzzer=0;
MSDelay(250);
buzzer=1;
MSDelay(250);
}

}


void MSDelay(...

My question is that we are initializing DSensor to 1 to make it input. But in the condition while (Dsensor == 1) checks that as long as that pin remain input the buzzer should sound. How will this condidtion ever become false? As far as I can see it will always remain true.
 
Technology news on Phys.org
  • #2
I am not familiar with the 8051. But with microcontrollers it is not uncommon for there to be cheap tricks where something "looks like" a variable-- but is not really a variable as you would normally think of it, it is actually a pin or a register or some sort of hardware object. When you access the "value" of Desensor probably it is not reading the value of a variable, probably it is querying the current value of the hardware pin. Likewise when you say "Dsensor = 1" you probably are not in fact setting the variable "Dsensor" to 1, probably you are just changing the voltage behavior of the line. AVR-GCC which I have used has variables which work very similarly like this.

I bet that if you look up sbit it is probably not a proper type but some kind of complicated #define, and your book probably goes into detail on the special behavior of the "sbit"s if you can find the right place.
 
  • #3
See if it's defined in reg51.h.
 
  • #4
I am actually confused that how does the program work. Can't understand the logic.
 
  • #5
Altairs said:
This is example 7-14 from "The 8051 Microcontroller and Embedded Systems" by Mazidis and McKinlay.

A door sensor is connected to P1.1 pin, and a buzzer is connected to P1.7. Write an 8051 C program to monitor the door sensor, and when it opens, sound the buzzer.

Code:
#include <reg51.h>
void MSDelay(unsigned int);
sbit Dsensor = P1^1;
sbit Buzzer = P1^7;

void main(void)
{
Dsensor = 1;

while (Dsensor ==1)
{
buzzer=0;
MSDelay(250);
buzzer=1;
MSDelay(250);
}

}


void MSDelay(...

My question is that we are initializing DSensor to 1 to make it input. But in the condition while (Dsensor == 1) checks that as long as that pin remain input the buzzer should sound. How will this condidtion ever become false? As far as I can see it will always remain true.

how does the "door sensor" , when it opens,P1^1 will receive the HIGH voltage 5V or Low voltage 0V??

It seems that the door sensor is normally HIGH voltage 5V, so ,you "While() Loop" could run continuesly and beep continuesly, But when the door sensor= 0V, then you can break the Loop, and exit the main().
 
  • #6
Ya that's what I thought. Thanks a lot.
 
  • #7
It's to do with the design of the ports on the 8051...

It doesn't have data direction registers as such (great idea that, chaps)...

Instead, it has weak pullups on the ports.

Therefore if you set a port pin to "1", it pulls it up, but external circuitry can overcome the weak pullup and change the state of the pin to "0" which the processor can read.

(What happens when you set the pin to "1" is that there's a strong pullup that is energised for a short time, then switched off, leaving only the weak pullup to maintain the state of the pin as a high. All this stuff is in the 8051 datasheet if you look).
 
  • #8
It seems to me that this program is backwards. The buzzer will beep on and off until the door opens, then it will stop.
 

Related to Write 8051 C Program to Monitor Door Sensor & Sound Buzzer

1. How do I write a program to monitor a door sensor and sound a buzzer using 8051 C?

To write a program to monitor a door sensor and sound a buzzer using 8051 C, you will need to first understand the hardware connections and pin configurations of the 8051 microcontroller and the door sensor. Then, you can use conditional statements and input/output functions in your C program to read the input from the door sensor and trigger the buzzer accordingly.

2. Can I use any type of buzzer with this program?

Yes, you can use any type of buzzer as long as it is compatible with the voltage and current levels of the 8051 microcontroller. However, the coding for activating the buzzer may differ depending on the type of buzzer used.

3. What is the purpose of monitoring a door sensor and sounding a buzzer?

The purpose of this program is to create a security system that can detect when a door is opened or closed. This can be used in homes, offices, or other buildings to alert the occupants of any unauthorized entry.

4. How can I modify this program to include additional sensors?

You can modify this program to include additional sensors by adding more input pins to the 8051 microcontroller and writing the appropriate code to read the input from those sensors. You may also need to adjust the conditional statements to account for the new inputs.

5. Can I use this program for other microcontrollers?

Yes, you can use a similar program for other microcontrollers, but the code will need to be modified to suit the pin configurations and input/output functions of the specific microcontroller you are using.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
Back
Top