⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 1302.c

📁 Init_DS1302();//初始化1302就可以用了 内有中文使用说明
💻 C
字号:


sbit RST =P1^5;
sbit SCLK=P1^0;
sbit DAT =P1^1;

sbit B_bit0=B^0;
sbit B_bit1=B^1;
sbit B_bit2=B^2;
sbit B_bit3=B^3;
sbit B_bit4=B^4;
sbit B_bit5=B^5;
sbit B_bit6=B^6;
sbit B_bit7=B^7;

#define SYSCLK 11059200

unsigned char Read_Ds1302(unsigned char w_addr)
{
    SCLK=0;     RST=1;         //将时钟芯片先拉到低电平,才能把复位引脚拉到高电平,1302开始工作

    B=w_addr;                  //将电址写入

    DAT=B_bit0;SCLK=1;SCLK=0;
    DAT=B_bit1;SCLK=1;SCLK=0;
    DAT=B_bit2;SCLK=1;SCLK=0;
    DAT=B_bit3;SCLK=1;SCLK=0;
    DAT=B_bit4;SCLK=1;SCLK=0;
    DAT=B_bit5;SCLK=1;SCLK=0;
    DAT=B_bit6;SCLK=1;SCLK=0;
    DAT=B_bit7;SCLK=1;SCLK=0;
    //读出数据
    SCLK=0;B_bit0=DAT,SCLK=1;
    SCLK=0;B_bit1=DAT;SCLK=1;
    SCLK=0;B_bit2=DAT;SCLK=1;
    SCLK=0;B_bit3=DAT;SCLK=1;
    SCLK=0;B_bit4=DAT;SCLK=1;
    SCLK=0;B_bit5=DAT;SCLK=1;
    SCLK=0;B_bit6=DAT;SCLK=1;
    SCLK=0;B_bit7=DAT;SCLK=1;

    RST=0;
    return B;
}

unsigned char Write_Ds1302(unsigned char w_addr,unsigned char w_data)    //写入的地址,写入的位置
{
    SCLK=0;     RST=1;         //将时钟芯片先拉到低电平,才能把复位引脚拉到高电平,1302开始工作

    B=w_addr;                  //将电址

    DAT=B_bit0;SCLK=1;SCLK=0;
    DAT=B_bit1;SCLK=1;SCLK=0;
    DAT=B_bit2;SCLK=1;SCLK=0;
    DAT=B_bit3;SCLK=1;SCLK=0;
    DAT=B_bit4;SCLK=1;SCLK=0;
    DAT=B_bit5;SCLK=1;SCLK=0;
    DAT=B_bit6;SCLK=1;SCLK=0;
    DAT=B_bit7;SCLK=1;SCLK=0;


    B=w_data;

    DAT=B_bit0;SCLK=1;SCLK=0;
    DAT=B_bit1;SCLK=1;SCLK=0;
    DAT=B_bit2;SCLK=1;SCLK=0;
    DAT=B_bit3;SCLK=1;SCLK=0;
    DAT=B_bit4;SCLK=1;SCLK=0;
    DAT=B_bit5;SCLK=1;SCLK=0;
    DAT=B_bit6;SCLK=1;SCLK=0;
    DAT=B_bit7;SCLK=1;SCLK=0;

    RST=0;

}
unsigned char time_minute,time_second,time_hour,time_date,time_day,time_mon,time_year;
void Init_DS1302(void)
{
     unsigned char temp;
     Write_Ds1302(0x8e,0x00);  //写入允许
     temp=Read_Ds1302(0x81);   //读秒入寄存器
     temp=temp&0x7f;           //最高位置0
     Write_Ds1302(0x80,temp);  //读入秒寄存器  启动时钟
     temp=Read_Ds1302(0x85);   //回读小时
     temp=temp&0x7f;           //最高位置0,24小时模式
     Write_Ds1302(0x84,temp);  //写入小时寄存器
     Write_Ds1302(0x90,0xa5);  //充电控制,允许允电,一个二极管,2K电阻
     Write_Ds1302(0x8e,0x80);  //写入保护

}

void Read_DS1302_Clock()
{
     unsigned char temp;
     //读DS1302秒
     temp=Read_Ds1302(0x81)&0x7f;
     time_second=temp%16+temp/16*10;
     //读DS1302分
     temp=Read_Ds1302(0x83);
     time_minute=temp%16+temp/16*10;
     //读DS1302小时
     temp=Read_Ds1302(0x85); //24小时
     time_hour=temp%16+temp/16*10;
     //读DS1302星期
     time_day=Read_Ds1302(0x8b);
     
     //读DS1302日期
     temp=Read_Ds1302(0x87);
     time_date=temp%16+temp/16*10;
     //读DS1302月
     temp=Read_Ds1302(0x89);
     time_mon=temp%16+temp/16*10;
     //读DS1302年
     temp=Read_Ds1302(0x8d);
     time_year=temp%16+temp/16*10;

  //   disp_num(time_minute*100+time_second,2);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -