DOC

DS1302

By Beth Freeman,2014-05-20 08:11
10 views 0
DS1302

驱动程序

//寄存器宏定义

    #define WRITE_SECOND 0x80

    #define WRITE_MINUTE 0x82

    #define WRITE_HOUR 0x84 #define READ_SECOND 0x81

    #define READ_MINUTE 0x83 #define READ_HOUR 0x85 #define WRITE_PROTECT 0x8E

//位寻址寄存器定义

    sbit ACC_7 = ACC^7;

    //管脚定义

    sbit SCLK = P3^5; // DS1302时钟信号

    7

    sbit DIO= P3^6; // DS1302数据信号

    6

    sbit CE = P3^7; // DS1302片选

    5

//地址、数据发送子程序

    void Write1302 ( unsigned char addr,dat )

    {

     unsigned char i,temp;

     CE=0; //CE引脚为低;数据传送中止

     SCLK=0; //清零时钟总线

     CE = 1; //CE引脚为高;逻辑控制有效

     //发送地址

     for ( i=8; i>0; i-- ) //循环8次移位

     {

     SCLK = 0;

     temp = addr;

     DIO = (bit)(temp&0x01); //每次传输低字

     addr >>= 1; //右移一位

     SCLK = 1;

     }

    //发送数据

     for ( i=8; i>0; i-- )

     {

     SCLK = 0;

     temp = dat;

     DIO = (bit)(temp&0x01);

     dat >>= 1;

     SCLK = 1;

     }

     CE = 0;

    }

//数据读取子程序

    unsigned char Read1302 ( unsigned char addr ) {

     unsigned char i,temp,dat1,dat2;

     CE=0;

     SCLK=0;

     CE = 1;

     //发送地址

     for ( i=8; i>0; i-- ) //循环8次移位

     {

     SCLK = 0;

     temp = addr;

     DIO = (bit)(temp&0x01); //每次传输低字

    

     addr >>= 1; //

    移一位

     SCLK = 1;

     }

     //读取数据

     for ( i=8; i>0; i-- )

     {

     ACC_7=DIO;

     SCLK = 0;

     ACC>>=1;

     SCLK = 1;

     }

     CE=0;

     dat1=ACC;

     dat2=dat1/16; //数据进制转换

     dat1=dat1%16; //十六进制转十

进制

     dat1=dat1+dat2*10;

     return (dat1);

    }

//初始化DS1302

    void Initial(void)

    {

     Write1302 (WRITE_PROTECT,0X00); //禁止写保

     Write1302 (WRITE_SECOND,0x56); //秒位初始

     Write1302 (WRITE_MINUTE,0x34); //分钟初始化

     Write1302 (WRITE_HOUR,0x12); //

    小时初始化

     Write1302 (WRITE_PROTECT,0x80); //

    允许写保护

Report this document

For any questions or suggestions please email
cust-service@docsford.com