📄 nembtconnect.txt
字号:
Nemesis
'File: NemBTConnect.txt Nemesis Blue Tooth Connection Demo
'aka NemBTConnV2.txt
'JEC 8/05, Rev 11/05
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++
' This program is for demonstration purposes only
' Use at your own risk
'++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'This program runs on Nemesis PIC, with Wintec Bluetooth Module.
'It connects to a BT GPS unit, and then displays the uploaded
'streaming GPS data packets on an LCD.
'It does NOT parse, process, and format the GPS data.
'It does illustrate the BLUETOOTH PROCESS,
'Discovery, Bond, and SPPConnect.
'NOTE: The UNIQUE BT ADDRESS for your own BT GPS receiver
'MUST in used in place of the current value, which is for
'my particular BT GPS unit.
'Change the BT Address in >>> TWO <<< subroutines below
'BondBTM and SPPConnectBTM
'BondBTM also requires the BT GPS's 4 character PIN Code
'Routines are marked with +*+*+* blow
'NOTE WELL: The BT ADDRESS inserted above is the ADDRESS
'for the BT GPS, NOT the Address for the Wintec BT Module
'connceted to the Nemesis PIC.
'The BT GPS Addr is displayed during DISCOVERY.
'It can also be determined by running the BT Module from
'a PC, see the article.
'Note: Set the two Mode Configuration Jumpers to position B
'to download this program to the Nemesis PIC.
'Be sure to set the jumpers to position C, the operational
'mode, to then run the program !!!!!!!!!!
'This version is functional, however further error handling
'could certainly be added, esp for handling multiple BT
'devices in the vicinity.
'This has Usr feedback to LCD during BT Connection process.
'Hardware Notes:
'Nemesis PIC (PIC16F88) & Wintec Blue Tooth Module, (BTM)
'Wintec BTM ships with 115,200 as its default baudrate.
'MUST first connect BTM to PC, and use Hyperterminal program
'to reset the default baudrate to 9600, which is used by the
'Nemesis PIC. See article, and MODE headers on board.
'Note: BTM uses 3.3 V, NOT 5 Volts!
'Note: Nemesis & BTM can both connect to a PC via the on board
'RS-232 serial comm interface.
'Note: Crystal Fontz 2 line x 16 Chars/line LCD is driven
'by the Nemesis, pins are NOT the default, and must be defined.
'NOTE WELL: Reading UART Buffer via Assem works well.
'Using getpacket is challenging.
'Using serin is challenging.
'Hardware Definition:
'Nemesis Port: Mode Function:
' Port 0 L LCD D4
' 1 L LCD D5
' 2 L LCD D6
' 3 L LCD D7
' 4 O TxData to PC for Programming, Debug
' 5 L LCD E
' 6 O LED: Orange, 1 = On
' 7 O Nem to BTM Serial Comm, 9600 (N81)
' 8 I Push Button Switch 2 Floats High, Pushed = Low
' 9 O BTM Controlled Reset, Active LOW for 5 mSec
' 10 L LCD RS
' 11 O LED: Blue, 1 = ON
' 12 I Nem RxData from PC for Programming, or BTM to run
' 13 I N/C Spare Pad on board
' 14 I N/C Spare Pad on board
Begin:
'Set up some variables to read in serial data sent from the BTM :
dim m, n, w, x, y, z, ct
'Set up pseudo one dimentional data array
dim DA, DB, DC, DD, DE, DF, DG, DH, DI, DJ, DK, DL, DM, DN, DO, DP, DQ, DR, DS, DT, DU, DV, DW, DX, DY, DZ
dim Buff, RdChar 'Buffer and Read in Char from Buffer
'Init LCD
'These are MOSTLY the default values:
const LCDD4 0
const LCDD5 1
const LCDD6 2
const LCDD7 3
const LCDRS 10 'was 4
const LCDE 5
const TxD 7 'TxData Port Nemesis to BTM, serial 9600, N81
const RxD 12 'TxData Port BTM to Nemesis, with USART, 80 char Rx Buffer
const CR 13 'CR for serial commands to BTM, 0D h, 13 dec
const LF 10 'LF, 0Ah, 10 dec
const Rst 9 'BTM Reset, Active LOW for 5 mSec minimum
const PBS 8 'Nem Port 8 is Frt Panel Push Button switch, Pushed = LOW
configio 4, 6, 7, 9, 11 'Define Output Ports, all others are Input by Default
lcdinit LCDRS,LCDE,LCDD4,LCDD5,LCDD6,LCDD7 'LCD Setup
high Rst 'BTM Reset to Active Mode
setbaud SBAUD9600 'Set Nemesis Baud Rate (To BT Module)
Main:
gosub LCDMSG 'Start-Up Message on LCD
gosub FlashLEDs 'Start Up, (2 Sec)
longpause 250, 4 '3 Sec Start Up system stabilization
gosub InitLink 'Initialize, (Set Up), a Bluetooth Data Connection
lcdcontrol 1 'Cls, Home
pause 2
lcdwrite "Successful Link"
longpause 250, 4 'To read the Msg
gosub EmptyBuf
gosub Streaming 'Display streaming GPS data on LCD, NON-Formatted
end
InitLink:
'Reset the BTM, and Initialize (set up) a BT link to it
'Display BT Module responses on LCD
'Issue a Hardware Reset to BT Module, read response
gosub HardReset
'Check BT Module's Firmware Version
gosub EmptyBuf 'Flush Nemesis USART Buffer of any BTM data
gosub Vers 'Send Version Command
'Issue a Software Reset to the BT Module
'(Optional after above hardware reset...)
gosub EmptyBuf 'Flush Nemesis USART Buffer of any BTM data
gosub SoftReset
'Issue Discovery Command to BT Module, read response(s)
gosub EmptyBuf 'Flush Nemesis USART Buffer of any BTM data
gosub Discovery
'Now Issue Bond to BT GPS Unit, already knowing its ID code
gosub EmptyBuf 'Flush Nemesis USART Buffer of any BTM data
gosub BondGPS
'Now Issue SPP Connect Command, Start Bi-Directional Data Flow
gosub SPPConn
return
HardReset:
'Issue a Hardware Reset to the BT Module, Read it's response
'Do a BTM HARDWARE Reset >>> DO THIS FIRST <<<
lcdcontrol 1 'Cls, Home
pause 2
lcdwrite "Hardware Reset"
longpause 250, 4 'Pause to read LCD Msg
low Rst 'HARDWARE RESET BTM This IS Necessary, PwrUp
pause 10 'Min 5 mSec
high Rst
'Get Power Up BTM Response (CommandMode and BDaddr....)
gosub GetBTMR 'Get Response
gosub DispResp '"AT-ZV -CommandMode-"
longpause 250, 6
lcdcontrol 1 'Cls, Home, First line, First Char position
pause 2
gosub GetBTMR 'Get Response
gosub DispResp '"BDAddress 000f70102085" (or its BT Address...)
longpause 250, 6
return
Discovery:
'Issue Discovery Command, see what BT Devices are out there
lcdcontrol 1 'Cls, Home
pause 2
lcdwrite "Issue Discovery"
longpause 250, 4
gosub DiscoveryBTM 'Send Discovery Command
longpause 250, 2
'Get Discovery BTM Response
lcdcontrol 1 'Cls, Home
pause 2
gosub GetBTMR 'First Response:"AT-ZV InqPending"
gosub DispResp
ct = 10 'Wait up to 10 Sec for Discovery Process
gosub CountDown 'LCD Countdown timer
gosub GetBTMR 'Response:"AT-ZV DiscoveryPending X"
'( X = number of devices found, 0, 1, ...10)
gosub DispResp
ct = 10 'Wait up to 10 Sec for Discovery Process
gosub CountDown 'LCD Countdown timer
'Now can have 0 to 10 devices Discovered.
'Devices report back: Device 00081b0ca81b "BT GPS" NoSvcs (or it's actual services...)
'Note: Could have Scrolling with User pushbuttons, and user Selects Device to Bond to...
for w = 1 to 10 'Up to 10 BT Devices can be reoported
gosub GetBTMR 'Get a single response, or empty buffer
if DA = ' ' then
'Empty Buffer, wait a bit and try again
longpause 250, 2 '1/2 Sec
else
'Have a response
lcdcontrol 1 'CLS, Home
pause 2
lcdwrite "Device ", dec w, ":"
longpause 250, 2
gosub DispResp 'Display response on LCD
longpause 250, 8 'Pause 2 Sec to read it
endif
next w
return
BondGPS:
'Issue a BOND Command to the BT Module
'>>> MUST <<< use your own BT GPS's BT Address
'This is hard coded, it doesn't scroll to it, then user selects it.
'Three possible responses: BondPending XXX, BondOk, OR BondFail
lcdcontrol 1 'Cls, Home
pause 2
lcdwrite "Issue Bond GPS"
longpause 250, 2
gosub BondBTM 'Send Bond Command
for w = 1 to 10 'Loop checking for Bond Responses, with a small delay
gosub GetBTMR 'Get a single response, or empty buffer
if DA = ' ' then
'Empty Buffer, wait a bit and try again
'longpause 250, 2 '1/2 Sec pause is at end of routine
else
'Have a response
lcdcontrol 1 'CLS, Home
pause 2
gosub DispResp 'Display response on LCD
longpause 250, 8 '2 Sec to read it
endif
if DE = 'F' then
'BondFail Response
goto BondGPS 'Try again
endif
if DF = 'k' then
'BondOk Response, move on, don't wait any more
goto BondExit
endif
longpause 250, 1 'Wait a bit, try again
next w
BondExit:
return
SPPConn:
'Now Issue Connect using SPP Protocol
lcdcontrol 1 'Cls, Home
pause 2
lcdwrite "Issue SPP Con"
longpause 250, 2 'Pause to read LCD
gosub SPPConnectBTM 'Issue SPP Connect Command
for w = 1 to 10 'Loop checking for SPP Connection Response, with a small delay
gosub GetBTMR 'Get a single response, or empty buffer
if DA = ' ' then
'Empty Buffer, wait a bit and try again
'longpause 250, 2 '1/2 Sec pause is at end of routine
else
'Have a response
lcdcontrol 1 'CLS, Home
pause 2
gosub DispResp 'Display response on LCD
longpause 250, 8 '2 Sec to read it
if DA <> 'C' then
'SPP Connection Failed
goto SPPConn 'Try again
else
goto SPPExit 'Are Connected
endif
endif
longpause 250, 1 'Wait a bit, try again
next w
SPPExit:
return
ResetBTM:
'Send a RESET Command to the BTM
high TxD
serout TxD, "AT+ZV Reset", CR
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -