/********************CCU_Capture.asm***************************
;Program function: Capture each level jump of a square wave, and use the captured value to calculate
the pulse width, duty cycle, period or other of the square wave.
;****************************************************************/
$NOMOD51
$INCLUDE(REG932.INC)
ICRAH_DAT1 DATA 41H ; High byte of the first capture value
ICRAL_DAT1 DATA 42H ; Low byte of the first capture value
ICRAH_DAT2 DATA 43H ; High byte of the second capture value
ICRAL_DAT2 DATA 44H ; Low byte of the second capture value
ICRAH_DAT3 DATA 45H ; High byte of the third capture value
ICRAL_DAT3 DATA 46H ; Low byte of the third capture value
CAP_FLAG1 BIT 10H ; first capture flag
CAP_FLAG2 BIT 11H ; second capture flag
KEY BIT P0.0
ORG 0000H
AJMP MAIN
ORG 005BH
AJMP CCU_ISR
;---------------------------------------
;Function: Main function
;---------------------------------------
ORG 0080H
MAIN:
MOV P2M1,#40H ;Port setting
MOV P2M2,#40H
MOV ICRAL_DAT1,#00H
MOV ICRAH_DAT1,#00H
MOV ICRAL_DAT2,#00H
MOV ICRAH_DAT2,#00H
MOV ICRAL_DAT3,#00H
MOV ICRAH_DAT3,#00H
MAIN_LOOP:
JB KEY,$ ;Wait for the key to
be pressed JNB KEY,$ ;Wait for the key to be released
ACALL INI_CCU ;Initialize CCU and set interrupt
ACALL DISP ;Output the captured value to display
SJMP MAIN_LOOP
RET
;--------------------------------------
;Function: CCU initialization
;--------------------------------------
INI_CCU:
MOV TOR2H,#00H ;Set CCU to reload high byte
MOV TOR2L,#00H ;Set CCU to reload low byte
MOV TPCR2H,#00H ;Set CCU pre- scaling controller
MOV TPCR2L,#5
MOV CCCRA,#10H ;Set the capture comparison channel A working mode to rising edge capture
MOV TICR2,#81H ;Set CCU interrupt enable controller
SETB ECCU ;Open CCU interrupt
SETB EA ;Open all interrupts
MOV TCR20,#00000001B ;Set CCU working mode, increment count
RET
;--------------------------------------
;Function: CCU interrupt service routine
;--------------------------------------
CCU_ISR:
CLR EA
MOV A,TISE2
ANL A,#07H
CJNE A,#07H,CAP ;Judge whether it is CCU timer interrupt
ANL TIFR2,#7FH ;If yes, clear TIFR2 (interrupt flag) to 0
MOV A,TISE2
CAP: CJNE A,#06H,INTS_CEXT ;Judge whether it is the interrupt of input capture event A, if not, exit
JB CAP_FLAG1,CAP2 ;Judge whether it is the second interrupt, if yes, jump
;------------------
CAP1:
PUSH ACC
MOV A,ICRAL
MOV ICRAL_DAT1,A
MOV A,ICRAH
MOV ICRAH_DAT1,A
SETB CAP_FLAG1 ;The first capture interrupt is completed
MOV CCCRA,#00H ;Set the capture compare A channel working mode to falling edge capture
POP ACC
AJMP INTS_CEXT
;------------------
CAP2:
PUSH ACC
JB CAP_FLAG2,CAP3 ; Check if it is the third interrupt? If yes, jump to CAP3.
MOV A,ICRAL ; Save the value captured for the second time
MOV ICRAL_DAT2,A
MOV A,ICRAH
MOV ICRAH_DAT2,A
SETB CAP_FLAG2 ;The second capture interrupt is completed
MOV CCCRA,#10H ;Set the capture comparison channel A working mode to rising edge capture
POP ACC
AJMP INTS_CEXT
;-------------------
CAP3:
MOV TCR20,#00H ;Stop CCU timer counting
PUSH ACC
MOV A,ICRAL ;Save the third capture value
MOV ICRAL_DAT3,A
MOV A,ICRAH
MOV ICRAH_DAT3,A
CLR CAP_FLAG1 ; Clear capture completion bit
CLR CAP_FLAG2
POP ACC
; AJMP INTS_CEXT
;------------------------
INTS_CEXT:
MOV TIFR2,#00H ; Clear capture interrupt flag bit to 0
SETB EA
RETI
;-------------------------------------
; Function: Please add your data processing program here
;-------------------------------------
DISP:
;...... Please add your own statement here, for example, high level pulse width = (ICRAH_DAT2, ICRAL_DAT2)-(ICRAH_DAT1, ICRAL_DAT1)
;...... Process the obtained data to obtain the required value
;...... and display or transmit it to the host computer
RIGHT
;-----------------------------------
END
;*********** ****************************************************** *****
Previous article:Making good use of microprocessors to simplify power supply design
Next article:Design of signal acquisition node based on P87C591 single chip microcomputer
- Popular Resources
- Popular amplifiers
- STC8 series MCU development guide: analysis and application of processors, programming and operating systems
- Compilation system perspective: Graphical explanation of the working mechanism and implementation principle of the GCC compiler
- Principles of Compilation 3rd Edition (Zhang Suqin, Wang Shengyuan, Dong Yuan, Lü Yingzhi, Jiang Weidu)
- Decompilation technology and software reverse analysis
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
- 3DH Model
- What kind of products need a dedicated shutdown discharge circuit?
- [First Round] Interview Questions for Embedded Engineers
- How to prevent PCB board from bending and warping during reflow oven
- Fully automatic high pressure steam sterilization controller
- The data of STM32 ADC is tampered when using DMA mode
- ECG ten electrodes and 12 leads
- Why can't the P0 port of STC8A8K32S4A12 output a high level?
- What do you think of when you see PID?
- Decomposition