当前位置:网站首页>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 !
)
边栏推荐
- Backup and restore SQL Server Databases locally
- Dart development server, do I have a fever?
- [data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
- Écouter le réseau d'extension SWIFT (source)
- 3-列表简介
- Part 1: building OpenGL environment
- 快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
- Solution of electric education system for intelligent supervision station
- GraphMAE----論文快速閱讀
- 模型效果优化,试一下多种交叉验证的方法(系统实操)
猜你喜欢

Part 2: drawing a window

Ad-gcl:advantageous graph augmentation to improve graph contractual learning

Resolution error: LNK2019 unresolved external symbol

Chapitre 2: dessiner une fenêtre

Hilbert Huang Transform

单片机STM32F103RB,BLDC直流电机控制器设计,原理图、源码和电路方案

第 2 篇:繪制一個窗口

1-4metasploitable2介绍

Swift Extension NetworkUtil(網絡監聽)(源碼)

Introduction to software engineering - Chapter 2 - feasibility study
随机推荐
Swift extension networkutil (network monitoring) (source code)
Decltype usage introduction
JS implementation to check whether an array object contains values from another array object
Chapter 3 curve graph of canvas
Case examples of corpus data processing (cases related to sentence retrieval)
June 27, 2021: given a positive array arr, it represents the weight of several people
宝塔面板安装php7.2安装phalcon3.3.2
VR is destined to reappear in the Jianghu?
Practice of opengauss database on CentOS, configuration
【资料上新】迅为基于3568开发板的NPU开发资料全面升级
OC Extension 检测手机是否安装某个App(源码)
从 jsonpath 和 xpath 到 SPL
5g industrial router Gigabit high speed low delay
Teach you how to use the reflect package to parse the structure of go - step 1: parameter type check
Chapter 4 line operation of canvas
Pipeline concept of graphic technology
Leetcode 515 find the leetcode path of the maximum [bfs binary tree] heroding in each row
Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
Model effect optimization, try a variety of cross validation methods (system operation)
Écouter le réseau d'extension SWIFT (source)