[复习]ds1307读写
ds1307读写
// #include
#include
#include
/**********************另外相关的定义
*********************************/
#define HIGH 1
#define LOW 0
#define TURE 1
#define FALSE 0
#define uchar unsigned char
#define uint unsigned int
/*************************以下是函数的申明部分*************************/
//void LCD_init(void); //LCD1602初始化
//void LCD_send_command(uchar command); //
//void LCD_send_data(uchar dat);
//void LCD_write_char(uchar x,uchar y,uchar dat);
//void LCD_write_string(uchar x,uchar y,char *Data);
void delay_ms(uint n);
// void LCD_check_busy(void);
/*********************结束
***********************************************/
/***************************DS1307开始
*****************************************************************/
#define Wait1us _nop_();
#define Wait2us {_nop_();_nop_();}
#define Wait4us {Wait2us;Wait2us;}
#define Wait8us {Wait4us;Wait4us;}
#define Wait10us {Wait8us;Wait2us;}
#define WRITEDS1307 0xD0
#define READDS1307 0xD1
uchar BP;
#define SDA1 P1_7 //读端口DS1307 Serial-Data Input
pin 5
#define SCL1 P1_6 //读端口DS1307 Serial-Clock Input
pin 6
#define SDA P1_2 //DS1307 Serial-Data Input pin 5
#define SCL P1_3 //DS1307 Serial-Clock Input pin 6
#define J1 P3_4 //DS1307 Serial-Clock Input pin 6
#define G2 P1_3 //DS1307 Serial-Clock Input pin 6
#define J3 P1_4 //DS1307 Serial-Clock Input pin 6
/************************************************************/
void I2C_start(void);//I2C start function
void I2C_stop(void);//I2C stop function
//void //void I2C_send_ack(void);//I2C send responsion function
I2C_send_ack(void);//I2C send responsion function
void I2C_send_noack(void);
void I2C_write_byte(unsigned char IIC_data);//I2C bus write
byte function
unsigned char I2C_read_byte(void);//I2C bus read byte function
void Read_RTC_j1_j3(void);
/***********************************************************************/
void Write1307(unsigned char add,unsigned char dat);//write information to ds1307
unsigned char Read1307(unsigned char add);//read information from ds1307
void Read_RTC(void);//read RTC
void Set_RTC(void);//set RTC
/***********************************************************************/
code unsigned char set_rtc_code[7];
code unsigned char
rtc_address[7]={0x00,0x01,0x02,0x03,0x04,0x05,0x06};
//code unsigned char
*day[7]={"Mon","Tue","Wen","Thu","Fri","Sat","Sun"};
unsigned char read_rtc_code[7];
unsigned char wr_j1j3_code[7];
unsigned char jk[7]={0,0,0,2,0,0,9};//设置时钟初始值
/***********************************************************************/
void Initial(void);//system initize function
void Display(void);//RTC display function
/***********************************************************************/
/***********************************************************************/
/*1ms延时子程序
如果X=50,时钟频率为12MHz,,则通过传递一个50的数值可以产生
约50ms的延时*/
void delay(uint x)
{
uint j;
while(x--)
{
for(j=0;j<82;j++)
{;}
}
}
void ReadWriet_J1_J3(void)
{
// P3_3=0; //指示灯
P3_5=1; //指示灯
P1_5=0;//继电器 ds1307连接 c2051
delay(250);
delay(250);
Read_RTC_j1_j3(); //读DS1307数据
}
//*********************************** void ReadWriet1307(void);//读写1307内容并写入另一DS1307
//外部中断0服务程序*********
void serviec_int0()interrupt 0 using 2 //int0中断服务程序,使用第二组寄存器
{
ReadWriet1307();
}
//外部中断0服务程序*********
void serviec_int1()interrupt 2 using 3 //int0中断服务程序,
使用第二组寄存器
{
ReadWriet_J1_J3();
}
void main(void)
{// #define SDA P1_7 //DS1307 Serial-Data Input pin
5
P3_3=1; //LED指示灯
P3_5=0; //LED指示灯
P3_7=1; //蜂鸣器
P1_5=1; //继电器 启动时读DS1307中的数据
//Initial();
while(1)
{
// while(P3_2==1){
// while(P3_3){}
/// }//未按下键循环等待
// delay(20);
// while(P3_2==0){
// while(P3_2==0){}
// }
// Read_RTC();
// Display();
}
}
/**********************************************************
*************/ void Initial(void) {
//LCD_init(); // Set_RTC(); }
/**********************************************************
*************/ void I2C_start(void) {
SDA=1;
SCL=1;
Wait8us;
SDA=0;
Wait8us;
SCL=0;
}
void I2C_start1(void) {
SDA1=1;
SCL1=1;
Wait8us;
SDA1=0;
Wait8us;
SCL1=0;
}
/**********************************************************
*************/ void I2C_stop(void) {
SDA=0;
SCL=1;
Wait8us;
SDA=1;
Wait4us;
SCL=0;
}
void I2C_stop1(void) {
SDA1=0;
SCL1=1;
Wait8us;
SDA1=1;
Wait4us;
SCL1=0;
}
/**********************************************************
************* void I2C_send_ack(void)
{
SDA=0;
SCL=1;
Wait4us;
SCL=0;
}
***********************************************************
**************/
void I2C_send_noack(void) {
SDA=1;
SCL=1;
Wait4us;
SCL=0;
}
/**********************************************************
*************/
void I2C_write_byte(unsigned char IIC_data)
{
unsigned char i;
B=0;
for(i=0;i<8;i++)
{
SCL=0;
if((IIC_data<0;i--)
for(j=0;j<1140;j++)
;
}
***********************************************************
**********/
void ReadWriet1307(void)//读写1307内容并写入另一DS1307
{
P3_3=0; //指示灯
P3_5=1; //指示灯
P1_5=0;//继电器 ds1307连接 c2051
delay(250);
delay(250);
Read_RTC();
Set_RTC(); //写
P3_3=1;
P3_5=0;
P3_7=0; //蜂鸣器开
delay(255);
P1_5=1;//继电器 DS1307连接18f2525
while(BP==1)
{
if(P3_2==0){BP=0;}
}
while(P3_2==0){}
P3_7=1; //蜂鸣器关
}