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

记数器

[复制链接]

3706

主题

1

回帖

1万

积分

管理员

积分
11870
发表于 2023-2-5 01:12:41 | 显示全部楼层 |阅读模式
main.c

  1. /*
  2. 由T0 做定时器 驱动T1 记数
  3. 重点在于 TMOD 的配置

  4. */
  5. #include <reg52.h>
  6. #include "main.h"



  7. void main()
  8. {
  9.     int display_count=0; //显示的记数
  10.     Sys_init();      // 初始化参数
  11.     Set_Time0(1, 3); // 设置定时器
  12.     Set_Counter0(1);//开启记数器
  13.     while (1) {
  14.         if (TF0 == 1)
  15.                 {
  16.                     TF0 = 0;
  17.                     
  18.                     if (use_count++ == Need_time)
  19.                                 {
  20.                                     //code
  21.                                     display_count++;
  22.                                     count_bit=0; //发生一个跳变沿,驱动T1(记录)
  23.                                     count_bit=1;//发生一个跳变沿,驱动T1(记录)
  24.                                     Set_Time0(100, 2);
  25.                                     use_count=0;
  26.                                 }
  27.                     
  28.                 }
  29.                          display(TL1); //输出计算数量_最大值255 .需要记录更多次数  再改
  30.                         //display(display_count);


  31.     }
  32. }

  33. /*初始化参数*/
  34. void Sys_init()
  35. {
  36.     Machine_Cycle = 1 / Crystal * Clock_Cycle; // 初始化 机器周期
  37.     use_count     = 0;
  38. }

  39. /*
  40. 设置定时器
  41. 参数1数量:   时间数量
  42. 参数2单位: 1.微秒 2.毫秒 3.秒
  43. 参数3模式: 0.定时器 1.计数器
  44. */
  45. void Set_Time0(Ulong U_time, int mod)
  46. {

  47.     /*时间转换*/
  48.     if (mod == 3) {

  49.         Need_time_US = U_time * 1000 * 1000;

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

  51.         Need_time_US = U_time * 1000;

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

  53.         Need_time_US = U_time;
  54.     }
  55.     /*设置定时器*/

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

  57.     TR0  = 1;
  58.     TMOD |= 0X01;
  59.     TH0  = (Ulong)(65535 - (Ulong)(Need_time_US / Machine_Cycle) % 65535) / 256; //(Need_time_US/Machine_Cycle)%65535 排除溢出位
  60.     TL0  = (Ulong)(65535 - (Ulong)(Need_time_US / Machine_Cycle) % 65535) % 256;
  61. }

  62. /*
  63. 设置记数器
  64. 参数1记数器开关:    1:开启 0.关闭

  65. */
  66. void Set_Counter0(int Counter_switch){
  67.     if (Counter_switch==0)
  68.     {
  69.        TR1 =0;
  70.         
  71.     }else{
  72.     TR1 =1;
  73.     TMOD |=0X50;
  74.     TH1 =0;
  75.     TL1 =0;
  76.     }
  77.    
  78. }
复制代码


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. sbit count_bit =P3^5;

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

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

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

  16. Ulong use_count; //已经执行的数量





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

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

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



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

  53. void showval(Uchar val, Uchar WEval);
  54. void delay(Uchar z, int mod);
  55. void display(Ulong Z);
  56. void Set_Counter0(int Counter_switch);




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

  61.         display(333);
  62.     }
  63. }
  64. /*
  65. 数码管显示函数:
  66. 参数1:显示的数据0-9
  67. 参数2:需要在显示的位1-8
  68. */
  69. void showval(Uchar val, Uchar WEval)
  70. {
  71.    
  72.    
  73.     WE = 1;              // 打开位选锁存
  74.     P0 = WEtabel[WEval]; // 选择位选
  75.     //P0 = 0XFE;
  76.     WE = 0;              // 位锁存
  77.    
  78.    
  79.     DU = 1;            // 打开段选锁存器
  80.     P0 = DUtabel[val]; // 送数据
  81.    // P0 = 0XFE;
  82.     DU = 0;            // 段锁存
  83.    
  84.    
  85. }
  86. /*
  87. 延迟函数.
  88. 参数1:z为数量
  89. 参数2:mod为模式
  90. 1:毫秒
  91. 2:秒
  92. */
  93. void delay(Uchar z, int mod)
  94. {
  95.     Ulong i;
  96.     if (mod == 1) {
  97.         for (i = 0; i < z; i++) {

  98.             unsigned char a, b;
  99.             for (b = 102; b > 0; b--)
  100.                 for (a = 3; a > 0; a--)
  101.                     ;
  102.         }
  103.     }

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

  121.     while (Z != 0) {
  122.         // 提取n的各个数位上的数
  123.         val[Zlen++] = Z % 10;
  124.         Z /= 10;
  125.     }

  126.     for (i = 0; i < Zlen; i++) {
  127.         P0 = 0XFF; // 清除段码
  128.         showval(val[i], 8 - i);
  129.         
  130.         delay(1, 1);
  131.     }
  132.    
  133. }
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-4-19 23:07 , Processed in 0.073684 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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