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

QQ登錄

只需一步,快速開始

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

DS1302最簡(jiǎn)單的一個(gè)小程序 是哪塊不對(duì)呢???

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:56673 發(fā)表于 2013-11-24 14:06 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
前面的讀寫字節(jié) 讀寫程序的都對(duì)     應(yīng)該是主函數(shù)的問題  查了很多資料 對(duì)BCD轉(zhuǎn)化還是不懂  是都要轉(zhuǎn)換嗎?

#include<reg52.h>
#define uchar unsigned char
uchar key,key1;
sbit CLK=P2^7;
sbit IO=P2^6;
sbit RES=P2^5;
unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,
                       0x99,0x92,0x82,0xf8,
                       0x80,0x90};                                                     //0--9代碼
void write_byte(uchar byte)                                                       //寫一個(gè)字節(jié)
{
uchar i;
RES=1;
CLK=0;
for(i=8;i>0;i--)
  {
   CLK=0;
   IO=byte&0x01;
   CLK=1;
   byte=byte>>1;
  }

}
uchar read_byte()                                                       //讀一個(gè)字節(jié)

{
uchar i,dat;
RES=1;
for(i=0;i<8;i++)
  {
   CLK=1;
   CLK=0;
      if(IO)
    dat=(dat|(0x01<<i));
  }
return( dat);
}
void  write_1302(uchar add,uchar dat)                                     //寫入數(shù)據(jù)
{
RES=0;
RES=1;
CLK=0;
write_byte(add);
    write_byte(dat);
RES=0;
   
}
read_1302(uchar add)                                                                 //讀出數(shù)據(jù)
{
RES=0;
RES=1;
CLK=0;
write_byte(add);
key=read_byte();
RES=0;
return (key);
   
}
   
void main()
{

P1=0x08;                                                     //數(shù)碼管位選
RES=0;
    CLK=0;
    write_1302(0x8e,0x00);                          //允許寫入
write_1302(0x80,0x80);                             //秒寫入0
write_1302(0x8e,0x80);                               //寫保護(hù)
while(1)
  {
      read_1302(0x81);                                    //讀出
                                                  
   P0=table[key1];                                             //段選
  }
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:56673 發(fā)表于 2013-11-24 14:10 | 只看該作者
如果向秒寄存器 0x80里寫數(shù)據(jù) 0 的話   00H BCD轉(zhuǎn)化完還是00   可秒寄存器第一位如果是0不就停止計(jì)時(shí)了嗎
回復(fù)

使用道具 舉報(bào)

板凳
ID:18297 發(fā)表于 2013-12-5 20:05 | 只看該作者
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit timesclk=P2^7;
sbit timeio=P2^6;
sbit timerst=P2^5;
uchar set_time_data[];
void delay(uint z);
/****************************************************
Copyright:      2013-2014
File name:      電子時(shí)鐘
Description:    DS1302寫數(shù)據(jù)子程序
Author:         aking   
Version:      1.0
Date:           2013-12-6
History
****************************************************/
void timewrite(uchar dat)
{
uchar i,temp;
delay(1);
temp=dat;
for(i=0;i<8;i++)
{
  timeio=temp&0x01;
  delay(1);
  timesclk=1;
  delay(1);
  timesclk=0;
  temp=temp>>1;
}
}  
/****************************************************
Copyright:      2013-2014
File name:      電子時(shí)鐘
Description:    DS1302讀取延時(shí)
Author:         aking   
Version:      1.0
Date:           2013-12-6
History
****************************************************/
void delay(uint z)
{
    uint x,y;
    for(x=z;x>0;x--)
       for(y=110;y>0;y--);
}
/****************************************************
Copyright:      2013-2014
File name:      電子時(shí)鐘
Description:    DS1302讀數(shù)據(jù)子程序
Author:         aking   
Version:      1.0
Date:           2013-12-6
History
****************************************************/
uchar timeread()
{
uchar i,temp;
delay(1);
temp=0;
for(i=0;i<8;i++)
{
  temp=temp>>1;
  if(timeio==1)temp=temp+0x80;
  delay(1);
  timesclk=1;
  delay(1);
  timesclk=0;
}
return temp;
}
/****************************************************
Copyright:      2013-2014
File name:      電子時(shí)鐘
Description:    DS1302向某地址寫數(shù)據(jù)子程序
Author:         aking   
Version:      1.0
Date:           2013-12-6
History
****************************************************/
void timedata(uchar add,uchar dat)
{
timerst=0;
timesclk=0;
timerst=1;
timewrite(add);                 // 地址,命令
timewrite(dat);                 // 寫1Byte數(shù)據(jù)
    timesclk=1;
    timerst=0;
    timeio=0;
}
/****************************************************
Copyright:      2013-2014
File name:      電子時(shí)鐘
Description:    DS1302初始化子程序
Author:         aking   
Version:      1.0
Date:           2013-12-6
History
****************************************************/
void timeinit()
{
uchar i;
for(i=0;i<5;i++)
{
  timedata(0x80+i*2,set_time_data[i]);  //初始化秒,時(shí),分,日,月
}
timedata(0x80+6*2,set_time_data[5]);
}
/****************************************************
Copyright:      2013-2014
File name:      電子時(shí)鐘
Description:    主函數(shù)程序
Author:         aking   
Version:      1.0
Date:           2013-12-6
History
****************************************************/
void main()
{
uchar i;
timedata(0x8e,0x00);            
timeinit();      //時(shí)間初始化
timedata(0x8e,0x80);
timerst=0;
timesclk=0;
while(1)
{
  timerst=1;
  timewrite(0x81);       //先寫地址(秒為0x81,分為0x83等等)
  i=timeread();              //再讀數(shù)據(jù)(i為秒)
  timerst=0;
  delay(1);                 //延時(shí)
  P1=i/16;                // P1輸出位秒的高位
  P3=i%16;              //P3輸出位秒的低位
}
}

//以上為完整的程序,經(jīng)過仿真。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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