Write 8051 C Program to Monitor Door Sensor & Sound Buzzer

  • Thread starter Thread starter Altairs
  • Start date Start date
  • Tags Tags
    Program
Click For Summary

Discussion Overview

The discussion revolves around writing an 8051 C program to monitor a door sensor and activate a buzzer when the door opens. Participants explore the logic of the program, the behavior of the hardware, and the implications of the code structure.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant questions the logic of initializing the door sensor (Dsensor) to 1, suggesting that the while loop condition will always be true, leading to continuous buzzer activation.
  • Another participant proposes that the behavior of Dsensor may not be as straightforward as a typical variable, indicating that it might represent a hardware pin whose state can change based on external conditions.
  • A suggestion is made to check the definition of "sbit" in the reg51.h file to clarify its behavior.
  • One participant expresses confusion about how the program operates and the logic behind it.
  • A later reply explains that the 8051 microcontroller uses weak pull-ups on its ports, allowing external circuitry to change the state of the pin, which could explain how the condition can become false.
  • Another participant argues that the program appears to be structured incorrectly, suggesting that the buzzer would beep continuously until the door opens, which contradicts the intended functionality.

Areas of Agreement / Disagreement

Participants express differing views on the logic of the program and the behavior of the hardware. There is no consensus on the correctness of the program's logic or its intended operation.

Contextual Notes

Participants note that the 8051 lacks dedicated data direction registers, which may affect how the program interacts with the hardware. The discussion highlights the complexity of interpreting hardware behavior through software.

Who May Find This Useful

This discussion may be useful for individuals interested in embedded systems programming, particularly those working with the 8051 microcontroller and similar hardware configurations.

Altairs
Messages
125
Reaction score
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
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.
 
See if it's defined in reg51.h.
 
I am actually confused that how does the program work. Can't understand the logic.
 
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().
 
Ya that's what I thought. Thanks a lot.
 
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).
 
It seems to me that this program is backwards. The buzzer will beep on and off until the door opens, then it will stop.
 

Similar threads

  • · Replies 7 ·
Replies
7
Views
4K