51 MCU Timer 2 Capture Mode Pulse Frequency Measurement

Publisher:时光如梦Latest update time:2022-08-10 Source: csdn Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The conventional method of measuring the frequency of pulses is to use two timers of the 51 single-chip microcomputer to measure, one timer for timing and one timer for counting. Is there a better way to meet the needs without using so much hardware and only use one timer? Of course, timer 2 has an input capture function. P1.1 is connected to an external pulse. When a pulse comes, timer 2 enters the interrupt service program and interrupts once. It also has a timing function. The following is the main program:


#include

#define uint unsigned int

#define uchar unsigned char

#define ulong unsigned long 

uint timer2_count;

uint cap_count;

uchar code dispbit[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; // define bit selection starting from the second digit

uchar code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //define segment code

uchar dispbuf[8]={0,0,0,0,0,0,10,10}; //display buffer

uchar temp[8];

uchar dispcount;

uint x;

bit flag;

sbit L1 = P0^7;

//sbit L2 = P0^1;

sbit W1=P1^2;

sbit W2=P1^3;

sbit W3=P1^4;

sbit W4=P1^5;

sbit W5=P1^6;

sbit W6=P1^7;


void display_wei(uchar dipcount)

{

 switch (dipcount){

 case 0: W1=1;W2=W3=W4=W5=W6=0;

   break;

 case 1: {W2=1;W1=W3=W4=W5=W6=0;}

   break;

 case 2: {W3=1;W2=W1=W4=W5=W6=0;}

   break;

 case 3: {W4=1;W2=W3=W1=W5=W6=0;}

   break;

 case 5: {W5=1;W2=W3=W4=W1=W6=0;}

   break;

 case 6: {W6=1;W2=W3=W4=W5=W1=0;}

   break;

 }

}

void timer2_init()

{

 ET2=1; //Start timer 2

   T2CON=0X09; //Set T2 to capture mode, and generate an interrupt on the falling edge

   TH2=(65535-5000)/256;

   TL2=(65535-5000)%256;


   EA=1; //Enable interrupt

   TR2=1; //Start the counter

}


void main()

{

  timer2_init();

  while(1)

  {

   uchar i;

 if(flag==1)

 {

  flag=0;

  x=cap_count; //Frequency formula The number of pulses measured in one second

  //x= x*563;

  //x=x/10;

  for(i=0;i<8;i++)

  {

   temp[i]=0; // array clear

  }

  i=0;   

  //x=123456;

  while(x/10)

  {

   temp[i]=x%10;

   x=x/10;

   i++;

  }

  temp[i]=x;

  for(i=0;i<6;i++)

  {

   dispbuf[i]=temp[i]; //Display 6-digit frequency (cache)

  }

  

  cap_count=0;

  TR2=1; 

 }

  }

}


void timer2_interrupt(void) interrupt 5 using 1 //interrupt service routine

{

  if(EXF2) //Captured interrupt

  {

    //L1=!L1;

 cap_count++;

        EXF2=0;

  }


  if(TF2)

  {

    TH2=(65535-5000)/256;

   TL2=(65535-5000)%256;

 timer2_count++;

 if(timer2_count==400)

 {

  //L2=!L2;

  flag=1;

  timer2_count=0;

  TR2=0; //stop counting

    }

 TF2=0;

 P0=dispcode[dispbuf[dispcount]]; //display data

 

 //P1 = ((P1&0x02)|dispbit[dispcount+2]); //bit selection

 display_wei(dispcount);

 dispcount++;

 if(dispcount==6)

 {

  dispcount=0;

 }

  }

}


Reference address:51 MCU Timer 2 Capture Mode Pulse Frequency Measurement

Previous article:51 MCU timer counter principle and application (square wave, pwm, pulse counting, high level pulse width measurement)
Next article:51 single chip microcomputer to make pulse generator

Recommended ReadingLatest update time:2024-11-23 18:15

51 single-chip revolving lantern C language program
#include          unsigned char RunMode;         void Delay1ms(unsigned int count)     {           unsigned int i,j;           for(i=0;i count;i++)           for(j=0;j 120;j++);     }          unsigned char code LEDDisplayCode = { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,      //0~7            
[Microcontroller]
Detailed explanation of the internal data memory RAM structure of MCS-51 single chip microcomputer
Internal data memory lower 128 cells The internal RAM of the 8051 microcontroller has a total of 256 units, which are usually divided into two parts according to their functions: the lower 128 units (unit address 00H ~ 7FH) and the upper 128 units (unit address 80H ~ FFH). The figure shows the configuration diagram
[Microcontroller]
Detailed explanation of the internal data memory RAM structure of MCS-51 single chip microcomputer
51 microcontroller realizes dynamic display of digital tube 60~0
Hardware circuit schematic diagram Parts list C language program  #include reg51.h unsigned char table ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //Nigital tube character code void show(unsigned char n); void delay(unsigned int k); void main() { unsigned char i; int j; while(1) { for(i=60;j=60,i 0;i--)
[Microcontroller]
51 microcontroller realizes dynamic display of digital tube 60~0
Detailed explanation of idata, xdata, pdata, and data of 51 MCU
data: fixedly refers to the first 128 RAM from 0x00 to 0x7f, which can be directly read and written with acc, with the fastest speed and the smallest generated code. bit: refers to the bit addressable area from 0x20 to 0x2f idata: fixedly refers to the first 256 RAM from 0x00 to 0xff, of which the first 128
[Microcontroller]
Smart curtain design: 51 single chip microcomputer + A/D conversion + photoresistor + LCD1602 + stepper motor
  The microcontroller source program is as follows: #include reg52.h           #define uchar unsigned char #define uint unsigned int #define yi 0x80 //The initial position of the first line of LCD, because the first character address D7 of LCD1602 is always 1 (100000000=80) #define er 0x80+0x40 //LCD second line in
[Microcontroller]
Smart curtain design: 51 single chip microcomputer + A/D conversion + photoresistor + LCD1602 + stepper motor
51 single chip microcomputer-simple application of timer
1. Details In the previous lecture, we know that the initial value of the "16-bit variable" synthesized by TH0 and TL0 must be at least 0 and cannot be a negative number. So we have to satisfy: 65536-(x/(12/11059200)) =0. The solution is x = 0.071111 seconds = 71.111 milliseconds. That is to say, the longest t
[Microcontroller]
Using 51 single chip microcomputer to make a cheap box robot
1. Box Robot BOXZ, nicknamed Box, is an open source interactive entertainment platform! Baidu has many tutorials on how to make Box, most of which are made with Arduino. I had nothing to do during the holidays, so I made a cheap BOXZ with my kids using 51. Here is a picture: It is not difficult to see from the
[Microcontroller]
Using 51 single chip microcomputer to make a cheap box robot
Design of advertising lamp based on 8051 single chip microcomputer
1. Experimental task Use the table to make port P1 change a single light: move left 2 times, move right 2 times, and flash 2 times (delay time 0.2 seconds). 2. Circuit diagram Figure 4.5.1 3. Hardware connection on the system board Connect P1.0-P1.7 in the "MCU system" area to the L1-L
[Microcontroller]
Design of advertising lamp based on 8051 single chip microcomputer
Latest Microcontroller Articles
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号