November 23, 2024, 12:25:58 PM

News:

Be sure to checkout our Vixen interfaces in the Library forum -- if you want PC automation at near zero cost, EFX-TEK and Vixen is a great combination of tools.


Anyone see why this code won't work?

Started by tds234, September 07, 2007, 05:17:12 AM

Previous topic - Next topic

tds234

' =========================================================================
'
'   File...... project1.SXB
'
' =========================================================================
' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID      "project1"
' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

AlarmLed   VAR   RB.0         ' LED pin
Sio      PIN   RC.7         ' Serial pin
Ap8status   VAR   Byte
Ap8Stat      VAR   Byte
' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

BlinkDelay   CON   250         ' blink rate
Baud       CON    "OT38400"
Timeout      CON    1000
On      CON   1

' =========================================================================
  PROGRAM Start
' =========================================================================


' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
Pause Timeout
RB = 00000000
output RB
output RC
RB.5 = On
Pause Timeout

SERIN Sio, Baud, Ap8status, Timeout, NoByte
GOTO ByteF
NoByte:
RB.4 = On
ByteF:

'SEROUT Sio, 0T2400, ("!!!AP8",%00, "P", 0)
SEROUT Sio, Baud, "!AP8"
SEROUT Sio, Baud, %11
SEROUT Sio, Baud, "G"
Pause 250
SERIN Sio, Baud, Ap8status, Timeout, NoInput
RB.4 = 0

Ap8stat = Ap8status XOR %00001000

IF Ap8stat = 1 GOTO NotSet
   RB.1 = On
   GOTO MainLoop
'!!AP8",%00, "P", 0)
NotSet:
   RB.2 = On
   GOTO MainLoop
NoInput:
   RB.3 = On
   GOTO MainLoop

MainLoop:
  DO
    PAUSE BlinkDelay                            ' delay
    TOGGLE AlarmLed                             ' toggle LED state
  LOOP                                       ' loop forever

tds234

