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

📄 usart_rx_irda2.pbas

📁 irda 2 example implementation with microcontroller
💻 PBAS
字号:
'*******************************************************************************
' Microcontroller:
'   P16F877A
'
' Project name:
'   USART_Rx_IrDA2 (Receive-side code for IrDA2 module test)
'
' Done by:
'   Vladimir Petrovic
'
' Description:
'   This project is designed to work with PIC 16F877A; with minor adjustments,
'   it should work with any other PIC MCU that has the USART device.
'
'   In this example a IR wirless connection between two PIC MCUs is demonstrated.
'   For this purpose, the mikroElektronika's IrDA2 board with Microchip's MCP2120
'   IrDA decoder /encoder device is used. One MCU acts as sender, the other one
'   beging the receiver device.
'
'   Please note that MCP2120 is only an decoder /encoder device which is
'   IrDA-compatible on hardware level. Therefore, the IrDA2 module can only be
'   used for an inter-connection between two MCUs and NOT with IrDA port on
'   the PC (for this purpose, the IrDA module with MCP2155 chip can be used).
'
'   Take care about the baud rate settings for the IrDA2 module! In this example,
'   a 3.6864 MHz crystal oscillator is used on IrDA2 module, and the baudrate is
'   set for 19200 Bps with jumpers B0 and B2 connected, and jumper B1
'   disconnected.
'
' Test configuration:
'   P16F877A     @ 04.0000MHz
'   IrDA2 module @ 3.6864MHz
'*******************************************************************************

program USART_Rx_IrDA2

dim inTxt as char[17]
dim ind as byte
dim rxIndex as byte
dim rxDone as byte
dim rxData as byte


' Global init
sub procedure initAll
  ind = 0                                            ' Init flags & indexes
  rxIndex = 1
  rxDone = 0
  TRISB = 0
  Lcd_Init(PORTB)                                    ' Init LCD
  Lcd_Cmd(LCD_CLEAR)
  Lcd_Cmd(LCD_CURSOR_OFF)
  USART_init(19200)                                  ' Init USART
end sub

' Performs input from USART to inTxt array
sub procedure rxIn
  rxData = 0
  rxIndex = 1
  rxDone = 0
  inTxt = "                "                         ' Clear inTxt[]
  if USART_Data_Ready=1 then                         ' Remove possible garbage
    rxData = USART_Read                              '  from Rx buffer
  end if
  while rxDone=0
    if USART_Data_Ready=1 then
      rxData = USART_Read
      inTxt[rxIndex] = rxData
      if (rxData=64) or (rxIndex>15) then           ' ASC(64) = '@' is used
        rxDone = 1                                  '  as termination string
      end if
      inc(rxIndex)
    end if
  wend
  dec(rxIndex)
end sub

' Updates LCD to see latest text received
sub procedure updateLcd_Rx
  LCD_Out(1,1,"Received:")
  if rxDone=1 then                                  ' If data receive was
    LCD_Out(2,1,inTxt)                              '  successful, put it on
    rxDone = 0                                      '  LCD
  end if
end sub


' *****************************************************************************
main:
  initAll

  while true
    rxIn
    updateLcd_Rx
  wend
end.

⌨️ 快捷键说明

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