📄 base1.asm
字号:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Project: Interfacing PICs
; Source File Name: BASE1.ASM
; Devised by: MPB
; Date: 31-1-06
; Status: RS232 added
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; Program to exercise the 16F877 BASE module
; with 8-bit analogue input, LCD, phone keypad
; and serial memory
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PROCESSOR 16F877
; Clock = XT 4MHz, standard fuse settings
__CONFIG 0x3731
; LABEL EQUATES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
INCLUDE "P16F877A.INC" ; standard register labels
; User register label allocation ;;;;;;;;;;;;;;;;;;;;;;;;;;
; GPR 20 - 2A local variables
; GPR 30 - 32 keyin subroutine
; GPR 60 - 65 SERMEM serial memory driver
; GPR 70 - 75 LCDIS display driver
; GPR 77 - 7A CONDEC BCD conversion routine
LCDport EQU 08 ; assign LCD to Port D
LCDdirc EQU 88 ; data direction register
Temp EQU 20 ; temp store
Tabin EQU 21 ; Table pointer
; Keypad registers
Cont EQU 30 ; Delay counter
Key EQU 31 ; Input key
Test EQU 32 ; Key check
;----------------------------------------------------------
; MAIN PROGRAM
;----------------------------------------------------------
ORG 0 ; Default start address
NOP ; required for ICD mode
; Port & display setup ------------------------------------
BANKSEL TRISA ; Select bank 1
MOVLW B'11001000' ; Port B code for
MOVWF TRISB ; keypad row outputs
MOVLW B'10010111' ; Port C code for
MOVWF TRISC ; rows and columns
CLRF TRISD ; Display port is output
BANKSEL PORTA ; Select bank 0
CLRF PORTD ; Clear display outputs
CLRF HiReg ; select memory page 0
CLRF LoReg ; select first location
CALL inimem ; initialise serial memory
CALL inid ; Initialise the display
;----------------------------------------------------------
; MAIN LOOP
;----------------------------------------------------------
start CLRW ; Select AN0 input
CALL adin ; read analogue input
CALL condec ; convert to decimal
CALL putdec ; display input
CALL store ; store input in memory
CALL putkey ; Fixed message
CALL keyin ; scan phone keypad
CALL send ; display key
GOTO start ; and again
;-----------------------------------------------------------
; SUBROUTINES
;----------------------------------------------------------
; Routine to scan 3x4 phone key pad
; Returns ASCII code in W
; Output rows: RB2,RB4,RB5,RC5
; Input cols: RC0,RC1,RC2
;----------------------------------------------------------
keyin NOP
BANKSEL TRISC
MOVLW B'10010111' ; Port C code for
MOVWF TRISC ; rows and columns
BANKSEL PORTC
BSF PORTB,2 ; Set
BSF PORTB,4 ; rows
BSF PORTB,5 ; high
BSF PORTC,5 ; initially
BSF Cont,0 ; Counter not zero
CLRF Test ; No key
; Scan keyboard -------------------------------------------
again CLRW ; No key yet
BCF PORTB,2 ; Row 1
NOP ; wait
NOP
BTFSS PORTC,0 ; key pressed?
MOVLW '1' ; yes - load ASCII code
BTFSS PORTC,1 ; next
MOVLW '2' ; etc
BTFSS PORTC,2 ;
MOVLW '3' ;
BSF PORTB,2 ; deselect row
; ---------------------------------------------------------
BCF PORTB,4 ; second row
BTFSS PORTC,0
MOVLW '4'
BTFSS PORTC,1
MOVLW '5'
BTFSS PORTC,2
MOVLW '6'
BSF PORTB,4
; ---------------------------------------------------------
BCF PORTB,5 ; third row
BTFSS PORTC,0
MOVLW '7'
BTFSS PORTC,1
MOVLW '8'
BTFSS PORTC,2
MOVLW '9'
BSF PORTB,5
; ---------------------------------------------------------
BCF PORTC,5 ; fourth row
BTFSS PORTC,0
MOVLW '*'
BTFSS PORTC,1
MOVLW '0'
BTFSS PORTC,2
MOVLW '#'
BSF PORTC,5
;Test key -------------------------------------------------
MOVWF Test ; get code
MOVF Test,F ; test it
BTFSS STATUS,Z ; if code found
GOTO once ; beep once
MOVF Key,W ; load key code and
RETURN ; if no key, or released
; Check if beep done --------------------------------------
once MOVF Cont,F ; beep already done?
BTFSC STATUS,Z ;
GOTO again ; yes - scan again
MOVF Test,W ; store key
MOVWF Key
; Beep ----------------------------------------------------
beep MOVLW 10 ; 10 cycles
MOVWF Cont
buzz BSF PORTB,0 ; one beep cycle
CALL onems ; 2ms
BCF PORTB,0
CALL onems
DECFSZ Cont ; last cycle?
GOTO buzz ; no
GOTO again ; yes
; End of keypad routine ------------------------------------
; ----------------------------------------------------------
; Display input test voltage on top line of LCD
;-----------------------------------------------------------
putdec BCF Select,RS ; set display command mode
MOVLW 080 ; code to home cursor
CALL send ; output it to display
BSF Select,RS ; and restore data mode
; Convert digits to ASCII ----------------------------------
MOVLW 030 ; load ASCII offset
ADDWF Huns ; convert hundreds to ASCII
ADDWF Tens ; convert tens to ASCII
ADDWF Ones ; convert ones to ASCII
; Display voltage on line 1 --------------------------------
CALL volmes ; Display text on line 1
MOVF Huns,W ; load hundreds code
CALL send ; and send to display
MOVLW '.' ; load point code
CALL send ; and output
MOVF Tens,W ; load tens code
CALL send ; and output
MOVF Ones,W ; load ones code
CALL send ; and output
MOVLW ' ' ; load space code
CALL send ; and output
MOVLW 'V' ; load volts code
CALL send ; and output
RETURN ; done
; Store voltage in serial memory --------------------------
store BSF SSPCON,SSPEN ; Enable memory port
MOVF ADRESH,W ; Get voltage code
MOVWF SenReg ; Load it to write
CALL writmem ; Write it to memory
INCF LoReg ; Next location
BCF SSPCON,SSPEN ; Disable memory port
RETURN ; done
;----------------------------------------------------------
; Display key input on bottom line of LCD
;----------------------------------------------------------
putkey BCF Select,RS ; set display command mode
MOVLW 0C0 ; code to home cursor
CALL send ; output it to display
BSF Select,RS ; and restore data mode
CALL keymes
RETURN ; done
;----------------------------------------------------------
; Display fixed messages
;----------------------------------------------------------
volmes CLRF Tabin ; Zero table pointer
next1 MOVF Tabin,W ; Load table pointer
CALL mess1 ; Get next character
MOVWF Temp ; Test data...
MOVF Temp,F ; ..for zero
BTFSC STATUS,Z ; Last letter done?
RETURN ; yes - next block
CALL send ; no - display it
INCF Tabin ; Point to next letter
GOTO next1 ; and get it
; ---------------------------------------------------------
keymes CLRF Tabin ; Zero table pointer
next2 MOVF Tabin,W ; Load table pointer
CALL mess2 ; Get next character
MOVWF Temp ; Test data...
MOVF Temp,F ; ..for zero
BTFSC STATUS,Z ; Last letter done?
RETURN ; yes - next block
CALL send ; no - display it
INCF Tabin ; Point to next letter
GOTO next2 ; and get it
;----------------------------------------------------------
; Text strings for fixed messages
;----------------------------------------------------------
mess1 ADDWF PCL ; Set table pointer
DT "Volts = ",0 ; Text for display
mess2 ADDWF PCL ; Set table pointer
DT "Key = ",0 ; Text for display
;-------------------------------------------------------------
;----------------------------------------------------------
; INCLUDED ROUTINES
;----------------------------------------------------------
; LCD DRIVER
; Contains routines:
; init: Initialises display
; onems: 1 ms delay
; xms: X ms delay
; Receives X in W
; send: sends a character to display
; Receives: Control code in W (Select,RS=0)
; ASCII character code in W (RS=1)
;
INCLUDE "LCDIS.INC"
;
;----------------------------------------------------------
; Convert 8 bits to 3 digit decimal
;
; Receives 8-bits in W
; Returns BCD diits in 'huns','tens','ones'
;
INCLUDE "CONDEC.INC";
;
;----------------------------------------------------------
; Read selected analogue input
;
; Receives channel number in W
; Returns 8-bit input in W
;
INCLUDE "ADIN.INC"
;
;----------------------------------------------------------
; SERIAL MEMORY DRIVER
; Write high address into 'HiReg' 00-3F
; Write low address into 'LoReg' 00-FF
; Load data send into 'SenReg'
; Read data received from 'RecReg'
;
; To initialise call 'inimem'
; To write call 'writmem'
; To read call 'readmem'
;
INCLUDE "SERMEM.INC"
;
;----------------------------------------------------------
END ; of source code
;----------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -