📄 irtest.asm
字号:
; irtest.asm
; Simple PIC16F876 Program to test my IR emitter
; uses PWM to modulate LED output at 38KHz
; IR receiver PNA4602 will output logic '1' when
; it detects IR light. Since receiver probably
; can't source current to turn on an LED indicator,
; have PIC turn on indicator LED for it
; TMR2 prescalar = 1
; PR2 = 0x82
; CCPR1L:CCP1CON<5:4> = 0x106
; Include the header file for our target chip
#include "p16f876.inc"
; First line, declare that we want a 16F876 PIC
; and that our numbers will be in Hexadecimal
; unless otherwise noted
LIST P=16f876, R=HEX
; Tell the programmer what options we want to use
; In this case:
; _WDT_OFF -- Watchdog Timer Off
; _XT_OSC -- Crystal Oscillator
; _CP_OFF -- Code Protect Off
__CONFIG _WDT_OFF & _XT_OSC & _CP_OFF & _LVP_OFF
; Variable Declarations
; Here we assign memory locations for our variables
cblock 0x20
counter1
endc
cblock 0x70 ; global variables -- visible across all banks
StatusSave
WRegSave
endc
; First column is always for labels. Anything in the first
; column will be interpreted by the assembler as a label
org 0
goto Start
org 0x04 ; interrupt handler goes here
Interrupt:
; for now, the interrupt routine will just step the motor once
btfss INTCON, T0IF ; test for non-TMR0 interrupts
retfie ; and return on them
movwf WRegSave ; save accumulator
swapf STATUS, w
movwf StatusSave
bcf STATUS, RP0
decfsz counter1
goto int_done
movlw 0x10
movwf counter1
btfss CCP1CON, 3
goto int_e_pwm
call PWM_Disable
goto int_done
int_e_pwm:
call PWM_Enable
int_done:
swapf StatusSave, w
movwf STATUS
swapf WRegSave, f
swapf WRegSave, w
bcf INTCON, T0IF
retfie
Start
CLRF PORTB ; Initialize Port B
CLRW ; Clear W register
BSF STATUS,RP0 ; Select Register Bank 1
MOVWF TRISB ; Set B to all outputs
movlw b'11111111'
movwf TRISA ; set A to all inputs
movlw 0x06
movwf ADCON1 ; set porta to digital inputs
bcf OPTION_REG, T0CS
movlw b'00000111'
iorwf OPTION_REG, f
bcf OPTION_REG, 3
bsf INTCON, T0IE
bsf INTCON, GIE
bcf STATUS, RP0
clrf TMR0
movlw 0x10
movwf counter1
call SetupPWM ; start IR led blinking on RC2
call PWM_Enable
loop btfss CCP1CON, 4
goto loop
btfss PORTA, 0
bsf PORTB, 7
btfsc PORTA, 0
bcf PORTB, 7
goto loop
SetupPWM:
; initializes PWM to 38KHz for IR LED
bsf STATUS, RP0
movlw 0x82
movwf PR2
bcf STATUS, RP0
bsf CCP1CON, 4
bcf CCP1CON, 5
movlw b'01000000'
movwf CCPR1L
bsf STATUS, RP0
bcf TRISC, 2
bcf STATUS, RP0
movlw b'00000100'
movwf T2CON
return
PWM_Enable:
bsf CCP1CON, 3
bsf CCP1CON, 2
return
PWM_Disable:
bcf CCP1CON, 3
bcf CCP1CON, 2
return
END ; End of program
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -