51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > ◆本站实例 > 详细内容
键盘+可调时钟+计算器+I2C闹钟(ADC0809扩展)
作者:月夜听风 来源:微控实验网  发布时间:2009/3/10  阅读次数:7632  字体大小: 【】 【】【

/****************************************/
void Write24c02(uchar *Wdata,uchar RomAddress,  //写入I2C程序
                                                   uchar number)
{
  Start();            //开始条件
  Write8Bit(WriteDeviceAddress);  //写8位设备地址
  TestAck();            
  Write8Bit(RomAddress);
  TestAck();
  for(;number!=0;number--)
  {
    Write8Bit(*Wdata);
    TestAck();
    Wdata++;
  }
  Stop();
  DelayMs(10);
}

/***************************************************/
uchar Read8Bit()    //读一个字节
  {
  uchar temp,rbyte=0;
  for(temp=8;temp!=0;temp--)
    {
    SCL=1;
    rbyte=rbyte<<1;
    if(SDA)rbyte|=0x01;
    else rbyte|=0x00;
    SCL=0;
    }
  return(rbyte);
}

void Read24c02(uchar *RamAddress,uchar RomAddress,  //读出I2C程序
                                                 uchar bytes)
{
  Start();
  Write8Bit(WriteDeviceAddress);
  TestAck();
  Write8Bit(RomAddress);
  TestAck();
  Start();
  Write8Bit(ReadDviceAddress);
  TestAck();
  while(bytes!=0)
  {
  *RamAddress=Read8Bit();
  Ack();          //应答
  RamAddress++;
  bytes--;
  }
  *RamAddress=Read8Bit();
  NoAck();        //非应答
  Stop();          //结束条件
}
/**************************///

//lcd
//以下是读忙状态函数
// 功能:在正常读写操作之前检测LCD模块的忙状态,
//D7 = 0:LCD控制器空闲;D7 = 1:LCD控制器忙
void CheckBusy( void )
{   
  CR_ADD=0XFF;                     //P0口写1
  Delay(1);
  while(CR_ADD&Busy );//若D7=1,则DataPort&Busy=1,说明忙,等待

}
//以下是写入IR寄存器命令函数
//功能:向LCD模块写入命令字符CMD
//若 AttribC = 1 检测忙信号,若 AttribC = 0 不检测忙信号
void WriteIR(uchar CMD,uchar AttribC)
  {
  if (AttribC) CheckBusy();//若AttribC =1,则检测忙信号,否则,顺序执行
  CW_ADD=CMD;

}
//以下是写入DDR寄存器函数
//功能:在当前光标位置显示一个字符
void WriteDDR( char c )
{
  CheckBusy();
  DW_ADD=c;        //数据发送

}
void d_sendCGRAM(uchar *c)
{
  uchar i;
  WriteIR(0x40,1);           //造字。年。月。日
  for(i=0;i<24;i++)
  {
  WriteDDR(*c++);
  }
}
//以下是光标定位函数
void LcdPos(uchar Xpos,uchar Ypos)
{
  uchar tmop;       //定义tmp为指令码
  Xpos&=0x0f;  //16xx型液晶的范围是0~15
  Ypos&=0x01;  //Y的范围是0~1
  tmop=Xpos;
  if(Ypos==1)                    
  tmop|=0xc0;                     //若Ypos为1(显示第2行),地址码+0xc0
  tmop|=0x80;                   //若Ypos为0(显示第1行),地址码+0x80
  WriteIR(tmop,0);
}
//以下是初始化函数
//功能:向LCD模块写入不同命令,完成必要的初始化过程
void LcdReset( void )
{
       WriteIR( 0x38,0);   //功能设置指令,8位接口,显示2行,5×7字符
  WriteIR( 0x38,1);  //设置显示模式(以后均检测忙信号)
       WriteIR( 0x08,1);  //显示开关控制指令,显示关闭
       WriteIR( 0x01,1);  //清屏指令,将DDRAM数据全部填入"空白"
  WriteIR( 0x06,1);  //输入方式设置指令,字符不动,光标自动右移一格
  WriteIR( 0x0f,1);  //显示开关控制指令,显示器开,光标开,光标闪烁
}
//以下是在指定行列显示字符函数
void WriteChar(uchar Xpos,uchar Ypos,char c)//Xpos=0(第1列),Ypos=1(第2行)
{
        LcdPos(Xpos,Ypos);
      WriteDDR(c);
}
//以下是显示字符串函数
void WriteString(uchar Xpos,uchar Ypos,char s[])
{
uchar p=0;
  for(;;)
   {
WriteChar(Xpos,Ypos,s[p]);
     p++;
     if(s[p]==0)
           break;
     if(++Xpos>=16)             //每行最多显示16个字符
    { Xpos=0;                   //如果一行显示不完,则转到下一行或上一行的第一个字符
     Ypos^=1;}               //如果一行显示不完,则转到下一行或上一行显示
   }
}
/************************/
/************************/
/************************/
void Lcd_xian()
{  
  uchar tmp;
  uchar sd[]="IN0: .     V           IN1: .     V           ";
  if(Sbz)//计算器启动显示
      {
        if(skbz)
          {
          skbz=0;
          WriteIR( 0x01,1);  //清屏指令,将DDRAM数据全部填入"空白"
               
          }
      
        if(xbz2){WriteChar(11,1,(0x30|b1));}else{WriteChar(11,1,' ');}
        if(xbz3){WriteChar(12,1,(0x30|c1));}else{WriteChar(12,1,' ');}
        if(xbz4){WriteChar(13,1,(0x30|d1));}else{WriteChar(13,1,' ');}
        if(xbz5){WriteChar(14,1,(0x30|e1));}else{WriteChar(14,1,' ');}
                  if(xbz6){WriteChar(15,1,(0x30|f1));}
      }
     else  if(Tbz)//调时钟显示标志
      {
       switch(kk)
          {case 0:{tmp=(m/10);  WriteChar(3,1,(0x30|tmp));tmp=(m%10);WriteChar(4,1,(0x30|tmp));  
              if(T05s)
                {tmp=(year/10);  WriteChar(2,0,(0x30|tmp));tmp=(year%10);WriteChar(3,0,(0x30|tmp));}
              else{WriteChar(2,0,' ');WriteChar(3,0,' ');}
              }break;  
           case 1:{tmp=(year/10);  WriteChar(2,0,(0x30|tmp));tmp=(year%10);WriteChar(3,0,(0x30|tmp));WriteChar(4,0,0x00);//year  
              if(T05s)
                {tmp=(month/10);  WriteChar(5,0,(0x30|tmp));tmp=(month%10);WriteChar(6,0,(0x30|tmp));}
              else{WriteChar(5,0,' ');WriteChar(6,0,' ');}
              }break;  
           case 2:{tmp=(month/10);  WriteChar(5,0,(0x30|tmp));tmp=(month%10);WriteChar(6,0,(0x30|tmp));WriteChar(7,0,0x01);//月  
              if(T05s)
                {tmp=(day/10);  WriteChar(8,0,(0x30|tmp));tmp=(day%10);WriteChar(9,0,(0x30|tmp));}
              else{WriteChar(8,0,' ');WriteChar(9,0,' ');}
              }break;  
           case 3:{tmp=(day/10);  WriteChar(8,0,(0x30|tmp));tmp=(day%10);WriteChar(9,0,(0x30|tmp));WriteChar(10,0,0x02);//日  
              if(T05s)
                {tmp=(h/10);  WriteChar(0,1,(0x30|tmp));tmp=(h%10);WriteChar(1,1,(0x30|tmp));}
              else{WriteChar(0,1,' ');WriteChar(1,1,' ');}
              }break;  
           case 4:{tmp=(h/10);  WriteChar(0,1,(0x30|tmp));tmp=(h%10);WriteChar(1,1,(0x30|tmp));WriteChar(2,1,':');  
              if(T05s)
                {tmp=(m/10);WriteChar(3,1,(0x30|tmp));tmp=(m%10);WriteChar(4,1,(0x30|tmp));}
              else{WriteChar(3,1,' ');WriteChar(4,1,' ');}
              }break;
           case 5:{tmp=(m/10);WriteChar(3,1,(0x30|tmp));tmp=(m%10);WriteChar(4,1,(0x30|tmp));}break;
              
               }

           }
     else  if(Jbz)//键盘输入标志
      {
        if(skbz)
          {
          skbz=0;
          WriteIR( 0x01,1);  //清屏指令,将DDRAM数据全部填入"空白"
               
          }
        WriteChar(12,1,(0x30|a));
        WriteChar(13,1,(0x30|b));
        WriteChar(14,1,(0x30|c));
        WriteChar(15,1,(0x30|d));
       }
  else if(Abz)//ADC0809启动显示程序
      {
        if(skbz)
          {
          skbz=0;TR1=0;
          WriteIR( 0x01,1);  //清屏指令,将DDRAM数据全部填入"空白"
           WriteString(0,0,sd);
               
          }

        tmp=Adcx0[1];WriteChar(4,0,(0x30|tmp));
              WriteChar(5,0,'.');
        tmp=Adcx0[2];WriteChar(6,0,(0x30|tmp));
        tmp=Adcx0[3];WriteChar(7,0,(0x30|tmp));
        tmp=Adcx0[4];WriteChar(8,0,(0x30|tmp));
        
        tmp=Adcx1[1];WriteChar(4,1,(0x30|tmp));
              WriteChar(5,1,'.');
        tmp=Adcx1[2];WriteChar(6,1,(0x30|tmp));
        tmp=Adcx1[3];WriteChar(7,1,(0x30|tmp));
        tmp=Adcx1[4];WriteChar(8,1,(0x30|tmp));
        TR1=1;                   //延是等待复位
      }

  else if(Shbz)//闹钟显示程序
      {
        if(skbz)
          {
          skbz=0;TR1=0;
          WriteIR( 0x01,1);  //清屏指令,将DDRAM数据全部填入"空白"
          tmp=Sh[0];tmp/=10;WriteChar(0,1,(0x30|tmp));tmp=Sh[0];tmp%=10;WriteChar(1,1,(0x30|tmp));
          WriteChar(2,1,':');
          tmp=Sh[1];tmp/=10;WriteChar(3,1,(0x30|tmp));tmp=Sh[1];tmp%=10;WriteChar(4,1,(0x30|tmp));
          TR1=1;
               }

         switch(kk)      
          {
          case 0:{
              if(T05s)
                {
                tmp=Sh[0];tmp/=10;WriteChar(0,1,(0x30|tmp));tmp=Sh[0];tmp%=10;WriteChar(1,1,(0x30|tmp));}
              else{WriteChar(0,1,' ');WriteChar(1,1,' ');}
              }break;  
           case 1:{tmp=Sh[0];tmp/=10;WriteChar(0,1,(0x30|tmp));tmp=Sh[0];tmp%=10;WriteChar(1,1,(0x30|tmp));WriteChar(2,1,':');  
              if(T05s)
                {
                tmp=Sh[1];tmp/=10;WriteChar(3,1,(0x30|tmp));tmp=Sh[1];tmp%=10;WriteChar(4,1,(0x30|tmp));}
              else{WriteChar(3,1,' ');WriteChar(4,1,' ');}
              }break;  
           case 2:{tmp=Sh[1];tmp/=10;WriteChar(3,1,(0x30|tmp));tmp=Sh[1];tmp%=10;WriteChar(4,1,(0x30|tmp));  
                  tmp=Sh[2];
                  if(tmp!=0)          
                    {
                   WriteChar(13,1,'O');
                   WriteChar(14,1,'N');
                  WriteChar(15,1,' ');
                    }
                  else
                    {
                   WriteChar(13,1,'O');
                   WriteChar(14,1,'F');
                   WriteChar(15,1,'F');
                    }
              }break;  
    
          }

           

      }

       else//时钟显示
      {
        if(slbz)    
          {
          TR1=0;
          slbz=0;
          WriteIR( 0x01,1);  //清屏指令,将DDRAM数据全部填入"空白"
          TR1=1;
          }
        else if(Key==0){jm=61;Delay(1);}
         if(jm!=m)
          {
          jm=m;
          tmp=(shiji/10);  WriteChar(0,0,(0x30|tmp));//
          tmp=(shiji%10);  WriteChar(1,0,(0x30|tmp));//
          tmp=(year/10);  WriteChar(2,0,(0x30|tmp));//
          tmp=(year%10);  WriteChar(3,0,(0x30|tmp));//
          WriteChar(4,0,0x00);//Xpos=4(第5列),Ypos=0(第1行)年
          tmp=(month/10);  WriteChar(5,0,(0x30|tmp));//
          tmp=(month%10);  WriteChar(6,0,(0x30|tmp));//
          WriteChar(7,0,0x01);//Xpos=7(第8列),Ypos=0(第1行)月
          tmp=(day/10);  WriteChar(8,0,(0x30|tmp));//
          tmp=(day%10);  WriteChar(9,0,(0x30|tmp));//
          WriteChar(10,0,0x02);//Xpos=10(第11列),Ypos=0(第1行)日
          tmp=(h/10);    WriteChar(0,1,(0x30|tmp));//
          tmp=(h%10);    WriteChar(1,1,(0x30|tmp));
          tmp=(m/10);    WriteChar(3,1,(0x30|tmp));
          tmp=(m%10);    WriteChar(4,1,(0x30|tmp));
          tmp=Sh[2];if(tmp!=0)
                  {
                   WriteChar(13,1,'O');
                   WriteChar(14,1,'N');
                  WriteChar(15,1,' ');
                  if((Sh[0]==h)&(Sh[1]==m))WriteChar(14,0,'$');else WriteChar(14,0,' ');
                  }
                 else{
                  WriteChar(13,1,'O');
                   WriteChar(14,1,'F');
                   WriteChar(15,1,'F');
                  }                  
                       


          }
  
          if(T05s){WriteChar(2,1,' ');}else{ WriteChar(2,1,':');}  //
             tmp=(s/10);          //
          WriteChar(6,1,(0x30|tmp));
          tmp=(s%10);  
          WriteChar(7,1,(0x30|tmp));

      }

}
            
