Using the 51 series single chip microcomputer timer function to measure pulse width

Publisher:不染尘埃Latest update time:2016-05-18 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The STC12C series enhanced MCU expands the functions of the basic 51 MCU on-chip, such as providing a PCA/PWM interface, and the timer can work in 1T mode (the clock of the basic 51 MCU is 12-divided by Fosc, and 1-divided in 1T mode).

PCA can be used to measure pulse width. However, protues does not currently support the simulation function of this series of microcontrollers, and repeated burning is also very troublesome, so it is better to use the basic 51 microcontroller to implement this function first, and then implement PCA to measure pulse width in the following blog post.

The implementation ideas are as follows:

After the highest bit GATEn of TMOD is set, Tn starts counting and is affected by INTn (Pin3.3) and TRn: TRn is 1, and Tn is allowed to count only when the INTn pin input is high. This function can be used to measure the width of the positive pulse on INTn.

First, here's the picture (forgive my drawing skills):Using the 51 series single chip microcomputer timer function to measure pulse width

1):1 before the rising edge, initialize TMOD, TRn=1;

2): INTn pin at 2 is at high level, and starts counting and measuring pulse width;

3): INTn pin at 3 is at low level, measurement ends and counting stops TRn=0

 

Here is the simulation picture:Using the 51 series single chip microcomputer timer function to measure pulse width

1). Select 5v square wave as the signal generator level. Note that the inverting end of the signal generator is grounded, otherwise the positive end will only output a 2.5v square wave (the remaining 2.5v outputs an inverted square wave, which can be connected to an oscilloscope for a try), and INTn will never receive a high level, and the expected effect will not be achieved.

2). The T0 timer is used as a counter, and an overflow occurs when a negative pulse is received, which starts T1;

3). T0 and T1 all work in mode 2 automatic loading count value mode.

Then, the code:

Working frequency 12Mhz

 

#include 
#include 

sbit P1_0 = P3^3;

#define MakeByte(target, Hi,Lo) \
do{ \
	target |= (((Hi)<<4)|(Lo)); \	
}while(0); \

#define SetTH(n,val) \
do{ \
	TH##n = val; \
}while(0); \

#define SetTL(n,val) \
do{ \
	TL##n = val; \
}while(0); \

#define EnableET(n) \
do{ \
	ET##n = 0x01; \
	IE |= 0x80; \
}while(0); \

unsigned int click;  
unsigned int oneMs;
unsigned char getPlusWidth;
int main()
{
	unsigned int totalus=0,maxPlusWidth=0;
	P3 = 0xFF;

	getPlusWidth = 0;
	MakeByte(TMOD,0x0A,0x06);
	SetTH(0,0xff);
	SetTL(0,0xff);
	SetTH(1,0x38);
	SetTL(1,0x38);
	EnableET(0);
	EnableET(1);
	TR0 = 0x01;
	while(1)
	{
		while(!getPlusWidth);
		//Wait for INT1 to go low
		while(INT1==0x01);
		//Wait for INT1 to reach high level
		while(INT1==0x00);
		//Wait for INT1 to be low level, pulse width ends
		while(INT1==0x01);
		TR1 = 0x00;

		totalus = 1000*(oneMs+(click*0.2))+(TL1-TH1);	
		oneMs = 0;
	}
	return 0;
}

//T0 pin receives negative transition
void IsrT0() interrupt 1
{
	TR1 = 0x00;
	getPlusWidth = 1;		
	TR1 = 0x01;
}

void IsrT1() interrupt 3
{
	//Each interruption is 0.2ms
	click++;
	if(click == 5)
	{
		oneMs++;
		click=0;
	}
}

Finally, the simulation results are:Using the 51 series single chip microcomputer timer function to measure pulse width

 

500Hz square wave, pulse width 981us

1kHz square wave, pulse width 587us

 
2kHz square wave, pulse width 234us

 

 
Reference address:Using the 51 series single chip microcomputer timer function to measure pulse width

Previous article:How to realize serial communication between PC and single chip microcomputer
Next article:How to generate the system clock of 51 series microcontroller

Recommended ReadingLatest update time:2024-11-16 13:38

Typical application of Timer_A of MSP430 - PWM
Program 1: #include  "msp430x14x.h" void  main(  void  ) {     WDTCTL  =  WDTPW  +  WDTHOLD;                //Turn off the watchdog     TACTL=TASSEL0+TACLR+MC0;                      //ACLK is the clock source, clear TAR, increment mode     TACCR0=512-1;                                                             
[Microcontroller]
MSP430F5529 (VI) Timer Timer_A-1
MSP430F5529 has two types of timers, three Timer_A timers and one Timer_B timer. According to the number of capture/compare registers, they are named Timer0_A (with 5 capture/compare registers), Timer1_A (3), Timer2_A (3), and Timer0_B (7).                         In this chapter, we will talk about Timer0_A. (All A t
[Microcontroller]
MSP430F5529 (VI) Timer Timer_A-1
PIC Timer (TIMER2)
1. PIC TIMER2 timer The function of Timer2 is somewhat different from Timer0. Timer2 is an eight-bit counter with an eight-bit counting register TMR2. Timer2 has the following functions: There are two frequency dividers, one is the pre-divider and the other is the post-divider. The frequency division can be set by s
[Microcontroller]
PIC Timer (TIMER2)
Interrupt-timer0
Use timer0 interrupt to flash the LED Heat. c /*  * init.c: do some initialization  */    #include "s3c24xx.h"   void disable_watch_dog(void); void clock_init(void); void memsetup(void); void copy_steppingstone_to_sdram(void); void init_led(void); void timer0_init(void); void init_irq(void); void delay(int n) { int
[Microcontroller]
Timer0 of PIC16F877
    Timer0 is an 8-bit timer counter with a programmable 8-bit prescaler. The hardware structure is shown in the figure below:   There are three registers associated with it: If counting mode is required, T0CKI should be set to input mode, that is, the corresponding position in TRISA is set to 1.     TMR
[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号