This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

how to dynamically assign port pins

I have several identical devices which I want to connect to differing port pins. I want to pass to my function, which device, and then have the function perform a switch statement dynamically assigning clock, oe, strobe, data lines to the port pins for that device. For example,
function(int ic)
sbit CLOCK;
sbit DATA_OUT;
sbit OE;
sbit STROBE;

switch(chip){
case 0:
CLOCK = P1^0;
DATA_OUT = P1^2;
OE = P1^7;
STROBE = P3^4;
break;

case 1:
sbit CLOCK = P1^0;
sbit DATA_OUT = P1^2;
sbit OE = P1^7;
sbit STROBE = P3^4; break;
}

but the compiler pukes on this--giving syntax error near each 'sbit' declaration and use. However, if I declare the sbit's fixed at the top of the file, the compiler is happy-- but then I would have to duplicate the function with multiple copies referencing the port pins in question for that device.

Any ideas on how to make this work?

THanks!

Parents
  • I have several identical devices which I want to connect to differing port pins. I want to pass to my function, which device, and then have the function perform a switch statement dynamically assigning clock, oe, strobe, data lines to the port pins for that device.

    What you are talking about is indirect addressing. Unfortunately, the 8051 does not support indirect addressing for SFRs. So, you'll probably have to have duplicated code.

    This is not such a big problem. If you're only accessing a few SFRs, the code generated is pretty small. If you have lots of other code in your algorithm the compiler can combine it for you if you use optimizer level 9 or higher.

    Jon

Reply
  • I have several identical devices which I want to connect to differing port pins. I want to pass to my function, which device, and then have the function perform a switch statement dynamically assigning clock, oe, strobe, data lines to the port pins for that device.

    What you are talking about is indirect addressing. Unfortunately, the 8051 does not support indirect addressing for SFRs. So, you'll probably have to have duplicated code.

    This is not such a big problem. If you're only accessing a few SFRs, the code generated is pretty small. If you have lots of other code in your algorithm the compiler can combine it for you if you use optimizer level 9 or higher.

    Jon

Children
  • I had a similar problem when I wanted to implement 4 dallas 1wire ports. I used a case statement to select each of the ports. This is a little wasteful, but the code is simple, obvious and it works.

    Can you not have your relay drivers share a common clock and latch, with a differing port pin for the data? That way you can update a number of the relay drivers in the one loop, thus saving some time. Another way is to structure your program so that the delay of sending the data to a string of your relay drivers is not a problem - in an interrupt routine maybe?