SERIN Word vs RX Character
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
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
Post Edited (JonnyMac) : 12/8/2007 9:27:28 PM GMT