Sei sulla pagina 1di 7

A 1 ms delay subroutine

STATUS
Z
MAIN

equ
3
equ
2
call
DELAY_1MS
; Test stub calling up delay subroutine
sleep
; *****************************************************************
; * FUNCTION: Delays for 1ms with a 4MHz crystal
*
; * ENTRY : None
*
; * EXIT : Flags and W altered
*
; *****************************************************************
N
equ
d'249' ; Delay parameter computed in the text
DELAY_1MS
movlw N
; Set up loop
1~
; LOOP -----------------------------------------------------------D_LOOP
addlw -1
; Decrement count
N~
btfss STATUS,Z
; Check: Is it zero?
N+1~
goto D_LOOP
; IF not THEN repeat
2*(N-1)~
; ----------------------------------------------------------------return
end
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

DELAY_100MS
STATUS
Z
MAIN

equ
equ
call

3
2
DELAY_100MS ; Test stub calling up delay subroutine
sleep
; *****************************************************************
; * FUNCTION: Delays for nominally 100ms with a 4MHz crystal *
; * ENTRY : None
*
; * EXIT : Flags and W altered. File h'30' zero
*
; *****************************************************************
COUNT1
equ
h'30' ; Use File h'30' as a loop counter
N
equ
d'249' ; Delay parameter computed in the text
DELAY_100MS movlw d'100' ; Initialize outer loop count to 100
movwf COUNT1
; Outer loop -----------------------------------------------------DELAY_1MS
movlw N
; Set up loop
; Inner loop -----------------------------------------------------D_LOOP
addlw -1
; Decrement count
btfss STATUS,Z
; Check: Is it zero?
goto D_LOOP
; IF not THEN repeat
; ----------------------------------------------------------------decfsz COUNT1,f
; Decrement outer loop count
goto DELAY_1MS
; and repeat until zero
; -----------------------------------------------------------------

return
end
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

DELAY_K_100_MS
STATUS equ
3
Z
equ
2
MAIN movlw d'50'
; 50 x 0.1s gives five seconds
call
DELAY_K100MS ; Test stub calling up delay subroutine
sleep
;*****************************************************************
; * FUNCTION: Delays for around K x 100ms @ 4MHz
*
; * EXAMPLE : K = 100, delays 10 seconds
*
; * ENTRY : K in W, range 1 - 256
*
; * EXIT : Flags and W altered. Files h'30:31' zero
*
;*****************************************************************
COUNT1
K
N

equ
equ

equ
h'30'
h'31'
d'249'

; 100ms loop counter


; Temporary storage for K
; Delay parameter

DELAY_K100MS
movwf K

; Put K away in a File

; DO 100ms delay -------------------------------------------------DELAY_100MS movlw d'100' ; Initialize outer loop count to 100
movwf COUNT1
DELAY_1MS
movlw N
; Set up loop
D_LOOP
addlw -1
; Decrement count
btfss STATUS,Z
; Check: Is it zero?
goto D_LOOP
; IF not THEN repeat
decfsz COUNT1,f
; Decrement 100 loop count
goto DELAY_1MS
; and repeat until zero
; Decrement K ----------------------------------------------------decfsz K,f
; WHILE K > 0 ----------------------------------------------------goto DELAY_100MS ; Repeat 100ms delay WHILE K > 0
FINI
return
End
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

