Shop OBEX P1 Docs P2 Docs Learn Events
8 bit data through a 4-bit port. — Parallax Forums

8 bit data through a 4-bit port.

kutalinelucaskutalinelucas Posts: 80
edited 2011-08-02 08:17 in BASIC Stamp
Hey guys, I was wondering if my logic is sound for sending a byte through OUTD on a basic stamp 2.

If I try to send an 8-bit value to 4 output data lines from the stamp, will the lower nibble or the upper nibble be sent?

Basically I have a 2-way 4 bit bus between a bs2 and a PIC18F. Reading data into the stamp is fine,
as is the handshaking between processors. I know 2 nibbles are sent from the bs2 using LEDs, but I'm not sure if this method first sends the upper nib of a byte, then the lower nibble...

So far, I:

1. Mask the lower nibble of the byte (eg 1011 0010 becomes 1011 0000
2. Shift the upper nibble 4 places to the right
3. Present on port

4. Swap nibbles of original byte (eg 1011 0010 becomes 0010 1011)
5. Mask lower nibbles (0010 0000)
6. Shift 4 places right
7. Present on port.

Here's what i'm thinking...
Temp_idx = (stalled_value & $0f0)                        'mask bottom 4 bits
  Stalled_upper_nibble = (Temp_idx >> 4)                 'move down for 4 bit bus
    OUTD = Stalled_upper_nibble                          'present upper nibble on data_line
OUT0 = 1
  PAUSE(100)                                             '[^^3^^ -> STAMP ENABLE HIGH FOR PIC TO READ PORTS]
    OUT0 = 0
'.................^^upper nib on port, then enable^^......................'
 
Temp_idx = (Stalled_value << 4) | (Stalled_value >> 4)   'swap nibbles
  Temp_lower_nibble = Temp_idx & $0f0                    '& to get rid of bottom bits
    Stalled_lower_nibble = Temp_lower_nibble >> 4        'shift left for 4 bit data transfer
      OUTD = Stalled_lower_nibble                        'send lower nibble

The problem is I can't really debug because it would take allot of re-wiring, and I can't really slow the sequence down because of the PIC timings.

If somebody could let me know if it should logically work than that would be brilliant.

if I can clarify in any way please let me know

Cheers

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-08-02 08:14
    As far as you're concerned, OUTD is just a 4-bit variable that happens to be part of a larger variable (OUTS) that happens to be used to set I/O pins 12-15 when those pins are set to output mode. When you assign a value to OUTD, the least significant 4-bits are used. Remember that all computations in PBasic are done on 16-bit values and anytime a value is assigned to a variable, the appropriate number of least significant bits are kept and the rest discarded.

    You've got the right idea. You don't need to do any masking because the extra bits are going to be discarded. The process is simpler than you think:
    OUTD = byteValue   ' output lower nib
    OUT0 = 1   ' signal PIC
    PAUSE 100
    OUT0 = 0
    OUTD = byteValue >> 4 ' output upper nib
    OUT0 = 1   ' signal PIC
    PAUSE 100
    OUT0 = 0
    
    You could even make things simpler by using PULSOUT. Make sure to initialize OUT0 to 1
    OUTD = byteValue
    PULSOUT 0,50000   ' output a 100ms pulse
    OUTD = byteValue >> 4
    PULSOUT 0,50000
    
    Note that in all cases, the time between the pulses that signal to the PIC is pretty short, maybe 300us compared to the 100ms for the signal pulse itself. Depending on how the PIC code handles this, you may want to put a short delay after the OUT0 = 0 or the PULSOUT.
  • kutalinelucaskutalinelucas Posts: 80
    edited 2011-08-02 08:17
    That does make more sense and the code must simpler. Thanks Mike
Sign In or Register to comment.