📄 p1.asm
字号:
;;;;;;; P1 for QwikFlash board ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Toggle B0 output every ten milliseconds for measuring looptime with scope.
; Blink "Alive" LED every two seconds.
; Use Timer0 for a loop time of approximately ten milliseconds.
;
;;;;;;; Program hierarchy ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Mainline
; Initial
; BlinkAlive
; LoopTime
;
;;;;;;; Assembler directives ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
list P=PIC18F452, F=INHX32, C=160, N=0, ST=OFF, MM=OFF, R=DEC, X=ON
#include P18F452.inc
__CONFIG _CONFIG1H, _HS_OSC_1H ;HS oscillator
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_ON_2L & _BORV_42_2L ;Reset
__CONFIG _CONFIG2H, _WDT_OFF_2H ;Watchdog timer disabled
__CONFIG _CONFIG3H, _CCP2MX_ON_3H ;CCP2 to RC1 (rather than to RB3)
__CONFIG _CONFIG4L, _LVP_OFF_4L ;RB5 enabled for I/O
;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cblock 0x000 ;Beginning of Access RAM
ALIVECNT ;Counter for blinking "Alive" LED
endc
;;;;;;; Macro definitions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
MOVLF macro literal,dest
movlw literal
movwf dest
endm
;;;;;;; Vectors ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
org 0x0000 ;Reset vector
goto Mainline
org 0x0008 ;High priority interrupt vector
goto $ ;Trap
org 0x0018 ;Low priority interrupt vector
goto $ ;Trap
;;;;;;; Mainline program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Mainline
rcall Initial ;Initialize everything
Loop
btg PORTB,RB0 ;Toggle pin, to support measuring loop time
call BlinkAlive ;Blink "Alive" LED
call LoopTime ;Make looptime be ten milliseconds
bra Loop
;;;;;;; Initial subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This subroutine performs all initializations of variables and registers.
Initial
MOVLF B'10001110',ADCON1 ;Enable PORTA & PORTE digital I/O pins
MOVLF B'11100001',TRISA ;Set I/O for PORTA
MOVLF B'11001100',TRISB ;Set I/O for PORTB
MOVLF B'11010000',TRISC ;Set I/O for PORTC
MOVLF B'00001111',TRISD ;Set I/O for PORTD
MOVLF B'00000100',TRISE ;Set I/O for PORTE
MOVLF B'10001000',T0CON ;Set up Timer0 (10 millisecond looptime)
MOVLF B'00010000',PORTA ;Turn off all four LEDs driven from PORTA
return
;;;;;;; BlinkAlive subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This subroutine briefly blinks the LED next to the PIC every two seconds.
BlinkAlive
bsf PORTA,RA4 ;Turn off LED
decf ALIVECNT,F ;Decrement loop counter and return if not zero
bnz BAend
MOVLF 200,ALIVECNT ;Reinitialize BLNKCNT
bcf PORTA,RA4 ;Turn on LED for ten milliseconds every 2 sec
BAend
return
;;;;;;; LoopTime subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; This subroutine waits for TMR0 to roll over from 65535 to 00000 and to set the
; TMR0IF flag. Then it resets TMR0 to 65536-25000 = 40536 to make TMR0 count
; with a scale of about 25000.
LoopTime
btfss INTCON,TMR0IF ;Wait until ten milliseconds are up
bra LoopTime
bcf INTCON,TMR0IF ;Clear flag
MOVLF high 40536,TMR0H ;Skip over 40536 counts
MOVLF low 40536,TMR0L
return
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -