⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nembtconnect.txt

📁 Bluetooth to display GPS data with PIC16F
💻 TXT
📖 第 1 页 / 共 2 页
字号:

DiscoveryBTM:
  'Send Command for BTM to Issue Discovery Command
  high TxD
  serout TxD, "AT+ZV Discovery", CR
  return 

BondBTM:
  '+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  '+*+*  NOTE: CHANGE the UNIQUE BT ADDRESS BELOW to that for 
  '+*+*  your own BT GPS Unit.
  '+*+*  The last four 0's are the PIN CODE, which also may be 
  '+*+*  different for your own BT GPS Unit.
  '+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  '
  'Send Command to BOND to BTGPS Unit
  'Need to knows its BT Address, and PIN code
  'Note: Can do Discovery to get it, or hard code for now...
  high TxD
  serout TxD, "AT+ZV BOND 00081b0ca81b 0000", CR
  return    
  
SPPConnectBTM:
  '+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  '+*+*  NOTE: CHANGE the UNIQUE BT ADDRESS BELOW to that for 
  '+*+*  your own BT GPS Unit.
  '+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*+*
  '
  'Send Command to BTM to perform SPPConnect
  'After Bonded, this sets up serial data flow, bi-directional
  'Skip sending SERVICE, as BTGPS has none
  high TxD
  serout TxD, "AT+ZV SPPConnect 00081b0ca81b", CR
  return    

GetBTMR:
  'Get BT Module Response, read in from Nemesis USART
  'This looks for a valid AT-ZV_xxx formatted response.
  'This version has a Time-Out, doesn't await data if there is none.
  'Used as some BT commands can have highly variable length responses.
  'Returns with the first AT-ZV_xxxx response in the buffer.
  'Returns if the buffer is empty.
  'Returns the response into the data array DA, DB, DC...
  'This version doesn't await partial responses to fill, it assumes they
  'are either error if < AT-ZV_, or done if greater than that received.
  'Uses x          Note: _ is actually a single SPACE character
  
  gosub NullArray             'Clear the Data Array for new response

GetLp:
  gosub GetChar2              'Get USART Char, if anything is in buffer
  if Buff = 0 then
    goto GetExit              'Buffer is empty, just return
  endif
  if RdChar <> 'A' then
    goto GetLp                'Keep looking for leading A, or empty buffer
  endif

  gosub GetChar2
  if Buff = 0 then
    goto GetExit              'Buffer is empty, just return, no Header found
  endif
  if RdChar <> 'T' then 
    goto GetLp                'Keep looking for Header, or Empty bufffer
  endif

  for x = 1 to 4              'Read in -ZV_, or hit empty buffer
    gosub GetChar2
    if Buff = 0 then
      goto GetExit            'Buffer is empty, just return, no Header found
    endif
  next x

  'If get here, have found a BT Module Response Header, AT-ZV_
  'Now read in the actual content of the response, to DA, DB, ...

  for x = 0 to 25    'Read in variable length response
    gosub GetChar2
    if Buff = 0 then
      goto GetExit                'Hit Empty Buffer, without actual CRLF...
    endif
    if RdChar = 13 then           'CRLF End Of Response
      goto GetExit                'Found EOT
    endif
    arrayset DA, x, RdChar        'Store in DA, DB, DC, DD... DZ
  next x

GetExit:
  return

GetChar:
  'Read in a single character from the USART Input Buffer in Nemesis
  'Reading in with Assem Lang on input buffer works well.  
  'On Exit: Buff = 0 : No char in buffer; Buff = 1 : Have a char in RdChar
  'This version awaits input in the buffer before returning
  startasm
    checkbuff Buff, RdChar   'read input buffer
    while Buff = 0
      checkbuff, Buff, RdChar
    wend
  endasm
  return

EmptyBuf:
  'Read in UART Input Buffer, Empty it
  'Buff = 0 = Empty flag
  longpause 250, 2            'Pause a bit to complete any incoming data
  startasm
    checkbuff, Buff, RdChar   'Read input buffer
    while Buff = 1
      checkbuff, Buff, RdChar
    wend
  endasm
  return

GetChar2:
  'Read in a single character from the USART input buffer in Nemesis
  'On Exit: Buff = 0 : No char in buffer; Buff = 1 : Have a char in RdChar
  'If buffer is empty, this version returns, it doesn't await data.
  startasm
    checkbuff Buff, RdChar   'read input buffer
  endasm
  return

NullArray:
  'Null (Erase) the Data Array, DA, DB, DC...
  'Used to hold Uploaded data from BT Module, load with Spaces
  for z = 0 to 25
   arrayset DA, z, ' '        
  next z
  return

DispResp: 
  'Display the BTM Response on the LCD, 2 Line, 16 Char / Line
  'Uses m, n, and Data Array (DA, DB, DC...)

    lcdcontrol 1          'Cls, Home
    pause 2
    for m = 0 to 15       'Length of LCD Line...
      arrayget DA, m, n   'Stored in DA, DB...
      lcdwrite n
    next m
    lcdcontrol 192   'LCD Line 2
    pause 2
    for m = 16 to 25      'Rest of data array, through DZ
      arrayget DA, m, n   'Stored in DA, DB...
      lcdwrite n
    next m
    for m = 1 to 6        'Overwrite end of LCD Line 2 with spaces
     lcdwrite " "
    next m
    return

CountDown:
    'Line 2 on LCD, Display a timed countdown while awaiting other 
    'things to happen, gives user feedback, it is not locked up.
    'On Entry have:  ct, the number of Seconds to count down from.
    'Uses x, ct
    for x = ct to 0 step -1
     lcdcontrol 200         'LCD Line 2 middle
     pause 2 
     lcdwrite "    "
     lcdcontrol 200         'LCD Line 2 middle
     pause 2
     lcdwrite dec x
     longpause 250, 4       '1 Second
    next x
    lcdcontrol 200          'LCD Line 2 middle
    pause 2
    lcdwrite "    "
    return    

LCDMSG:
  'LCD Start Up Message
    lcdcontrol 1            'Cls, Home
    pause 2
    lcdwrite ">> Bluetooth  <<"
    lcdcontrol 192          'Start of Second Line
    lcdwrite "Connection  Demo"
  return  

'DisconnectBTM:
  'Send Command to BTM to perform SPPDisconnect 
  'Note: Are not currently using this ... 
'  high TxD
'  serout TxD, "AT+ZV SPPDisconnect", CR
'  return 

Vers:
   'Query BTM Version, get single packet response
   'If first response is not correct, see if there is a valid
   'response in the buffer, before re-issuing the command.
   'Could have other data in buffer...

   lcdcontrol 1    'Cls, Home, First line, First Char position
   pause 2
   lcdwrite "Get BTM Version"
   longpause 250, 1
   gosub VersionBTM          'Issue the Command to the BT Module
   longpause 250, 4          'Time for BTM to respond with it's data
VLp:
   gosub GetBTMR             'Get Response:  "AT-ZV ZerialVer 6.1"
   gosub DispResp
   longpause 250, 1

   if DA = ' ' then
      lcdcontrol 1    'Cls, Home
      pause 2
      lcdwrite "Re-issue Version"
      longpause 250, 2
      goto Vers              'Send Command again, buffer was empty
   endif

   if DA <> 'Z' then
     goto VLp                'Not desired response, but had a response in the buffer
                             'See if another, desired, response is in the buffer
                             'without re-issuing the command.
   else
     longpause 250, 8        'Read Version #
   endif
   return
 
VersionBTM:                                
  'Query for BTM Version #                
  high TxD
  serout TxD, "AT+ZV Version", CR
  return   

SoftReset:  
   'Reset BTM  (Optional, but good to watch, and to read its own ID) 
   'Could add incremental error checking, if desired...
   lcdcontrol 1    'Cls, Home
   pause 2
   lcdwrite "Issue Soft Reset"
   longpause 250, 4
   gosub ResetBTM             'Send Software RESET BTM Command
   longpause 250, 8           ' Tweak later
   gosub GetBTMR              'Get Response: "AT-ZV ResetPending" 
   gosub DispResp
   longpause 250, 4
   gosub GetBTMR              '"AT-ZV -CommandMode-" 
   gosub DispResp
   longpause 250, 4
   gosub GetBTMR              ' Addr of this BT Module
   gosub DispResp             '"AT-ZV BDAddress 000f70...   "
   longpause 250, 4
   return

Streaming:
  'Have Linked to BT GPS
  'Display the Streaming data on LCD, don't parse, process, format
  'Primative, doesn't handle short packets right now, but conveys the point
  lcdcontrol 1              'Cls, Home
  pause 2
  lcdwrite "Streaming Data: "
  longpause 250, 4
  lcdcontrol 1              'Cls, Home
  pause 2
StrLp:
  gosub GetChar
  if RdChar = '$' then
    'Found start of new packet, simplified parser
    lcdcontrol 1            'Cls, Home
    pause 2
    lcdwrite "$"            'The start of the packet...
    for m = 1 to 16
      gosub GetChar
      lcdwrite RdChar
    next m
    lcdcontrol 192          'Line 2
    for m = 1 to 16
      gosub GetChar
      lcdwrite RdChar
    next m
    longpause 100, 2        'Pause long enough to see Packet Header name
  else
    goto StrLp              'Look for SOT
  endif 
  goto StrLp
  return                    'null return 

FlashLEDs:
  for x = 1 to 10
    high 11              'LED Blue
    low 6                'LED Orange
    pause 100
    low 11
    high 6
    pause 100
  next
  low 6
  return


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -