LIST P=16F877
INCLUDE "P16F877.INC"
; Clock display range: 00.00 seconds - 99.99 seconds, resolution: 0.01 seconds; The registers used are as follows:
S0 EQU 0X20 ; 0.01 second timer
S1 EQU S0+1; 0.1 second timer
S2 EQU S0+2; 1 second timer
S3 EQU S0+3; 10 second timer
SREG EQU S0+4 ;soft counter
LEDF EQU S0+5 ; Displayed LED position indication register
XW_TEMP EQU S0+6; used to protect the value of W during interruption
XHOSTS EQU S0+7; used to protect the STATUS value in the interrupt
DEYH EQU S0+8
DEYL EQU S0+9; The above two registers are used for software delay
HOSTF EQU S0+0A; used to protect the value of FSR during interruption
ORG 0X00
GOTO MAIN
ORG 0X04
GOTO CLKINT ; Go to interrupt service routine
ORG 0X10
CONVERT; Common anode code table without decimal point
ADDWF PCL,1
RETLW 0XC0
RETLW 0XF9
RETLW 0XA4
RETLW 0XB0
RETLW 0X99
RETLW 0X92
RETLW 0X82
RETLW 0XD8
RETLW 0X80
RETLW 0X90
RETURN
CONVERT2 ; Common anode code table with decimal point
ADDWF PCL,1
RETLW 0X40
RETLW 0X79
RETLW 0X24
RETLW 0X30
RETLW 0X19
RETLW 0X12
RETLW 0X02
RETLW 0X78
RETLW 0X00
RETLW 0X10
RETURN
TRANSMIT; SPI sends display submodule
CLRF PORTA; LACK sends a low level to prepare for latching
MOVWF SSPBUF ; Start sending
WAIT BANKSEL PIR1
BTFSS PIR1, SSPIF
GOTO WAIT ; Wait for sending to complete
BCF PIR1, SSPIF; Clear interrupt flag
NOP
RETURN
; **************Display subroutine module************
DISPLAY MOVLW 0X01
MOVWF LEDF
MOVLW 0XA0
MOVWF FSR
AGAINXIAN MOVF LEDF,W
SUBLW 0X02
BTFSS STATUS,Z
GOTO XIANB
MOVF INDF,W
CALL CONVERT2; if it is the second digit, check the code table with decimal point
GOTO TRAN
XIANB MOVF INDF, W
CALL CONVERT
TRAN CALL TRANSMIT ;Send a display data
INCF FSR
INCF LEDF
MOVF LEDF,W
SUBLW 0X05
BTFSS STATUS,Z
GOTO AGAINXIAN
MOVLW 0X01
MOVWF LEDF
BSF PORTA, 5; finally a latch signal is given, indicating that a display task is completed
RETURN
; ********** S0 count overflow processing subroutine ******************
CS0 CLRF S0
INCF S1
RETURN
; ********** S1 count overflow processing subroutine ******************
CS1 CLRF S1
INCF S2
RETURN
; ********** S2 count overflow processing subroutine ******************
CS2 CLRF S2
INCF S3
RETURN
; ********** S3 count overflow processing subroutine ******************
CS3 CLRF S3
RETURN
; **************Clock interrupt service routine********************
CLKINT MOVWF XW_TEMP ;Save the value of W
SWAPF STATUS,W
MOVWF XHOSTS ; Temporarily store the value of STATUS
MOVF FSR,W
MOVWF HOSTF ;Save the value of FSR. The above program is for interrupt scene protection
MOVLW 0X14
MOVWF TMR0 ; Write an adjustment value to TMR0, because writing
; TMR0 cannot be incremented in the next two cycles
BCF INTCON, T0IF; Clear interrupt flag
INCF SREG
MOVF SREG,W
SUBLW 0X28; TMR0 interrupts every 250 μs
BTFSS STATUS, Z; it will execute S0, S1, S2, S3 after 40 interrupts.
;operate
GOTO TIFAN
CLRF SREG
INCF S0
MOVF S0,W
SUBLW 0X0A
BTFSC STATUS,Z
CALL CS0
MOVF S1,W
SUBLW 0X0A
BTFSC STATUS,Z
CALL CS1
MOVF S2,W
SUBLW 0X0A
BTFSC STATUS,Z
CALL CS2
MOVF S3,W
SUBLW 0X0A
BTFSC STATUS,Z
CALL CS3
TIFAN MOVF HOSTF, W; The following is the interruption scene recovery
MOVWF FSR ; Restore the value of the indirect addressing pointer FSR
SWAPF XHOSTS,W
MOVWF STATUS ; Restore the value of STATUS
SWAPF XW_TEMP,1
SWAPF XW_TEMP, W ; Restore the value of W
RETFIE
; ************** TMR0 initialization subroutine ******************
CLKINSUB BANKSEL OPTION_REG
BCF OPTION_REG, T0CS; TMR0 works in timer mode
BSF OPTION_REG, PSA; TMR0 does not need frequency division
BANKSEL INTCON
BCF INTCON, T0IF; Clear the interrupt flag of TMR0
BCF INTCON, GIE; general interrupt disable
BSF INTCON, T0IE; TMR0 interrupt enable
RETURN
; ******************System initialization subroutine********************
MAINSUB BANKSEL TRISA
BCF TRISA, 5; Set RA5 to output mode to output latch signal
BCF TRISB, 1
BCF TRISB, 2
BSF TRISB, 4
BSF TRISB, 5; Set the input and output mode of each port related to the keyboard
BCF TRISC, 5
BCF TRISC, 3; Set SCK and SDO to output mode
BCF INTCON, GIE ; turn off all interrupts
MOVLW 0XC0
MOVWF SSPSTAT; Set the SSPSTAT register
BANKSEL SSPCON
MOVLW 0X30
MOVWF SSPCON; Set the SPI control mode to allow SSP mode.
BCF STATUS, IRP; Indirect addressing selects BANK0, BANK1
RETURN ; Return
; **************Key scanning subroutine********************
KEYSCAN CLRF PORTB ; send 0 on both B1 and B2 lines
NOP
NOP; after a period of delay, avoid the transition process of the pin level
MOVF PORTB,W
ANDLW 0X30; only check B4 and B5, shield the other bits
SUBLW 0X30
RETURN
;******************************************
KEYDELAY MOVLW 0X80 ; keyboard debounce subroutine (about 8ms software delay)
MOVWF DEYH
AGAIN2 MOVLW 0XFF
MOVWF DEYL
AGAIN1 DECFSZ DEYL,1
GOTO AGAIN1
DECFSZ DEYH,1
GOTO AGAIN2
RETURN
;****************Display buffer processing subroutine****************
XIANHUAN MOVLW 0XA0
MOVWF FSR
MOVF S3,W
MOVWF INDF
INCF FSR
MOVF S2,W
MOVWF INDF
INCF FSR
MOVF S1,W
MOVWF INDF
INCF FSR
MOVF S0,W
MOVWF INDF
RETURN
MAIN NOP
CALL MAINSUB ;System initialization
CALL CLKINSUB ; Call the clock initialization subroutine
DENJIAN BCF INTCON, GIE ; Disable interrupts
BANKSEL S0
CLRF S0
CLRF S1
CLRF S2
CLRF S3
CLRF SREG
CLRF LEDF ; Clear S0=S1=S2=S3=0
CALL XIANHUAN ; load the values of S3, S2, S1, and S0 into the display buffer
CALL DISPLAY ; Display
DENAN BCF INTCON, GIE; disable interrupt
CALL KEYSCAN ; Perform key scan
BTFSS STATUS,Z
GOTO XIAODOU1; if the start key is pressed, the jitter is eliminated
GOTO DENAN; if the start key is not pressed, continue waiting
XIAODOU1 CALL KEYDELAY; delay to eliminate jitter
CALL KEYSCAN ;Perform key scan again
BTFSC STATUS,Z
GOTO DENAN; if it is interference, wait for the key to be pressed again
BANKSEL S0
CLRF S0
CLRF S1
CLRF S2
CLRF S3
CLRF SREG ; Set the initial conditions for a new timing
WAITS CALL KEYSCAN
BTFSS STATUS,Z
GOTO WAITS; To prevent the key from being too sensitive, wait until the key is released before proceeding.
; The following operation
MOVLW 0X08
MOVWF TMR0
BSF INTCON, GIE; open general interrupt
HERE CALL XIANHUAN ; Display buffer processing
CALL DISPLAY; Update the display content immediately
CALL KEYSCAN
BTFSS STATUS,Z
GOTO XIAODOU; if the stop timing key is pressed, the bounce will be eliminated.
GOTO HERE; If the stop timing key is not pressed, continue to wait for the interrupt timing
XIAODOU CALL DISPLAY; Call the display program using the debounce time
CALL DISPLAY
CALL DISPLAY
CALL DISPLAY
CALL DISPLAY
CALL DISPLAY
CALL DISPLAY
CALL DISPLAY
CALL DISPLAY
CALL KEYSCAN
BTFSC STATUS,Z
GOTO HERE; if it is interference, continue to wait for the stop key to be pressed
BCF INTCON, GIE; If the stop key is pressed, the interrupt is disabled
CALL DISPLAY; if it is not a disturbance, disable interruption and update the display content
WAITR CALL KEYSCAN
BTFSS STATUS,Z
GOTO WAITR; To prevent the key from being too sensitive, wait until the key is released before proceeding.
; The following operation
DENDAI CALL KEYSCAN
BTFSC STATUS,Z
GOTO DENDAI ; loop key scan, wait for the reset key to be pressed
CALL KEYDELAY
CALL KEYSCAN
BTFSC STATUS,Z
GOTO DENDAI; delay debounce
WAIT19 CALL KEYSCAN
BTFSS STATUS,Z
GOTO WAIT19 ; Wait until the key is released
GOTO DENJIAN; a new time starts
END
Previous article:Discussion on PIC microcontroller controlling LED 8-bit water light
Next article:Temperature DS18b20 and PIC microcontroller communication program
Recommended ReadingLatest update time:2024-11-16 16:22
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How to measure BLE power consumption (one of the most viewed app reports)
- Can anyone help explain the authentication process when the NB-IOT card is connected to the telecom network?
- [Erha Image Recognition Artificial Intelligence Vision Sensor] 4. Color recognition and object recognition
- 1. [Learning LPC1768 library functions] LED experiment
- [Synopsys IP Resources] How many steps are required for a successful RDC sign-off?
- Use Burn in software to test and vibration test the hard disk and a temperature alarm appears
- [Bing Dwen Dwen Award List] 2022 Digi-Key Innovation Design Competition
- Have you ever done this kind of impedance marking that makes people collapse and go to the hospital?
- Transimpedance amplifier circuit, question about the maximum optical power that a photodiode can receive
- Why has the GaN charger that Lei Jun recommends in various ways become a charging magic tool?