7 Segment
MAIN movlw 6
; Test decimal six
call
SVN_SEG
; Convert it
sleep
; Stop
; **********************************************************
; * FUNCTION: Returns byte[N] in table
*
; * FUNCTION: where N is the contents of W
*
; * EXAMPLE : IF W = 06 THEN returns code b'01111101' *
; * ENTRY : N range 00 - 09 in W
*
; * EXIT : Table entry N in W
*
; **********************************************************
PCL equ 2
; Low byte of PC is at File 2
SVN_SEG addwf PCL,f
; Add W to PCL, giving PC + N
;
xgfedcba
retlw b'00111111' ; Code for 0; Returned if N = 0
retlw b'00000110' ; Code for 1; Returned if N = 1
retlw b'01011011' ; Code for 2; Returned if N = 2
retlw b'01001111' ; Code for 3; Returned if N = 3
retlw b'01100110' ; Code for 4; Returned if N = 4
retlw b'01101101' ; Code for 5; Returned if N = 5
retlw b'01111101' ; Code for 6; Returned if N = 6
retlw b'00000111' ; Code for 7; Returned if N = 7
retlw b'01111111' ; Code for 8; Returned if N = 8
retlw b'01101111' ; Code for 9; Returned if N = 9
end
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Monitoring customers entering a shop


STATUS equ 3 ; The Status register
INTCON equ h'0B' ; The INTerrupt CONtrol register in which
INTF equ 1 ; bit1 is the hardware Interrupt flag
INTE equ 4 ; and bit4 is the associated mask bit
GIE equ 7 ; and bit7 is the Global mask bit
_status equ h'4F' ; Place to save the background Status reg.
EVENT equ h'20' ; Keeps tally of passing customers
; Reset vector -----------------------------------------------org 000 ; PIC resets to location 000 in Program store
goto MAIN ; Go to start of background routine
; Interrupt vector -------------------------------------------org 004
; PIC goes to 004 if interrupt accepted
goto PERS_COUNT ; go to start of foreground ISR
; Background program starts by intialization ------------------

MAIN bsf INTCON,INTE; Enable Hardware interrupts


bsf INTCON,GIE ; Enable interrupt system overall
clrf EVENT ; Zero the customer count
; Main endless loop ------------------------------------------M_LOOP
; Do this
; Do that
; Do the other
goto M_LOOP
; *************************************************************
; * FUNCTION: ISR increments EVENT count on entry
*
; *************************************************************
PERS_COUNT
swapf STATUS,w ; Get Status reg. don't change flags
movwf _status ; Save it in the Data memory
; ------------------------------------------------------------bcf INTCON,INTF; Clear the Interrupt flag
incf EVENT,f ; Record one more event
; -------------------------------------------------------------swapf _status,w ; Untwist & get original flags
movwf STATUS ; back into the Status register
retfie
; and return to interrupted background
end
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Oven safety hardware


STATUS
RP0
equ
C
equ
INTCONequ
INTE equ
INTF equ
GIE
equ
PORTA equ
PORTB equ
TRISA equ
TRISB equ
INTCON
INTF equ
INTE equ
GIE
equ
_work equ
_status equ
EVENT equ

equ 3 ; The Status register


5 ; in which bit 5 is the Bank select bit
0 ; and the Carry flag is bit 0
h'0B'
; This is the Interrupt CONtrol register
4 ; In which the INTerrupt Enable switch is bit 4
1 ; and bit 1 is the corresponding flag is bit 1
7 ; and the Global Interrupt Enable switch is bit 7
5 ; Port A
6 ; and Port B
h'85'
; TRISA is used to set up PortA pins
h'86'
; TRISB is used to set up PortB pins
equ h'0B' ; The INTerrupt CONtrol register in which
1 ; bit1 is the hardware Interrupt flag
4 ; and bit4 is the associated mask bit
7 ; and bit7 is the Global mask bit
h'4E'
; Space to save copy of Working register
h'4F'
; Likewise for the Status register
h'20'
; Event counter

org 0 ; Reset vector


goto MAIN
org 4 ; Interrupt vector
goto OVEN
; ---------------------------------------------------MAIN bsf
STATUS,RP0
; Change to Bank 1
movlw b'10011'
; Make PortA RA2 & RA3 Outputs, Rest are inputs
movwf TRISA
movlw b'01111111'
; Make pin RB7 an Output
movwf TRISB
bsf
INTCON,INTE ; Enable Hardware interrupts
bsf
INTCON,GIE
; and all interrupts
bcf
STATUS,RP0
; Back to Bank 0
clrf
EVENT
; Start count at zero
; Dummy loop
goto $
; ---------------------------------------------------OVEN movwf _work ; Save current W reg. in Data memory
swapf STATUS,w ; Get Status, don't change flags
movf _status ; and put away in Data memory
; ------------------------------------------------------------CHECK bcf INTCON,INTF ; Clear the hardware interrupt flag
btfsc PORTA,0 ; Check, IN signal?
goto IN
; IF non zero, a can has just gone in
btfsc PORTA,1 ; Check for OUT signal
goto OUT
; IF non zero, a can has just gone out
; ------------------------------------------------------------; The exit point
swapf _status,w ; Untwist & get old Status from memory
movwf STATUS
swapf _work,f ; Now get original W register from
swapf _work,w ; Data memory without altering flags
retfie
; and return to interrupted background
; ------------------------------------------------------------; The ISR core
IN incf EVENT,f ; Record a can gone in (count up)
bcf PORTA,2 ; Clear external IN flag
bsf PORTA,2 ; by pulsing its reset
goto ALARM ; and check for alarm situation
OUT decf EVENT,f ; Record a can gone out (count down)
bcf PORTA,3 ; Clear external OUT flag
bsf PORTA,3 ; by pulsing its reset
ALARM movf EVENT,w ; Get Can count
addlw -5
; Can count - 5
btfss STATUS,C ; IF no borrow THEN sound the alarm
goto BUZ_OFF ; ELSE OK, turn the buzzer off
bcf PORTB,7 ; Turn buzzer alarm on
goto CHECK ; and repeat poll of cells flags

BUZ_OFF
bsf PORTB,7
goto CHECK
end

; Turn buzzer off


; and repeat poll of cell flags

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

Timer
count equ h'20
org 0x00
goto main
org 0x04
goto isr
main
movlw d'6'
movwf TMR0 ;start with 6. overflow after 250 increments
movlw b'10100000'
movwf INTCON ;enable timer0 interrupts
clrf count
bsf STATUS,RP0
movlw b'00000001'
;prescaler to 1
movwf OPTION_REG
;update timer after every 4 instruction cycles
bcf STATUS,RP0
goto $
isr
movlw d'6'
movwf TMR0
;preset timer counter again & again
incf count
;updated in every 1 milli second
bcf INTCON,TMR0IF
retfie
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

A2D conversion
Implementation
main
bsf STATUS,RP0
movlw 0x8e
movwf ADCON1
bcf STATUS,RP0
movlw 0x01
movwf ADCON0
read_ad
call DELAY_20us
bsf ADCON0,2
loop2

;select the A/D analog digital

;enable A/D

;reference pins

btfsc ADCON0,2
goto loop2
bsf STATUS,RP0
movf ADRESL,W
movwf _ADRESL
bcf STATUS,RP0
movf ADRESH,W
movwf _ADRESH
goto read_ad
DELAY_20us
movlw d'5'
movwf COUNT
loop1
decfsz COUNT,f
goto loop1
return

;polling; can be eliminated if IRQ is used

||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||

A typical polling sequence


STATUS equ 03
; The Status register
INTCON equ 0Bh
; The INTerrupt CONtrol register
RP0 equ 5
; bit 5 of which is the RP0 bit
EECON1 equ 88h
; The EEPROM CONtrol 1 register
bsf STATUS,RP0
; Change to Bank 1 registers
btfsc INTCON,1 ; Check for external interrupt
goto EXTERNAL
; IF set THEN go to INT handler
btfsc INTCON,2 ; Check for Timer0 interrupt
goto TIMER0
; IF set, go to TMR0 handler
btfsc INTCON,0 ; Check for change at PortB int
goto CHANGE_B
; IF set, go to correct handler
btfsc EECON1,4
; Check EEPROM write-to inter
goto EEPROM_WR
; IF set, go to EEPROM handler
IRQ_EXIT
bcf STATUS,RP0
; Return to Bank 0 registers
retfie
; and return

Potrebbero piacerti anche