Microcontroller Exercise - Timer

Publisher:云淡风轻2014Latest update time:2016-12-29 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

For the relevant special registers, please refer to the relevant data

program code as follows:

Timer
 1 #include
 2 // Make the LED light up for 1000ms, dark for 300ms, light up for 300ms, dark for 1000ms, light up for 1000ms . Repeat this cycle .
 3 /*
 4 Test the 4 working modes of the timer
 5 The microcontroller clock is 11.0592MHz, and 1 machine cycle is 1.085us.
 6 Calculate the initial value: Take timer 0 as an example, the calculated value is Nus
 7 Mode 0: TH0 = (2^13 - (N/1.085)) / 2^5 = (8192 - N/1.085) / 32, TL0 = (8192 - N/1.085) % 32;
 8 Mode 1: TH0 = (2^16 - (N/1.085)) / 2^8 = (65536 - N/1.085) / 256, TL0 = (65536 - N/1.085) % 256;
 9 Mode 2: TH0 = TL0 = 2^8 - N/1.085 = 256 - N/1.085;
10 Mode 3: For timer 0, divide into 2 8-bit counters; For timer 1, stop counting
11 */ 
12
13 unsigned char t0Count, t1Count; //Store the number of timer interrupts
14 unsigned char t0H, t0L, t1H, t1L; //Store the initial value
15
16 //T0 mode 0, timing 5ms
17 void time0() interrupt 1
18 {
19     t0Count++;
20 }
21
22 //T1 mode 1, timing 50ms
23 void time1() interrupt 3
24 {
25     t1Count++;
26 }
27
28 void main()
29 {
30     t0Count = 0;
31     t1Count = 0;
32     t0H = (8192 - 5000 / 1.085) / 32;
33     t0L = (8192 - 5000 / 1.085);
34     t0L %= 32;
35     t1H = (65536 - 50000 / 1.085) / 256;
36     t1L = (65536 - 50000 / 1.085);
37     t1L %= 256;
38
39     TH0 = t0H; TL0 = t0L; // Initial value, timing 5ms
40     TH1 = t1H; TL1 = t1L; // Timing 50ms
41
42       EA = 1; //Enable interrupts
43     ET0 = 1; //Enable timer 0, timer 1 interrupts
44     ET1 = 1;
45     TMOD = 0x10; //T1 mode 1, T0 mode 0: TMOD = 0001 0000B
46     P1 = 0;
47     TR1 = 1; //Start timing T1, stop T0
48     TR0 = 0;
49     while(1)
50     {
51         if(t1Count==20) //Enough for 1000ms
52         {
53             if(!P1) //The diode is on, then change the frequency
54             {
55                 TR1 = 0; //Start timing T0, stop T1
56                 TR0 = 1;
57             }
58             TH1 = t1H; TL1 = t1L; //Time 50ms
59             t1Count = 0;
60             P1 = ~P1;
61        }
62         else if(t0Count==60) //Enough for 300ms
63         {
64             if(!P1) //The diode is on, then change the frequency
65             {
66                 TR1 = 1; //Let T1 start timing, T0 stop
67                 TR0 = 0;
68             }
69             TH0 = t0H; TL0 = t0L; //Timing 5ms
70             t0Count = 0;
71             P1 = ~P1;
72         }
73     }
74 }


Reference address:Microcontroller Exercise - Timer

Previous article:MCU Exercise - DS18B20 Temperature Conversion and Display
Next article:Microcontroller Exercise - Timer

Recommended ReadingLatest update time:2024-11-16 14:42

MCU timer (timer0 working mode 2)
; Let the LED light change every R1ms COUNT EQU 92; for a crystal oscillator of 11.0592, 92 is equivalent to 100us LED EQU P1.1   ORG 0000H   MOV R0,#00H   DJNZ R0,$     MOV SP,#60H   MOV R1,#00H ; Use R1 as the overall count and use   MOV R2,#00H   //MOV A,TMOD   //ANL A,#11110000B    //CLR ACC.3   //CL
[Microcontroller]
s3c6410 hardware WATCHDOG TIMER watchdog timer
  First, let's briefly describe the working process of the watchdog. The watchdog is actually a timer with a counter inside. Every time a clock signal arrives, the counter register decreases by one. If it decreases to 0, the system will restart; if the system sets the counter to a larger value before it decreases to 0,
[Microcontroller]
s3c6410 hardware WATCHDOG TIMER watchdog timer
PIC16F1829 TIMER1 Initialization Procedure
Introduction: Here I share with you a PIC16F1829 TIMER1 initialization program. Interested friends can take a look. //timer1 frequency division value #define TIMER1_DIV1 (0 4) #define TIMER1_DIV2 (1 4) #define TIMER1_DIV4 (2 4) #define TIMER1_DIV8 (3 4) //------------- #define T1_DARK (1 6) #define T1_FOS
[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号