-3

I am supposed to make a mux using a Dallas 8051 chip. p1.0, p1.1, and p1.2 are inputs. P1.3 is the enable line, and output are P2.0 to p2.3. All outputs work fine while debugging apart from when all inputs are zero and enable is one. (only zero should be on in the output in the Keil debugger view, but that is not the case).

#include<reg51.h>

sbit input0 = p1^0;
sbit input1 = p1^1;
sbit input2 = p1^2;
sbit enable = p1^3;
sbit output0 = p2^0;
sbit output1= p2^1;
sbit output2 = p2^2;
sbit output3 = p2^3;
sbit output4 = p2^4;
sbit output4 = p2^5;
sbit output6 = p2^6;
sbit output7 = p2^7;

void main (void) {
    P2 = 0x00;

    if (enable==1)
    {
        P1&=0x07;
        switch(P1)

        {
            case 1: output1=1; break;
            case 2: output2=1; break;
            case 3: output3=1; break;
            case 4: output4=1; break;
            case 5: output5=1; break;
            case 6: output6=1; break;
            case 7: output7=1; break;
            default: output0 =1; break;
        }

    }

    else {P2&=0x07;}

}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
rddead
  • 103
  • 10

1 Answers1

2


you ask why the dont work when all inputs are zero and enable is one. I am sorry, I cannot see a reason for that behavior in your code but I have some questions and suggestions.

Where do you stop your debugger to read the output? Are you sure that the statement to set output0 already was executed before?

What is the reason for the P2&=0x07; in the else part?

What do you want to do with the P1&=0x07; before the switch?
If you want to mask the bits 4..7 it would be better to do that in the switch statement directly. What you do now depends on the signal in the inputs on bits 0..3 of P1. If one of them is 0, a 0 will be written on that bit of P1.
The ports of the 8051 can be used bidirectional. If you write a 1, the pin is only driven to 1 by a weak resistor. But if you write a 0 it will be driven to 0 by an output transistor which can drive several mA. The 0 will force the pin of the 8051 to be an output.

Therefore I suggest to replace

P1&=0x07;
switch(P1)

with

switch(P1 & 0x07)

Best regards and good luck.

Helmut

Helmut
  • 21
  • 1
  • This actually fixed the error, thank you. What is the reason for the P2&=0x07; in the else part? This was actually a typo. P2 should have been set to 0x00. – rddead May 18 '16 at 13:20