找回密码
 立即注册
搜索
热搜: 中医 针灸 咳嗽
查看: 160|回复: 0

定时器

[复制链接]

3711

主题

1

回帖

1万

积分

管理员

积分
11937
发表于 2023-2-3 23:00:17 | 显示全部楼层 |阅读模式
main.c


  1. #include <reg52.h>
  2. #include "main.h"



  3. void main()
  4. {
  5.     int display_count=0;
  6.     Sys_init();      // 初始化参数
  7.     Set_Time0(1, 3); // 设置定时器

  8.     while (1) {
  9.         display(display_count);
  10.         if (TF0 == 1)
  11.                 {
  12.                     TF0 = 0;
  13.                     
  14.                     if (use_count++ == Need_time)
  15.                                 {
  16.                                     //code
  17.                                     display_count++;
  18.                                     
  19.                                     Set_Time0(1, 3);
  20.                                     use_count=0;
  21.                                 }
  22.                     
  23.                 }
  24.     }
  25. }

  26. /*初始化参数*/
  27. void Sys_init()
  28. {
  29.     Machine_Cycle = 1 / Crystal * Clock_Cycle; // 初始化 机器周期
  30.     use_count     = 0;
  31. }

  32. /*
  33. 设置定时器
  34. 参数1数量:   时间数量
  35. 参数2单位: 1.微秒 2.毫秒 3.秒
  36. */
  37. void Set_Time0(Ulong U_time, int mod)
  38. {

  39.     /*时间转换*/
  40.     if (mod == 3) {

  41.         Need_time_US = U_time * 1000 * 1000;

  42.     } else if (mod == 2) {

  43.         Need_time_US = U_time * 1000;

  44.     } else if (mod == 1) {

  45.         Need_time_US = U_time;
  46.     }
  47.     /*设置定时器*/

  48.     Need_time = (Ulong)(Need_time_US / Machine_Cycle) / 65535; // 获得溢出总量

  49.     TR0  = 1;
  50.     TMOD = 0X01;
  51.     TH0  = (Ulong)(65535 - (Ulong)(Need_time_US / Machine_Cycle) % 65535) / 256; //(Need_time_US/Machine_Cycle)%65535 排除溢出位
  52.     TL0  = (Ulong)(65535 - (Ulong)(Need_time_US / Machine_Cycle) % 65535) % 256;
  53. }

复制代码


main.h

  1. #define Ulong unsigned long
  2. #define Uint  unsigned int
  3. #define Uchar unsigned char
  4. #define Uint  unsigned int
  5. #define Uchar unsigned char

  6. sbit DU =P2^6;//数码管段选
  7. sbit WE =P2^7;//位选


  8. double code Crystal     = 11.0592; // 晶振频率 设定
  9. double code Clock_Cycle = 12;      // 时钟周期
  10. double Machine_Cycle;              // 机器周期
  11. Ulong Need_time_US;                // 微秒总数量

  12. Ulong Need_time_MS; // 计算溢出的毫秒
  13. Ulong Need_time_S;  // 计算溢出的微秒

  14. Ulong Need_time; // 溢出的总数量计算

  15. Ulong use_count;





  16. Uchar code DUtabel[] = {
  17.     0x3F,
  18.     0x06,
  19.     0x5B,
  20.     0x4F,
  21.     0x66,
  22.     0x6D,
  23.     0x7D,
  24.     0x07,
  25.     0x7F,
  26.     0x6F,
  27.     0x77,
  28.     0x7C,
  29.     0x39,
  30.     0x5E,
  31.     0x79,
  32.     0x71,
  33.     0x76,
  34.     0x38,
  35.     0x40,
  36.     0x00,
  37. };

  38. Uchar code WEtabel[] = {
  39.         0x00,//全亮,补位选空位
  40.     0xFE,
  41.     0xFD,
  42.     0xfb,
  43.     0xf7,
  44.     0xef,
  45.     0xdf,
  46.     0xbf,
  47.     0x7f,
  48. };

  49. void Sys_init();
  50. void Set_Time0(Ulong Us_time, int mod);



  51. void showval(Uchar val,Uchar WEval);

  52. void showval(Uchar val, Uchar WEval);
  53. void delay(Uchar z, int mod);
  54. void display(Ulong Z);





  55. void main1()
  56. {
  57.     long int a=0;
  58.     while (1) {

  59.         display(333);
  60.     }
  61. }
  62. /*
  63. 数码管显示函数:
  64. 参数1:显示的数据0-9
  65. 参数2:需要在显示的位1-8
  66. */
  67. void showval(Uchar val, Uchar WEval)
  68. {

  69.    
  70.     WE = 1;              // 打开位选锁存
  71.     P0 = WEtabel[WEval]; // 选择位选
  72.     WE = 0;              // 位锁存
  73.    
  74.    
  75.     DU = 1;            // 打开段选锁存器
  76.     P0 = DUtabel[val]; // 送数据
  77.     DU = 0;            // 段锁存
  78.    
  79.    
  80. }
  81. /*
  82. 延迟函数.
  83. 参数1:z为数量
  84. 参数2:mod为模式
  85. 1:毫秒
  86. 2:秒
  87. */
  88. void delay(Uchar z, int mod)
  89. {
  90.     Ulong i;
  91.     if (mod == 1) {
  92.         for (i = 0; i < z; i++) {

  93.             unsigned char a, b;
  94.             for (b = 102; b > 0; b--)
  95.                 for (a = 3; a > 0; a--)
  96.                     ;
  97.         }
  98.     }

  99.     else if (mod == 2) {
  100.         for (i = 0; i < z * 1000; i++) {
  101.             // 10000us //误差 -0.000000000002us
  102.             unsigned char a, b;
  103.             for (b = 102; b > 0; b--)
  104.                 for (a = 3; a > 0; a--) ;
  105.         }
  106.     }
  107. }
  108. /*显示长数字*/
  109. void display(Ulong Z)
  110. {
  111.     // Z长度
  112.     int Zlen = 0;
  113.     /*存储Z每个位的数字数组变量*/
  114.     int val[8];
  115.     int i;

  116.     while (Z != 0) {
  117.         // 提取n的各个数位上的数
  118.         val[Zlen++] = Z % 10;
  119.         Z /= 10;
  120.     }

  121.     for (i = 0; i < Zlen; i++) {
  122.         P0 = 0XFF;           // 清除段码
  123.         showval(val[i], 8 - i);
  124.         delay(1, 1);
  125.     }
  126.    
  127. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|私人站点 ( 冀ICP备2023028127号-2 )

GMT+8, 2025-7-5 08:10 , Processed in 0.078762 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表