Shop OBEX P1 Docs P2 Docs Learn Events
Method for 4 bit parallel outout? — Parallax Forums

Method for 4 bit parallel outout?

T ChapT Chap Posts: 4,217
edited 2006-05-26 01:29 in BASIC Stamp
I have a motor controller that uses a 4 bit word to determine one of 16 positions that it will go to. First you send it the address, then the START bit. Each position has its own programmable position, speed, acceleration, etc.

I am brand new to basic, so here are my first thoughts, but surely there is a better way, i.e. one variable or constant for each of the 16 outputs?

Lets use out0 for input A, out1 as B, out2 as C, and out3 as D to produce 0000 output:

input 11
input 12

button1 var in11
button2 var in12

output 0 ' A
output 1 ' B
output 2 ' C
output 3 ' D
output 4 'start bit

A var out0
B var out1
C var out2
D var out3

START var out4

Main:

GOSUB BUT1
GOSUB BUT2


BUT1:
if button1 = 0 then BUT1_0
if button1 = 1 then

d = 0
c = 0
b = 0
a = 1 '0001 position

START = 1 'sends start command for 20ms
Pause 20
START = 0

d = 0 'resets to 0
c = 0 'resets to 0
b = 0 'resets to 0
a = 0 'resets to 0

BUT1_0:
RETURN

BUT2:
if button2 = 0 then BUT2_0
if button2 = 1 then

d = 0
c = 0
b = 1 '0010 position
a = 0

START = 1
pause20
START = 0

d = 0 'resets to 0
c = 0 'resets to 0
b = 0 'resets to 0
a = 0 'resets to 0

BUT2_0:
RETURN

goto main



The better way would be like this:

if button1 = 1 then

ADDRESS = 0001

or this

if button1 = 1 then A 'sets 4 bit word to 0001


Thanks for any tips

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-05-25 19:08
    Sure,
    use OUTA which are the four output pins out0, out1, out2 and out3. So something like

    MCTL VAR OUTA

    then you can set the value directly like:

    MCTL = %0000

    MCTL = %0010

    etc.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    1+1=10
  • T ChapT Chap Posts: 4,217
    edited 2006-05-25 19:20
    OK great, just confused as to how to associate OUTA with those specific 4 pins?

    Is there a method to asign OUTA resectively to pins 0, 1, 2, 3 for MCTL to then apply the %0001 to derive this example of 0001 where :

    pin 0 = 0
    pin 1 = 0
    pin 2 = 0
    pin 3 = 1?


    Thanks
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-25 19:33
    If you look in the memory and variables section of the Help File or BASIC Stamp Manual it explains how the ports are broken down.· OUTA always refers to P0 through P3, OUTB refers to P4 through P7, etc.· OUTL refers to P0 through P7.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • T ChapT Chap Posts: 4,217
    edited 2006-05-25 19:52
    AHA!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-25 21:38
    Got it!· Okay, so you see you can output in groups to 1, 4, 8 or 16 pins at the same time.· The pins cannot cross boundries when using nibbles or byte sized ports.· For example you couldn't (easily) output to P6 through P13 at the same time.· Instead you would use P8 through P15.· I hope this helps.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • T ChapT Chap Posts: 4,217
    edited 2006-05-25 22:29
    Yes I have gotten the concept sort of. I think you are saying that once you set OUTA for a VAR, you dont want to try to an output arrangement outside the 4 or 8 bit preset words relative to their pin assignments, that makes sense. But can you affect one of the pins individually ie out0 1 and at other times call the outputs by OUTA = %0001 for example?

    Just curious if there is a speed advantage to this method? Obviously there are less lines to read, but for the chip to change the outs with outa is that faster as well? It would seem so.

    Thanks
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-05-26 00:36
    Okay, maybe I have over complicated it for you and led you off the path.· I was trying to be general, not specific.· You can always address any pin or group directly regardless of having assigned it as a VAR.· In fact you could·say:

    OUTA = %1001
    Temp = INB
    OUTD = INC
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • allanlane5allanlane5 Posts: 3,815
    edited 2006-05-26 01:18
    "OutA" is a pre-defined 'variable', which refers to bits 0,1,2, and 3.

    It enables you to set all 4 bits at the same time.
  • T ChapT Chap Posts: 4,217
    edited 2006-05-26 01:29
    Yep, I got it now. Thanks!
Sign In or Register to comment.