In SX Microcontrollers, SX/B Compiler and SX-Key Tool, JonnyMac wrote: Since you're open to learning and I use SX/B 2.0 every day (just delivered an 88-channel, networked animation control system last week that uses SX28s on an RS-485 network) I used your program as a brain warm-up before I start my own coding today. What I've done is bring your program up to the latest 2.0 standards. Seeing it may help you make Hint: If one SUB/FUNC A is called by SUB/FUNC B then A should appear further down the listing -- this lets you use conditional compilation of routines to preserve space as your projects get bigger. Note, too, how with SX/B 2.0 the WAIT_MS subroutine is simplified to one [working] line. [code]DEVICE SX28, OSCXT2, BOR42 FREQ 20_000_000 ID "BTN2MIDI" ' ------------------------------------------------------------------------- ' I/O Pins ' ------------------------------------------------------------------------- TX PIN RA.1 Btns PIN RB INPUT PULLUP SCHMITT LEDS PIN RC OUTPUT ' ------------------------------------------------------------------------- ' Constants ' ------------------------------------------------------------------------- Baud CON "OT31250" ' for MIDI Channel CON 0 CtrChange CON $C0 | Channel ' ------------------------------------------------------------------------- ' Variables ' ------------------------------------------------------------------------- tmpBtns VAR Byte chan VAR Byte mask VAR Byte check VAR Byte tmpB1 VAR Byte ' for subs/funcs tmpB2 VAR Byte tmpW1 VAR Word ' ------------------------------------------------------------------------- ' Subroutine / Function Declarations ' ------------------------------------------------------------------------- GET_BTNS FUNC 1, 0, 0, Byte ' Poll for button press CNG_PGM SUB 1, 1, Byte ' Change MIDI program TX_STR SUB 2, 2, Word ' transmit a string TX_BYTE SUB 1, 1, Byte ' transmit a byte WAIT_MS SUB 2, 2, Word ' replaces PAUSE ' ========================================================================= PROGRAM Main ' ========================================================================= Main: tmpBtns = GET_BTNS IF tmpBtns = %00000000 THEN Main ' wait for button press chan = 0 ' start with Button 1 Check_ChBtn: mask = 1 << chan ' create mask for input check = tmpBtns & mask ' test the button IF check > 0 THEN ' was button pressed? CNG_PGM chan ' yes, change selected program WAIT_MS 1000 ' pause for load time on device Leds = mask ' light its LED DO tmpBtns = GET_BTNS LOOP UNTIL tmpBtns = %00000000 ' force release of all buttons GOTO Main ENDIF INC chan ' next channel IF chan < 6 THEN Check_ChBtn ' more? GOTO Main ' ------------------------------------------------------------------------- ' Subroutine / Function Code ' ------------------------------------------------------------------------- ' Use: var = GET_BTNS FUNC GET_BTNS '{$IFUSED GET_BTNS} gbIdx VAR tmpB1 gbResult VAR tmpB2 gbResult = %11000000 ' bit 6,7 not used, assume pressed FOR gbIdx = 1 TO 25 ' 25ms debounce WAIT_MS 1 gbResult = gbResult | RB ' scan inputs NEXT gbResult = gbResult ^ $FF ' invert (1 = pressed) RETURN gbResult '{$ENDIF} ENDFUNC ' ------------------------------------------------------------------------- ' Use: TX_STR [String | Label] ' -- "String" is an embedded string ' -- "Label" is a DATA label with z-String SUB TX_STR '{$IFUSED TX_STR} tsAddr VAR tmpW1 tsChar VAR __PARAM1 tsAddr = __WPARAM12 ' get string address DO READINC tsAddr, tsChar ' get a character IF tsChar = 0 THEN EXIT ' abort on 0 TX_BYTE tsChar LOOP '{$ENDIF} ENDSUB ' ------------------------------------------------------------------------- ' Use: TX_BYTE aByte ' -- shell for SEROUT SUB TX_BYTE '{$IFUSED TX_BYTE} SEROUT TX, Baud, __PARAM1 '{$ENDIF} ENDSUB ' ------------------------------------------------------------------------- ' Use: CNG_PGM program# SUB CNG_PGM '{$IFUSED CNG_PGM} newPgm VAR tmpB1 newPgm = __PARAM1 TX_BYTE CtrChange ' Control Change TX_BYTE newPgm ' Program Number '{$ENDIF} ENDSUB ' ------------------------------------------------------------------------- ' Use: WAIT_MS milliseconds ' -- replaces PAUSE SUB WAIT_MS '{$IFUSED WAIT_MS} PAUSE __WPARAM12 '{$ENDIF} ENDSUB ' -------------------------------------------------------------------------[/code] ---------- End of Message ---------- You can view the post on-line at: http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=394524#m395259 Need assistance? Send an email to the Forum Administrator at forumadmin@parallax.com The Parallax Forums are powered by dotNetBB Forums, copyright 2002-2009 (http://www.dotNetBB.com)