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

📄 红外应用源程序集4.txt

📁 单片机开发源代码集锦 31例
💻 TXT
字号:
 
  
;TITLE 'Infra Red Proximity Detector - uses Sharp 38 or 40KHz IRM
;www.nbglin.com 电话:0574-88464538
;歌林电子制作工作室

LIST P = 12C508, F = INHX8M 
IFNDEF __12C508
MESSG "Processor-header file mismatch. Verify selected processor."
ENDIF
W EQU H'0000'
F EQU H'0001'
;----- Register Files -------------------------------
INDF EQU H'0000' 
TMR0 EQU H'0001' 
PCL EQU H'0002' 
STATUS EQU H'0003' 
FSR EQU H'0004' 
OSCCAL EQU H'0005' 
GPIO EQU H'0006' 
;----- STATUS Bits -----------Page 14-------------
GPWUF EQU H'0007' ; GPIO reset bit
PA0 EQU H'0005' 
NOT_TO EQU H'0004' 
NOT_PD EQU H'0003' 
ZERO EQU H'0002' 
DC EQU H'0001' 
CARRY EQU H'0000' 
;----- OPTION Bits -----------Page 15-----------
NOT_GPWU EQU H'0007' 
NOT_GPPU EQU H'0006' 
T0CS EQU H'0005' 
T0SE EQU H'0004' 
PSA EQU H'0003' 
PS2 EQU H'0002' 
PS1 EQU H'0001' 
PS0 EQU H'0000' 
;=============================================
_MCLRE_ON EQU H'0FFF' 
_MCLRE_OFF EQU H'0FEF' 
_CP_ON EQU H'0FF7' 
_CP_OFF EQU H'0FFF' 
_WDT_ON EQU H'0FFF' 
_WDT_OFF EQU H'0FFB' 
_LP_OSC EQU H'0FFC' 
_XT_OSC EQU H'0FFD' 
_IntRC_OSC EQU H'0FFE' 
_ExtRC_OSC EQU H'0FFF' 
__CONFIG ( _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC )
;==========================================================================

#define LEFTDETECT GPIO,5 ; Pin 2 = GP5 = Bit 5
#define RIGHTDETECT GPIO,4 ; Pin 3 = GP4 = Bit 4
#define INHIBIT GPIO,3 ; Pin 4 = GP3 = Bit 3 
#define RIGHTLED GPIO,2 ; Pin 5 = GP2 = Bit 2
#define IRDETECT GPIO,1 ; Pin 6 = GP1 = Bit 1
#define LEFTLED GPIO,0 ; Pin 7 = GP0 = Bit 0 
#define LLIMIT D'30' ; Lower limit of allowable hits in a window
#define LHITLIM D'5' ; Lower hits limit
#define UMISSLIM D'20' ; Upper miss limit

LIST
WAIT EQU 9 
FAKEHIT EQU D'14' 
LHITS EQU D'15' 
RHITS EQU D'16' 
FAKEHIST EQU D'17' 
SLHITS EQU D'11' 
SLMISS EQU D'13' 
SRHITS EQU D'18' 
SRMISS EQU D'19' 


ORG 0x1FF
start
ORG 0x000
MOVWF OSCCAL 
MOVLW B'11001010' ; 
TRIS GPIO 
MOVLW B'01000000' 
OPTION 
CLRF FAKEHIST 
CLRF SLHITS 
CLRF SLMISS 
CLRF SRHITS
CLRF SRMISS
MOVLW B'11001010' 
MOVWF GPIO

;***************************************************************************
; This version of my IR detector recognizes that there is background noise *
; out there that needs to be overcome to get good readings no matter what. *
; 工业机器人检测器 *
; It alternates readings of both the left and the right IR LEDs each of *
; them cycled at 40KHz for 2.5ms on and 2.5ms off, a 50% duty cycle. *
; During the 'off' cycle we will look for false hits, denoting noise in *
; our environment. It saves these noise readings, if subsequent readings *
; are higher, then the higher reading is saved, if lower, then 1/4 of the *
; newer reading will be subtracted from the history. So noise levels are *
; reduced faster than they are raised so a signal can still sneak in there.*
; Then the IR LED is flashed at 40KHz and a reading is taken on every *
; cycle. These readings are taken in the same cycle "time slots" as the *
; ones taken in the 'off' cycle. In each case, 100 cycles are sampled. *
; After a full on/off cycle, a reading is considered good if there are more*
; good detections than false hits AND there are at least 20 good hits. *
; A counter is incremented every time there is a good hit on any given *
; side, at that time, a miss counter is decremented. When there is a bad *
; cycle, the miss counter for that side is incremented and the hit counter *
; is decremented. When the threshhold for hits is passed, the detect is *
; indicated on that side and the miss counter is zeroed out. Conversely, *
; if the miss threshhold is exceeded (always larger than the hit one) then *
; a detect is disabled and the hit counter is zeroed. In this way you get *
; a sort of lock on a good signal that takes effort to lose. Also, noise *
; is rejected better and you have a more solid result. *
; *
; Sampling times, threshholds and limits were arrived at completely by hit*
; and miss. Eventually, I stopped fiddling and declared "good enough" *
; because the more I fiddled, the worse it seemed to get... *
; *
; Revision 2.0 2/16/98 Copyright Dennis Clark and TTT Ent. *
;***************************************************************************

noflash
BTFSC INHIBIT 
GOTO do_looks 
CLRF FAKEHIST 
CLRF SLHITS 
CLRF SLMISS 
CLRF SRHITS
CLRF SRMISS
MOVLW B'11001010' 
MOVWF GPIO
GOTO noflash
do_looks
call do_left 
call do_right 
goto noflash 

;================================
do_left
CALL delayloop 
CALL pulseleft 
MOVF FAKEHIST,W 
SUBWF LHITS,W 
BTFSS STATUS,CARRY 
GOTO nlgood 
MOVLW LLIMIT 
SUBWF LHITS,W 
BTFSS STATUS,CARRY 
GOTO nlgood ; 

clgood INCF SLHITS,F 
DECF SLMISS,F 
MOVLW LHITLIM 
SUBWF SLHITS,W 
BTFSS STATUS,CARRY 
GOTO lldone 

lgood BSF LEFTDETECT 
CLRF SLMISS 
GOTO lldone 

nlgood INCF SLMISS,F 
DECF SLHITS,F 
MOVLW UMISSLIM 
SUBWF SLMISS,W 
BTFSS STATUS,CARRY 
GOTO lldone 

no_left 
BCF LEFTDETECT 
CLRF SLHITS 
lldone retlw 0

do_right
CALL delayloop 
CALL pulseright 
MOVF FAKEHIST,W
SUBWF RHITS,W 
BTFSS STATUS,CARRY 
GOTO nrgood 
MOVLW LLIMIT 
SUBWF RHITS,W 
BTFSS STATUS,CARRY 
GOTO nrgood 

crgood INCF SRHITS,F 
DECF SRMISS,F 
MOVLW LHITLIM 
SUBWF SRHITS,W 
BTFSS STATUS,CARRY 
GOTO lrdone 

rgood BSF RIGHTDETECT 
CLRF SRMISS 
GOTO lrdone 

nrgood INCF SRMISS,F 
DECF SRHITS,F 
MOVLW UMISSLIM 
SUBWF SRMISS,W 
BTFSS STATUS,CARRY 
GOTO lrdone 

no_right 
BCF RIGHTDETECT 
CLRF SRHITS 
lrdone retlw 0

delayloop 
MOVLW D'100' 
MOVWF WAIT 
CLRF FAKEHIT 
loop BCF LEFTLED 
BCF RIGHTLED 
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP 
NOP 
NOP 
NOP 
NOP 
BTFSS IRDETECT 
INCF FAKEHIT,F 
NOP 
NOP 
DECFSZ WAIT,F 
goto loop 
MOVF FAKEHIST,W 
SUBWF FAKEHIT,W 
BTFSS STATUS,CARRY 
GOTO newless 
MOVF FAKEHIT,W 
MOVWF FAKEHIST 
GOTO bdone 
newless RRF FAKEHIT,F 
; RRF FAKEHIT,F
RRF FAKEHIT,W 
ANDLW 0x3f 
SUBWF FAKEHIST,F 
bdone RETLW 0 


pulseleft 
MOVLW D'100' 
MOVWF WAIT 
CLRF LHITS 
leloop BSF LEFTLED 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
BCF LEFTLED 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
BTFSS IRDETECT 
INCF LHITS,F 
NOP 
NOP 
DECFSZ WAIT,F 
GOTO leloop 
RETLW 0 

pulseright 
MOVLW D'100' 
MOVWF WAIT 
CLRF RHITS 
riloop BSF RIGHTLED 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
BCF RIGHTLED 
NOP 
NOP 
NOP 
NOP 
NOP 
NOP 
BTFSS IRDETECT 
INCF RHITS,F 
NOP 
NOP 
DECFSZ WAIT,F
GOTO riloop 
RETLW 0 

END

 

⌨️ 快捷键说明

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