標(biāo)題:
DS1302最簡單的一個(gè)小程序 是哪塊不對呢???
[打印本頁]
作者:
ヽミ慵懶_、陽光
時(shí)間:
2013-11-24 14:06
標(biāo)題:
DS1302最簡單的一個(gè)小程序 是哪塊不對呢???
前面的讀寫字節(jié) 讀寫程序的都對 應(yīng)該是主函數(shù)的問題 查了很多資料 對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]; //段選
}
}
作者:
ヽミ慵懶_、陽光
時(shí)間:
2013-11-24 14:10
如果向秒寄存器 0x80里寫數(shù)據(jù) 0 的話 00H BCD轉(zhuǎn)化完還是00 可秒寄存器第一位如果是0不就停止計(jì)時(shí)了嗎
作者:
zjjhtony
時(shí)間:
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)過仿真。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1