蓝桥杯模板(六)DS1302

文章目录

ds1302.c

同样我们先搬来赛点资源包里的ds1302.c文件

/*	# 	DS1302代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/								
#include "da1302.h"
//
void Write_Ds1302(unsigned  char temp) //和之前的delay一样被引用的
{
	unsigned char i;
	for (i=0;i<8;i++)     	
	{ 
		SCK = 0;
		SDA = temp&0x01;
		temp>>=1; 
		SCK=1;
	}
}   

//
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; 
}

//
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)
{
	unsigned char temp;

	Write_Ds1302_Byte(0x8e,0);
	
	temp = ((ucRTc[0]/10)<<4) | (ucRtc[0]%10);
	Write_Ds1302_Byte(0x84,temp);

	temp = ((ucRtc[1]/10)<<4) | (ucRtc[1]%10);
	Write_Ds1302_Byte(0x82,temp);
    
    temp = ((ucRtc[2]/10)<<4) | (ucRtc[2]%10);
    Write_Ds1302_Byte(0x80,temp);

	Write_Ds1302_Byte(0x8e,0x80);
}

void Read_Rtc(unsigned char *ucRtc)
{
	unsigned char temp;
	
	temp = Read_Ds1302_Byte (0x85);
	ucRtc[0] = ((temp>>4)*10) | (temp & 0x0f);

	temp = Read_Ds1302_Byte (0x83);
	ucRtc[1] = ((temp>>4)*10) | (temp & 0x0f);
	
	temp = Read_Ds1302_Byte (0x81);
	ucRtc[2] = ((temp>>4)*10) | (temp & 0x0f);
}

ds1302.h

然后我们来编写ds1302.h

#ifndef __DS1302_H__
#define __DS1302_H__

#include <stc14f2s60k2.h>
#include <intrins>

//一定不能忘记定义引脚号,可以去原理图中找,从c文件中可看出有仨需要定义,RST,SDA,SCK
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;

void Set_Rtc(unsigned char *ucRtc);
void Read_Rtc(unsigned char *ucRtc);

#endif

main.c

在主函数里

#include "da1302.h"
#include "stdio.h"

unsigend char ucRtc[3]={01,59,24};

void main(void)
{
	Set_Rtc(ucRtc);	
	while(1)
	{
		
	}
}

void Seg_Proc()
{
	if(++Seg_Slow) return;
	Seg_Slow=1;

	sprintf(seg_string,"%02d-%02d-%02d",(unsigned int)ucRtc[0],(unsigned int)ucRtc[1],(unsigned int)ucRtc[2]);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值