蓝桥杯单片机组-16 DS1302

要求

DS1302实时时钟

DS1302

简单来说,DS1302可以理解为一个电子手表,里面除了存有时、分、秒、日,还带有一个31字节的RAM内存。可以用来记录实时时间、其它数据

代码

文件的基本操作

1.需要将底层驱动引入刚刚建好的工程中。

2.检查底层驱动文件:ds1302.c

        引脚是否和开发板对应!

 考得难点的话,可能直接不写引脚,要补充上去!

3.检查底层驱动文件:ds1302.h

        头文件是否申明!

4.引入xmf_smg_ca.cxmf_smg_ca.h

DS1302基本配置 

DS1302的基本配置:设定时间参数+读取实时时间

背!

/*============日历时钟参数配置=========*/
//配置
void DS1302_Config()
{
	char i;
	
	Write_Ds1302_Byte(0x8e, 0x00);  //打开保护
	for(i = 0; i < 7; i++)
	{
		Write_Ds1302_Byte(Write_DS1302_adrr[i], Timer[i]);
	}
	Write_Ds1302_Byte(0x8e, 0x80);
}

温度读取:背!

//读取
void Read_DS1302_Timer()
{
	unsigned char i;
	for(i = 0; i < 7; i++)
	{
		Timer[i] = Read_Ds1302_Byte(Read_DS1302_adrr[i]);
	}
}

数码管显示时间

//数码管显示
void DisPlay_DS1302()
{
	DisPlaySMG_Bit(0,SMGNoDot_CA[Timer[2]/16]);    //时间的十位
	DelaySMG(100);
	DisPlaySMG_Bit(1,SMGNoDot_CA[Timer[2]%16]);    //时间的个位
	DelaySMG(100);
	
	DisPlaySMG_Bit(2,0xbf);    //显示”-“
	DelaySMG(100);
	
	DisPlaySMG_Bit(3,SMGNoDot_CA[Timer[1]/16]);
	DelaySMG(100);
	DisPlaySMG_Bit(4,SMGNoDot_CA[Timer[1]%16]);
	DelaySMG(100);
	
	DisPlaySMG_Bit(5,0xbf);    //显示”-“
	DelaySMG(100);
	
	DisPlaySMG_Bit(6,SMGNoDot_CA[Timer[0]/16]);
	DelaySMG(100);
	DisPlaySMG_Bit(7,SMGNoDot_CA[Timer[0]%16]);
	DelaySMG(100);
	
	DisPlay_All(0xff);        //关闭全部数码管
}

完整代码

main

#include "reg52.h"
#include "ds1302.h"
#include "xmf_smg_ca.h"

unsigned char Write_DS1302_adrr[7] = {0X80, 0X82, 0X84, 0X86, 0X88, 0X8A, 0X8C};
unsigned char Read_DS1302_adrr[7] = {0X81, 0X83, 0X85, 0X87, 0X89, 0X8B, 0X8D};
//24年2月29日,星期4
unsigned char Timer[7] = {0X24, 0X59, 0X23, 0X18, 0X04, 0X06, 0X20};

unsigned char code SMG_duanma[18] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};//分别是0-9(对应下标),A-F,“-”,“.”

//配置
void DS1302_Config()
{
	char i;
	
	Write_Ds1302_Byte(0x8e, 0x00);  //打开保护
	for(i = 0; i < 7; i++)
	{
		Write_Ds1302_Byte(Write_DS1302_adrr[i], Timer[i]);
	}
	Write_Ds1302_Byte(0x8e, 0x80);
}
//读取
void Read_DS1302_Timer()
{
	unsigned char i;
	for(i = 0; i < 7; i++)
	{
		Timer[i] = Read_Ds1302_Byte(Read_DS1302_adrr[i]);
	}
}
//数码管显示
void DisPlay_DS1302()
{
	DisPlaySMG_Bit(0,SMGNoDot_CA[Timer[2]/16]);    //时间的十位
	DelaySMG(100);
	DisPlaySMG_Bit(1,SMGNoDot_CA[Timer[2]%16]);    //时间的个位
	DelaySMG(100);
	
	DisPlaySMG_Bit(2,0xbf);    //显示”-“
	DelaySMG(100);
	
	DisPlaySMG_Bit(3,SMGNoDot_CA[Timer[1]/16]);
	DelaySMG(100);
	DisPlaySMG_Bit(4,SMGNoDot_CA[Timer[1]%16]);
	DelaySMG(100);
	
	DisPlaySMG_Bit(5,0xbf);    //显示”-“
	DelaySMG(100);
	
	DisPlaySMG_Bit(6,SMGNoDot_CA[Timer[0]/16]);
	DelaySMG(100);
	DisPlaySMG_Bit(7,SMGNoDot_CA[Timer[0]%16]);
	DelaySMG(100);
	
	DisPlay_All(0xff);        //关闭全部数码管
}

void main()
{
	DS1302_Config();
	while(1)
	{
		Read_DS1302_Timer();
		DisPlay_DS1302();
	}
}

ds1302.c

此为底层驱动文件

#include "ds1302.h"  									
#include "intrins.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);			
}

ds1302.h

注意引脚的申明

#ifndef __DS1302_H
#define __DS1302_H

#include <reg52.h>
#include <intrins.h>

sbit SCK = P1^7;		
sbit SDA = P2^3;		
sbit RST = P1^3; 

void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );

#endif

xmf_smg_ca.c

这个需要直接写!背!

#include"absacc.h"

void DelaySMG(unsigned int t)
{
	while(t--);
}

//数码管显示
void DisPlaySMG_Bit(unsigned char pos,unsigned char dat)
{
  XBYTE[0xE000] = 0xff;             //数码管关闭
	XBYTE[0xC000] = 0x01 << pos;      //数码管显示的位
	XBYTE[0xE000] = dat;
}

//对所有数码管统一操作的函数
void DisPlay_All(unsigned char dat)
{
	XBYTE[0xC000] = 0xff;      //数码管显示的位
	XBYTE[0xE000] = dat;
}

xmf_smg_ca.h

这个需要自己写!背!

#ifndef XMF_SMG_CA_H
#define XMF_SMG_CA_H

code unsigned char SMGNoDot_CA[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
code unsigned char SMGDot_CA[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

void DelaySMG(unsigned int t);
void DisPlaySMG_Bit(unsigned char pos,unsigned char dat);
void DisPlay_All(unsigned char dat);

#endif 

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值