微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 我想知道那个定时器的初值是怎么来的

我想知道那个定时器的初值是怎么来的

时间:10-02 整理:3721RD 点击:

/超声波模块显示程序

  1. <div align="left"><font color="black"><font face="" "=""><font style="font-size: 10.5pt">#include <reg52.h>     //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">包括一个</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">52</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">标准内核的头文件</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  2. #define uchar unsigned char //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">定义一下方便使用</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  3. #define uint  unsigned int
  4. #define ulong unsigned long
  5. sbit Tx  = P3^3; //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">产生脉冲引脚</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  6. sbit Rx  = P3^2; //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">回波引脚</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  7. uchar code SEG7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};//</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">数码管</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0-9 </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  8. uint distance[4];  //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">测距接收缓冲区</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  9. uchar ge,shi,bai,temp,flag,outcomeH,outcomeL,i;  //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">自定义寄存器</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  10. bit succeed_flag;  //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">测量成功标志</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  11. //********</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">函数声明</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  12. void conversion(uint temp_data);
  13. void delay_20us();
  14. void pai_xu();
  15. void main(void)   // </font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">主程序</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  16. {  uint distance_data,a,b;
  17.    uchar CONT_1;   
  18.    i=0;
  19.    flag=0;
  20.     Tx=0;       //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">首先拉低脉冲输入引脚</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  21.     TMOD=0x11;    //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">,定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">1</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">,</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">16</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">位工作方式</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  22.     TR0=1;         //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">启动定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0 </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  23.    IT0=0;        //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">由高电平变低电平,触发外部中断</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  24.     ET0=1;        //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">打开定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">中断</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  25.     EX0=0;        //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">关闭外部中断</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  26.     EA=1;         //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">打开总中断</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0     </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  27.    
  28.      
  29. while(1)         //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">程序循环</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  30.     {
  31.   EA=0;
  32.          Tx=1;
  33.         delay_20us();
  34.         Tx=0;         //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">产生一个</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">20us</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">的脉冲,在</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">Tx</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">引脚</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">   </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  35.         while(Rx==0); //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">等待</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">Rx</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">回波引脚变高电平</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  36.          succeed_flag=0; //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">清测量成功标志</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  37.          EX0=1;          //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">打开外部中断</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  38.            TH1=0;          //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">1</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">清零</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  39.         TL1=0;          //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">1</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">清零</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  40.          TF1=0;          //
  41.         TR1=1;          //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">启动定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">1 </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  42.    EA=1;

  43.       while(TH1 < 30);//</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">等待测量的结果,周期</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">65.535</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">毫秒(可用中断实现)</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">   </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  44.           TR1=0;          //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">关闭定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">1 </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  45.         EX0=0;          //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">关闭外部中断</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">

  46.     if(succeed_flag==1)
  47.          {      
  48.            distance_data=outcomeH;                //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">测量结果的高</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">8</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">位</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  49.            distance_data<<=8;                   //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">放入</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">16</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">位的高</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">8</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">位</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  50.              distance_data=distance_data|outcomeL;//</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">与低</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">8</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">位合并成为</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">16</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">位结果数据</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  51.             distance_data*=12;                  //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">因为定时器默认为</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">12</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">分频</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  52.            distance_data/=58;                   //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">微秒的单位除以</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">58</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">等于厘米</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  53.          }                                      //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">为什么除以</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">58</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">等于厘米,</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">  Y</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">米</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">=</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">(</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">X</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">秒</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">*344</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">)</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">/2 </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  54.                                                    // X</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">秒</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">=</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">(</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt"> 2*Y</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">米)</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">/344 ==</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">》</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">X</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">秒</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">=0.0058*Y</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">米</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt"> ==</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">》厘米</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">=</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">微秒</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">/58  </font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  55.     if(succeed_flag==0)
  56.            {
  57.             distance_data=0;                    //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">没有回波则清零</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">

  58.            }

  59.            distance<i>=distance_data; //</i></font></font></font><i><font color="black"><font face="宋体"><font style="font-size: 10.5pt">将测量结果的数据放入缓冲区</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  60.             i++;
  61.              if(i==3)
  62.                {
  63.                  distance_data=(distance[0]+distance[1]+distance[2]+distance[3])/4;
  64.                pai_xu();
  65.              distance_data=distance[1];

  66.       
  67.        a=distance_data;
  68.        if(b==a) CONT_1=0;
  69.        if(b!=a) CONT_1++;
  70.        if(CONT_1>=3)
  71.            { CONT_1=0;
  72.               b=a;
  73.               conversion(b);
  74.             }        
  75.                i=0;
  76.                }         
  77.      }
  78. }
  79. //***************************************************************
  80. //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">外部中断</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">,用做判断回波电平</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  81. INTO_()  interrupt 0   // </font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">外部中断是</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">号</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  82. {     
  83.      outcomeH =TH1;    //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">取出定时器的值</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  84.      outcomeL =TL1;    //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">取出定时器的值</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  85.      succeed_flag=1;   //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">至成功测量的标志</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  86.      EX0=0;            //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">关闭外部中断</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  87.   }
  88. //****************************************************************
  89. //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">中断</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">,</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">用做显示</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  90. timer0() interrupt 1  // </font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">中断是</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">1</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">号</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  91.    {
  92.       TH0=0xfd; //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">写入定时器</font></font></font><font color="black"><font face="" "=""><font style="font-size: 10.5pt">0</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">初始值</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  93.      TL0=0x77;         
  94.      switch(flag)   
  95.       {case 0x00:P0=ge; P2=0x7f;flag++;break;
  96.         case 0x01:P0=shi;P2=0xbf;flag++;break;
  97.         case 0x02:P0=bai;P2=0xdf;flag=0;break;
  98.       }
  99.    }

  100. //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">显示数据转换程序</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  101. void conversion(uint temp_data)   
  102. {   
  103.     uchar ge_data,shi_data,bai_data ;
  104.     bai_data=temp_data/100 ;
  105.     temp_data=temp_data%100;   //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">取余运算</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  106.     shi_data=temp_data/10 ;
  107.     temp_data=temp_data%10;   //</font></font></font><font color="black"><font face="宋体"><font style="font-size: 10.5pt">取余运算</font></font></font> <font color="black"><font face="" "=""><font style="font-size: 10.5pt">
  108.     ge_data=temp_data;

  109.     bai_data=SEG7[bai_data];
  110.     shi_data=SEG7[shi_data]&0x7f;
  111.     ge_data =SEG7[ge_data];

  112.     EA=0;
  113.     bai = bai_data;
  114.     shi = shi_data;
  115.     ge  = ge_data ;  
  116.      EA=1;
  117. }
  118. //******************************************************************


  119. void delay_20us()
  120. {  uchar bt ;
  121.     for(bt=0;bt<60;bt++);
  122. }
  123.   void pai_xu()
  124.   {  uint t;
  125.   if (distance[0]>distance[1])
  126.     {t=distance[0];distance[0]=distance[1];distance[1]=t;}  
  127.   if(distance[0]>distance[2])
  128.     {t=distance[2];distance[2]=distance[0];distance[0]=t;}  
  129.   if(distance[1]>distance[2])
  130.     {t=distance[1];distance[1]=distance[2];distance[2]=t;}   
  131.     } </font></font></font></i></div><i></i>

复制代码

没太看懂你问的是什么意思,初值不是你给TH0,TL0赋值了么

这个程序我是网上找的 不明白TH0 TL0的初值是怎么来的  有没有学习超声波测距的视频或好的资料 谢了

根据你的定时时间算出来的。
LZ应该还不太了解定时器这个东西。建议好好看看课本哦。

谁能帮我解释一下那两个中断程序是如何触发的?

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top