📄 irserout.bas
字号:
' Title IRSerout.Bas
' @ L. Johnson 20th September 1999
'
' Send Asyncronous Serial Data over an Infra-Red Link
' At Various Baud Rates and Inverted Mode
' An Infra-Red LED should be Connected to PORTB.0
' If longer range is required and NPN transistor could be employed
' as an amplifier, as in the BASIC STAMP1 Application Notes
' Place Include "Irserout.Bas"
' At the front of the Program to load the new subroutine in
'
' To Use the New Routine, Load the Variable:-
' IR_ByteOut with the appropriate value (0-255)
' And Call the Subroutine:-
' Gosub IRSerout
'
' Three New Defines have been added:-
' IRSEROUT_PORT.....Selects the Port for sending (ie PORTB)
' IRSEROUT_PIN......Selects the Pin for sending (ie 1)
' IRSEROUT_BAUD.....Selects the Baud Rate (ie 300,600,1200,2400)
' IRSerout works with 4Mhz, 8Mhz, 10Mhz and 12Mhz Xtals
' *** The Subroutine is only 56 Bytes in length ***
' This routine is for the 16F84, but any 14bit core PIC could be used
' Define Variables
Dlctr Var Byte ' Counter For Serial Delay Routines
Bctr Var Byte ' Number Of Transmitted Bits
Tr_B Var Byte ' Variable Used In Transmitter
Ir_Byteout Var Byte ' Value To Be Send Out
IR_Bit Var Bit ' Pulse ON or OFF
Dl_Temp Var Byte ' Counter2 For Serial Delay Routines
ON_Delay Var DL_Temp
Off_Delay Var DL_Temp
' ******************************
' ** Jump Over the Subroutine **
' ******************************
Goto Over_IRSEROUT
IRSerout:
Asm
#Define IR_LED IRSEROUT_PORT , IRSEROUT_PIN ;' Get the Port and Pin from the two new Defines
;' Set Defaults for IRSEROUT_PORT and IRSEROUT_PIN to PORTA BIT 0
Ifndef IRSEROUT_PORT
IRSEROUT_PORT = PortA
endif
Ifndef IRSEROUT_PIN
IRSEROUT_PIN = 0
endif
;' Set Default to 1200 Baud Inverted
Ifndef IRSEROUT_BAUD
IRSEROUT_BAUD = 1200
endif
Bcf IRSEROUT_PORT,IRSEROUT_PIN ;' Clear the appropriate pin
Bsf Status,5 ;' Set to Bank1
Bcf IRSEROUT_PORT,IRSEROUT_PIN ;' Make the appropriate pin an Output
Bcf Status,5 ;' Back to Page 0
;' Send out the Data serially at Various bauds, Inverted
Mov w,_Ir_Byteout ;' Move the Value into the W register
Clrwdt ;' Walk the Dog
Movwf _TR_B
Movlw 08
Movwf _Bctr ;' Eight Bits In A Byte!.
Bsf _IR_Bit
Call Send_Bit ;' Send the Start Bit at Approx 39Khz.
SIRXmt Rrf _Tr_B ;' Rotate Right Moves the Data Bits Into the Carry
;' Starting With Bit 0.
Btfsc Status,0 ;' Test the Carry Flag
Bcf _IR_Bit ;' Send a Zero
Btfss Status,0 ;' Test the Carry Flag
Bsf _IR_Bit ;' Send A One
Call Send_Bit ;' Send the Data Bit at Approx 39 Khz.
Decfsz _Bctr ;' Not Eight Bits Yet? Send the Next Data Bit
Goto SIRXmt
Bcf _IR_Bit ;' Set LO
Call Send_Bit ;' Send the stop bit at Approx 39khz
;' Delay for X amount of time
Clrwdt ;' Walk the Dog
Movlw 255
Movwf _DlCtr
IRSInner Clrwdt ;' Walk the Dog
Nop
Nop
Nop
Nop
Nop
Nop
Decfsz _DlCtr ;' Do the Inner Loop
Goto IRSInner
Return
;' This Routine Sends a Pulse modulated at Approx 39Khz
;' Out of the Appropriate pin within a Delay of 1 bit time
Send_Bit
;' Assemble this code if the Baud rate is 300
If (IRSEROUT_BAUD == 300)
Movlw 128 ;' Set the Delay Time for 300 Baud
endif
;' Assemble this code if the Baud rate is 600
If (IRSEROUT_BAUD == 600)
Movlw 64 ;' Set the Delay Time for 600 Baud
endif
;' Assemble this code if the Baud rate is 1200
If (IRSEROUT_BAUD == 1200)
Movlw 32 ;' Set the Delay Time for 1200 Baud
endif
;' Assemble this code if the Baud rate is 2400
If (IRSEROUT_BAUD == 2400)
Movlw 16 ;' Set the Delay Time for 2400 Baud
endif
Pulse_Bit Movwf _Dlctr ;' Store it
Irlp1 Clrwdt ;' Walk The Dog
Btfsc _Ir_Bit ;' If Ir_Bit=1 Then
Bsf IR_LED ;' Turn the IR_Led On
Btfss _Ir_Bit ;' If Ir_Bit=0 Then
Bcf IR_LED ;' Turn the IR_Led Off
;' Assemble this code if the Xtal is 4Mhz
if (OSC == 4)
Movlw 3
Movwf _On_Delay
Inner1 Decfsz _On_Delay
Goto Inner1
endif
;' Assemble this code if the Xtal is 8Mhz
if (OSC == 8)
Movlw 8
Movwf _On_Delay
Inner1 Decfsz _On_Delay
Goto Inner1
endif
;' Assemble this code if the Xtal is 10Mhz
if (OSC == 10)
Movlw 12 ;' *****Not Sure Untested ***
Movwf _On_Delay
Inner1 Decfsz _On_Delay
Goto Inner1
endif
;' Assemble this code if the Xtal is 12Mhz
if (OSC == 12)
Movlw 17
Movwf _On_Delay
Inner1 Decfsz _On_Delay
Goto Inner1
endif
Bcf IR_LED ;' Turn the IR_LED Off
;' Assemble this code if the Xtal is 4Mhz
if (OSC == 4)
Nop
Nop
Nop
Nop
Nop
Nop
endif
;' Assemble this code if the Xtal is 8Mhz
if (OSC == 8)
Movlw 5
Movwf _Off_Delay
Inner2 Decfsz _Off_Delay
Goto Inner2
endif
;' Assemble this code if the Xtal is 10Mhz
if (OSC == 10)
Movlw 5
Movwf _Off_Delay
Inner2 Decfsz _Off_Delay
Goto Inner2
endif
;' Assemble this code if the Xtal is 12Mhz
if (OSC == 12)
Movlw 5
Movwf _Off_Delay
Inner2 Decfsz _Off_Delay
Goto Inner2
endif
Decfsz _Dlctr
Goto Irlp1
Return
Endasm
Over_IRSEROUT:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -