Shop OBEX P1 Docs P2 Docs Learn Events
Setting a Propeller pin from within Excel — Parallax Forums

Setting a Propeller pin from within Excel

Kurt FinnieKurt Finnie Posts: 21
edited 2011-10-03 04:54 in Propeller 1
What I want to do is to set I/O pin 1 high (to start a motor) from within an Excel vba sub,
where the Propeller dev. board is attached by USB to a laptop running Windows 7.

And subsequently to set I/O pin 1 low to stop the motor.


On the Excel end, I click on a button and among other things run a sub that states:
intPortID = 1
lngStatus = CommOpen(intPortID, "COM" & CStr(intPortID), "baud9600 parity=N data=8 stop=1")
strData = "1"
lngStatus = CommWrite(inpPortID, strData)
Call CommClose(IntPortID)

I believe this sends a "1" out port 1.


On the Propeller end, I expect I want a loop that includes:
repeat
outa[1]:=ser(rx)

... or some variation of this?

Will this keep I/O pin high until I send a "0" out port 1?


What if I wanted the result to be that 1 would be assigned to the variable named Motor within spin.
How would that be done?


Mike Green stated in a response years ago that sending data through the USB cable would reset the Propeller unless that Propeller function was overridden. How is this accomplished? And when I do need to restart the Propeller fresh, is it just a matter of turning the power off and repowering on?


Getting this motor started and stopped is my last hurdle to complete this project but it just continues to stump me.

