Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A 8051 program in C

  1. Jul 4, 2008 #1
    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 (Text):
    #include <reg51.h>
    void MSDelay(unsigned int);
    sbit Dsensor = P1^1;
    sbit Buzzer = P1^7;

    void main(void)
    Dsensor = 1;

    while (Dsensor ==1)


    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.
  2. jcsd
  3. Jul 4, 2008 #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.
  4. Jul 4, 2008 #3


    User Avatar
    Homework Helper

    See if it's defined in reg51.h.
  5. Jul 5, 2008 #4
    I am actually confused that how does the program work. Can't understand the logic.
  6. Jul 5, 2008 #5


    User Avatar

    how does the "door sensor" , when it opens,P1^1 will receive the HIGH volatage 5V or Low volatage 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().
  7. Jul 6, 2008 #6
    Ya that's what I thought. Thanks a lot.
  8. Jul 7, 2008 #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).
  9. Jul 7, 2008 #8


    Staff: Mentor

    It seems to me that this program is backwards. The buzzer will beep on and off until the door opens, then it will stop.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook