Shop OBEX P1 Docs P2 Docs Learn Events
Using a pin as a variable... — Parallax Forums

Using a pin as a variable...

BamseBamse Posts: 561
edited 2006-08-30 13:13 in General Discussion
Howdy...

Is it possible to use a pin as a variable ?
Right now I have eight pins defined as following:
CanRX1 PIN RC.7 input 'CAN Receive to MCP2515 PIN 15 SO 
CanTX1 PIN RC.6 output 'CAN Transmit to MCP2515 PIN 14 SI 
PINcs1 PIN RC.5 output 'CAN Chip Select to MCP2515 PIN 16 CS 
PINclk1 PIN RC.4 output 'CAN Clock to MCP2515 PIN 13 SCK 
CanRX2 PIN RB.7 input 'CAN Receive to MCP2515 PIN 15 SO 
CanTX2 PIN RB.6 output 'CAN Transmit to MCP2515 PIN 14 SI 
PINcs2 PIN RB.5 output 'CAN Chip Select to MCP2515 PIN 16 CS 
PINclk2 PIN RB.4 output 'CAN Clock to MCP2515 PIN 13 SCK

These pins represent chip·1 and chip 2 of the same type chip.

And then two subroutines using these pins.
resetCan1: 
LOW PINcs1 
SHIFTOUT CANTX1, PINclk1, MSBFIRST, $C0 'Send a RESET command 
HIGH PINcs1 
return 
resetCan2: 
LOW PINcs2 
SHIFTOUT CANTX2, PINclk2, MSBFIRST, $C0 'Send a RESET command 
HIGH PINcs2 
return

Now every time I add a subroutine, I have to add two of them, one for chip 1 and one for chip 2.

What I want to do is to to use one routine and pass a variable 1 or 2 depending on which chip I want to access.
For two chips it might be easier to just do an if statement and then select either SHIFTOUT command. However I probably want to add more chips and I wonder if it is possible to do something like this.

CanRX1 PIN RC.7 input 'CAN Receive to MCP2515 PIN 15 SO 
CanTX1 PIN RC.6 output 'CAN Transmit to MCP2515 PIN 14 SI 
PINcs1 PIN RC.5 output 'CAN Chip Select to MCP2515 PIN 16 CS 
PINclk1 PIN RC.4 output 'CAN Clock to MCP2515 PIN 13 SCK 
CanRX2 PIN RC.3 input 'CAN Receive to MCP2515 PIN 15 SO 
CanTX2 PIN RC.2 output 'CAN Transmit to MCP2515 PIN 14 SI 
PINcs2 PIN RC.1 output 'CAN Chip Select to MCP2515 PIN 16 CS 
PINclk2 PIN RC.0 output 'CAN Clock to MCP2515 PIN 13 SCK 
CanRX3 PIN RB.7 input 'CAN Receive to MCP2515 PIN 15 SO 
CanTX3 PIN RB.6 output 'CAN Transmit to MCP2515 PIN 14 SI 
PINcs3 PIN RB.5 output 'CAN Chip Select to MCP2515 PIN 16 CS 
PINclk3 PIN RB.4 output 'CAN Clock to MCP2515 PIN 13 SCK 
CanRX4 PIN RB.3 input 'CAN Receive to MCP2515 PIN 15 SO 
CanTX4 PIN RB.2 output 'CAN Transmit to MCP2515 PIN 14 SI 
PINcs4 PIN RB.1 output 'CAN Chip Select to MCP2515 PIN 16 CS 
PINclk4 PIN RB.0 output 'CAN Clock to MCP2515 PIN 13 SCK
 
CanRX         PIN RB.3 input    'CAN Receive      to MCP2515 PIN 15  SO
CanTX         PIN RB.2 output   'CAN Transmit     to MCP2515 PIN 14  SI
PINcs         PIN RB.1 output   'CAN Chip Select  to MCP2515 PIN 16  CS
PINclk        PIN RB.0 output   'CAN Clock        to MCP2515 PIN 13  SCK

.
.
.
resetCan:
 chip = __PARAM1
 if chip = 1 then
  PINcs = PINcs1
  CANTX = CANTX1
  PINclk = PINclk1
 elseif chip = 2 then
  PINcs = PINcs2
  CANTX = CANTX2
  PINclk = PINclk2
 elseif chip = 3 then
  PINcs = PINcs3
  CANTX = CANTX3
  PINclk = PINclk3
 elseif chip = 4 then
  PINcs = PINcs4
  CANTX = CANTX4
  PINclk = PINclk4
 endif
  LOW PINcs
  SHIFTOUT CANTX, PINclk, MSBFIRST, $C0   'Send a RESET command
  HIGH PINcs
 return