//延时程序
void Delay(uchar i)           //延时程序,i是形式参数
{
  uchar j;
  while(i--)                         //变量i由实际参数传入一个值,因此i不能赋初值
  for(j=0;j<125;j++);

}

//以下是定时/计数器1中断函数
void timer1() interrupt 3
{
  uchar tmp;

  TR1=0;
    TH1=(65536-9000)/256;
  TL1=(65536-9000)%256;  //定时时间
  if(Sbz)//计算器启动显示
      {
        if(b1!=0){xbz2=1;xbz3=1;xbz4=1;xbz5=1;xbz6=1;}
        else if(c1!=0){xbz2=0;xbz3=1;xbz4=1;xbz5=1;xbz6=1;}  
        else if(d1!=0){xbz2=0;xbz3=0;xbz4=1;xbz5=1;xbz6=1;}
        else if(e1!=0){xbz2=0;xbz3=0;xbz4=0;xbz5=1;xbz6=1;}
             else {xbz2=0;xbz3=0;xbz4=0;xbz5=0;xbz6=1;}
        
        if(xbz3){tmp=tab[c1];PB8255=tmp;led0=1;Delay(2);led0=0;}
        if(xbz4){tmp=tab[d1];PB8255=tmp;led1=1;Delay(2);led1=0;}
        if(xbz5){tmp=tab[e1];PB8255=tmp;led2=1;Delay(2);led2=0;}
        if(xbz6){tmp=tab[f1];PB8255=tmp;led3=1;Delay(2);led3=0;}    
      }
     else  if(Tbz)//调时钟显示标志
      {

        tmp=tab[h/10];if(kk==3){if(T05s)tmp=0;}PB8255=tmp;led0=1;Delay(2);led0=0;
        tmp=tab[h%10];if(kk==3){if(T05s)tmp=0;}PB8255=tmp;led1=1;Delay(2);led1=0;
        tmp=tab[m/10];if(kk==4){if(T05s)tmp=0;}PB8255=tmp;led2=1;Delay(2);led2=0;
        tmp=tab[m%10];if(kk==4){if(T05s)tmp=0;}PB8255=tmp;led3=1;Delay(2);led3=0;
           }
     else  if(Jbz)//键盘输入标志
      {
        PB8255=tab[a];led0=1;Delay(2);led0=0;
        PB8255=tab[b];led1=1;Delay(2);led1=0;
        PB8255=tab[c];led2=1;Delay(2);led2=0;
        PB8255=tab[d];led3=1;Delay(2);led3=0;
       }
  else if(Abz)//ADC0809启动显示程序
      {  

      
        if(Axbz)  //in0 huo in1
        {              
        tmp=Adcx0[1];tmp=tab1[tmp];PB8255=tmp;led0=1;Delay(2);led0=0;
        tmp=Adcx0[2];tmp=tab[tmp];PB8255=tmp;led1=1;Delay(2);led1=0;
        tmp=Adcx0[3];tmp=tab[tmp];PB8255=tmp;led2=1;Delay(2);led2=0;
        tmp=Adcx0[4];tmp=tab[tmp];PB8255=tmp;led3=1;Delay(2);led3=0;
        }
      else{        
        tmp=Adcx1[1];tmp=tab1[tmp];PB8255=tmp;led0=1;Delay(2);led0=0;
        tmp=Adcx1[2];tmp=tab[tmp];PB8255=tmp;led1=1;Delay(2);led1=0;
        tmp=Adcx1[3];tmp=tab[tmp];PB8255=tmp;led2=1;Delay(2);led2=0;
        tmp=Adcx1[4];tmp=tab[tmp];PB8255=tmp;led3=1;Delay(2);led3=0;
        }      

      }
     else  if(Shbz)//调时钟显示标志
      {

    tmp=Sh[0];tmp=tab[tmp/10];if(kk==0){if(T05s)tmp=0;}PB8255=tmp;led0=1;Delay(2);led0=0;
    tmp=Sh[0];tmp=tab[tmp%10];if(kk==0){if(T05s)tmp=0;}PB8255=tmp;led1=1;Delay(2);led1=0;
    tmp=Sh[1];tmp=tab[tmp/10];if(kk==1){if(T05s)tmp=0;}PB8255=tmp;led2=1;Delay(2);led2=0;
    tmp=Sh[1];tmp=tab[tmp%10];if(kk==1){if(T05s)tmp=0;}PB8255=tmp;led3=1;Delay(2);led3=0;
           }

  

       else//时钟显示
      {

        tmp=tab[h/10];PB8255=tmp;led0=1;Delay(2);led0=0;tmp=(h/10);  
        tmp=tab[h%10];if(T05s){tmp=(tmp|0x80);}PB8255=tmp;led1=1;Delay(2);led1=0;
        tmp=tab[m/10];PB8255=tmp;led2=1;Delay(2);led2=0;
        tmp=tab[m%10];PB8255=tmp;led3=1;Delay(2);led3=0;
      }
  TR1=1;
}

