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

📄 ds-1302.c

📁 利用DS1302
💻 C
字号:


sbit B0=B^0;
sbit B1=B^1;
sbit B2=B^2;
sbit B3=B^3;
sbit B4=B^4;
sbit B5=B^5;
sbit B6=B^6;
sbit B7=B^7;


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


void Write_DS1302(unsigned char WR_Addr, unsigned char WR_Data)  //写入的地址,写入的位置
{
     SCLK=0;  RST=1;       //写完了地址,8位的
     B=WR_Addr;
     DIO=B0;  SCLK=1; SCLK=0;
     DIO=B1;  SCLK=1; SCLK=0;
     DIO=B2;  SCLK=1; SCLK=0;
     DIO=B3;  SCLK=1; SCLK=0;
     DIO=B4;  SCLK=1; SCLK=0;
     DIO=B5;  SCLK=1; SCLK=0;
     DIO=B6;  SCLK=1; SCLK=0;
     DIO=B7;  SCLK=1; SCLK=0;
     //写入数据
     B=WR_Data;
     DIO=B0;  SCLK=1; SCLK=0;
     DIO=B1;  SCLK=1; SCLK=0;
     DIO=B2;  SCLK=1; SCLK=0;
     DIO=B3;  SCLK=1; SCLK=0;
     DIO=B4;  SCLK=1; SCLK=0;
     DIO=B5;  SCLK=1; SCLK=0;
     DIO=B6;  SCLK=1; SCLK=0;
     DIO=B7;  SCLK=1;
     RST=0;
}


unsigned char Read_DS1302(unsigned char WR_Addr)
{
     SCLK=0;   RST=1;
     B=WR_Addr;
     //写入地址
     DIO=B0;  SCLK=1; SCLK=0;
     DIO=B1;  SCLK=1; SCLK=0;
     DIO=B2;  SCLK=1; SCLK=0;
     DIO=B3;  SCLK=1; SCLK=0;
     DIO=B4;  SCLK=1; SCLK=0;
     DIO=B5;  SCLK=1; SCLK=0;
     DIO=B6;  SCLK=1; SCLK=0;
     DIO=B7;  SCLK=1;
     //读出数据
     SCLK=0;  B0=DIO; SCLK=1;
     SCLK=0;  B1=DIO; SCLK=1;
     SCLK=0;  B2=DIO; SCLK=1;
     SCLK=0;  B3=DIO; SCLK=1;
     SCLK=0;  B4=DIO; SCLK=1;
     SCLK=0;  B5=DIO; SCLK=1;
     SCLK=0;  B6=DIO; SCLK=1;
     SCLK=0;  B7=DIO; SCLK=1;
     RST=0;
     return B;
}

unsigned char temp,time_sec,time_min,time_hour,time_date,time_day,time_mon,time_year;
void Init_DS1302(void)
{
     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(void)
{
      unsigned char temp;
      //读秒
      temp=Read_DS1302(0x81)&0x7f;
      time_sec=temp%16+temp/16*10;
      //读分钟
      temp=Read_DS1302(0x83);
      time_min=temp%16+temp/16*10;      
      //读小时
      temp=Read_DS1302(0x85);
      time_hour=temp%16+temp/16*10;
      //读星期      
      time_day=Read_DS1302(0x8b);
      //读日期
      temp=Read_DS1302(0x87);
      time_date=temp%16+temp/16*10;
      //读月
      temp=Read_DS1302(0x89);
      time_mon=temp%16+temp/16*10;
      //读年
      temp=Read_DS1302(0x8d);
      time_year=temp%16+temp/16*10;
}

⌨️ 快捷键说明

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