zoukankan      html  css  js  c++  java
  • 51单片机定时器简单示例

    第一次写博客这玩意,不太会,算了,直接粘代码吧。

     1 #include <reg52.h>
     2 #include <intrins.h> //头文件,将会用到循环右移
     3 //宏定义
     4 #define uchar unsigned char
     5 #define uint  unsigned int
     6 //位、变量声明
     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 //延时函数
    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 //流水灯函数    
    20 void display_led ()
    21 {
    22     while(1) {
    23             led_flow = _cror_(led_flow, 1);
    24             P1 = led_flow;
    25             delay(500);//调用延时函数
    26     }
    27 }
    28 //数码管函数
    29 void display_digitron(uchar i)
    30 {
    31     uchar tens, ones;
    32     tens = i / 10;//求模得到十位
    33     ones = i % 10;//求余得到个位
    34     
    35     P0   = 0xff;//使所有数码管熄灭
    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 //定时器初始化函数
    56 void timer_init()
    57 {
    58     TMOD = 0x10;//TMOD的地址是0x89,不能被8整除,只能对字节操作,不能位操作
    59     TH1  = 0x4c;
    60     TL1  = 0x00;
    61     TR1  = 1;    //TCON的地址是0x88,可以对位操作
    62 }
    63 //主函数main
    64 void main()
    65 {
    66     timer_init();//调用定时器初始化函数
    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累加20次是1秒
    75             counter_ms = 0;
    76             counter_s++;
    77         }
    78         if (counter_s ==60) {//counter_s累加60次是1分钟
    79             wela = 1;
    80             P0   = 0xff;
    81             wela = 0;
    82             TR1 = 0;
    83             display_led();//调用流水灯函数    
    84         /*
    85         led_flow = _cror_(led_flow, 1);
    86             P1       = led_flow;
    87             delay(500);
    88         */
    89         } else {
    90         display_digitron(counter_s);//调用数码管函数
    91         }
    92     }
    93 }
  • 相关阅读:
    Windows Server 2003 服务器备份和恢复技巧
    查询表一张表的列名及字段类型
    aix 维护常用命令
    从 p12 格式 SSL 证书解出 pem 格式公钥私钥给 Postman 使用
    微信添加好友、加群的限制
    python requests 设置 proxy 和 SSL 证书
    blog post template(步骤类)
    post template(调查类)
    clip at cnblogs log
    《什么才是公司最好的福利》读后感
  • 原文地址:https://www.cnblogs.com/yllinux/p/6870171.html
Copyright © 2011-2022 走看看