3989|14

98

帖子

0

TA的资源

一粒金砂(初级)

楼主
 

请大侠们帮我查查错啊!我找了一周了,还是没找到。 [复制链接]

/**************************************************************/
//                HT1380实时时钟LCD显示
//                使用Keil C51
//                2010.03.19
/*************************************************************/
/****使用AT89S51单片机,12MHz晶振,HT1380时钟芯片,LCD显示****/
//key0为调时功能键,key1为加1键,key2为减1键
#include
#include
#define uchar unsigned char
#define uint unsigned int
/**********HT1380引脚定义*************/
sbit clock_data=P1^1;
sbit clock_sclk=P1^0;
sbit clock_rst=P1^2;
/*********LCD读写引脚定义************/
sbit lcdrs=P1^3;
sbit lcdrw=P1^4;
sbit lcden=P1^5;
/**********调时按键引脚定义**********/
sbit negative=P3^0;
sbit key1=P3^4;
sbit key2=P3^5;
sbit key3=P3^6;

uchar num=0,shi,ge;//调时参数
uchar wei;
uchar date=1,mon=1,day=1;
uint sec=0,min=0,hour=0,year=0;
uchar data databuf;//数据缓冲

uchar code table1[]=" 2003-01-14 Mon ";
uchar code table2[]="    01:03:45    ";
uchar code table3[][3]={{'M','o','n'},{'T','u','e'},{'W','e','d'},{'T','h','u'},{'F','r','i'},{'S','a','t'},{'S','u','n'}};

void delay(uchar n)  //时间延时函数
  {
          uchar x,y;
        for(x=n;x>0;x--)
                for(y=0;y<110;y++);                                       
  }
/**************向HT1380读数据*************/
uchar read_ht1380(uchar cmd)
{
        uchar i;
        clock_rst=0;
        _nop_();
        clock_sclk=0;
        _nop_();
        clock_rst=1;//复位信号拉高
        _nop_();_nop_();_nop_();_nop_();
        for(i=0;i<8;i++)        //写入读数据命令
        {
                clock_sclk=0;
                if((cmd&0x01)==1)
                        clock_data=1;
                else
                        clock_data=0;
                clock_sclk=1;
                cmd>>=1;//低位在前
        }
        for(i=0;i<8;i++) //读出数据
        {
            clock_sclk=1;
                _nop_();
                clock_sclk=0;
                if(clock_data==1)
                        databuf|=0x01;
                else
                        databuf&=0xfe;
                databuf=_cror_(databuf,1);//循环右移一位,8次
                  clock_sclk=0;//下降沿读数
                  _nop_();       
        }
        clock_sclk=0;
        clock_rst=0; //复位信号拉低,关闭读数
        databuf=((databuf&0xf0)/16)*10+(databuf&0x0f);//读出的数据组
        return databuf;
        _nop_();
    _nop_();
        _nop_();
        _nop_();
}
/**************向HT1380写数据*************/
void write_ht1380(uchar cmd,uchar date)
{
        uchar i;
        date=(date/10<<4)|date%10; //10进制转16进制
        clock_rst=0;
        _nop_();
        clock_sclk=0;
        _nop_();
        clock_rst=1; //复位信号拉高,准备写入
        _nop_();
        _nop_();
        _nop_();
        _nop_();
        for(i=0;i<8;i++) //写入写命令
        {
            clock_sclk=0;
                if((cmd&0x01)==1)
                        clock_data=1;
                else
                        clock_data=0;
                clock_sclk=1;//上升沿写入
                cmd>>=1;//低位在前
        }
        for(i=0;i<8;i++) //写入数据
        {
                clock_sclk=0;
                if((date&0x01)==0)
                        clock_data=0;
                else
                        clock_data=1;
                clock_sclk=1; //上升沿写数据
                date>>=1;
        }
        clock_sclk=0;
        clock_rst=0;
        _nop_();
        _nop_();
        _nop_();
        _nop_();
}
         /*******写指令函数****/
  void write_com(uchar cmd)
   {
                   P0=cmd;
                lcdrs=0;
                lcdrw=0;
                lcden=0;
                delay(1);
                lcden=1;
                  
   }

