We have done the timer experiment before, now let’s take a look at the counting experiment.
There are often two requirements for counting at work: first, to display the counted value, and second, to interrupt and alarm when the counted value reaches a certain level. The first type is various counters and odometers, and the second type is the counting on the production line mentioned in the previous example. Let's look at the first type first. Our hardware is connected like this: the oscillator composed of 324 is connected to the external pin T1 of the timer/counter 1. We use this to do a counting experiment. To display the counted value, of course, it is best to use a digital tube, but we haven't talked about this part yet. In order to avoid complicating the problem, we use the 8 LEDs of the P1 port to display the counted data.
The procedure is as follows:
ORG 0000H ,http://www.51hei.com
AJMP START
ORG 30H
START:
MOV SP,#5FH
MOV TMOD,#01000000B; Timer/Counter 1 is used for counting, no need to set all 0s
SETB TR1 ;Start counter 1 to start running.
LOOP: MOV A,TL0
MOV P1,A
AJMP LOOP
END
Connect the output of 324 to T1 with a wire (there are pads on the printed board) and run this program. Pay attention to placing the board in the correct position (LM324 is placed on the left hand side, and the LEDs are arranged from high to low). What do you see? As the LED connected to 324 flashes, the 8 LEDs of the microcontroller are also changing continuously. Pay attention to observe whether they are in binary:
00000000
00000001
00000010
00000011
Is the order changing? That's right, this is the data in TL0.
Procedure 2:
ORG 0000H
AJMP START
ORG 001BH
AJMP TIMER1 ; Timer 1 interrupt processing
ORG 30H
START: MOV SP,#5FH
MOV TMOD,#01010000B; Timer/Counter 1 is used for counting, mode 1, 0 is not used, all bits are set to 0
MOV TH1,#0FFH
MOV TL1,#0FAH; preset value, requiring every 6 pulses counted to be an event
SETB OF
SETB ET1; Enable general interrupt and timer 1 interrupt
SETB TR1 ;Start counter 1 to start running.
AJMP $
HOUR1:
PUSH ACC
PUSH PSW
CPL P1.0; when the count value reaches, P1.0 is inverted
MOV TH1,#0FFH
MOV TL1,#0FAH ; Reset the initial value of the count
POP PSW
POP ACC
RARELY
END
The work done by the above single-chip program is very simple, that is, to invert P1.0 once after every 6 pulses arrive, so the result of the experiment should be: the LED connected to the LM324 turns on and off 6 times, and the LED connected to the P1.0 port turns on or off once. This is actually the second application of the counter we mentioned above.
Procedure 3: External interrupt experiment
ORG 0000H
AJMP START
ORG 0003H ;Direct entry to external interrupt
AJMP INT0
ORG 30H
START: MOV SP,#5FH
MOV P1,#0FFH; all lights off
MOV P3,#0FFH; Set P3 port to high level
SETB OF
SETB EX0
AJMP $
INT0:
PUSH ACC
PUSH PSW
CPL P1.0
POP PSW
POP ACC
RARELY
END
The function of this program is very simple. Pressing button 1 (connected to pin 12) once will trigger interrupt 0 and invert P1.0. Therefore, theoretically, pressing it once will turn the light on and pressing it once will turn the light off. However, in actual experiments, you may find that sometimes it does not work. When you press it, it does not respond, but most of the time it is correct. What is going on? We will explain this when we talk about the keyboard. There is no problem with this program itself.
Previous article:51 MCU Tutorial from Scratch - 19 MCU Interrupt System
Next article:51 MCU Tutorial from Scratch—— 22 MCU Serial Port Introduction
Recommended ReadingLatest update time:2024-11-16 17:42
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
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
- Gaoyun Yunyuan software edits multiple lines at the same time, and the problem of Chinese input method
- Raspberry Pi Windows IoT Development (Part 3) Flashing LED
- If you are doing technology, you must learn to read datasheets!
- Programming the pyboard with Arduino
- 【Silicon Labs Development Kit Review】+ Try running the light-up demo
- [GD32E231 DIY Contest] (II): USART0
- The microcontroller reads PT1000 temperature. Please recommend a more stable conversion chip
- Interaction between data collection and MATLAB
- Tips for taming ADC
- MSP430 MCU Timer A Structure and Application Examples