找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1461|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

簡(jiǎn)易頻率計(jì)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:355496 發(fā)表于 2018-6-20 20:21 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include <AT89X52.H>
unsigned char code dispbit[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};     //P2的掃描位
unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,0x66,
                               0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x40};    //數(shù)碼管的字形編碼
unsigned char dispbuf[8]={0,0,0,0,0,0,10,10}; //初始化顯示值
unsigned char temp[8];     //存放顯示的數(shù)據(jù)
unsigned char dispcount;   //顯示計(jì)數(shù)器值
unsigned char T0count;     //T0的計(jì)數(shù)器值
unsigned char timecount;   //計(jì)時(shí)計(jì)數(shù)器值
bit flag;                  //標(biāo)志位
unsigned long x;           //頻率值
//頻率計(jì)算函數(shù)
void HzCal(void)
{
  unsigned char i;
  x=T0count*65536+TH0*256+TL0; //得到T0的16位計(jì)數(shù)器值
  for(i=0;i<8;i++)
  {
    temp[i]=0;
  }
         i=0;
         while(x/10)         //拆分
           {
             temp[i]=x%10;
             x=x/10;
             i++;
           }
         temp[i]=x;
         for(i=0;i<6;i++)    //換算為顯示數(shù)據(jù)
           {
             dispbuf[i]=temp[i];
           }
         timecount=0;
         T0count=0;
}

void main(void)
{

  TMOD=0x15;              //設(shè)置定時(shí)器工作方式
  TH0=0;
  TL0=0;
  TH1=(65536-5000)/256;
  TL1=(65536-5000)%256;       //初始化T1
  TR1=1;
  TR0=1;
  ET0=1;
  ET1=1;
  EA=1;                     //開(kāi)中斷

  while(1)
   {
     if(flag==1)
       {
         flag=0;
         HzCal();   //頻率計(jì)算函數(shù)
         TH0=0;
         TL0=0;
         TR0=1;
       }
   }
}
//定時(shí)器T0中斷服務(wù)子函數(shù)
void t0(void) interrupt 1 using 0
{
  T0count++;
}
//定時(shí)器T1中斷服務(wù)子函數(shù)  
void t1(void) interrupt 3 using 0
{
  TH1=(65536-5000)/256;
  TL1=(65536-5000)%256;          //初始化T1預(yù)裝值,1ms定時(shí)
  timecount++;                   //掃描
  if(timecount==200)             //秒定時(shí)
    {
      TR0=0;                     //啟動(dòng)T0
      timecount=0;
      flag=1;
    }
  P2=0xff;                         //初始化選擇引腳
  P0=dispcode[dispbuf[dispcount]]; //輸出待顯示數(shù)據(jù)
  P2=dispbit[dispcount];
  dispcount++;                    //切換到下一個(gè)選擇引腳
  if(dispcount==8)                //如果已經(jīng)掃描完成切換
    {
      dispcount=0;
    }
}
復(fù)
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:364241 發(fā)表于 2018-7-3 20:14 | 只看該作者
有電路圖沒(méi)   
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表