;;;;;;; PBtest for Mengjin Su's PIC2840A Prototype Board ;;;;;;;;;;;;;;;;;;;;;;; ; ; Use Timer2 for ten millisecond looptime (actually 9.9904 ms) ; Blink "Alive" LED connected to RA4 every two and a half seconds. ; ;;;;;;; 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 rcall BlinkAlive ;Blink "Alive" LED rcall 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'00000001',TRISA ;Set I/O for PORTA MOVLF B'11000000',TRISB ;Set I/O for PORTB MOVLF B'11000000',TRISC ;Set I/O for PORTC MOVLF B'00000000',TRISD ;Set I/O for PORTD MOVLF B'00000000',TRISE ;Set I/O for PORTE MOVLF 222,PR2 ;Set up Timer2 as per Figure 16-3b MOVLF B'00110110',T2CON ; with A=16, B=223, C=7 bsf PORTA,RA4 ;Turn off LED initially return ;;;;;;; BlinkAlive subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; This subroutine briefly blinks the LED next to the PIC every two-and-a-half ; seconds. BlinkAlive bsf PORTA,RA4 ;Turn off LED decf ALIVECNT,F ;Decrement loop counter and return if not zero bnz BAend MOVLF 250,ALIVECNT ;Reinitialize ALIVECNT bcf PORTA,RA4 ;Turn on LED for ten milliseconds every 2.5 sec BAend return ;;;;;;; LoopTime subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; This subroutine waits forthe TMR2IF flag to indicate that 16x223x7 = 24976 ; internal clock cycles have passed since the last time that this flag was set. ; This gives a loop time of slightly less (0.1%) than ten milliseconds. LoopTime btfss PIR1,TMR2IF ;Wait until ten milliseconds are up bra LoopTime bcf PIR1,TMR2IF ;Clear flag return end