/******写数据函数*******/               
  void write_dat(uchar date)
   {
                   P0=date;
                lcdrs=1;
                lcdrw=0;
                lcden=0;
                delay(1);
                lcden=1;
   }
       /***************年月日函数************/
void write_nyr(uchar add,uchar date)
{
        shi=date/10;
        ge=date%10;
        write_com(0x80+add);
        write_dat(0x30+shi);
        write_dat(0x30+ge);
}
/********************************/
  /*******时分秒函数*******/
void write_sfm(uchar add,uchar date)
{
        shi=date/10;
        ge=date%10;
        write_com(0x80+0x40+add);
        write_dat(0x30+shi);
        write_dat(0x30+ge);

}
/******************初始化函数********************/
void init()
{
                write_com(0x01); //lcd初始化
                write_com(0x38);
                write_com(0x0c);
                write_com(0x06);
                write_com(0x80);
                write_ht1380(0x8e,0); //允许写寄存器;
                write_ht1380(0x80,0); //振荡器起振 CH=0
                write_ht1380(0x80,0); //sec
                write_ht1380(0x82,0);//min
                write_ht1380(0x84,0);//hour
                write_ht1380(0x86,1);//date
                write_ht1380(0x88,1);//month
                write_ht1380(0x8a,1);//day          weak
                write_ht1380(0x8c,0);//year
                for(num=0;num<16;num++)        //写入初始化数据
                {
                         write_dat(table1[num]);
                         delay(10);
                }
                write_com(0x80+0x40);
                for(num=0;num<16;num++)
                {
                        write_dat(table2[num]);
                        delay(10);
                }
                       
}
/***************lcd显示函数*****************/
void lcd_display()
{
        sec=(read_ht1380(0x81)&0x7f);//read sec
        delay(2);
        min=read_ht1380(0x83);        //read min
        delay(2);
        hour=read_ht1380(0x85);        //read hour
        delay(2);
        date=read_ht1380(0x87);        //read date
        delay(2);
        mon=read_ht1380(0x89); //read month
        delay(2);
        day=read_ht1380(0x8b);//read day week
        delay(2);
        year=read_ht1380(0x8d);//read year
        delay(2);
        write_nyr(3,year);delay(2);//显示年月日时分秒和星期
        write_nyr(6,mon);delay(2);
        write_nyr(9,date);delay(2);
        write_sfm(4,hour);delay(2);
        write_sfm(7,min);delay(2);
        write_sfm(10,sec);delay(2);
        write_com(0x80+12);delay(2);
        for(num=0;num<3;num++)
                write_dat(table3[day-1][num]);
                delay(2);

}
uchar wei1(uchar wei1)//光标显示
{
                  if(wei1==1)
                  {
                          write_com(0x80+0x40+12);
                        write_com(0x0f);
                        return 0x80+0x40+12;
                  }
                  if(wei1==2)
                  {
                          write_com(0x80+0x40+9);
                        write_com(0x0f);
                        return 0x80+0x40+9;
                  }
                  if(wei1==3)
                  {
                          write_com(0x80+0x40+6);
                        write_com(0x0f);
                        return 0x80+0x40+6;
                  }
                  if(wei1==4)
                  {
                          write_com(0x80+15);
                        write_com(0x0f);
                        return 0x80+15;
                  }
                  if(wei1==5)
                  {
                   write_com(0x80+11);
                   write_com(0x0f);
                   return 0x80+11;
                  }
                  if(wei1==6)
                  {
                          write_com(0x80+8);
                        write_com(0x0f);
                        return 0x80+8;
                  }
                  if(wei1==7)
                  {
                          write_com(0x80+5);
                        write_com(0x0f);
                        return 0x80+5;
                  }
                  if(wei1==8)
                  {
                          wei=0;
                        sec=read_ht1380(0x81);
                        write_ht1380(0x80,sec);
                        write_com(0x0c);
                        return 0x0c;
                  }
                  delay(100);
}
/****************键盘扫描函数*********************/
void keyscan()
{
         uchar temp,b=0;  
         negative=0; //负极线P3^7
         while(b==0)
         {

                  if(key1==0)
                  {
                          delay(5);
                        if(key1==0)           //功能键的移动
                        {
                          while(!key1);                //松开按键
                          write_ht1380(0x80,(0x80|sec));
                          wei++;       
                          temp=wei1(wei);
                        }
                   }
                        if(key2==0)           //加键
                        {
                                delay(5);
                                if(key2==0)
                                {
                                while(!key2);
                                          if(wei==1)
                                          {
                                                  sec=(read_ht1380(0x81)&0x7f);
                                                if(sec>=59)sec=0;
                                                ++sec;
                                                write_ht1380(0x80,0x80|sec);就是这里啊,怎么弄都错

                                          }
                                          if(wei==2)
                                          {
                                                  min=read_ht1380(0x83);
                                                if(min==59)min=-1;
                                                write_ht1380(0x82,++min);
                                          }
                                          if(wei==3)
                                          {
                                                  hour=read_ht1380(0x85);
                                                if(hour==23)hour=-1;
                                                write_ht1380(0x84,++hour);
                                          }
                                          if(wei==4)
                                          {
                                                  day=read_ht1380(0x8b);
                                                if(day==7)day=0;
                                                write_ht1380(0x8a,++day);
                                          }
                                          if(wei==5)
                                          {
                                            date=read_ht1380(0x87);
                                                if(date==28)date=0;
                                                write_ht1380(0x86,++date);
                                          }
                                          if(wei==6)
                                          {
                                                  mon=read_ht1380(0x89);
                                                if(mon==12)mon=0;
                                                write_ht1380(0x88,++mon);
                                          }
                                          if(wei==7)
                                          {
                                                  year=read_ht1380(0x8d);
                                                if(year==99)year=-1;
                                                write_ht1380(0x8c,++year);
                                          }
                                          write_com(0x0c);
                                          lcd_display();
                                          //write_ht1380(0x80,sec+0x80);
                                          delay(100);
                                          write_com(temp);
                                          write_com(0x0f);
                                }
                  }
                  if(key3==0)        //减键
                        {        delay(5);
                                if(key3==0)
                                {
                                      while(!key3);
                                          if(wei==1)
                                          {
                                                  sec=(read_ht1380(0x81)&0x7f);
                                                if(sec<=0)sec=60;
                                                --sec;
                                                write_ht1380(0x80,0x80|sec);
                                          }
                                          if(wei==2)
                                          {
                                                  min=read_ht1380(0x83);
                                                if(min==0)min=60;
                                                write_ht1380(0x82,--min);
                                          }
                                          if(wei==3)
                                          {
                                                  hour=read_ht1380(0x85);
                                                if(hour==0)hour=24;
                                                write_ht1380(0x84,--hour);
                                          }
                                          if(wei==4)
                                          {
                                                  day=read_ht1380(0x8b);
                                                if(day==1)day=8;
                                                write_ht1380(0x8a,--day);
                                          }
                                          if(wei==5)
                                          {
                                            date=read_ht1380(0x87);
                                                if(date==1)date=29;
                                                write_ht1380(0x86,--date);
                                          }
                                          if(wei==6)
                                          {
                                                  mon=read_ht1380(0x89);
                                                if(mon==1)mon=13;
                                                write_ht1380(0x88,--mon);
                                          }
                                          if(wei==7)
                                          {
                                                  year=read_ht1380(0x8d);
                                                if(year==0)year=100;
                                                write_ht1380(0x8c,--year);
                                          }
                                          write_com(0x0c);
                                          lcd_display();
                                          //write_ht1380(0x80,sec+0x80);
                                          delay(100);
                                          write_com(temp);
                                          write_com(0x0f);
                                }
                  }
        if(wei==0) b=1;         
           }
}


void main()
{
        init();
    while(1)
    {
                lcd_display();
                keyscan();
                delay(100);
    }
}

最新回复

sec=(read_ht1380(0x81)&0x3f);0x7f if(++sec>59)sec=0; write_ht1380(0x80,0x80|sec); 在不清楚这款时钟具体格式的情况下,这只是个人的一种猜测.  详情 回复 发表于 2010-3-27 16:38
点赞 关注

回复
举报

56

帖子

0

TA的资源

一粒金砂(初级)

沙发
 
帮顶加路过
 
 

回复

73

帖子

0

TA的资源

一粒金砂(初级)

板凳
 
sec=(read_ht1380(0x81)&0x7f);

你散开些看看
sec=read_ht1380(0x81);
sec&=0x7f;
 
 
 

回复

79

帖子

0

TA的资源

一粒金砂(初级)

4
 
不错,试试
 
 
 

回复

72

帖子

0

TA的资源

一粒金砂(初级)

5
 
秒的最高位是CH(Clock Halt)。写1以后,时间计数器就停止了。

程序里面没有考虑到如何利用这个位吧。应该在手动改变数据之前停止时间,之后再开始。

  1. ……
  2. if(wei==1)
  3. {
  4.         sec=(read_ht1380(0x81)&0x7f);
  5.         write_ht1380(0x80,0x80|sec);            //先停止时间计数
  6.         ++sec;
  7.         if(sec>=59)sec=0;
  8.         write_ht1380(0x80,/*0x80|*/sec);    //不能再最高位写1
  9. }
  10. ……
