单片机c语言ds1302,STC单片机控制DS1302 (1TMCU C程序)

#include "REG51.H"

#include "INTRINS.H"

typedef unsigned char BYTE;

sbit SCLK = P1^0;                   //DS1302时钟口P1.0

sbit IO = P1^1;                     //DS1302数据口P1.1

sbit RST = P1^2;                    //DS1302片选口P1.2

//秒    分    时    日    月  星期    年

BYTE code init[] = {0x00, 0x00, 0x20, 0x01, 0x01, 0x05, 0x10};

BYTE data now[7];

void DS1302_Initial();

void DS1302_SetTime(BYTE *p);

void DS1302_GetTime(BYTE *p);

void main()

{

DS1302_Initial();               //初始化DS1302

DS1302_SetTime(init);           //设置初始时间

DS1302_GetTime(now);            //读取当前时间

while (1);

}

/**************************************

延时X微秒(STC12C5A60S2@12M)

不同的工作环境,需要调整此函数

此延时函数是使用1T的指令周期进行计算,与传统的12T的MCU不同

**************************************/

void Delay()

{

_nop_();

_nop_();

}

/**************************************

从DS1302读1字节数据

**************************************/

BYTE DS1302_ReadByte()

{

BYTE i;

BYTE dat = 0;

for (i=0; i<8; i++)             //8位计数器

{

SCLK = 0;                   //时钟线拉低

Delay();                //延时等待

dat >>= 1;                    //数据右移一位

if (IO) dat |= 0x80;        //读取数据

SCLK = 1;                   //时钟线拉高

Delay();                //延时等待

}

return dat;

}

/**************************************

向DS1302写1字节数据

**************************************/

void DS1302_WriteByte(BYTE dat)

{

char i;

for (i=0; i<8; i++)             //8位计数器

{

SCLK = 0;                   //时钟线拉低

Delay();                //延时等待

dat >>= 1;                  //移出数据

IO = CY;                    //送出到端口

SCLK = 1;                   //时钟线拉高

Delay();                //延时等待

}

}

/**************************************

读DS1302某地址的的数据

**************************************/

BYTE DS1302_ReadData(BYTE addr)

{

BYTE dat;

RST = 0;

Delay();

SCLK = 0;

Delay();

RST = 1;

Delay();

DS1302_WriteByte(addr);         //写地址

dat = DS1302_ReadByte();        //读数据

SCLK = 1;

RST = 0;

return dat;

}

/**************************************

往DS1302的某个地址写入数据

**************************************/

void DS1302_WritEDAta(BYTE addr, BYTE dat)

{

RST = 0;

Delay();

SCLK = 0;

Delay();

RST = 1;

Delay();

DS1302_WriteByte(addr);         //写地址

DS1302_WriteByte(dat);          //写数据

SCLK = 1;

RST = 0;

}

/**************************************

写入初始时间

**************************************/

void DS1302_SetTime(BYTE *p)

{

BYTE addr = 0x80;

BYTE n = 7;

DS1302_WriteData(0x8e, 0x00);   //允许写操作

while (n--)

{

DS1302_WriteData(addr, *p++);

addr += 2;

}

DS1302_WriteData(0x8e, 0x80);   //写保护

}

/**************************************

读取当前时间

**************************************/

void DS1302_GetTime(BYTE *p)

{

BYTE addr = 0x81;

BYTE n = 7;

while (n--)

{

*p++ = DS1302_ReadData(addr);

addr += 2;

}

}

/**************************************

初始化DS1302

**************************************/

void DS1302_Initial()

{

RST = 0;

SCLK = 0;

DS1302_WriteData(0x8e, 0x00);   //允许写操作

DS1302_WriteData(0x80, 0x00);   //时钟启动

DS1302_WriteData(0x90, 0xa6);   //一个二极管+4K电阻充电

DS1302_WriteData(0x8e, 0x80);   //写保护

}

STC 单片机 控制DS1302 (1TMCU C程序)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值