51 MCU - Input Capture

Publisher:美好未来Latest update time:2021-08-25 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Capture high level time

We use the counting function of timer 0 to capture the high level time of the external pin. Timers are called differently in different usages. For example, if we want to know how long a process lasts, the counting method of the timer is called a counter.


The external pin we choose this time is still P1.6, and initially let this pin output a low level.


We have not mentioned before what happens when the third bit GATE in the lower four bits of TMOD is 1. Here is an explanation. When this bit is set to 1, if "TR0=1;" and P3.2 must be high, the count of timer 0 will be triggered (it will not be triggered when P3.2 is low), that is, TL0 will increase by 1 every (12/11059200) seconds, and after adding to 256 and becoming 0, TH0 will increase by 1. When it continues to add to 65535, there will be "TL0=255;" and "TH0=255;", and adding 1 will turn all to 0. Here we review the knowledge we have learned before.


So we connect P1.6 and P3.2. When TR0 is set to 1, the counting function will be turned on as long as P1.6 outputs a high level, and the counting function will be stopped when P1.6 outputs a low level. We then use the three digital tubes on the left to display the number of TH0, and the three on the right to display the number of TL0.


So we're going to do this

P1.6=0;

TR0=1;

P1.6=1; //Start counting

delay_ms(30); //delay for a while

P1.6=0; //Stop counting

Code First use software to test how much time "delay_ms(30);" actually takes.

8.13.png

Therefore, P1.6 maintains a high level time of 41.784ms.


After connecting P1.6 and P3.2 with Dupont wire, please note that our delay time should not exceed 71ms, that is, the duration of high level should not exceed 71ms, which is the same as the timing time cannot be set to 71ms at a time. Because the maximum count can only reach 65535.


2. Code

Download the code and observe the value displayed on the digital tube.

#include  

#include //See Chapter 6, Lecture 8 for details

  

//Please use Dupont wire to connect P1.6 and P3.2

void main()

{     

    LED_Init(); //Initialize LED hardware module

    TMOD=0x09; //lower four bits 1001 

   

    BEEP=0; //Let P1.6 output low level first

    TR0=1;

    BEEP=1; //Start counting  

    delay_ms(30);

    BEEP=0; //Stop counting  

   

    LedBuff[0]=LedChar[TL0%10];

    LedBuff[1]=LedChar[(TL0/10)%10];

    LedBuff[2]=LedChar[(TL0/100)%10]; 

   

    LedBuff[3]=0x7F&LedChar[TH0%10]; //Add decimal point for easy distinction

    LedBuff[4]=LedChar[(TH0/10)%10];

    LedBuff[5]=LedChar[(TH0/100)%10];

 

    while(1)

    {     

        SEG_Scan(); //Read the values ​​of TH0 and TL0  

    }   

}


The digital tube shows 150.109, so

(150*256+109)*(12/11059200)= 0.0417849s

The captured high-level time is 41.7849ms, which can be said to be quite accurate.

If you have forgotten why it is “150*256”, please go back and review the previous content of this chapter.

The function of the seventh bit GATE of TMOD is the same as that of the third bit GATE, except that it uses timer 1 to count, and the pin that triggers the counting is P3.3. For details, please refer to section 5.2.2 of the document "Teaching You to Learn 51 Microcontrollers Step by Step"

Reference address:51 MCU - Input Capture

Previous article:51 MCU-separate file package
Next article:51 MCU-Data Type Forced Conversion

Recommended ReadingLatest update time:2024-11-16 19:37

51 single-chip multi-channel DS18B20 temperature measurement display and serial number reading
/**************************************************** Function: Read serial number and match serial number, and read temperature value Multifunctional test board showing multi-channel temperature acquisition Normally display the temperature of 2 DS18B20 Read the DS18B20 serial number normally Microcontroller: STC1
[Microcontroller]
51 single-chip multi-channel DS18B20 temperature measurement display and serial number reading
51 single chip ultrasonic distance measurement component
/**  * Ultrasonic distance measuring components  * File name: ultrasonic.h  */   #ifndef _ULTRASONIC_H #define _ULTRASONIC_H   #define somenop(); {     _nop_();_nop_();_nop_();_nop_();_nop_();     _nop_();_nop_();_nop_();_nop_();_nop_(); }   sbit TX = P1^0; //transmit pin sbit RX = P1^1; //receive pin   void tmr1_sp
[Microcontroller]
Design of intelligent lighting control system based on 51 microcontroller
1. System Overview The modules used in the system include AT89C51 microcontroller + LCD1602 display screen + photoresistor module + ADC0832 + small light. This design uses 51 microcontroller as the core control, and uses LCD1602 to display the collected illumination intensity. The illumination intensity is displayed i
[Microcontroller]
Design of intelligent lighting control system based on 51 microcontroller
51 MCU instruction set design example
After you have written a program, you can see the program's instruction set through software debugging. From the instruction set you can understand how the microcontroller works internally, such as the registers and addresses for data access. The following is an instruction set article found by Electrician's Home, whi
[Microcontroller]
51 microcontroller learning: IO expansion (serial to parallel) experiment-74HC595
Experiment name: IO expansion (serial to parallel) experiment-74HC595 wiring description: Experimental phenomenon: After downloading the program, the 8*8 LED dot matrix displays a circular scrolling display in one line. Note: The J24 yellow jumper cap next to the LED dot matrix is ​​shorted to GND. One end ***********
[Microcontroller]
51 single chip microcomputer to make infrared remote control
      Do you have such a scene in your life, to turn on the TV, you look for the remote control in the whole house, and you can't find which one is for the TV after looking through a lot of remote controls? We visited many families and got an amazing data. At least one family of three will have 6 remot
[Microcontroller]
Detailed explanation of C51 MCU interrupt
Drawings first question **A running light that lights up one light at a time, define P1 as led1. External interrupt 0 enables interruption: EX0=1; falling edge trigger: IT0=1; main switch: EA=1; The interrupt is to execute the four diodes of the P0 pin to flash six times. In fact, there is another thing I want t
[Microcontroller]
Detailed explanation of C51 MCU interrupt
Serial port interrupt setting of 51 single chip microcomputer
First, you need to enable interrupts using software. That is, in C language, EA = 1; enable total interrupt ES = 1; //Enable serial port interrupt assembly SETB EA ; Enable general interrupt SETB ES ; Enable serial port interrupt When the microcontroller receives a frame of data, RI will be set to 1 and an i
[Microcontroller]
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号