复制代码
 
 
 

回复

71

帖子

0

TA的资源

一粒金砂(初级)

6
 
……
if(wei==1)
{
    sec=(read_ht1380(0x81)&0x7f);
    write_ht1380(0x80,0x80|sec);        //先停止时间计数
    ++sec;
    if(sec>59)sec=0;                    //这个……
    write_ht1380(0x80,/*0x80|*/sec);    //不能再最高位写1
}
……
 
 
 

回复

64

帖子

0

TA的资源

一粒金砂(初级)

7
 
我有停止时间计数的,在key1按下时:
if(key1==0) //功能键的移动
{
while(!key1); //松开按键
write_ht1380(0x80,(0x80|sec));
wei++;
开始计数,在wei=8,跳出函数时:

if(wei1==8)
{
wei=0;
sec=read_ht1380(0x81);
write_ht1380(0x80,sec);
write_com(0x0c);
return 0x0c;
}
 
 
 

回复

71

帖子

0

TA的资源

一粒金砂(初级)

8
 
问题就是我加秒时只能加到三十一,再后又是零了
 
 
 

回复

81

帖子

0

TA的资源

一粒金砂(初级)

9
 
1380返回的数据都是BCD码,你的59实际上师0X3B,被1380识别为0x30+(0X0B-10)=0X31;所以你最大只能设到31。
应该作个bcd转换,设定的数据要

sec++;
if(sec>59)sec=0; //这个……
sec=(scc/10)&0x0f<<4+(sec%10)&0x0f;
write_ht1380(0x80,sec); //
 
 
 

回复

61

帖子

0

TA的资源

一粒金砂(初级)

10
 
读出的时候也一样要变换一下!
sec=read_ht1380(0x81);
sec=((sec&0xf0)>>4)*10+(sce&0x0f);
if(sec>59)sec=0;
 
 
 

回复

93

帖子

0

TA的资源

一粒金砂(初级)

11
 
数据转换一个编码格式
 
 
 

回复

76

帖子

0

TA的资源

一粒金砂(初级)

12
 
帮顶。。
 
 
 

回复

91

帖子

0

TA的资源

一粒金砂(初级)

13
 
好贴··收了·将来做个小设计什么的就copy了哈···谢谢楼主
 
 
 

回复

81

帖子

0

TA的资源

一粒金砂(初级)

14
 
楼上的啊,提醒你一下,我的有个错误!希望你搞懂之后再copy哦!
 
 
 

回复

62

帖子

0

TA的资源

一粒金砂(初级)

15
 
sec=(read_ht1380(0x81)&0x3f);0x7f

if(++sec>59)sec=0;

write_ht1380(0x80,0x80|sec);

在不清楚这款时钟具体格式的情况下,这只是个人的一种猜测.
 
 
 

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

随便看看
查找数据手册?

EEWorld Datasheet 技术支持

相关文章 更多>>
关闭
站长推荐上一条 1/6 下一条

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

About Us 关于我们 客户服务 联系方式 器件索引 网站地图 最新更新 手机版

站点相关: 国产芯 安防电子 汽车电子 手机便携 工业控制 家用电子 医疗电子 测试测量 网络通信 物联网

北京市海淀区中关村大街18号B座15层1530室 电话:(010)82350740 邮编:100190

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved
快速回复 返回顶部 返回列表