#Picaxe 14M2
#Terminal 4800
; AXE171 .------------------------------------------.
; PICAXE-14M2 | |
; .-----------. | -.- V+ V+ -.- SPE035 (DFP-Mini) |
; -| B.5 C.0 |- | | | .-----------------. |
; .---| B.4 C.1 |- | | `---| VCC BUSY |---'
; | -| B.3 C.2 |<--' | .------------->| RX USB- |-
; | -| B.2 C.3 |<-----|--|--------------| TX USB+ |-
; | -| B.1 C.4 |- | | -| DACR ADKEY2 |-
; | -| B.0 C.5 | | | |\ _ -| DACL ADKEY1 |-
; .--|---| 0V _ V+ |------' | | \| |---| SPK1 ___ IO2 |-
; | | `----' `----' ___ | 8R | | | -| GND | | GND |---.
; | `------------------|___|--' | /|_|---| SPK2 | | IO1 |- |
; | 1K0 |/ + `------|___|------' |
; _|_ 0V 0V _|_
Symbol TX = B.4
Symbol RX = C.3
Symbol BUSY_PIN = pinC.2
Symbol BAUD_FREQ = M16 ; Note M16
Symbol BAUD = T9600_16
Symbol cmd = b0
Symbol arg = w1 ; b3:b2
Symbol arg.lsb = b2
Symbol arg.msb = b3
Symbol rxSom = b10
Symbol rxVer = b11
Symbol rxCmd = b12
Symbol rxLen = b13
Symbol rxFbk = b14
Symbol rxArg.msb = b15
Symbol rxArg.lsb = b16
Symbol rxEom = b17
High TX
; Get initial power-on response from the module
; Note there is no response after a download, only
; after download and the AXE171 is power-cycled
Gosub WaitForResponse
; Delay until Terminal is displayed
Pause 2000
SerTxd("Started", CR, LF )
; Check that USB memory / SD card is inserted
SerTxd( "Initial response = " )
If rxSom = $00 Then
SerTxd( "No response", CR, LF )
Else
Gosub ShowResponse
If rxCmd <> $3F Then
SerTxd( "* Unexpected response", CR, LF )
Else
b0 = rxArg.lsb
If bit0 = 1 Then ; $01
SerTxd( "* USB memory detected", CR, LF )
rxCmd = $00
End If
If bit1 = 1 Then ; $02
SerTxd( "* SD Card detected", CR, LF )
rxCmd = $00
End If
If bit2 = 1 Then ; $02
SerTxd( "* PC connection detected", CR, LF )
rxCmd = $00
End If
If rxCmd <> $00 Then
SerTxd( "* Unknown source detected", CR, LF )
End If
End If
End If
; Reset the module
SerTxd("Reset the module", CR, LF )
cmd = $3F : arg = %1111 : Gosub SendAndWaitForResponse
; Check that USB memory / SD card is inserted
SerTxd( "Reset response = " )
If rxSom = $00 Then
SerTxd( "No response", CR, LF )
Else
Gosub ShowResponse
If rxCmd <> $3F Then
SerTxd( "* Unexpected response", CR, LF )
Else
b0 = rxArg.lsb
If bit0 = 1 Then ; $01
SerTxd( "* USB memory detected", CR, LF )
rxCmd = $00
End If
If bit1 = 1 Then ; $02
SerTxd( "* SD Card detected", CR, LF )
rxCmd = $00
End If
If bit2 = 1 Then ; $02
SerTxd( "* PC connection detected", CR, LF )
rxCmd = $00
End If
If rxCmd <> $00 Then
SerTxd( "* Unknown source detected", CR, LF )
End If
End If
End If
; Get firmware version number
SerTxd("Get firmware version number", CR, LF )
cmd = $46 : Gosub SendAndWaitForResponse
SerTxd( "Firmware version response = " )
Gosub ShowResponse
SerTxd( "* Version ", #rxArg.msb, ".", #rxArg.lsb, CR, LF )
; Select the SD Card for use
SerTxd("Select TF Card", CR, LF )
cmd = $09 : arg = $0002 : Gosub Send
Pause 4000
; Set an initial volume
SerTxd("Volume 20", CR, LF )
cmd = $06 : arg = 20 : Gosub Send
Pause 1000
; Play the first song found on card
SerTxd("Play first song", CR, LF )
cmd = $03 : arg = 1 : Gosub SendAndWaitForResponse
; The program will not continue until a response has
; been received
; Report the response
; Note there is no timeout because we don't know how
; long a song will be, so no idea when a response
; will arrive.
SerTxd( "After play response = " )
Gosub ShowResponse
If rxCmd = $3D Then
SerTxd( "* End of song", CR, LF )
Else
SerTxd( "* Unexpected response", CR, LF )
End If
Sertxd("Done", CR, LF )
Stop
Send:
SetFreq BAUD_FREQ
Pause 10
SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
SetFreq MDEFAULT
arg = 0
Return
SendAndWaitForResponse:
SetFreq BAUD_FREQ
Pause 10
rxSom = $7E
SerOut TX, BAUD, ( $7E, $FF, $06, cmd, $00, arg.msb, arg.lsb, $EF )
SerIn RX, BAUD, ( rxSom ), rxVer, rxLen, rxCmd, rxFbk, rxArg.msb, rxArg.lsb, rxEom
SetFreq MDEFAULT
arg = 0
Return
WaitForResponse:
SetFreq BAUD_FREQ
rxSom = $7E
SerIn [5000,TimedOut], RX, BAUD, ( rxSom ), rxVer, rxLen, rxCmd, rxFbk, rxArg.msb, rxArg.lsb, rxEom
SetFreq MDEFAULT
Return
TimedOut:
rxSom = $00
SetFreq MDEFAULT
Return
ShowResponse:
b0 = rxSom : Gosub ShowHexByte
b0 = rxVer : Gosub ShowHexByte
b0 = rxLen : Gosub ShowHexByte
b0 = rxCmd : Gosub ShowHexByte
b0 = rxFbk : Gosub ShowHexByte
b0 = rxArg.msb : Gosub ShowHexByte
b0 = rxArg.lsb : Gosub ShowHexByte
b0 = rxEom : Gosub ShowHexByte
SerTxd( CR, LF )
Return
ShowHexByte:
b1 = b0 / $10 : Gosub ShowHexDigit
b1 = b0 : Gosub ShowHexDigit
SerTxd( " " )
Return
ShowHexDigit:
b1 = b1 & $F + "0"
If b1 > "9" Then
b1 = b1 + 7
End If
SerTxd( b1 )
Return