#include <reg52.h>
#define HIGH 1
#define LOW 0
#define TRUE 1
#define ZERO 0
#define MSB 0x80
#define LSB 0x01
sbit en = P3^4;
sbit mov = P3^5;
sbit up = P3^6;
sbit down = P3^7;
// ds1302 PreDefinition
#define DS1302_WP 0x8E
#define DS1302_RESET RST="LOW";SCL=LOW;RST=HIGH
#define DS1302_WP_ENABLE Write_Ds1302(DS1302_WP,0X00)
#define DS1302_WP_DISENABLE Write_Ds1302(DS1302_WP,0x80)
#define LCDPORT P0
sbit SCL = P2^0;// DS1302 Serial-Clock Input pin 5
sbit SDA = P2^1;// DS1302 Serial-Data Input pin 6
sbit RST = P2^2;// DS1302 Chip-Seclet Input pin 7
code unsigned char set_rtc_code[7]={0x08,0x08,0x28,0x03,0x21,0x07,0x50};
code unsigned char write_rtc_address[7]={0x8c,0x88,0x86,0x8a,0x84,0x82,0x80};
code unsigned char read_rtc_address[7]={0x8d,0x89,0x87,0x8b,0x85,0x83,0x81};
unsigned char read_rtc_code[7]; //DS1302读出信息(年份,星期,时间)暂存数组,
/////////////////////////////////////////////////////////////////////
//**************ds1302读写程序*************************//
void Write_Ds1302_Byte(unsigned char temp) //写一个字节
{
unsigned char i;
for (i=0;i<8;i++)
{
SCL="LOW";
SDA="temp"&LSB;
temp>>=1;
SCL="HIGH";
}
}
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat ) //写ds1302
{
DS1302_RESET;
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=LOW;
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address ) //读ds1302
{
unsigned char i,temp=0x00,temp_temp;
DS1302_RESET;
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
if(SDA)
temp|=0x80;
SCL="LOW";
temp>>=1;
SCL="HIGH";
}
RST=LOW;
temp_temp=temp/16;
temp=temp%16;
temp=temp+temp_temp*10;
return (temp);
}
/****************************************************************************/
void Read_RTC(void) //读出DS1302里的相关信息,存放在read_rtd_code[]数组中
{
unsigned char i,*p;
p=read_rtc_address;
for(i=0;i<7;i++)
{
read_rtc_code=Read_Ds1302(*p);
p++;
}
}
/***********************************************************************/
void Set_RTC(void) //初始化设置DS1302
{
unsigned char i,*p;
p=write_rtc_address;
for(i=0;i<7;i++)
{
Write_Ds1302(*p,set_rtc_code);
p++;
}
}
/****************************************************/
void initial_ds1302(void) //初始化ds1302
{
unsigned a;
delay_nms(10);
Read_RTC();
DS1302_RESET;
DS1302_WP_ENABLE;
Set_RTC(); //初始时间设置,已经调整好了,可以不编译
delay_nms(10);
a=Read_Ds1302 (0x81);
Write_Ds1302(0x80,((a/10)<<4 | (a%10))&0x7f);
DS1302_WP_DISENABLE;
}
/**************ds1302读写程序到此完************************/
///////////////////////////////////////////////////
//系统初始化
文章评论(0条评论)
登录后参与讨论