//以下是定时/计数器0中断函数
void timer0() interrupt 1 using 1
{  
static  uchar a;

    TH0=(65536-46075)/256;
  TL0=(65536-46075)%256;  //定时时间为50MS
  a++;
  if(a>9)T05s=1;
   if(a>19){s++;a=0;T05s=0;}// 20*50ms=1000ms=1s
   if(s>59){m++;s=0;}
   if(m>59){h++;m=0;}
   if(h>23){h=0;day++;}
   if(day>30){day=0;month++;}
   if(month>12){month=0;year++;}
   if(year>99)year=0;
}

来源:微控实验网
我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[26]
  • 评论人:[匿名] 时间: [2019/12/6 23:54:58] IP:[5.153.178.9*]
  • 8TD 2826cc NEW ROK Manual Lock Free Wheel Hubs Nissan GQ GU Patrol Ford Maverick Y60 Y61. .. Excellent Value for Money. The Adventure Kings 12v Accessory Panel is the best way to access your camping 12v systems battery a href=http//makemoneyonlinemoney.info/the-universal-mary-sue-litmus-test-simple-ways-to-make-moneysimple ways to make money/a
  • 评论人:[匿名] 时间: [2019/12/6 20:29:32] IP:[5.153.178.9*]
  • ately after removal of pressure, Stram.; elevated red blotches, in palms, Fluor. ac.; bright red spot on ball of right, with itching in evening, Sep.; red, hard spot on skin, Hands, fever; shaking begins (intermittent), LIGels. af Hands, formication a href=http//itchypatchesonskin.ml/organic-riot-dazzle-serum-for-even-skin-tone-perfect-skin-care-for-dry-itchydry itchy patches on skin/a
  • 评论人:[匿名] 时间: [2019/11/23 9:30:13] IP:[5.153.178.9*]
  • 1 hour ago Getting your moneys worth plan your dining Outside of meal plans and points, there are ways to save money during Judiciary rules Student Union President and Vice President violated constitution, Senate responds. a href=http//howtosavemoneyfast.cf/are-snail-creams-good-for-your-how-to-save-your-money-fast-skinhow to save your money fast/a
  • 评论人:[匿名] 时间: [2019/11/5 18:40:48] IP:[5.153.178.9*]
  • Home. There is no doubt; insects have the capability of being the creepiest Learn about different insect parts, which insects can walk on water, how ants The damage is mostly unsightly but, in the long-term, may kill the branches they feed on. EDIS is the Electronic Data Information Source of UF/IFAS Extension, a href=http//howtogetridofantsinhouse.ga/inground-how-to-get-rid-of-sugar-ants-in-the-house-liners-bloghow to get rid of sugar ants in the house/a
  • 评论人:[匿名] 时间: [2019/10/15 12:08:48] IP:[5.153.178.9*]
  • Introduction. While dental software has been around since the 1980s, few dentists can say they have a truly вpaperlessв office. Practice management systems such as Dentrix, Carestream Health, and DentiMax have seen widespread adoption, but fewer dentists use software for charting or clinical documentation. a href=http//dentures.denta.top/art/affordable-dentures/Affordable dentures/a

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号