蓝桥杯单片机备赛之DS1302模块

  定义:DS1302为时钟芯片。

  配置特点:1.定位到地址 2.写入数据。

    如你要写59秒,那便先确定他的地址0x80,在编写它的数据0x59。若要写9月,那便先写0x88(它的地址,再写数据0x09)。

 ( 手册中的详细地址)

它的编写时序(规则)代码如下

#include "ds1302.h"  									

//写字节
void Write_Ds1302(unsigned  char temp) 
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//向DS1302寄存器写入数据
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )     
{
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
 	Write_Ds1302(dat);		
 	RST=0; 
}

 //从DS1302寄存器读出数据
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
	unsigned char i,temp=0x00;
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1;	_nop_();
 	Write_Ds1302(address);
 	for (i=0;i<8;i++) 	
 	{		
		SCK=0;
		temp>>=1;	
 		if(SDA)
 		temp|=0x80;	
 		SCK=1;
	} 
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
	SCK=1;	_nop_();
	SDA=0;	_nop_();
	SDA=1;	_nop_();
	return (temp);			
}
void Set_Rtc(unsigned char*ucRtc)//写函数
{
  Write_Ds1302_Byte(0x8e,0x00);//关闭保护位
  Write_Ds1302_Byte(0x84,ucRtc[0]);//时
  Write_Ds1302_Byte(0x82,ucRtc[1]);//分
  Write_Ds1302_Byte(0x80,ucRtc[2]);//秒
  Write_Ds1302_Byte(0x8e,0x80);//打开保护位
}
void Read_Rtc(unsigned char*ucRtc)//读函数
{
   ucRtc[0]=Read_Ds1302_Byte(0x85);//时
   ucRtc[1]=Read_Ds1302_Byte(0x83);//分
   ucRtc[2]=Read_Ds1302_Byte(0x81);//秒
}

小细节,需将.h中的一些已经声明的头文件复制到.c当中去

记得在.h文件中声明这两个变量,带分号。

主函数需要做的:

数码管设置:

最后改个0xbf.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值