Write 8051 C Program to Monitor Door Sensor & Sound Buzzer

  • Thread starter Thread starter Altairs
  • Start date Start date
  • Tags Tags
    Program
AI Thread Summary
The discussion revolves around an 8051 C program designed to monitor a door sensor connected to pin P1.1 and activate a buzzer on pin P1.7. The program initializes the door sensor as high (1), leading to confusion about how the while loop condition can ever become false. Participants clarify that the door sensor is normally high (5V), and when the door opens, it can drop to low (0V), breaking the loop. The 8051's design includes weak pull-ups on its ports, allowing external circuitry to change the pin state, which explains the program's behavior. Ultimately, the program is deemed to function in reverse, as the buzzer sounds continuously until the door opens.
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
Views
4K
Back
Top