' ' Program: Modem AppMod Example ' Author: (c) 2008 by A Fox Consulting & Design ' http://www.afox-consulting.com ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' Playing with the Parallax Modem AppMod ' -----[ I/O Definitions ]------------------------------------------------- ' _RedLED PIN 0 ' red LED through 330 ohm _GreenLED PIN 1 ' green LED through 330 ohm _BlueLED PIN 2 ' blue LED through 330 ohm _RI PIN 12 ' ring indicator - ACTIVE LOW _DCD PIN 13 ' carrier detect - ACTIVE LOW _RX PIN 14 ' CH1786 "Rx" pin _TX PIN 15 ' CH1786 "Tx" pin ' -----[ Constants ]------------------------------------------------------- ' T2400 CON 396 ' 2400 baud for modem: 8N No CON 1 Yes CON 0 FF CON 12 ' clear screen ' -----[ Variables ]------------------------------------------------------- ' InByte VAR Byte ' input character from modem riFltr VAR Byte ' -----[ Initialization ]-------------------------------------------------- ' DIRS = %1000000000000111 OUTL = %0111 ' -----[ Main Code ]------------------------------------------------------- ' Main: DEBUG "Initializing Modem",CR GOSUB Initialize_Modem ' initialize modem DEBUG "Waiting for carrier detect",CR GOSUB Wait_DCD ' carrier detect routine DEBUG "Sending data to HypterTerminal",CR GOSUB Communicate END ' -----[ Subroutines ]----------------------------------------------------- ' Initialize_Modem: PAUSE 1000 ' allow modem to power up SEROUT _TX, T2400,["AT", CR] ' setup modem for speed SERIN _RX, T2400, 2500, Error, [WAIT ("OK")] PAUSE 250 SEROUT _TX, T2400,["ATS0=2", CR] ' answer on second ring. SERIN _RX, T2400, 2500, Error, [WAIT ("OK")] PAUSE 250 SEROUT _TX, T2400,["ATS7=50", CR]' max carrier detect is 50 secs SERIN _RX, T2400, 2500, Error, [WAIT ("OK")] PAUSE 250 SEROUT _TX, T2400,["ATS2=43", CR]' escape character To "+" SERIN _RX, T2400, 2500, Error, [WAIT ("OK")] PAUSE 250 SEROUT _TX, T2400,["AT&C1", CR] ' enable DCD SERIN _RX, T2400, 2500, Error, [WAIT ("OK")] PAUSE 250 PAUSE 600 SEROUT _TX, T2400,["AT&D0", CR] ' disable DTR SERIN _RX, T2400, 2500, Error, [WAIT ("OK")] PAUSE 200 RETURN ' wait for carrier detect Wait_DCD: PAUSE 5 IF _RI = 0 THEN GOSUB DoRing IF _DCD = 0 THEN Connect ' wait for carrier GOTO Wait_DCD ' process ring indicator ' - filters pulsing ring indicator ' - waits for about 0.25 second of no RI pulsing before returning ' DoRing: riFltr = 0 ' clear the "no pulses" counter RingLoop: IF _RI = 0 THEN DoRing ' still pulsing riFltr = riFltr + 1 ' not pulsing, increment count IF riFltr > 50 THEN RingExit PAUSE 5 GOTO RingLoop RingExit: DEBUG "RING...",CR RETURN ' done - outta here Connect: PAUSE 8000 ' Wait for connecting modem to be happy Communicate: SEROUT _TX, T2400, [FF] SEROUT _TX, T2400, ["================================", CR, LF] SEROUT _TX, T2400, ["Fox's CH1786 LED Demo Program ", CR, LF] SEROUT _TX, T2400, ["================================", CR, LF] SEROUT _TX, T2400, ["Command options: ", CR, LF] SEROUT _TX, T2400, [" [R] Red LED toggle ", CR, LF] SEROUT _TX, T2400, [" [G] Green LED toggle ", CR, LF] SEROUT _TX, T2400, [" [B] Blue LED toggle ", CR, LF] SEROUT _TX, T2400, [" [E] End the call ", CR, LF] SEROUT _TX, T2400, [CR, LF] SEROUT _TX, T2400, [" Red LED status = ", BIN1 _RedLED,CR, LF] SEROUT _TX, T2400, [" Green LED status = ", BIN1 _GreenLED,CR, LF] SEROUT _TX, T2400, [" Blue LED status = ", BIN1 _BlueLED,CR, LF] SEROUT _TX, T2400, [CR, LF] SEROUT _TX, T2400, ["Enter command ==> "] PAUSE 500 SERIN _RX,T2400,[InByte] ' get byte IF InByte = "E" THEN Hang_up IF InByte = "R" THEN Toggle_red IF InByte = "G" THEN Toggle_green IF InByte = "B" THEN Toggle_blue GOTO Communicate ' hang up modem Hang_up: DEBUG "Hanging up modem",CR OUTL=%0111 ' LEDs off SEROUT _TX, T2400,[CR,LF,LF,"Disconnect requested", CR,LF] PAUSE 2000 SEROUT _TX, T2400, ["+++"] PAUSE 2000 SEROUT _TX, T2400, 10, ["ATH0", CR] RETURN Toggle_red: TOGGLE _RedLED GOTO Communicate Toggle_green: TOGGLE _GreenLED GOTO Communicate Toggle_blue: TOGGLE _BlueLED GOTO Communicate Error: DEBUG "Communication error!",CR END