当前位置:网站首页>51 single chip microcomputer_ External interrupt and timer / Counter interrupt

51 single chip microcomputer_ External interrupt and timer / Counter interrupt

2022-06-24 08:12:00 Across the mountains and rivers, across the sea

External interrupt and timing / The counter is interrupted

introduction

Learning SCM for some time , I don't quite understand the knowledge of interruption , I also read a lot of information on the Internet , Understand the general meaning of interrupt , For example, one of them : You call again LOL Man-machine , Suddenly the water heater at home turned on , You must pause first LOL, Go to the water heater first , To avoid unnecessary safety accidents caused by overheating of the water heater , When you handle the water heater properly , You call again LOL, It's called : The treatment of the water heater is interrupted . This is actually very general . Let's talk about it in detail here : The simplest are external interrupts and timer interrupts .

External interrupt

External interrupt In fact, it belongs to key processing , The most famous external interrupts are IT0 了 , if IT0 The assignment is 0, This button is actually a low level trigger ; if IT0 The assignment is 1, This button is actually triggered by the bottom edge .( Here is an ad : What is the difference between low level trigger and lower edge trigger ? Low level trigger means that the key is at low level , The code in the external interrupt function will be executed , The interrupt is always valid as long as the pin is low , Will continue to enter the interrupt , Until the level changes to high , Only by interrupting the incident can we give up ; The lower edge trigger must change from high level trigger to low level trigger , in other words , The pin remains low / High level , Nor can it be triggered more than once , Only one interrupt can be triggered . summary , Press the key once , Low level trigger mode can trigger one or more external interrupts , The lower edge trigger can trigger only one external interrupt ) This is the external interrupt .( Here is an advertisement : External interrupt , Generated for button events !) So here comes the question , Button press events can be handled with external interrupts , What else can I do ? It's simple , Directly in main In the function :

if(key1 == 0)
		{
    
			delay10ms();
			if(key1 == 0)	// There was a button pressed 
			{
    
				 Write down that you press someone else's button key1 What are you doing 
			}
		}

That's all External interrupt

Next, let's talk about Timer interrupt

Timer interrupt

A timer is actually a countdown , And timer T0 and T1 The corresponding pins are P3.4 and P3.5.

( Insert an advertisement :EA Represents the main gate , Whether it's an external interrupt , Or the timer interrupt should be started , First of all EA Gate opening , The external interrupt or timer interrupt can be operated only after the switch is opened .)

A counter is actually a count down , The registers commonly used by counters are TL0,TH0,TL1,TH1. With TL0 and TH0 For example ,TL0 and TH0 All are 8 Bit register , from TL0 and TH0 Form high and low octet registers , Used to count , altogether 16 position . The scope is 0~65535, altogether 65536(2 Of 16 Power ).

TCON register :
Here we only talk about timing / Counter section !
TF1 For the timer 1 Overflow position ,TR1 For the timer 1 Control bits ( On or off );TF0 For the timer 0 Overflow position ,TR0 For the timer 0 Control bits ( On or off ).

IE register :
Here we only talk about timing / Counter section !
ET1: Timer 1 Interrupt switch
ET0: Timer 0 Interrupt switch

TMOD register :
There are two timers , They are timers 0 And timers 1
There are two groups GATE,C/T,M1,M0;
among ,GATE Door call control position , Beginners directly make him equal to 0 that will do , And then let TR0 or TR1 be equal to 1, Corresponding timer T0 or T1 And it started .
C/T:0 Is timed ,1 For count .
final M0 and M1 I watched it together , There are four cases , Respectively 00,01,10,11 , Beginners use 01 that will do , Express 16 Bit timer / Counter , The maximum count is 2^16 = 65536
in summary ,TMOD The most commonly used is 0X01.( namely TMOD The binary representation of the register is :0000 0001, Just let TR0 or TR1 be equal to 1, Corresponding timer T0 or T1 And it starts , At the same time, the range is 0~65535, common 65536 Number )

In addition, I'm adding : How to calculate the initial value of the timer :
65536 - 10000 = 55536 , intend Counter from 65536 Down to 55536 The time taken is 10ms.
55536 The hexadecimal of is 0xD8F0,
So set TH0 = 0XD8,TL0=0XF0,
in other words , The initial value is set to 0xD8F0, Then every 10ms Just trigger the timer once 0 or 1 interrupt

// Understand the above knowledge points , You should be able to understand the following code !
#include<reg52.h>
sbit LED0=P1^0;
unsigned char i;
 
void main()
{
    
	TMOD=0X01;  // Set the use timer 0,16 Bit timing / Count register 
	TL0=0xD8;    // The lower octet gives the initial value 
	TH0=0XF0;    // The high octave gives the initial value 
	ET0=1;          // Turn on timer 0 interrupt 
	TR0=1;         // Run timer 0
	EA=1;          // Turn on the main interrupt switch 
	while(1);
}
 
void Delay(void) interrupt 1 using 0
{
    
	i++;
	TR0=0;           // When entering the interrupt function , off timer 
	TL0=0XD8;    // Reset the initial value 
	TH0=0XF0;
	if(i==20)        // because 10ms One trigger does not show obvious results , So let 20 The light changes only once after being triggered 
	{
    
		LED0=~LED0;
	}
	TR0=1;     // Turn the timer back on 
}

( Finally, I'd like to add :
timing / The working principle of the counter is to count .
When counting internal clock pulses , As timer ; When counting external pulse signal , Is the counter . Whether as a timer or a counter , It's all counters in essence .
therefore , Beginners don't have to worry about whether to use a timer or a counter , Because the two are actually the same thing , Only one is the internal clock pulse , One is an external pulse ,
therefore , Beginners use timers T0,T1 The internal pulse of is right , That is to let C/T=0 That's it !

原网站

版权声明
本文为[Across the mountains and rivers, across the sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240435479873.html