September 07, 2007, 05:19:53 AM #1 Last Edit: September 07, 2007, 05:21:39 AM by tds234
The LEDS light in such as way ( i use them with the trainer to track progress, debug lights if you will) that suggest that the serial IO is not working. The code goes to the timeout labels each time.
I have tried both baud rates ( it's an SX board)..
I cannot get code like 'SEROUT Sio, 0T2400, ("!!!AP8",%00, "P", 0)' to work at all, anything in "()" throws errors....

I just cannot seem to get a serial connection to work with the Ap-8...I do get the blinking LED at the end

JonnyMac

September 07, 2007, 10:08:45 AM #2 Last Edit: September 07, 2007, 10:21:12 AM by JonnyMac
You need to use an external clock source with SERIN and SEROUT, the internal clock source is not accurate enough -- especially at 38.4 K.  You should probably use a 20 MHz resonator and update your header to this:

DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX, BOR42
FREQ            20_000_000


If you'll tell me what you want the program to do, I'll code it for you and show you a few SX/B tricks that will be helpful, especially since in SX/B, SERIN and SEROUT only work with bytes.
Jon McPhalen
EFX-TEK Hollywood Office

JonnyMac

September 07, 2007, 02:18:04 PM #3 Last Edit: September 07, 2007, 02:38:12 PM by JonnyMac
Okay, here's a program that does work -- you'll see that with SX/B there's a little more effort required than with PBASIC; but the effort is worth it in the performance that the SX gives (not really used by this program).  Note that SX/B is a compile-in-place compiler, so using lots of SERINs, SEROUTs, and PAUSE instructions will eat through your code space.  My program, below, puts these instructions into subroutines so that they can have additional flexibility and conserve code space.

Hopefully, this will set you in the right direction.

' =========================================================================
'
'   File...... AP-8_Demo.SXB
'   Purpose...
'   Author.... Jon Williams, EFX-TEK
'   E-mail.... jwilliams@efx-tek.com
'   Started...
'   Updated...
'
' =========================================================================


' -------------------------------------------------------------------------
' Program Description
' -------------------------------------------------------------------------
'
' AP-8 (v1.2) demo program.
'
' Note: Give the AP-8 at least 125 milliseconds before requesting status
'       after a play command; the AP-8 does a lot of internal housekeeping
'       and this [short] delay ensures that it can be done before receiving
'       a status request.


' -------------------------------------------------------------------------
' Conditional Compilation Symbols
' -------------------------------------------------------------------------


' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSCXT2, TURBO, STACKX, OPTIONX, BOR42
FREQ            20_000_000
ID              "AP8_Demo"


' -------------------------------------------------------------------------
' I/O Pins
' -------------------------------------------------------------------------

Sio             PIN     RC.7                    ' no SETUP, no ULN

Leds            PIN     RB   OUTPUT             ' use Prop-1 Trainer
Request        PIN     RB.5 OUTPUT
Playing        PIN     RB.3 OUTPUT


' -------------------------------------------------------------------------
' Constants
' -------------------------------------------------------------------------

BaudMode        CON     "OT38400"

IsOn            CON     1
IsOff           CON     0


' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------

sfx             VAR     Byte
ap8Status       VAR     Byte

tmpB1           VAR     Byte                    ' for subs/funcs
tmpB2           VAR     Byte
tmpB3           VAR     Byte
tmpW1           VAR     Word


' =========================================================================
  PROGRAM Start
' =========================================================================


' -------------------------------------------------------------------------
' Subroutine / Function Declarations
' -------------------------------------------------------------------------

DELAY_MS        SUB     1, 2                    ' delay in milliseconds

RX_BYTE         FUNC    1, 0                    ' receive a byte

TX_BYTE         SUB     1                       ' transmit a byte
TX_STR          SUB     2                       ' transmit a string

' AP-8 Routines

PLAY_TRACK      SUB     1, 2                    ' play AP-8 track (0..7)
GET_STATUS      FUNC    1, 0                    ' returns AP-8 status byte


' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
  PLP_C = %10000000                             ' pullup unused pins
  PLP_A = %0000

  DELAY_MS 250                                  ' let AP-8 initialize
  TX_STR "!!!!!!"                               ' restore sync if reset

  sfx = 0

Main:
  Request = IsOn                                ' show TX
  IF sfx < 7 THEN
    PLAY_TRACK sfx                              ' start track
  ELSE
    PLAY_TRACK sfx, 2                           ' demo soft looping
  ENDIF
  DELAY_MS 50                                   ' pad for LED
  Request = IsOff                               ' TX lamp off

  Leds.0 = sfx.0                                ' show track
  Leds.1 = sfx.1
  Leds.2 = sfx.2

  DELAY_MS 125                                  ' let audio start

  DO
    ap8Status = GET_STATUS
    Playing = ap8Status.7                       ' show playing status
    DELAY_MS 100
  LOOP UNTIL ap8Status.7 = IsOff

  INC sfx                                       ' next track
  IF sfx < 8 THEN Main                          ' done?


Demo_Done:
  Leds = %00000000
  DO
    Request = ~Request                          ' toggle request LED
    DELAY_MS 50
  LOOP


' -------------------------------------------------------------------------
' Subroutine / Function Code
' -------------------------------------------------------------------------

' Use: DELAY_MS mSecs
' -- delays program in milliseconds

SUB DELAY_MS
  IF __PARAMCNT = 1 THEN
    tmpW1 = __PARAM1
  ELSE
    tmpW1 = __WPARAM12
  ENDIF
  PAUSE tmpW1
  ENDSUB

' -------------------------------------------------------------------------

' Use: byteVal = RX_BYTE
' -- receive "byteVal" at "BaudMode" on pin "Sio"

FUNC RX_BYTE
  SERIN Sio, BaudMode, tmpB1
  RETURN tmpB1
  ENDFUNC

' -------------------------------------------------------------------------

' Use: TX_BYTE byteVal
' -- transmit "byteVal" at "BaudMode" on pin "Sio"

SUB TX_BYTE
  SEROUT Sio, BaudMode, __PARAM1
  ENDSUB

' -------------------------------------------------------------------------

' Use: TX_STR [string | label]
' -- "string" is an embedded string constant
' -- "label" is DATA statement label for stored z-String

SUB TX_STR
  tmpW1 = __WPARAM12                            ' get address of string
  DO
    READINC tmpW1, tmpB1                        ' read a character
    IF tmpB1 = 0 THEN EXIT                      ' if 0, string complete
    TX_BYTE tmpB1                               ' send character
  LOOP
  ENDSUB

' -------------------------------------------------------------------------

' Use: PLAY_TRACK trackNum {, loops }
' -- "trackNum" is 0 to 7
' -- [optional] loops uses soft looping of AP-8 v1.2 firmware
' -- invalid track number gets truncated

SUB PLAY_TRACK
  IF __PARAMCNT = 1 THEN
    tmpB2 = __PARAM1 & %111                     ' save track #
    tmpB3 = 0                                   ' no looping
  ELSE
    tmpB2 = __PARAM1 & %111                     ' save track #
    tmpB3 = __PARAM2
  ENDIF
  TX_STR  Header
  TX_BYTE %00                                   ' send address
  IF tmpB3 = 0 THEN
    TX_BYTE "P"                                 ' play command
    TX_BYTE tmpB2                               ' send track #
  ELSE
    TX_BYTE "L"                                 ' soft loop command
    TX_BYTE tmpB2                               ' send track #
    TX_BYTE tmpB3
  ENDIF
  ENDSUB

' -------------------------------------------------------------------------

' Use: statByte = GET_STATUS

FUNC GET_STATUS
  TX_STR  Header
  TX_BYTE %00                                   ' send address
  TX_BYTE "G"                                   ' request status
  tmpB2 = RX_BYTE                               ' get status byte
  RETURN tmpB2                                  ' return status to caller
  ENDFUNC


' -------------------------------------------------------------------------
' User Data
' -------------------------------------------------------------------------

Header:
  DATA  "!AP8", 0



Please don't be nervous about the subroutines and functions -- as you can see, most of them are my "standards" and the only ones I created special for this program was PLAY_TRACK and GET_STATUS.  Note that PLAY_TRACK uses an optional parameter so that you can play a sound just once, or loop it.
Jon McPhalen
EFX-TEK Hollywood Office

tds234

Wow, I mean, wow, it's unreal how fast you can crank that code out. Thanks very much!
I'm just not much of an embedded systems guy. Give me Java/Ruby/Cobol/C/SQL/HTML just about any modern block structured language in a business context and I can rock but my assembly and lower level stuff is really rusty...
I think I made a fundamental mistake in assuming the much more flexible SX was as easy to program as the Prop1 and 2. I think I'll start with them and work forwards. What I want to do is conceptually simple: sequence a few 120 volt AC items , sounds and some solenoids for props but I kept looking at the prop 1 examples and thinking that was going to apply to the SX,....I can't get this code to work either I'm assuming it'll work with an external crystal...

I'm going to start back with the prop-1 ( already ordered so I just added more work to your plate) and try again....

JonnyMac

September 07, 2007, 07:27:48 PM #5 Last Edit: September 07, 2007, 07:29:44 PM by JonnyMac
It won't take you very long to build up a set of useful routines -- be patient with yourself.  SX/B is a special beast as it's design causes keywords to be compiled in place (versus called from some arcane library of unreadable code) so that you can "look under the hood" and see how BASIC translates to Assembly.  Once you get the hang of it you can take the assembly code output and modify it to your needs.  I've become pretty good (not great) at Assembly and I'm learning it by studying the SX/B compiler output.

With serial stuff you MUST have an external clock source as the internal oscillator is just not accurate enough for serial bit timing, especially at 38.4k baud.  Parallax sells 4, 20, and 50 MHz resonators -- you might want to get some to keep on hand.  I typically use 50 because it gives me the best resolution -- faster is better.  Yes, it uses a bit more current, but I'm not running my Prop-SX from batteries.  If you're really serious about hardcore SX stuff you might want to get an SX-Key from Parallax; the Key has the ability to provide an external clock while you're debugging.  They're a tad more expensive than the Blitz and most of our customers won't want them so we don't have them in our catalog -- still, I often encourage people to get them if their SX work is going to move beyond basics.  I didn't actually plug a resonator into my Prop-SX when I was testing that program as the Key was providing the clock source for me.

I'm trying to teach myself Python and Ruby so that I can develop cross-platform products for EFX-TEK.  I guess Java would work too, but I seem not to like it as much as other languages (can't explain why, though....).
Jon McPhalen
EFX-TEK Hollywood Office