找回密码
 立即注册

QQ登录

只需一步,快速开始

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

这个单片机程序是按键设置有问题吗?

[复制链接]
跳转到指定楼层
楼主
ID:2724 发表于 2008-7-26 18:02 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


 void outkey()                       //P1.4
{
 uchar Second;
   if(out==0)        
   {
  delay(10);
  if(out==0)
  {
   
   while(out==0);
   
  }
  count=0;
  //count_up=0;
  //count_down=0;
  hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;
  Second=Read1302(DS1302_SECOND);
     write1302(0x8e,0x00);    //写入允许
  write1302(0x80,Second&0x7f);
  write1302(0x8E,0x80);          //禁止写入
  done=0;          
   }
}

//**************************************************************************
//升序按键******************************************************************
//**************************************************************************
void Upkey()         //P1.2
{   
   // static char count_up=0;
    Up=1;
             if(Up==0)
          {
     delay(10);
     if(Up==0)
     {
      
      //count_up=count_up+1;  / tkey按一次,count就加1
      count++;
      P21=~P21;
      done=1;    //进入调整模式
      while(Up==0);
     
     }
     
        switch(count_up)
        {
      case 1:
          P01=~P01;  
                                  temp=Read1302(DS1302_SECOND);  //读取秒数
          temp=((temp&0x70)>>4)*10 + (temp&0x0F);
          temp=temp+1;   //秒数加1
                                  up_flag=1;     //数据调整后更新标志
          if((temp)>59)    //超过59秒,清零
                                  {
           temp=0;
          }     
            temp=temp/10*16+temp%10;
          break;
              case 2:
                                  P02=~P02;
          temp=Read1302(DS1302_MINUTE);  //读取分数
          temp=((temp&0x70)>>4)*10 + (temp&0x0F);
          temp=temp+1;  //分数加1
                                  up_flag=1;
          if(temp>59)          //超过59分,清零
          {
           temp=0;
          }
          temp=temp/10*16+temp%10;
          break;
         case 3:

         case 4:

             
           default:break;
             }
      
     //  while(Up==0);
      }
}


//**********************************************************************
//降序按键**************************************************************
//**********************************************************************
void Downkey()        //P1.0
{    
  //static char count_down=0;
  Down=1;
     if(Down==0)
  {
   delay(10);
   if(Down==0)
   {
    //count_down=count_down+1;  / tkey按一次,count就加1
    count++;
   
    done=1;    //进入调整模式
    while(Down==0);
    //P01=~P01;
   }
   switch(count)
   {
    case 1:
                  temp=Read1302(DS1302_SECOND);  //读取秒数
      temp=((temp&0x70)>>4)*10 + (temp&0x0F);
      temp=temp-1;          //秒数减1
                  down_flag=1;       //数据调整后更新标志
      if(temp==-1)     //小于0秒,返回59秒
      temp=59;
      temp=temp/10*16+temp%10;
      break;

    case 2:
                        temp=Read1302(DS1302_MINUTE);  //读取分数
      temp=((temp&0x70)>>4)*10 + (temp&0x0F);
      temp=temp-1;  //分数减1
                        down_flag=1;
      if(temp==-1)
      temp=59;      //小于0秒,返回59秒
      temp=temp/10*16+temp%10;
      break;

 

    case 3:

    case 4:
    
    case 5:

   
         default:break;
   }

 

     
     //  while(Down==0);
  }
}

void show_time()   //数码管显示程序
{
//  DS1302_GetTime(&CurrentTime);  //获取时钟芯片的时间数据
//  Print(CurrentTime.TimeString); //显示时间
//  GotoXY(0,0);
 // Print(CurrentTime.DateString); //显示日期
 // GotoXY(15,0);
 // Print(week_value);             //显示星期
 // GotoXY(11,0);
 // Print("Week"); //在液晶上显示 字母 week
  delay(400);                 //扫描延时
}

 

//****************************************************
//**************模式选择按键**************************
//****************************************************
void Setkey()
{
  Set=1;
  if(Set==0)     //P1.1
     {
           delay(10);
     if(Set==0)
           { //static char count=0;
    count=count+1;  / tkey按一次,count就加1
    P13=~P13;
    P20=~P20;
    done=1;    //进入调整模式
    while(Set==0);
   }
   }
 

//*****************************************************
//**************按键功能执行***************************
//*****************************************************
void keydone()
{        uchar Second;
  /* if(flag==0)    //关闭时钟,停止计时
         { Write1302(0x8e,0x00); //写入允许
           temp=Read1302(0x80);
           Write1302(0x80,temp|0x80);
        Write1302(0x8e,0x80); //禁止写入
           flag=1;
         }*/
         Setkey();                //扫描模式切换按键
   switch(count)
   {
  
   case 1:do             //count=1,调整秒
            {
                  
                   outkey();      //扫描跳出按钮
       Upkey();                //扫描加按钮
       Downkey();              //扫描减按钮
       if(up_flag==1||down_flag==1)  //数据更新,重新写入新的数据
       {
         write1302(0x8e,0x00); //写入允许
         write1302(0x80,temp); //写入新的秒数
         write1302(0x8e,0x80); //禁止写入
         up_flag=0;
         down_flag=0;
      }
                     if(Down!=0&&Up!=0)
                     {
      hide_sec++;
            if(hide_sec>3)
            hide_sec=0;
                     }
      else hide_sec=0;
      show_time();         //显示数据
      }while(count==2);break; 
    case 2:do             //count=2,调整分
            { P22=~P22;
        hide_sec=0;
        outkey();
        Upkey();
        Downkey();
       if(temp>0x60)
         temp=0;
       if(up_flag==1||down_flag==1)
       {
         write1302(0x8e,0x00); //写入允许
         write1302(0x82,temp); //写入新的分数
          write1302(0x8e,0x80); //禁止写入
          up_flag=0;
          down_flag=0;
      }
                     if(Down!=0&&Up!=0)
                     {
        hide_min++;
            if(hide_min>3)
            hide_min=0;
                     }
      else hide_min=0;
      show_time();
      }while(count==3);break;
  
    case 8: count=0;hide_year=0;  //count8, 跳出调整模式,返回默认显示状态
               Second=Read1302(DS1302_SECOND);
                  write1302(0x8e,0x00); //写入允许
               write1302(0x80,Second&0x7f);
               write1302(0x8E,0x80);          //禁止写入
      done=0;
    break; //count=7,开启中断,标志位置0并退出
    default:break;

   }

}
 void main(void)
{
 flag=1;            //时钟停止标志
 P2=0x00;    //消隐
// P1=0xff;
 up_flag=0;   //数据是否更新标志
 down_flag=0;  //数据是否更新标志
 Initial_inti_time();  //中断初始化
 Initial_DS1302();   //时钟芯片DS_1302初始化
 while(1)
 {  
       // while(done==0)           
 // while(done==1)
          keydone();     //进入调整模式
      { 
            show_time();    //数码管显示数据
            flag=0;                 
        Setkey();  //扫描各功能键
   }
 }
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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