51 MCU Learning Road—— 1.6 MCU Interrupts (2)

Publisher:CelestialSoulLatest update time:2016-12-28 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Last time we briefly understood the concept of interruption of microcontroller

This time we focus on understanding the timer interrupt of the microcontroller

 

First add some knowledge

Supplement: Several cycles of the microcontroller

      1: Clock cycle: also known as oscillation period, which is the reciprocal of the external crystal oscillator. For example, for a 12M crystal oscillator, the clock cycle is 1/12um, which is the most basic and smallest unit of time.

       2: State cycle: twice the clock cycle.

       3: Machine cycle. The basic operation cycle of the microcontroller. The microcontroller completes a basic operation in one cycle. It consists of 12 clock cycles. For example, for a 12M crystal oscillator, the machine cycle is 1um. In fact, the machine cycle is the time to complete a single instruction.

       4: Instruction cycle. It refers to the time required for the CPU to execute an instruction. For example, there are single-cycle instructions, two-cycle instructions, three-cycle instructions, and so on.

 

1. Simple understanding of the concept of timer

  

  1 The timer system is an independent hardware part inside the microcontroller

  2 The timer is connected to the crystal oscillator CPU

  3 Once the start timing is set, the timer automatically starts timing under the action of the crystal oscillator

   

      

  

  

    Note: 1 The byte address 89H of TMOD is not addressable.

        So when writing code, you can't do bit operations, you can only set TMOD = 0x01; like this

       2 The upper four bits of TMOD are used to set timer 1 and the lower four bits are used to set timer 0

       3 TMOD commonly used modes are mode 1 (common) 2 (occasionally) and the rest are rarely used

       4 TMOD only controls the working mode and function selection of the timer

    For example:

       TMOD = 0x01; // 0000 0001  

(Timer 0, GATE=0 start is only controlled by register TRX C/T=0 timer mode M1=0 M0=1 working mode 1 16-bit timer)

      

 2 TCON

    

    Note: 1 TCON byte address bit 88H can be bit-addressed

       2 MCU reset TCON is all cleared

    Here we first focus on a few

       1 TF1 Timer 1 overflow flag

       2 TF0 Timer 0 overflow flag

         3 TR1 Timer 1 run control bit  

       4 TR0 Timer 0 run control bit 

 

3 Working Principle

 

 

 

4 Initial Value Problem

    

      Note: 1. TH0 and TL0 are not set. The default values ​​are both 0.  

           So 65536 numbers are needed to overflow: 1111 1111 1111 1111

          (65535 reaches the full value, 65536 overflows and sets TF0 to 1)

    For example:

      If we want to time it to 50ms (a more commonly used time)

      The crystal oscillator of the microcontroller is 12MHz. 12 clock cycles are 1 machine cycle (1/12MHz)*12 = 1 microsecond

      We time it every 50ms, that is, after the initial value is loaded, it overflows after (50/1)*10^3 = 50000 numbers.

      So the initial value is 65536-50000 = 15536 ​​numbers

      15536 ​​needs to be converted to hexadecimal and the high and low 8 bits are separated first.

      15536/256=60 loaded into TH0 15536%256=176 loaded into TL0

 

5 How to write an interrupt service routine

    

    

      The timer interrupt sequence number is 3 Timer 1

 

2 Programming

   

   1 Objective: Make the first LED flash on and off for 1s

     

   2 Code

#include

 2 #define uint unsigned int

 3 #define uchar unsigned char

 4sbit LED1 = P1^0;

 5 uchar num;

 6 void main()

 7 {

 8 TMOD = 0x01; // Timer 0 working mode 1

 9 TH0 = (65536 - 45872)%256; 

10 TL0 = (65536 - 45872)/256;

11 EA = 1; //switch is always off

12 ET0 = 1; // Enable timer 0 interrupt

13 TR0 = 1; //Start timer 0

14 while(1); //The program stops and waits for an interrupt to occur

15 }

16 

17 void T0_time() interrupt 1

18 {

19 TH0 = (65536 - 45872)%256;

20 TL0 = (65536 - 45872)/256;

21 num++;

22 if(num == 20)

twenty three {

24 num = 0;

25 LED1 = ~LED1;

26 }

27 

28 }

 

   3 Analysis:

      

       1 After setting TMOD, correspond to ET0 TR0 TH0 TL0

       2 Don't save trouble by writing 45872 as (5000/(12/11.0592))

       11.0592 is a decimal and cannot be converted into hexadecimal.

       3 In order to ensure that the timer interrupt is 50ms each time, we need to set TH0 and TL0 in the interrupt function each time

       Reload initial value

       4 is set to 50ms. Use a variable num to count every 20 executions of the program, that is, every 1s.

Improved code:

#include
 2 #define uint unsigned int
 3 #define uchar unsigned char
 4sbit LED1 = P1^0;
 5 uchar num;
 6 void main()
 7 {
 8 TMOD = 0x01; // Timer 0 working mode 1
 9 TH0 = (65536 - 45872)%256; 
10 TL0 = (65536 - 45872)/256;
11 EA = 1; //switch is always off
12 ET0 = 1; // Enable timer 0 interrupt
13 TR0 = 1; //Start timer 0
14 while(1); //The program stops and waits for an interrupt to occur
15 if(num == 20)
16 {
17 num = 0;
18 LED1 = ~LED1;
19 }
20 
twenty one }
twenty two 
23 void T0_time() interrupt 1
twenty four {
25 TH0 = (65536 - 45872)%256;
26 TL0 = (65536 - 45872)/256;
27 num++;    
28 }

 

Leave an assignment for everyone to write.

Use timer 0 to flash the first LED at 200ms intervals and use timer 1 to cycle the first two digits of the digital tube for 59s.

      

 

The above content is the blogger's own thoughts after reading books. You should read more books to understand better.


Reference address:51 MCU Learning Road—— 1.6 MCU Interrupts (2)

Previous article:MCU simulation software reset
Next article:51 MCU Learning Road—— 1.8 Matrix Keyboard

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号