Shop OBEX P1 Docs P2 Docs Learn Events
SERIN Word vs RX Character — Parallax Forums

SERIN Word vs RX Character

gtslabsgtslabs Posts: 40
edited 2007-12-08 21:13 in General Discussion
I am trying to set a variable to a word value based on the input from my Computer Serial Port.

Right now it is looking at each character in the RX stream but I need the option to read a Word value.
This is the part of the code I am having the problem with:

IF char = "F" THEN Set_Frequency

I want to send out !BUDF6000 to set my frequency to 6000 hz.
ie.
!BUDF10 = 10Hz
!BUDF6000 = 6KHZ


The Set_Frequency·sub looks for the LSB then the MSB of the next 2 RX Characters.
How can I modify this to accept in a Word value?· I can convert on the Sending side to a 4 digit Hex if needed. I guess I need 4 RX_Byte lines then arrange them into the correct format?

Thanks
Steve
' =========================================================================
'
'   File...... SX48_PWM.SXB
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail.... [url=mailto:jwilliams@efx-tek.com]jwilliams@efx-tek.com[/url]
'   Started...
'   Updated...
'
' =========================================================================

' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------

' -------------------------------------------------------------------------
' Conditional Compilation Symbols
' -------------------------------------------------------------------------

' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX48, OSCXT2, BOR42
FREQ            4_000_000
ID              "SX48PWM"

' -------------------------------------------------------------------------
' I/O Pins
' -------------------------------------------------------------------------

'UnusedRA PIN RA    INPUT  PULLUP
UnusedRB PIN RB    INPUT  PULLUP
UnusedRC PIN RC    INPUT  PULLUP
UnusedRD PIN RD    INPUT  PULLUP
UnusedRE PIN RE    INPUT  PULLUP

RX  PIN RA.0  INPUT  NOPULLUP

Enable  Pin RA.2  OUTPUT NOPULLUP
Direction Pin RA.3  OUTPUT NOPULLUP
Led  PIN RB.6  OUTPUT NOPULLUP ' for TIMER1 output

' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------
ToUpper  CON 1
Baud  CON "N4800"

Yes  CON 1
No  CON 0
Up  CON  1
Down  CON 0

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
char  VAR Byte   ' serial input
frOut  VAR Word   ' frequency of output

tmpB1  VAR Byte   ' for subs/funcs
tmpB2  VAR Byte
tmpW1  VAR Word
tmpW2  VAR Word

' =========================================================================
'  INTERRUPT
' =========================================================================

'  RETURNINT

' =========================================================================
  PROGRAM Start
' =========================================================================

' -------------------------------------------------------------------------
' Subroutine / Function Declarations
' -------------------------------------------------------------------------

SET_FREQ SUB 2   ' set freq of TIMER1
RX_BYTE  FUNC 1, 0   ' rx one byte

' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  Enable = No   'Set RA.2 = 0 for startup state
  frOut = 1   ' set default frequency
  SET_FREQ frOut

Main:

  char = RX_BYTE

' wait for header
  IF char <> "!" THEN Main
  char = RX_BYTE 'ToUpper
  IF char <> "B" THEN Main
  char = RX_BYTE 'ToUpper
  IF char <> "U" THEN Main
  char = RX_BYTE 'ToUpper
  IF char <> "D" THEN Main

Get_Cmd:
  char = RX_BYTE 'ToUpper
  IF char = "E" THEN Enable_PWM  '!BUDE
  IF char = "D" THEN Disable_PWM '!BUDD
  IF char = "U" THEN Run_Up  '!BUDU
  IF char = "O" THEN Run_Down  '!BUDO
  IF char = "F" THEN Set_Frequency '!BUDF10 = 10Hz  !BUDF6000 = 6KHZ
  GOTO Main

'Turn on the Stepper Enable pin
Enable_PWM:
  Enable = Yes 'Set RA.2 = 1
  GOTO Main

'Turn off the Stepper Enable pin
Disable_PWM:
  Enable = No 'Set RA.2 = 0
  GOTO Main

'Set Stepper Drive Direction to UP
Run_Up:
  Direction = Up 'Set RA.3 = 1
  GOTO Main

'Set Stepper Drive Direction to Down
Run_Down:
  Direction = Down 'Set RA.3 = 0
  GOTO Main

Set_Frequency:
 frOut_LSB = RX_BYTE    ' receive new freq
 frOut_MSB = RX_BYTE
 SET_FREQ frOut

' WATCH frOut, 16, UDEC    ' use Debug --> Poll
' BREAK
GOTO Main



' -------------------------------------------------------------------------
' Subroutine / Function Code
' -------------------------------------------------------------------------

' Use: SET_FREQ hz
' -- sets TIMER1 to PWM mode, 50% duty cycle, output in "hz"

SUB SET_FREQ
  tmpW1 = __WPARAM12    ' capture freq

  IF tmpW1 = 1 THEN    ' fix div by 1 problem
    tmpW1 = 31_250
    tmpW2 = 62_500
  ELSE
    tmpW2 = 62_500 / tmpW1   ' calc units
    tmpW1 = tmpW2 >> 1    ' set to 50% DC
  ENDIF
  TIMER1 PRESCALE, 6    ' 1:64
  TIMER1 PWM, tmpW1, tmpW2   ' activate PWM
  ENDSUB

' -------------------------------------------------------------------------

' Use: result = RX_BYTE
' -- receives one byte on RX pin at Baud

FUNC RX_BYTE
  SERIN RX, Baud, tmpB1
  RETURN tmpB1
  ENDFUNC

' -------------------------------------------------------------------------
' User Data
' -------------------------------------------------------------------------

Comments

  • JonnyMacJonnyMac Posts: 9,217
    edited 2007-12-08 21:13
    To receive a decimal value as text can be very cumbersome (all those details are hidden in the DEC modifier of PBASIC) -- here's a function that duplicates DEC up to five digits. Note that if you are sending a value that is less than 5-digits wide you must either zero-pad it (e.g, 10 becomes "00010") or add a terminating, non-digit character (e.g., 10 becomes "10.")

    ' Use: wResult = RX_DEC
    ' -- text to decimal converter
    ' -- up to 5 digits; non-character input terminates
    
    FUNC RX_DEC
      tmpW1 = 0                                     ' clear result
      tmpB3 = 5                                     ' max characters allowed
      DO WHILE tmpB3 > 0
        tmpB2 = RX_BYTE                             ' get byte from stream
        IF tmpB2 < "0" THEN EXIT                    ' validate
        IF tmpB2 > "9" THEN EXIT
        tmpB2 = tmpB2 - "0"                         ' convert char to number
        tmpW1 = tmpW1 * 10                          ' shift result left
        tmpW1 = tmpW1 + tmpB2                       ' add to result
        DEC tmpB3                                   ' update digit count
      LOOP
      RETURN tmpW1
      ENDFUNC
    

    Post Edited (JonnyMac) : 12/8/2007 9:27:28 PM GMT
Sign In or Register to comment.