找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1779|回复: 2
打印 上一主题 下一主题
收起左侧

想要搞个简单单片机频率计 中断1秒计算频率,请帮忙看看

[复制链接]
跳转到指定楼层
楼主
ID:940243 发表于 2021-6-25 10:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include"reg51.h"
  2. #include"lcd1602.h"
  3. #define uchar unsigned char
  4. #define uint unsigned int
  5. uchar count=0,valueH=0,valueL=0;
  6. uchar str[]={"0123456789"};
  7. uchar str1[]={"FREQUENCE:"};
  8. uint fre=0;
  9. uint num;

  10. void initex0()
  11. {
  12.         IT0=1;
  13.         EX0=1;
  14.         EA=1;
  15. }

  16. void inittimer0()
  17. {
  18.         TMOD=0x01;
  19.         TH0=(65536-10000)/256;
  20.         TL0=(65536-10000)%256;
  21. }
  22. void T0_time()interrupt 1
  23. {
  24.         TH0=(65536-50000)/256;
  25.         TL0=(65536-50000)%256;
  26.         num++;
  27.                 if(num==20)
  28.                 {}
  29. }
  30. void datadeal()
  31. {
  32.         uint value=0;
  33.         value=valueH*256+valueL;
  34.         fre=10000000/value;
  35. }

  36. void display()
  37. {
  38.         uchar temp0=0,temp1=0,temp2=0,temp3=0,temp4=0,temp5=0;
  39.         uchar i=0;
  40.         temp0=(fre%1000000)/100000;
  41.         temp1=(fre%100000)/10000;
  42.         temp2=(fre%10000)/1000;
  43.         temp3=(fre%1000)/100;
  44.         temp4=(fre%100)/10;
  45.         temp5=fre%10;
  46.         writecom(0x80);
  47.         delay(1);
  48.         while(str1[i]!='\0')
  49.         {
  50.                 writedat(str1[i]);
  51.                 delay(1);
  52.                 i++;
  53.         }
  54.         writecom(0x80+0x40+5);
  55.         delay(1);
  56.         writedat(str[temp0]);
  57.         delay(1);
  58.         writedat(str[temp1]);
  59.         delay(1);
  60.         writedat(str[temp2]);
  61.         delay(1);
  62.         writedat(str[temp3]);
  63.         delay(1);
  64.         writedat(str[temp4]);
  65.         delay(1);
  66.         writedat('.');
  67.         delay(1);
  68.         writedat(str[temp5]);
  69.         delay(1);
  70.         writedat('H');
  71.         delay(1);
  72.         writedat('Z');
  73.         delay(1);
  74. }

  75. void main()
  76. {
  77.         initex0();
  78.         inittimer0();
  79.         initlcd();
  80.         while(1)
  81.         {
  82.                  datadeal();
  83.                 display();
  84.         }
  85. }

  86. void ex0_isr() interrupt 0
  87. {
  88.         if(count==0)
  89.         {
  90.                 TH0=0x00;
  91.                 TL0=0x00;
  92.                 TR0=1;
  93.         }
  94.         else if(count==1)
  95.         {
  96.                 TR0=0;
  97.                 valueH=TH0;
  98.                 valueL=TL0;
  99.                 count=0;
  100.         }
  101.          count++;
  102. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:94031 发表于 2021-6-25 11:28 | 只看该作者
还是一步步来,你的外中断计数就有问题。
回复

使用道具 举报

板凳
ID:123289 发表于 2021-6-25 13:44 | 只看该作者
本帖最后由 yzwzfyz 于 2021-6-26 10:03 编辑

计数要做如下三件事,你是否同意?
1、何时开始?
2、何时结束?
3、谁来数数?
这三条你是如何做的呢?
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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