红外线发射与接收源程序,做智能小车必备啊。。
![none.gif](static/image/common/none.gif)
红外线发射与接收一例:
- include<p=16f877.inc>
-
- ;歌林电子制作实验室www.nbglin.com
- ;TEL:0574-88464538 email:nbglin@126.com
- ;------------------
- STATUS equ 0X03
- PORTA equ 0x05
- PORTB equ 0x06
- W equ 0x00
- F equ 0x01
- Z equ 0x02
- C equ 0x00
- ;------------------
- n55ks equ 0x0C ;
- nchks equ 0x0D ;
- avgIR equ 0x0E ;
- nsamp equ 0x0F ;
- tdetect equ 0x10 ;
- ;------------------------------------
- org 0x000
- goto start
- org 0x004
- start
-
- movlw b'00000001'
- tris PORTA ; RA0 红外输入
- movlw b'00000000'
- tris PORTB ;RB0 红外输出
- ; 2us in goto at end of loop
- blip call doblip ; +6.248ms = 6.250ms total
- call seeblip ; 6.250ms total
- call seeblip ; 6.250ms total
- call seeblip ; 6.250ms total
- call seeblip ; 6.250ms total
- call seeblip ; 6.250ms total
- call seeblip ; 6.250ms total
- call seeblip ; 6.250ms total
- nop
- nop
- call doblip
- call seeblip
- call seeblip
- call seeblip
- call seeblip
- call seeblip
- call seeblip
- call seeblip
- goto blip ;
-
- ;============ 产生 55.5kHz锯形波(346*18us=6.228ms)======
- doblip
- movlw 0xFF
- movwf n55ks
- osc nop
- call make55k ;RB0 红外输出
- decfsz n55ks,F
- goto osc
- ;--------------
- morblip ;6253us DELAY
- movlw 0x5B
- movwf n55ks
- oscmore call make55k ;RB0 红外输出
- nop
- decfsz n55ks,F
- goto oscmore
- ;-------------
- call delay8u
- nop
- nop
- nop
- nop
- nop
- bcf PORTB,2 ; RB2 LED指示
- return
-
- ;---------------------------
- make55k bsf PORTB,0 ;RB0 红外输出
- call delay8u
- bcf PORTB,0
- return
- delay8u nop
- nop
- nop
- nop
- return
-
- ;========= 6.250ms ===================
- seeblip
- clrf avgIR
- movlw 0xF9
- movwf nchks
- chk call chkIR ;//
- decfsz nchks,F
- goto chk
-
- movf avgIR,W ;
- sublw 0X78
- btfss STATUS,C
- goto detectd
- bcf PORTB,0 ;RB0 红外输出 OFF
- goto chkdone
- detectd bsf PORTB,0
- nop
- chkdone
- call delay8u
- nop
- nop
- nop
- nop
- return
-
- ;========================
- chkIR
- clrf nsamp
- btfss PORTA,0
- incf nsamp,F
- btfss PORTA,0 ; RA0 红外输入
- incf nsamp,F
- btfss PORTA,0 ; RA0 红外输入计数值存到nsamp
- incf nsamp,F
- btfss PORTA,0
- incf nsamp,F
- btfss PORTA,0
- incf nsamp,F
- btfss PORTA,0 ;共六次 nsamp》5,avgIR增一
- incf nsamp,F
- nop
- movf nsamp,W
- sublw 0X04
- btfss STATUS,C
- incf avgIR,F
- return
-
- ;============================
- end
复制代码
|
文件到原文下载,原文出自:https://bbs.usoftchina.com/thread-208673-1-1.html