51 MCU Tutorial from Scratch - MCU Timer/Counter Experiment

Publisher:eaff86Latest update time:2012-02-16 Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Keywords:MCU Reference address:51 MCU Tutorial from Scratch - MCU Timer/Counter Experiment

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

Power Timing Socket Made with PIC Microcontroller
This power timing socket has three operation buttons S1, S2, S3, and uses two LED digital tubes to display the timing time. The two buttons S2 and S3 are used to preset and set the timing time values ​​of the ones and tens digits respectively. The setting time adjustment range is 0 ~ 99 minutes. The S1 button is use
[Microcontroller]
Power Timing Socket Made with PIC Microcontroller
51 single chip microcomputer (STC89C52RC) lcd1602 experiment summary
  LCD1602 is a liquid crystal display module with its own controller, which can display two lines of dot matrix data. Please google its pin parameters.      Phenomena encountered during actual use:   The black block on the first line and the blank on the second line are the signs that the 1602 itself has successfull
[Microcontroller]
Audio Signal Analysis Based on MSP430F1611 Microcontroller
This system will use a single-chip microcomputer integrated with the μC/OS-Ⅱ operating system, and use the fast Fourier transform and window function method to analyze the various parameters of the audio signal. 1 System Overall Plan The signal first passes through an 8th-order active Butterworth filter fo
[Microcontroller]
Audio Signal Analysis Based on MSP430F1611 Microcontroller
Realization of communication between touch screen and single chip microcomputer
As the application and output of touch screens increase, prices fall. Therefore, it is possible to use touch screens as keyboards and display devices for single-chip microcomputer controlled devices, thus improving the quality of single-chip microcomputer controlled devices. When the touch screen is used in conjun
[Power Management]
Realization of communication between touch screen and single chip microcomputer
MCU CPU Difference
MCU (Micro Controller Unit), also known as Single Chip Microcomputer, or MCU for short, refers to a chip-level computer that integrates the computer's CPU, RAM, ROM, timer and multiple I/O interfaces on a single chip with the emergence and development of large-scale integrated circuits. MCU can be divided into two t
[Microcontroller]
Design of RTC clock of STM8 single chip microcomputer
The STM8 rtc clock can use an internal low-frequency clock source or an external low-speed 32768Hz clock source. The rtc code is as follows: #include #include voidinit_rtc(void) { RTC_InitTypeDefRTC_InitStr; RTC_TimeTypeDefRTC_TImeStr; RTC_DateTypeDefRTC_DateStr; #if0 /*LSI*/ CLK_RTCClockConfig(CLK_RTCCLKSource_L
[Microcontroller]
Design of RTC clock of STM8 single chip microcomputer
56-Design of IC card intelligent water meter based on 51 single chip microcomputer
Specific implementation functions The system consists of STC89C52 microcontroller + LCD1602 + solenoid valve + relay + flow meter + IC card reader module. Specific functions: (1) The first line shows the IC card ID number. At this time, the IC card is not read, so the ID is displayed as 0. The second line shows th
[Microcontroller]
56-Design of IC card intelligent water meter based on 51 single chip microcomputer
DDS+MCU realizes the operational amplifier parameter measurement system
introduction   In order to help users accurately grasp the various parameters of the operational amplifier in their hands, this paper provides a measurement system using a programmable DDS chip and MCU , which can automatically measure the five basic parameters of the integrated operational amplifier, display the me
[Analog Electronics]
DDS+MCU realizes the operational amplifier parameter measurement system
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号