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
  • Yes-- you're right. Normally these 5832 relay drivers are chained as you describe. However, I'm trying to make them non-chained in order to speed code execution and limit host overhead in software.



    Since posting this, I have managed to make it compile, but am totally unsure about if the resultant code will work. Here's a snippet of the list file it generated. Do you think it'll work?



    21 sbit CLOCK1 = P1^0;

    22 sbit DATA_OUT1 = P1^2;

    23 sbit OE1 = P1^7;

    24 sbit STROBE1 = P3^4;

    25

    26 sbit CLOCK2 = P2^0;

    27 sbit DATA_OUT2 = P2^2;

    28 sbit OE2 = P2^7;

    29 sbit STROBE2 = P2^4;

    ...

    void clk_relay_data(Byte chip)

    68 {

    69 1 int ic;

    70 1 int dbit;

    71 1 unsigned long x;

    72 1

    73 1 bit clk;

    74 1 bit dout;

    75 1 bit oe;

    76 1 bit strobe;

    77 1

    78 1 switch(chip){

    79 2 case 0:

    80 2 clk = CLOCK1;
    81 2 dout= DATA_OUT1; oe = OE1;
    83 2 strobe= STROBE1 84 2 break;

    85 2

    86 2 case 1:

    87 2 clk = CLOCK2;
    88 2
    89 2 oe = OE2;
    90 2 strobe= STROBE2;
    91 2 break;

    92 2 }

    93 1

    94 1

    95 1 oe = 0;
    96 1 strobe = 0;//STROBE = 0;

    97 1 for(ic = NUM_5832; ic >=0; ic--){

    98 2 for(dbit = 31; dbit >=0; dbit--){

    99 3 clk = 0;//CLOCK = 0;

    100 3 x = relay_drv5832[ic];

    101 3 if(x & (0x00000001<< dbit))

    102 3 dout = 1;//DATA_OUT = 1;

    103 3 else

    104 3 dout = 0; //DATA_OUT = 0;

    105 3 delay(1);

    106 3 clk = 1;
    107 3 clk = 0;
    108 3 } 109 2 } 110 1
    111 1 strobe = 1;
    112 1 strobe = 0;
    113 1 oe = 1;
    114 1 }

    <snip>

    ; FUNCTION _clk_relay_data (BEGIN)

    ; SOURCE LINE # 67

    ;---- Variable 'chip' assigned to Register 'R7' ----

    ; SOURCE LINE # 68

    ; SOURCE LINE # 78

    0000 EF MOV A,R7

    0001 14 DEC A

    0002 6015 JZ ?C0005

    0004 04 INC A

    0005 7022 JNZ ?C0003

    ; SOURCE LINE # 79

    0007 ?C0004:

    ; SOURCE LINE # 80

    0007 A290 MOV C,CLOCK1

    0009 9200 R MOV clk,C

    ; SOURCE LINE # 81

    000B A292 MOV C,DATA_OUT1

    000D 9200 R MOV dout,C

    ; SOURCE LINE # 82

    000F A297 MOV C,OE1

    0011 9200 R MOV oe,C

    ; SOURCE LINE # 83

    0013 A2B4 MOV C,STROBE1

    0015 9200 R MOV strobe,C

    ; SOURCE LINE # 84

    0017 8010 SJMP ?C0003

    ; SOURCE LINE # 86

    0019 ?C0005:

    ; SOURCE LINE # 87

    0019 A2A0 MOV C,CLOCK2

    001B 9200 R MOV clk,C

    ; SOURCE LINE # 88

    001D A2A2 MOV C,DATA_OUT2

    001F 9200 R MOV dout,C

    ; SOURCE LINE # 89

    0021 A2A7 MOV C,OE2

    0023 9200 R MOV oe,C

    ; SOURCE LINE # 90

    0025 A2A4 MOV C,STROBE2

    0027 9200 R MOV strobe,C

    ; SOURCE LINE # 91

    ; SOURCE LINE # 92

    0029 ?C0003:

    ; SOURCE LINE # 95

    0029 C200 R CLR oe

    ; SOURCE LINE # 96

    002B C200 R CLR strobe

    ; SOURCE LINE # 97

    <snip> ; SOURCE LINE # 98

    <snip> ; SOURCE LINE # 99

    003F C200 R CLR clk

    ; SOURCE LINE # 100

    <snip> ; SOURCE LINE # 102

    00A4 D200 R SETB dout

    00A6 8002 SJMP ?C0013

    00A8 ?C0012:

    ; SOURCE LINE # 104

    00A8 C200 R CLR dout

    00AA ?C0013:

    ; SOURCE LINE # 105

    00AA 7F01 MOV R7,#01H

    00AC 7E00 MOV R6,#00H

    00AE 120000 E LCALL _delay

    ; SOURCE LINE # 106

    00B1 D200 R SETB clk

    ; SOURCE LINE # 107

    00B3 C200 R CLR clk

    ; SOURCE LINE # 108

    <snip>
    SOURCE LINE # 109

    <snip>00CD ?C0008:

    SOURCE LINE # 111

    00E5 D200 R SETB strobe

    ; SOURCE LINE # 112

    00E7 C200 R CLR strobe

    ; SOURCE LINE # 113

    00E9 D200 R SETB oe

    ; SOURCE LINE # 114

    00EB 22 RET

    ; FUNCTION _clk_relay_data (END)

