51 Single-Chip Microcomputer timer simple example, 51 Single-Chip Microcomputer Timer

Source: Internet
Author: User

51 Single-Chip Microcomputer timer simple example, 51 Single-Chip Microcomputer Timer

The first time I wrote a blog, I don't know. Forget it. Just stick the Code directly.

1 #include <reg52.h>
 2 #include <intrins.h> // The header file will be used to rotate right
 3 // Macro definition
 4 #define uchar unsigned char
 5 #define uint unsigned int
 6 // Bit, variable declaration
 7 sbit led1 = P1 ^ 0;
 8 sbit dula = P2 ^ 6;
 9 sbit wela = P2 ^ 7;
10 uchar counter_ms, counter_s, led_flow = 0xfe;
11 uchar code digitron_data [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
12 // Delay function
13 void delay (uint z)
14 {
15 uint x, y;
16 for (x = 0; x <z; x ++)
17 for (y = 0; y <114; y ++);
18}
19 // Flow lamp function
20 void display_led ()
twenty one {
22 while (1) {
23 led_flow = _cror_ (led_flow, 1);
24 P1 = led_flow;
25 delay (500); // Call the delay function
26}
27}
28 // Digital tube function
29 void display_digitron (uchar i)
30 {
31 uchar tens, ones;
32 tens = i / 10; // Modulus gets ten digits
33 ones = i% 10; // find the remainder by finding the remainder
34
35 P0 = 0xff; // Turn off all digital tubes
36 wela = 1;
37 P0 = 0xfe;
38 wela = 0;
39
40 dula = 1;
41 P0 = digitron_data [tens];
42 dula = 0;
43 delay (1);
44
45 P0 = 0xff;
46 wela = 1;
47 P0 = 0xfd;
48 wela = 0;
49
50 dula = 1;
51 P0 = digitron_data [ones];
52 dula = 0;
53 delay (1);
54}
55 // Timer initialization function
56 void timer_init ()
57 {
58 TMOD = 0x10; // The address of TMOD is 0x89, cannot be divisible by 8, can only operate on bytes, not bit
59 TH1 = 0x4c;
60 TL1 = 0x00;
61 TR1 = 1; // The address of TCON is 0x88, you can operate on the bit
62}
63 // main function main
64 void main ()
65 {
66 timer_init (); // Call the timer initialization function
67 while (1) {
68 if (TF1 == 1) {
69 TF1 = 0;
70 TH1 = 0x4c;
71 TL1 = 0x00;
72 counter_ms ++;
73}
74 if (counter_ms == 20) {// counter_ms accumulating 20 times is 1 second
75 counter_ms = 0;
76 counter_s ++;
77}
78 if (counter_s == 60) {// counter_s accumulating 60 times is 1 minute
79 wela = 1;
80 P0 = 0xff;
81 wela = 0;
82 TR1 = 0;
83 display_led (); // Call the running light function
84 / *
85 led_flow = _cror_ (led_flow, 1);
86 P1 = led_flow;
87 delay (500);
88 * /
89} else {
90 display_digitron (counter_s); // Call the digital tube function
91}
92}
93}

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.