日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]此時(shí)間程序沒(méi)有用到ds1302等芯片,而是用單片機(jī)自帶的定時(shí)器制作的,并且可調(diào)整時(shí)間。請(qǐng)大家多多指教 #include"reg52.h" #include "intrins.h" #define uchar unsigned char #define uint unsigned int ui

此時(shí)間程序沒(méi)有用到ds1302等芯片,而是用單片機(jī)自帶的定時(shí)器制作的,并且可調(diào)整時(shí)間。請(qǐng)大家多多指教
#include"reg52.h"
#include "intrins.h"
#define uchar unsigned char
#define uint  unsigned int
 
uint schedule[6];
uint count,b; //計(jì)數(shù):以秒為單位
uint  number[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};  //對(duì)應(yīng)數(shù)碼管的0-9
uint a[4]={0xfe,0xfd,0xfb,0xf7}; //對(duì)應(yīng)的四個(gè)數(shù)碼管
uint minute[2],second[2] ;
sbit P30=P3^0; //P3口作為鍵盤(pán)控制端
sbit P31=P3^1;
sbit P36=P3^6;
sbit P35=P3^5;
sbit P34=P3^4;
sbit P33=P3^3;
sbit P32=P3^2;
sbit P37=P3^7;
void assign();
void show();
 
void delay(unsigned n);
void main()
{
  uint i ;
  EA=1;//總中斷打開(kāi)
  IT0=1;//外部中斷打開(kāi)
  EX0=1; //
  count=43200;
  TMOD=0x10;
  P30=0;
  P31=1;
  P32=1;
  P33=1;
  P34=0;
  P35=1;
  P36=1;
  P37=1;
  while(1)
  { 
      for(i=0;i<=19;i++)//每循環(huán)一次用時(shí)50ms.
   {
       TR1=1;//選擇計(jì)數(shù)器1
    TH1= 0x3c;
    TL1=0xb0;//賦初值
    show(); //顯示函數(shù)
    TF1=0;   //恢復(fù)標(biāo)志

   }
   count++;
 
       
  }

}
void assign() //給小時(shí),分鐘,秒賦值
{
     
  if(count>=46800) //滿了13個(gè)小時(shí)轉(zhuǎn)化為1點(diǎn)鐘
    count=count-43200;
 if(count>=36000)  //計(jì)算小時(shí) 前一位 減少算法
      schedule[0]=minute[0]=1;
 else
   schedule[0]=minute[0]=0;
 schedule[1]=minute[1]=count/3600%10;//計(jì)算小時(shí)的后一位
 schedule[2]=second[0]=  count%3600/600;  //計(jì)算分鐘的前一位
 schedule[3]=second[1]=  count%3600/60%10;//計(jì)算分鐘的后一位
        
}
void show()   //顯示函數(shù)
{
     uint i;
  
    while(1)
 {
   
    assign();
    for(i=0;i<4;i++)
    { 
        P1=a[i]; //亮那個(gè)數(shù)碼管
       P2=number[schedule[i]]; //亮一個(gè)什么樣的數(shù)字
      if(i==1 )
       P2=P2-0x80;  //亮小數(shù)點(diǎn)便于好看
     delay(50);//延時(shí)函數(shù),便于亮的清晰
   
  
  
    }
 if(TF1==1) //已經(jīng)有50ms
    break;
 }
}
void delay(unsigned n) //延時(shí)程序
{
    uint i;
 for(i=1;i<=n;i++)
 {

     if(TF1==1) //已經(jīng)有50ms
    break;
if(P31==0)   //調(diào)表
{
          
        for(b=0;b<3000;b++) //放抖動(dòng):按的時(shí)間太短
             {
       
    }          
       if(P31==1)
    goto back5;
    
 
   
    if(P31==0)
  {
  while(1)
      {
     
      show();
   TF1=0;   //恢復(fù)標(biāo)志
   if(P32==0) //加一分鐘
   {
        for(b=0;b<3000;b++)
             {}  
       if(P32==1)
    goto back1;
       count=count+60;
    if(P32==0)
    
         {
  while(1)
  {
  show();
  TF1=0;   //恢復(fù)標(biāo)志 
  if(P32==1)
  break;
  }
  }
  
      }
   back1: show(); 
  if(P33==0)   //減一分鐘
      {
        for(b=0;b<3000;b++)
             {}  
       if(P33==1)
    goto back2;
       count=count-60;

    if(P33==0)
    {
  while(1)
  {
  show();TF1=0;   //恢復(fù)標(biāo)志
  if(P33==1)
  break;
  }
  }
  
      }
      
      back2:show(); 
   if(P36==0)  //加一個(gè)小時(shí)
      {
        for(b=0;b<3000;b++)
             {}  
       if(P36==1)
    goto back3;
       count=count+3600;

    if(P36==0)
    {
  while(1)
  {
  show(); TF1=0;   //恢復(fù)標(biāo)志
  if(P36==1)
  break;
  }
  }
  
      }
      
      back3:show();
   if(P37==0)   //減一個(gè)小時(shí)
      {
        for(b=0;b<3000;b++)
             {}  
       if(P37==1)
    goto back4;
    if(schedule[0]==0&&schedule[1]==1)  //處理1點(diǎn)鐘到12點(diǎn)
    count=count+39600;
    else
       count=count-3600;

    if(P37==0)
    {
  while(1)
  {
  show(); TF1=0;   //恢復(fù)標(biāo)志
  if(P37==1)
  break;
  }
  }            
  
      }
      
      back4:show(); 
   if(P35==0)//沒(méi)有必要防抖
    break;

  }
}
   back5: show(); //按的時(shí)間太duan

  }   
  
      }
    
 
 }


 此程序有幾個(gè)缺點(diǎn):一,調(diào)時(shí)間的時(shí)候有點(diǎn)暗。二,算法比較差。三,代碼比較難看。四,當(dāng)時(shí)我想從0到24小時(shí)計(jì)時(shí),但count為int溢出了,后面我不用count計(jì)時(shí),可是可以但是調(diào)時(shí)間的話有比較難寫(xiě),不想亂費(fèi)時(shí)間了,改成長(zhǎng)整型有不知道那個(gè)地方出問(wèn)題。希望高手指點(diǎn)。
分享到:
 

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