Reply
  • Yes-- you're right. Normally these 5832 relay drivers are chained as you describe. However, I'm trying to make them non-chained in order to speed code execution and limit host overhead in software.



    Since posting this, I have managed to make it compile, but am totally unsure about if the resultant code will work. Here's a snippet of the list file it generated. Do you think it'll work?



    21 sbit CLOCK1 = P1^0;

    22 sbit DATA_OUT1 = P1^2;

    23 sbit OE1 = P1^7;

    24 sbit STROBE1 = P3^4;

    25

    26 sbit CLOCK2 = P2^0;

    27 sbit DATA_OUT2 = P2^2;

    28 sbit OE2 = P2^7;

    29 sbit STROBE2 = P2^4;

    ...

    void clk_relay_data(Byte chip)

    68 {

    69 1 int ic;

    70 1 int dbit;

    71 1 unsigned long x;

    72 1

    73 1 bit clk;

    74 1 bit dout;

    75 1 bit oe;

    76 1 bit strobe;

    77 1

    78 1 switch(chip){

    79 2 case 0:

    80 2 clk = CLOCK1;
    81 2 dout= DATA_OUT1; oe = OE1;
    83 2 strobe= STROBE1 84 2 break;

    85 2

    86 2 case 1:

    87 2 clk = CLOCK2;
    88 2
    89 2 oe = OE2;
    90 2 strobe= STROBE2;
    91 2 break;

    92 2 }

    93 1

    94 1

    95 1 oe = 0;
    96 1 strobe = 0;//STROBE = 0;

    97 1 for(ic = NUM_5832; ic >=0; ic--){

    98 2 for(dbit = 31; dbit >=0; dbit--){

    99 3 clk = 0;//CLOCK = 0;

    100 3 x = relay_drv5832[ic];

    101 3 if(x & (0x00000001<< dbit))

    102 3 dout = 1;//DATA_OUT = 1;

    103 3 else

    104 3 dout = 0; //DATA_OUT = 0;

    105 3 delay(1);

    106 3 clk = 1;
    107 3 clk = 0;
    108 3 } 109 2 } 110 1
    111 1 strobe = 1;
    112 1 strobe = 0;
    113 1 oe = 1;
    114 1 }

    <snip>

    ; FUNCTION _clk_relay_data (BEGIN)

    ; SOURCE LINE # 67

    ;---- Variable 'chip' assigned to Register 'R7' ----

    ; SOURCE LINE # 68

    ; SOURCE LINE # 78

    0000 EF MOV A,R7

    0001 14 DEC A

    0002 6015 JZ ?C0005

    0004 04 INC A

    0005 7022 JNZ ?C0003

    ; SOURCE LINE # 79

    0007 ?C0004:

    ; SOURCE LINE # 80

    0007 A290 MOV C,CLOCK1

    0009 9200 R MOV clk,C

    ; SOURCE LINE # 81

    000B A292 MOV C,DATA_OUT1

    000D 9200 R MOV dout,C

    ; SOURCE LINE # 82

    000F A297 MOV C,OE1

    0011 9200 R MOV oe,C

    ; SOURCE LINE # 83

    0013 A2B4 MOV C,STROBE1

    0015 9200 R MOV strobe,C

    ; SOURCE LINE # 84

    0017 8010 SJMP ?C0003

    ; SOURCE LINE # 86

    0019 ?C0005:

    ; SOURCE LINE # 87

    0019 A2A0 MOV C,CLOCK2

    001B 9200 R MOV clk,C

    ; SOURCE LINE # 88

    001D A2A2 MOV C,DATA_OUT2

    001F 9200 R MOV dout,C

    ; SOURCE LINE # 89

    0021 A2A7 MOV C,OE2

    0023 9200 R MOV oe,C

    ; SOURCE LINE # 90

    0025 A2A4 MOV C,STROBE2

    0027 9200 R MOV strobe,C

    ; SOURCE LINE # 91

    ; SOURCE LINE # 92

    0029 ?C0003:

    ; SOURCE LINE # 95

    0029 C200 R CLR oe

    ; SOURCE LINE # 96

    002B C200 R CLR strobe

    ; SOURCE LINE # 97

    <snip> ; SOURCE LINE # 98

    <snip> ; SOURCE LINE # 99

    003F C200 R CLR clk

    ; SOURCE LINE # 100

    <snip> ; SOURCE LINE # 102

    00A4 D200 R SETB dout

    00A6 8002 SJMP ?C0013

    00A8 ?C0012:

    ; SOURCE LINE # 104

    00A8 C200 R CLR dout

    00AA ?C0013:

    ; SOURCE LINE # 105

    00AA 7F01 MOV R7,#01H

    00AC 7E00 MOV R6,#00H

    00AE 120000 E LCALL _delay

    ; SOURCE LINE # 106

    00B1 D200 R SETB clk

    ; SOURCE LINE # 107

    00B3 C200 R CLR clk

    ; SOURCE LINE # 108

    <snip>
    SOURCE LINE # 109

    <snip>00CD ?C0008:

    SOURCE LINE # 111

    00E5 D200 R SETB strobe

    ; SOURCE LINE # 112

    00E7 C200 R CLR strobe

    ; SOURCE LINE # 113

    00E9 D200 R SETB oe

    ; SOURCE LINE # 114

    00EB 22 RET

    ; FUNCTION _clk_relay_data (END)

Children
  • "I have managed to make it compile, but am totally unsure about if the resultant code will work"

    You still haven't read those instructions on posting code, have you?
    It's right there directly above the white 'Message:' box where you type your posting!

    Your code was even less legible than the first time, but I couldn't see anywhere that you actually write to the output pins?

  • Andrew,

    Sorry about the illegible post.. I didn't see the instructions until you pointed them out. My apology..

    It looks as if what I am trying to do can simply not be done in the 8051 architecture. I guess I'll re-arange the HW configuration or have a LOT of duplicate functions to handle the pins to use.

    Regards,

    Paul