I tried this·and the defined pins will not chage.

Is this possible ???
Any other suggestions ???

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Living on the planet Earth might be expensive but it includes a free trip around the sun every year...

Experience level:
[noparse][[/noparse] ] Let's connect the motor to pin 1, it's a 6V motor so it should be fine.
[noparse][[/noparse] ] OK, I got my resistors hooked up with the LEDs.
[noparse][[/noparse]X] I got the Motor hooked up with the H-bridge and the 555 is supplying the PWM.
[noparse][[/noparse] ] Now, if I can only program the BOE-BOT to interface with he Flux Capacitor.
[noparse][[/noparse] ] I dream in SX28 assembler...

/Bamse

Comments

  • BeanBean Posts: 8,129
    edited 2006-08-30 03:40
    Bamse,
    Pins are constants. They cannot be change at runtime. You'll need routines for each set of pins.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.
    ·
  • BamseBamse Posts: 561
    edited 2006-08-30 12:17
    Thanks Bean,
    That is pretty much what I figured...

    I just need a way of figuring out how to do something similar otherwise all my subroutines will eat up all the programming space... wink.gif
    I have 5 subroutines per chip, making it a total of 10 subroutines today, if I added two more chips, that would br 20 subroutines...

    Well, using two SX chips would probably work as well, one SX chip per two MCP2515...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Living on the planet Earth might be expensive but it includes a free trip around the sun every year...

    Experience level:
    [noparse][[/noparse] ] Let's connect the motor to pin 1, it's a 6V motor so it should be fine.
    [noparse][[/noparse] ] OK, I got my resistors hooked up with the LEDs.
    [noparse][[/noparse]X] I got the Motor hooked up with the H-bridge and the 555 is supplying the PWM.
    [noparse][[/noparse] ] Now, if I can only program the BOE-BOT to interface with he Flux Capacitor.
    [noparse][[/noparse] ] I dream in SX28 assembler...

    /Bamse
  • BeanBean Posts: 8,129
    edited 2006-08-30 13:13
    Bamse,
    · If you're only using the SHIFTIN and SHIFTOUT commands, it wouldn't be too hard to make specialized versions that WOULD allow different pins to be used. If you don't need high speed, you could probably write them in SX/B too.

    · I would use a method of numbering the pins from RA.0 to RC.7 with number from 0 to 23 (I know that RA.3-RA.7 don't exist, but it makes the code easier to pretend they do). Then do something like:
    'UNTESTED CODE
     
    pinNumber VAR Byte
    pinBit    VAR Byte
    pinAddr   VAR Byte
     
    PinHigh  SUB 1
    PinLow   SUB 1
    
     
    PinHigh:
      pinNumber = __PARAM1                      ' Get passed pin number (0 to 23)
      pinBit = pinNumber AND 7                  ' Get pin bit position
      pinBit = 1 << pinBit                      ' Make pin mask
      pinAddr = pinNumber >> 3                  ' Get pin port address
      pinAddr = pinAddr + @RA                   ' Start at port RA (notice @)
      __RAM(pinAddr) = __RAM(pinAddr) OR pinBit ' Set the port bit high
      RETURN
     
    PinLow:
      pinNumber = __PARAM1                       ' Get passed pin number (0 to 23)
      pinBit = pinNumber AND 7                   ' Get pin bit position
      pinBit = 1 << pinBit                       ' Make pin mask
      pinAddr = pinNumber >> 3                   ' Get pin port address
      pinAddr = pinAddr + @RA                    ' Start at port RA (notice @)
      pinBit = ~pinBit                           ' Invert bit mask for ANDing
      __RAM(pinAddr) = __RAM(pinAddr) AND pinBit ' Set the port bit high
      RETURN
    

    · I would just make routines to set a pin number high and low. Then just call them as needed in your higher level subroutines (like shiftout).

    · Let me know if you need for info, but I think that will work for ya.

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap used 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com
    SX-Video Display Modules www.sxvm.com

    There are only two guaranteed ways to become weathy.
    Spend less than you make.
    Make more than you spend.


    Post Edited (Bean (Hitt Consulting)) : 8/30/2006 1:21:42 PM GMT
Sign In or Register to comment.