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

QQ登錄

只需一步,快速開始

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

自己做的基于51單片機(jī)+lcd1602的計(jì)數(shù)器 希望可以幫到大家

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
這是個(gè)人自己做的,希望可以幫到大家、


單片機(jī)代碼:
  1. #include<AT89X51.H>
  2. #include<intrins.h>

  3. sbit RS=P3^3;
  4. sbit RW=P3^6;
  5. sbit E=P3^5;
  6. sbit BF=P1^7;   
  7. unsigned char chan=0xc0;

  8. int tcount=0,t,p;  
  9. long q;              

  10. void delay1ms()     
  11. {  unsigned char i,j;
  12.    for(i=0;i<10;i++)
  13.            for(j=0;j<33;j++);
  14. }

  15. void delay(unsigned int n)  //延時(shí)
  16. {  unsigned int i;
  17.    for(i=0;i<n;i++)
  18.      delay1ms();
  19. }

  20. void nop(unsigned s)  //1602的延時(shí)
  21. {  unsigned char i;      
  22.    for(i=0;i<s;i++)            
  23.      _nop_();
  24. }

  25. unsigned char busytest(void)  
  26. {   bit result;      
  27.         RS=0;      
  28.         RW=1;                
  29.         E=1;      
  30.         nop(4);      
  31.         result=BF;      
  32.         E=0;      
  33.         return result;
  34. }

  35. void writecontrol(unsigned condata)  
  36. {while(busytest()==1);      
  37.         RS=0;      
  38.         RW=0;      
  39.         E=0;      
  40.         nop(2);      
  41.         P1=condata;      
  42.         nop(4);      
  43.         E=1;      
  44.         nop(4);      
  45.         E=0;
  46. }

  47. void writedata(unsigned d)  
  48. {while(busytest()==1);      
  49.         RS=1;      
  50.         RW=0;      
  51.         E=0;      
  52.         P1=d;      
  53.         nop(4);
  54.         E=1;      
  55.         nop(4);      
  56.         E=0;
  57. }

  58. void lcdinit(void)  
  59. {delay(15);      
  60.         writecontrol(0x38);
  61.         delay(5);      
  62.         writecontrol(0x38);      
  63.         delay(5);      
  64.         writecontrol(0x38);      
  65.         delay(5);      
  66.         writecontrol(0x0c);      
  67.         delay(5);      
  68.         writecontrol(0x06);      
  69.         delay(5);      
  70.         writecontrol(0x01);      
  71.          delay(5);
  72. }


  73. void display(long temp)  
  74. {  
  75. int a[6],i;
  76.          a[0]=temp/100000;        //65536
  77.          a[1]=(temp%100000)/10000;
  78.          a[2]=(temp%10000)/1000;
  79.          a[3]=(temp%1000)/100;
  80.          a[4]=(temp%100)/10;
  81.          a[5]=temp%10;
  82.           writecontrol(0x80+0x00);
  83.                 writedata('C');
  84.           writedata('0');   
  85.                 writedata('N');       
  86.                 writedata('T');
  87.           writedata('E');   
  88.                 writedata('R');
  89.                 writedata(':');
  90.           writecontrol(0x80+0x40+0X05);
  91.     for(i=0;i<6;i++)
  92.          {writedata(a[i]+0x30);
  93.    }
  94. }

  95. void cry()  //初始化定時(shí)器,定時(shí)器0計(jì)數(shù),定時(shí)器1計(jì)時(shí)50ms
  96. { TMOD=0x15;
  97.         TH0=0x00;
  98.         TL0=0x00;
  99.         TH1=0x3c;
  100.         TL1=0xb0;
  101.         TR0=1;TF0=0;
  102.         TR1=1;TF1=0;
  103.         EA=1;
  104.         ET0=1;
  105.         ET1=1;
  106.         p=0;
  107.         tcount=0;
  108. }

  109. void jishi() interrupt 3   //定時(shí)器1定時(shí)0.5s
  110. {        TH1=0x3c;
  111.         TL1=0xb0;
  112.         TF1=0;
  113.         tcount++;
  114.         if(tcount==10)
  115.         {tcount=0;
  116.          t=1;
  117.         }
  118. }

  119. void jishu() interrupt 1   //定時(shí)器0計(jì)數(shù),每次溢出p+1,計(jì)算溢出次數(shù)
  120. { TH0=0x00;
  121.         TL0=0x00;
  122.         TF0=0;
  123.         p=p+1;
  124. }

  125. void main()
  126. {
  127.   lcdinit();
  128.         P2=0x0f;
  129.         P2=0X1F;
  130.         display(q);
  131.         cry();
  132.   while(1)
  133.         {
  134.           while(t==1)  //當(dāng)0.5s到了
  135.                 {
  136.                  q=(p*65536+TH0*256+TL0)*2;   //計(jì)算頻率
  137.                  display(q);//送1602顯示
  138.                  t=0;p=0;cry();  //重新初始化
  139.                 }
  140.         }
  141.        
  142. }
復(fù)制代碼

下載:
計(jì)數(shù)器.zip (85.89 KB, 下載次數(shù): 42)



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩

相關(guān)帖子

回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:372020 發(fā)表于 2018-7-15 16:18 | 只看該作者
為什么這個(gè)代碼沒有數(shù)字變化?
回復(fù)

使用道具 舉報(bào)

板凳
ID:381237 發(fā)表于 2018-8-7 21:45 | 只看該作者
這個(gè)就是單片機(jī)1602LCD顯示數(shù)字頻率計(jì),并不是計(jì)數(shù)器。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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