' ========================================================================= ' ' File...... OE-8X.SXB ' Purpose... 8-Output Expander for the Prop-1 controller ' Author.... Jon Williams -- Parallax EFX ' E-mail.... jwilliams@parallax.com ' Started... ' Updated... 27 MAR 2005 ' ' ========================================================================= ' ------------------------------------------------------------------------- ' Program Description ' ------------------------------------------------------------------------- ' ' Acts as a serial slave output device from microcontrollers. Serial ' input is at 2400 baud using open mode communications so that the ' messaging can be bi-directional on the same pin. 2400 baud is used ' to allow the BS1 and compatible controllers to use the OE-8X. ' ' Note: Code written in SX/B version 1.2 (beta) ' ------------------------------------------------------------------------- ' Device Settings ' ------------------------------------------------------------------------- DEVICE SX18, OSCXT2, TURBO, STACKX, OPTIONX FREQ 4_000_000 ' ------------------------------------------------------------------------- ' IO Pins ' ------------------------------------------------------------------------- Sio VAR RA.3 ' bi-direction serial OutPort VAR RB ' output port ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- Baud CON "OT2400" ' open mode for chaining ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- myAddr VAR Byte ' address of module serByte VAR Byte ' serial IO byte idx VAR Byte regAddr VAR Byte ' register address temp1 VAR Byte ' work vars temp2 VAR Byte ' ========================================================================= PROGRAM Start ' ========================================================================= Rev_Code: DATA "0.1", 0 ' firmware version ' ------------------------------------------------------------------------- ' Subroutine Declarations ' ------------------------------------------------------------------------- SERRX SUB 1 ' receive serial byte SERTX SUB 1 ' transmit serial byte DELAY SUB 1, 2 ' delay in milliseconds ' ------------------------------------------------------------------------- ' Program Code ' ------------------------------------------------------------------------- Start: PLP_A = %1000 ' enable pull-ups on 0-2 TRIS_A = %1111 ' RA = all inputs TRIS_B = %00000000 ' RB = all outputs myAddr = RA & %0111 ' read module address Get_Header: SERRX @serByte IF serByte <> "!" THEN Get_Header ' wait for "!" SERRX @serByte IF serByte <> "O" THEN Get_Header ' wait for "O" SERRX @serByte IF serByte <> "E" THEN Get_Header ' wait for "E" SERRX @serByte IF serByte <> "8" THEN Get_Header ' wait for "8" Check_Addr: SERRX @serByte ' get address byte IF serByte <> myAddr THEN Start ' check against module Get_Command: SERRX @serByte ' wait for command byte Show_Version: IF serByte <> "V" THEN Get_Status ' "V" = show version string idx = 0 ' initialize index DELAY 1 ' let host get ready Send_Version: READ Rev_Code + idx, serByte ' get character from string IF serByte <> 0 THEN ' valid character? SERTX serByte ' yes, send it INC idx ' update index GOTO Send_Version ENDIF GOTO Start Get_Status: IF serByte <> "G" THEN Set_Status ' "G" = return output status serByte = OutPort ' get copy of current outputs DELAY 1 ' let host get ready SERTX serByte ' send it GOTO Start Set_Status: IF serByte <> "S" THEN Cmd_X ' "S" = set new outputs SERRX @serByte ' get new status value OutPort = serByte ' update active outputs GOTO Start Cmd_X: ' for future expansion GOTO Start ' ------------------------------------------------------------------------- ' Subroutines Code ' ------------------------------------------------------------------------- ' Use: SERRX @aVar ' -- receives byte on 'Sio' at 'Baud' and places in 'aVar' ' -- will timeout after one second SERRX: regAddr = __PARAM1 ' save address of target SERIN Sio, Baud, temp1, 1000, Start ' get serial byte __RAM(regAddr) = temp1 ' put serial byte in target RETURN ' Use: SERTX aVar ' -- transmits 'aVar' on 'Sio' at 'Baud' SERTX: temp1 = __PARAM1 ' make copy of output SEROUT Sio, Baud, temp1 ' send it RETURN ' Use: DELAY milliseconds {, multiplier} ' -- pause for milliseconds x (optional) multiplier DELAY: temp1 = __PARAM1 IF __PARAMCNT = 1 THEN ' if only base time temp2 = 1 ' set multiplier to 1 ELSE temp2 = __PARAM2 ' else use multiplier ENDIF PAUSE temp1 * temp2 RETURN