SERIN Data Misunderstanding?
Bill Chennault
Posts: 1,198
All--
EDIT: I am using the 2.0 Beta compiler.
When I push a button on my game pad, data is sent to an SX48 that causes one of two Leds to light. When I release the button, the Led goes off. While holding the button down, the Led blinks rapidly because my VB code keeps resending the data and the SX48 code keeps acting on it.
This is not what I want. What I want is for the Led to stay on even when I release the button. I want to latch the Led in the on state. (I will worry about turning it off later.) For some reason, I cannot get this simple piece of logic in SX/B to work. I have done this a zillion times before . . . except in SX/B, therefore, I must be dumming off. Here's the code, will you please tell me where I am going wrong?
Device····· ·SX48, OSCHS1
Freq········· 20_000_000
oldRecData·var·byte
RecData ··· var·Byte
RX·············Pin·RB.0
Left········ ··Pin·RB.2
Right······· ··Pin·RB.3
Baud········· Con·"T9600"
Program Start
Start:
·
· IF RecData = 65 THEN
··· HIGH Left
··· LOW Right
· ENDIF
· IF RecData = 66 THEN
··· LOW Left
··· HIGH Right
· ENDIF
· IF RecData = 90 THEN
··· LOW Left
··· LOW Right
· ENDIF
··· oldRecData = RecData······················· ' at this point "oldRecData" equals "RecData"
TightLoop:
· SERIN RX, Baud, RecData····················· ' get new data from eb501, if available
· if oldRecData = RecData then TightLoop· ' if old data (oldRecData) is the same as the new data (RecData), stay in the "TightLoop"
· GOTO Start
There lies a stupido in both the code above and in my head. Please remove it!
Thanks.
--Bill
ps It is my GUESS that I don't understand what is happening with RecData.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
Post Edited (Bill Chennault) : 3/11/2009 8:12:22 PM GMT
EDIT: I am using the 2.0 Beta compiler.
When I push a button on my game pad, data is sent to an SX48 that causes one of two Leds to light. When I release the button, the Led goes off. While holding the button down, the Led blinks rapidly because my VB code keeps resending the data and the SX48 code keeps acting on it.
This is not what I want. What I want is for the Led to stay on even when I release the button. I want to latch the Led in the on state. (I will worry about turning it off later.) For some reason, I cannot get this simple piece of logic in SX/B to work. I have done this a zillion times before . . . except in SX/B, therefore, I must be dumming off. Here's the code, will you please tell me where I am going wrong?
Device····· ·SX48, OSCHS1
Freq········· 20_000_000
oldRecData·var·byte
RecData ··· var·Byte
RX·············Pin·RB.0
Left········ ··Pin·RB.2
Right······· ··Pin·RB.3
Baud········· Con·"T9600"
Program Start
Start:
·
· IF RecData = 65 THEN
··· HIGH Left
··· LOW Right
· ENDIF
· IF RecData = 66 THEN
··· LOW Left
··· HIGH Right
· ENDIF
· IF RecData = 90 THEN
··· LOW Left
··· LOW Right
· ENDIF
··· oldRecData = RecData······················· ' at this point "oldRecData" equals "RecData"
TightLoop:
· SERIN RX, Baud, RecData····················· ' get new data from eb501, if available
· if oldRecData = RecData then TightLoop· ' if old data (oldRecData) is the same as the new data (RecData), stay in the "TightLoop"
· GOTO Start
There lies a stupido in both the code above and in my head. Please remove it!
Thanks.
--Bill
ps It is my GUESS that I don't understand what is happening with RecData.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
Post Edited (Bill Chennault) : 3/11/2009 8:12:22 PM GMT
Comments
I may have missed it in another thread but have you described the rest of your setup? It sounds like your joystick is connected to a PC with a VB app to interpret the joystick signals and then send out a byte via the serial port to the SX chip. One program that may help (or confuse things even more) is PortMon from sysinternals:
http://technet.microsoft.com/en-us/sysinternals/bb896644.aspx
You can run it on your PC and it will show all the activity on the PC serial ports. Very useful to see if your PC is sending out the codes you think it should be sending!
Another bit of troubleshooting is to use a set of 8 LED's connected to one of the unused ports (perhaps RC.0 through RC.7) and then just take whatever serial data you receive and put it on port RC. With that you can see exactly what byte ends up at the SX48. Another option if you are using the SX48 DIP module is to connect a DB-9 to the SOUT and gound, connect that to an open serial port on your PC, and with hyperterminal up on the PC, use a SEROUT command on port A to echo data back to the PC. This is the method I used when debugging my code for my SC-01 speech translation project.
Which version of SX/B are you trying to compile this with?
Best Regards,
Robert
EDIT: I am using the 2.0 Beta compiler.
I wrote a little GUI in VB that successfully communicates with my game pad, captures the joysticks' (two of them) movements and the button presses and then transmits them via Bluetooth to a Stamp that merely turns LEDs on and off either in response to the joysticks or the button presses. (Yes, I described this, elsewhere but, other than getting an eb501 to work correctly, it was not very exciting!)
Last night and this afternoon, I replaced the Stamp (a BS2px24) with an SX48 Module from a guy we all know and love. The joysticks still turn on the LEDs as they did in the Stamp environment. The buttons--which I never tested all the way to the Stamp, previously--must be held down to keep the LEDs tied to the SX48 Module lit. Of course, what is happening is that the LEDs are blinking rapidly as VB transmits the same commands over and over again.
I would like to correct this on the SX/B side by simply turning on the appropriate LED, which I am doing now, and then LATCHING it on, no matter what comes next from the VB GUI/eb501 arrangement. The code I show above is my attempt to do it. To me, it seems that if no changes ocurred in RecData, then the "TightLoop" at the bottom of the code would keep it there until something DID change. If the change was a different button press (I only have two up and running), then the other LED would come on.
I am sure an HB-25 would not really like to be zapped with a continuous off/on cycle in this manner!
If I write about this long enough, even I am bound to see the dumb mistake in my logic!
--Bill
ps If no one sees my logic error, including me, I will doubtless stick 8 LEDs on RC as you suggest.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
' =========================================================================
'
' File...... TESTCOM.SXB
' Purpose... Serial Port test on SX48 OEM Module (tweaked for Bill)
' Author....
' E-mail....
' Website...
' Created... 11 MAR 2009
' Updated...
'
' Version... 1.0
'
' =========================================================================
'
' Program Description
'
'
' Program to wait for a byte from a VB app running on a PC via an eb501 module
' It requires an external 20mhz resonator.
'
' Device Settings
'
DEVICE SX48, OSCXT2 ' May also try OSCHS1
FREQ 20_000_000
ID "TESTCOM"
'
' IO Pins
'
Sout VAR RA.3 ' Serial output (s
Sin VAR RA.2 ' Serial input
SIO VAR TRIS_A ' I/O direction of Port A
RX VAR RB.0 ' RX from VB app on PC
Left VAR RB.2 ' Left LED
Right VAR RB.3 ' Right LED
BIO VAR TRIS_B ' I/O direction of Port B
CIO VAR TRIS_C ' I/O direction of Port C
DIO VAR TRIS_D ' I/O direction of Port D
EIO VAR TRIS_E ' I/O direction of Port E
LEDs VAR RC ' Port C
'
' Constants
'
True CON 1
False CON 0
Baud CON "T9600"
'
' Variables
'
sData VAR Byte ' Serial data in from host
oldRecData VAR Byte
RecData VAR Byte
tmpB1 VAR Byte ' subroutine work vars
tmpB2 VAR Byte
tmpW1 VAR Word
'
PROGRAM Start
'
'
' Function and Subroutine Declarations
'
TX_Byte SUB 1 ' TX to Serial I/O
DELAY SUB 1, 2 ' delay in milliseconds
'
' Program Code
'
' Setup IO ports: Direction, etc.
Start:
SIO = %00000111 ' Set RA.3 as out and RA.2 as in for Serial port
BIO = %11110011 ' Setup port B as inputs (except for RB2 and RB3)
CIO = %00000000 ' Setup port C as outputs for LED's
DIO = %11111111 ' Setup port D as inputs
EIO = %11111111 ' Setup port E as inputs
RecData = 0
' Set outputs to known state
HIGH Left ' Or use LOW
HIGH Right ' Or use LOW
Main:
IF RecData = 65 THEN
HIGH Left
LOW Right
ENDIF
IF RecData = 66 THEN
LOW Left
HIGH Right
ENDIF
IF RecData = 90 THEN
LOW Left
LOW Right
ENDIF
oldRecData = RecData ' at this point "oldRecData" equals "RecData"
TightLoop:
SERIN RX, Baud, RecData ' get new data from eb501, if available
if oldRecData = RecData then TightLoop ' if old data (oldRecData) is the same as the new data (RecData), stay in the "TightLoop"
GOTO Main
'
' Subroutine Code
'
'
' Use: DELAY ms
' -- 'ms' is delay in milliseconds, 1 - 65535
DELAY:
IF __PARAMCNT = 1 THEN
tmpW1 = __PARAM1 ' save byte value
ELSE
tmpW1 = __WPARAM12 ' save word value
ENDIF
PAUSE tmpW1
RETURN
'
' Use: TX_Byte char
' -- 'Transmits 'char' over serial connection
TX_Byte:
tmpB1 = __PARAM1 ' get the passed byte value
SEROUT Sout, Baud, tmpB1 ' Send the byte
RETURN
' =========================================================================
' User Data
' =========================================================================
Gee. I don't know what to say other than thank you!
I will give it a shot first thing in the morning.
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.