Setting a Propeller pin from within Excel
Kurt Finnie
Posts: 21
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
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
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?
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.
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
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.
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
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.
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'.