SXB to C programming
Hi there, I'm new to the group so I'm not sure if this is the right place to post this.··I have some code that was written in SXB and a friend asked if I can convert it to C. I'm not familiar with C but have been trying to start. I was wondering if someone here could possibly help me out.· I am going to post the code:· Thank you in advance for any help provided.
'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
TX···············VAR·RA.2···' serial to/from host
RX·············· VAR·RA.3
CS·············· VAR·RB.0
Clock··········· VAR·RB.1
Dio··············VAR·RB.2
·
WATCH Dio, 12, UDEC
'
' Constants
'
Baud··CON·"OT9600"
LF············· CON·10
FF············· CON·12
CR············· CON·13
'
' Variables
'
adcLo········· VAR·Byte
adcHi··········VAR·Byte
temp1········· VAR·Byte···' subroutine work vars
temp2········· VAR·Byte
temp3········· VAR·Byte
temp4········ ·VAR·Byte
temp5········· VAR·Byte
' =========================================================================
· PROGRAM Start
' =========================================================================
CRLF:
· DATA CR, LF, 0
'
' Subroutine Declarations
'
WAIT_MS········ SUB·1, 2
TX_BYTE········ SUB·1
TX_STR········· SUB·2
TX_HEX2········ SUB·1
RD_1298········ SUB·1
'
' Program Code
'
Start:
· PLP_A = %0000
· PLP_B = %00000111
· PLP_C = %00000000
· HIGH CS················· ' deselect adc
· LOW Clock··············· ' init clock for 0-1-0
·
Main:
· DO
··· TX_BYTE FF············ ' clear ASCII terminal
··· TX_STR "LTC1298"
··· TX_STR CRLF
··· TX_STR "
"
··· TX_STR CRLF
··· TX_STR "Ch0: "
··· RD_1298 0··············' read channel 0
··· TX_HEX2 adcHi········· ' display (hex)
··· TX_HEX2 adcLo
··· TX_STR CRLF
··· TX_STR "Ch1: "
··· RD_1298 1··············' read channel 1
··· TX_HEX2 adcHi········· ' display (hex)
··· TX_HEX2 adcLo
··· TX_STR CRLF
··· WAIT_MS 250, 2
· LOOP
· END
'
' Subroutine Code
'
' Use: WAIT_MS milliseconds {, multiplier}
' -- multiplier is optional
WAIT_MS:
· temp1 = __PARAM1····························· ' get milliseconds
· IF __PARAMCNT = 1 THEN······················· ' if no multiplier
··· temp2 = 1·································· '·· set to 1
· ELSE········································· ' else
··· temp2 = __PARAM2··························· '·· get multiplier
· ENDIF
· IF temp1 > 0 THEN···························· ' no delay if either 0
··· IF temp2 > 0 THEN
····· PAUSE temp1 * temp2······················ ' do the delay
··· ENDIF
· ENDIF
· RETURN
'
' Use: TX_BYTE aByte
' -- transmits one byte to serial port
TX_BYTE:
· temp1 = __PARAM1····························· ' copy outgoing byte
· SEROUT TX, Baud, temp1
· RETURN
'
' Use: TX_STR [noparse][[/noparse] string | label ]
' -- "string" is an embedded literal string
' -- "label" is DATA statement label for stored z-String
TX_STR:
· temp3 = __PARAM1····························· ' get string offset
· temp4 = __PARAM2····························· ' get string base
· DO
··· READ temp4 + temp3, temp5·················· ' read a character
··· IF temp5 = 0 THEN EXIT····················· ' if 0, string complete
··· TX_BYTE temp5··················· ·········· ' send the byte
··· INC temp3·································· ' point to next character
··· temp4 = temp4 + Z·························· ' update base on overflow
· LOOP
· RETURN
'
' Use: TX_HEX2 aByte
' -- sends "aByte" to host in HEX2 format (text)
TX_HEX2:
· temp2 = __PARAM1····························· ' copy byte
· FOR temp3 = 1 TO 2··························· ' send two nibbles
··· temp4 = temp2 & $F0························ ' get a nibble
··· SWAP temp4
··· IF temp4 < $A THEN
····· temp4 = temp4 + "0"······················ ' convert to "0".."9"
··· ELSE
····· temp4 = temp4 + 55························' convert to "A".."F"
··· ENDIF
··· TX_BYTE temp4······························ ' send to host
··· temp2 = temp2 << 4························· ' position next nibble
· NEXT
· RETURN
'
' Use: RD_1298 channel
' -- puts ADC value for "channel" (0 or 1) in adcHi\adcLo
RD_1298:
· temp1 = %1011································ ' set for single-ended mode
· temp1.2 = __PARAM1.0··························' select channel
· CS = 0
· SHIFTOUT Dio, Clock, LSBFIRST, temp1\4········' send configuration
· SHIFTIN· Dio, Clock, MSBPOST, adcHi\4·········' get high nib of adc
· SHIFTIN· Dio, Clock, MSBPOST, adcLo\8········ ' get low nib of adc
· CS = 1
· RETURN
'
' Device Settings
'
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX
FREQ··········· 4_000_000
'
' IO Pins
'
TX···············VAR·RA.2···' serial to/from host
RX·············· VAR·RA.3
CS·············· VAR·RB.0
Clock··········· VAR·RB.1
Dio··············VAR·RB.2
·
WATCH Dio, 12, UDEC
'
' Constants
'
Baud··CON·"OT9600"
LF············· CON·10
FF············· CON·12
CR············· CON·13
'
' Variables
'
adcLo········· VAR·Byte
adcHi··········VAR·Byte
temp1········· VAR·Byte···' subroutine work vars
temp2········· VAR·Byte
temp3········· VAR·Byte
temp4········ ·VAR·Byte
temp5········· VAR·Byte
' =========================================================================
· PROGRAM Start
' =========================================================================
CRLF:
· DATA CR, LF, 0
'
' Subroutine Declarations
'
WAIT_MS········ SUB·1, 2
TX_BYTE········ SUB·1
TX_STR········· SUB·2
TX_HEX2········ SUB·1
RD_1298········ SUB·1
'
' Program Code
'
Start:
· PLP_A = %0000
· PLP_B = %00000111
· PLP_C = %00000000
· HIGH CS················· ' deselect adc
· LOW Clock··············· ' init clock for 0-1-0
·
Main:
· DO
··· TX_BYTE FF············ ' clear ASCII terminal
··· TX_STR "LTC1298"
··· TX_STR CRLF
··· TX_STR "
"
··· TX_STR CRLF
··· TX_STR "Ch0: "
··· RD_1298 0··············' read channel 0
··· TX_HEX2 adcHi········· ' display (hex)
··· TX_HEX2 adcLo
··· TX_STR CRLF
··· TX_STR "Ch1: "
··· RD_1298 1··············' read channel 1
··· TX_HEX2 adcHi········· ' display (hex)
··· TX_HEX2 adcLo
··· TX_STR CRLF
··· WAIT_MS 250, 2
· LOOP
· END
'
' Subroutine Code
'
' Use: WAIT_MS milliseconds {, multiplier}
' -- multiplier is optional
WAIT_MS:
· temp1 = __PARAM1····························· ' get milliseconds
· IF __PARAMCNT = 1 THEN······················· ' if no multiplier
··· temp2 = 1·································· '·· set to 1
· ELSE········································· ' else
··· temp2 = __PARAM2··························· '·· get multiplier
· ENDIF
· IF temp1 > 0 THEN···························· ' no delay if either 0
··· IF temp2 > 0 THEN
····· PAUSE temp1 * temp2······················ ' do the delay
··· ENDIF
· ENDIF
· RETURN
'
' Use: TX_BYTE aByte
' -- transmits one byte to serial port
TX_BYTE:
· temp1 = __PARAM1····························· ' copy outgoing byte
· SEROUT TX, Baud, temp1
· RETURN
'
' Use: TX_STR [noparse][[/noparse] string | label ]
' -- "string" is an embedded literal string
' -- "label" is DATA statement label for stored z-String
TX_STR:
· temp3 = __PARAM1····························· ' get string offset
· temp4 = __PARAM2····························· ' get string base
· DO
··· READ temp4 + temp3, temp5·················· ' read a character
··· IF temp5 = 0 THEN EXIT····················· ' if 0, string complete
··· TX_BYTE temp5··················· ·········· ' send the byte
··· INC temp3·································· ' point to next character
··· temp4 = temp4 + Z·························· ' update base on overflow
· LOOP
· RETURN
'
' Use: TX_HEX2 aByte
' -- sends "aByte" to host in HEX2 format (text)
TX_HEX2:
· temp2 = __PARAM1····························· ' copy byte
· FOR temp3 = 1 TO 2··························· ' send two nibbles
··· temp4 = temp2 & $F0························ ' get a nibble
··· SWAP temp4
··· IF temp4 < $A THEN
····· temp4 = temp4 + "0"······················ ' convert to "0".."9"
··· ELSE
····· temp4 = temp4 + 55························' convert to "A".."F"
··· ENDIF
··· TX_BYTE temp4······························ ' send to host
··· temp2 = temp2 << 4························· ' position next nibble
· NEXT
· RETURN
'
' Use: RD_1298 channel
' -- puts ADC value for "channel" (0 or 1) in adcHi\adcLo
RD_1298:
· temp1 = %1011································ ' set for single-ended mode
· temp1.2 = __PARAM1.0··························' select channel
· CS = 0
· SHIFTOUT Dio, Clock, LSBFIRST, temp1\4········' send configuration
· SHIFTIN· Dio, Clock, MSBPOST, adcHi\4·········' get high nib of adc
· SHIFTIN· Dio, Clock, MSBPOST, adcLo\8········ ' get low nib of adc
· CS = 1
· RETURN
Comments
If it is the SX chip, you'll need to state which compiler is being used, since they all work slightly differently in how they handle C syntax.
If you are targetting a different microcontroller, you would have more success by visiting a forum that targets C on that particular chip.
Either way, to learn more about C, take a look at my post here:
http://forums.parallax.com/showthread.php?p=635438
Why do you want to convert the program to C ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
djfx1, are you trying to make this code run on a PC ?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
If they are going to run it on a PC, 80% of the program would need to be re-written, as most of the program is written to send the labels and data to a terminal. This hardly makes sense if the software will be running on a PC (you'd use the PC display screen, and assumedly, some type of windows form).
If they are using another type of microprocessor, then you'd need to know how that processor (and C compiler) handled the communications for the terminal (asynchronous) and the "1298" device (synchronous).
I don't think the way to approach this would be a "line by line" conversion, but rather a "re-write" based on the specific tool used.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John R.
Click here to see my Nomad Build Log