Kurt

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-10-02 14:26
    At the very least, you should open the port and keep it open, rather than opening it and closing it every time you need to send a motor command. The reason is that opening and/or closing the port is likely to reset the Propeller, since the default action on opening is to assert DTR and to deassert it on closing.

    -Phil
  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-10-02 14:31
    ...

    Mike Green stated in a response years ago that sending data through the USB cable would reset the Propeller unless that Propeller function was overridden. How is this accomplished?...

    Kurt,

    I'm just curious: why must you communicate to the Prop via USB? Could you possibly have the laptop port set the state of a pin and have that pin trigger an input pin on the Propeller? If all you're trying to do is turn something on and off (binary), why do you have to transfer the value of a variable via USB?
  • mindrobotsmindrobots Posts: 6,506
    edited 2011-10-02 15:20
    If I understand, you want to be able to send the ASCII character for "1" from EXCEL, where the VBA script opens up the COM port connected to the propeller (which happens to be across the USB port (prop plug or built-in USB). The propeller should see this ASCII "1" come in and then set its PIN #1 HIGH. When it sees the ASCII "0" come through, it would set pin #1 LOW.

    If your VBA script sends "1" and "0" as required, then the lest the Propeller needs to do is monitor its serial port (various objects available to do this) and when it sees the ASCII "1" ($31) come in, it would do the OUTA[1] = 1 to set pin #1 to HIGH; when it saw the ASCII 0 ($30) come in, it would do the OUTA[1] = 0 to set pin #1 to LOW. The propeller could ignore anything else coming across the serial port until you start to implement more commands.

    Your SPIN code needs to monitor the serial port and perform various actions based on the characters it sees come in. A simple command loop: read a character, process it, repeat.
  • Kurt FinnieKurt Finnie Posts: 21
    edited 2011-10-02 15:37
    ElectricAye,

    I thought I had to use USB to interconnect the two devices...but I'll do whatever gets the job done. I just want to be able to turn that pin on and off.
    So how do you set the state of a pin in the laptop port... and assure it sets the #1 pin in the Propeller?

    I can envision that there will come a time when I want to control a number of events by another device and I just don't have a grasp of the mechanics of doing RS232.


    Phil,

    Mike had brought up the issue of overriding the assert DTR but didn't explain how to.


    Kurt
  • Mike GreenMike Green Posts: 23,101
    edited 2011-10-02 15:46
    The only way to prevent the PC from resetting the Propeller is to cut the wire from the USB interface from DTR to the reset line of the Propeller. You could always install a jumper or switch to reconnect the two so you could download new programs to the Propeller. Alternatively, you could use another USB to serial interface (like Parallax's PropPlug or USB2SER) attached to two Propeller I/O pins (and ground) with the DTR line not connected.

    As others have mentioned, you could also move the OPEN for the serial port to some kind of VBA initialization code and just write data to the serial port when your action is executed (then close the port during VBA termination code. Consider using some kind of more complex interchange between Excel and the Propeller than just sending "1" and "0". If there's noise on the line, the 1 could be changed to 0 or vice-versa. Typically adding redundancy helps that.
  • Kurt FinnieKurt Finnie Posts: 21
    edited 2011-10-02 16:21
    Mindrobots,

    You've clearly stated what I want to accomplish and I assume the proper object is FullDuplexSerial. It's the writing of that simple command loop that's got me... and I haven't seen any examples to copy from.

    Kurt
  • Mike GMike G Posts: 2,702
    edited 2011-10-02 16:33
    You can easily simulate your serial process with the Parallax Serial Terminal which is a derivative of FullDuplexSerial. There's even a demo located In the "Parallax Library - Demo" that comes with the Propeller Tool.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2011-10-02 18:26
    Just in case you're interested in alternatives, here's an article written by Jon Williams on programming the Propeller with PropBasic:

    http://www.parallax.com/Portals/0/Downloads/docs/cols/nv/prop/col/nvp5.pdf

    PropBasic uses syntax very similar to SX/B and PBasic, and the article includes discussion on serial i/o.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-10-02 19:21
    I'm doing something very similar and have learnt a few things along the way.

    1) Your excel code looks fine.
    2) Opening and closing the port should not reset the propeller (it doesn't on my computer).
    3) You can use the same cable for programming the propeller and for communcations and that could be a solution if you are getting a prop reset when opening and closing ports. So you shouldn't need any more hardware. If you do want separate cables that can be done for a few dollars - eg a USB to serial cable ($2), a D9 socket (50c) and a max3232 ($1) plus a few resistors and capacitors. I'd stick with using the hardware you have for the moment.
    4) Regarding the serial object in spin. I've had a few issues with timing on the standard serial object. At 9600 baud it should be fine, but I can't push it any further than 38400. So I've been using Tim Moore's 4 port object and that works reliably at 115200. Search "4 serial" in the Obex.
    5) If you have any trouble starting up the serial object please let us know.
    6) The serial object sits in a cog and monitors the lines. There are two coding solutions - you can either call a pub that waits for an input like you suggest in your first post. That work ok but it hangs the rest of your program. I have found a better solution is to call a different pub (all the serial objects have these) that asks 'do you have any bytes in the input buffer?' If yes, read the byte(s) and act on them until there are none left. If no, then go off and do other things.
    7) You might want to add a led and the very first thing your program does on startup is flash that led. It makes it easier debugging 'reset on DTR' faults as you can be sure the propeller was reset by something (on my computer opening and closing ports does not change DTR, but turning my computer on and off does).

    I suspect in the debugging phase what you will be doing is programming the propeller with F10 'program to ram', which starts your program which then waits for the input from excel. Then when your program is perfect, program with F11.

    Because you will be opening the port with the prop tool to download, then opening the same port from excel, and doing this over and over, it would be better to use code like you have that explicitly closes the port after the data is sent. Otherwise, if the port is left open the proptool won't be able to download your next program.

    I think you are only 3-4 lines of code away from receiving a byte on the propeller and doing something with it, even if it is just to light a led when you send '1' and turn the led off when you send '0'.
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2011-10-03 01:01
    Cutting the wire to the DTR is a bit dramatic. You just need to make an adapter that sits between the Propeller and the RS-232 that does NOT include the DTR line. All your important stuff remains intact for other projects and you just sandwich this 'cheater device' in when you want it.
  • Kurt FinnieKurt Finnie Posts: 21
    edited 2011-10-03 04:54
    Thanks everyone for your assistance. I'll crack this code now!
Sign In or Register to comment.