In SX Microcontrollers, SX/B Compiler and SX-Key Tool, djfx1 wrote: 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 [ 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 ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=195175 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2007 (http://www.dotNetBB.com)