蓝桥杯单片机必备知识—–(10)DS1302时钟

蓝桥杯单片机必备知识—–(10)DS1302时钟

蓝桥杯单片机必备知识—–(10)DS1302时钟

DS1302:

在这里插入图片描述

写保护:

在这里插入图片描述

ds1302芯片:

在这里插入图片描述

ds1302.h添加代码

void ds1302_write();
void ds1302_read();

ds1302.c

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

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位 

void Write_Ds1302(unsigned  char temp) 
{
   
	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 )     
{
   
	//需增加
	unsigned char num;
	
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
	//需增加
	num=(dat/10<<4)|(dat%10);
	//替换为num
	Write_Ds1302(num);
 	//Write_Ds1302(dat); 
 	RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
   
	//需增加
	unsigned char dat_low,dat_high;
	
 	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_();
	//需增加
		/*以下是进制转化*/
	dat_high=temp/16;
	dat_low=temp%16;
	temp=dat_high*10+dat_low;	
	/*以上是进制转化*/
	
	return (temp);			
}




/***************以下都是需要自己写的*******************************/
unsigned int code shijian[]={
   50,59,23,0,0,0,0};//ds1302赋值为23:59:50
unsigned int time[7];//用于存放1302读取来的值

//1302读
//不做注释,比赛强行记忆
void ds1302_read()
{
   
  unsigned int i;
  unsigned char add;
	add=0x81;
	Write_Ds1302_Byte(0x8e,0x00);	//写保护关
	for(i=0;i<7;i++)
	{
   
		time[i]=Read_Ds1302_Byte(add);
		add=add+2;
	}
	Write_Ds1302_Byte(0x8e,0x80);	//写保护开
}

//1302写
//不做注释,比赛强行记忆
void ds1302_write()
{
   
  unsigned int i;
	unsigned char add;
	add=0x80;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<7;i++)
	{
   
		Write_Ds1302_Byte(add,shijian[i]);
		add=add+2;
	}
	Write_Ds1302_Byte(0x8e,0x80);
}


主函数中添加

extern time[]; //标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义

extern time[];//标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义

测试结果:

在这里插入图片描述

整个代码粘贴

ds1302.h

#ifndef __DS1302_H
#define __DS1302_H

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

void ds1302_write();
void ds1302_read();
#endif

ds1302.c

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

sbit SCK=P1^7;		
sbit SDA=P2^3;		
sbit RST = P1^3;   // DS1302复位 

void Write_Ds1302(unsigned  char temp) 
{
   
	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 )     
{
   
	//需增加
	unsigned char num;
	
 	RST=0;	_nop_();
 	SCK=0;	_nop_();
 	RST=1; 	_nop_();  
 	Write_Ds1302(address);	
	//需增加
	num=(dat/10<<4)|(dat%10);
	//替换为num
	Write_Ds1302(num);
 	//Write_Ds1302(dat); 
 	RST=0; 
}

unsigned char Read_Ds1302_Byte ( unsigned char address )
{
   
	//需增加
	unsigned char dat_low,dat_high;
	
 	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_();
	//需增加
		/*以下是进制转化*/
	dat_high=temp/16;
	dat_low=temp%16;
	temp=dat_high*10+dat_low;	
	/*以上是进制转化*/
	
	return (temp);			
}




/***************以下都是需要自己写的*******************************/
unsigned int code shijian[]={
   50,59,23,0,0,0,0};//ds1302赋值为23:59:50
unsigned int time[7];//用于存放1302读取来的值

//1302读
//不做注释,比赛强行记忆
void ds1302_read()
{
   
  unsigned int i;
  unsigned char add;
	add=0x81;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<7;i++)
	{
   
		time[i]=Read_Ds1302_Byte(add);
		add=add+2;
	}
	Write_Ds1302_Byte(0x8e,0x80);
}

//1302写
//不做注释,比赛强行记忆
void ds1302_write()
{
   
  unsigned int i;
	unsigned char add;
	add=0x80;
	Write_Ds1302_Byte(0x8e,0x00);
	for(i=0;i<7;i++)
	{
   
		Write_Ds1302_Byte(add,shijian[i]);
		add=add+2;
	}
	Write_Ds1302_Byte(0x8e,0x80);
}


main.c

#include <stc15f2k60s2.h>
#include "ds1302.h"

#define uchar unsigned char
#define uint unsigned int
	
uchar tab[] = {
   0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff,0xbf};
uchar dspbuf[] = {
   10,10,10,10,10,10,10,10};

extern time[];

void display();
void load();
void cls()
{
   
	P2 = (P2 & 0x1f) | 0x80;
	P0 = 0xff;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xa0;
	P0 = 0x00;
	P2 = 0x1f;
}

void main()
{
   
	cls();
	AUXR = 0xc0;
	TMOD = 0x00;
	TL0 = 0xcd;
	TH0 = 0xd4;
	
	TR0 = 1;
	ET0 = 1;
	EA = 1;
	ds1302_write();
	while(1)
	{
   
		ds1302_read();
	}
}

void time0() interrupt 1
{
   
	display();
}

void load()
{
   
	dspbuf[0] = time[2]/10;
	dspbuf[1] = time[2]%10;
	dspbuf[2] = 11;
	dspbuf[3] = time[1]/10;
	dspbuf[4] = time[1]%10;
	dspbuf[5] = 11;
	dspbuf[6] = time[0]/10;
	dspbuf[7] = time[0]%10;
}

void display()
{
   
	static unsigned char dspcom = 0;
	
	load();
	
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = 0xff;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xc0;
	P0 = 1 << dspcom;
	P2 = 0x1f;
	
	P2 = (P2 & 0x1f) | 0xe0;
	P0 = tab[dspbuf[dspcom]];
	P2 = 0x1f;
	
	if(++dspcom == 8) dspcom = 0;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/100137.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • eigen库使用_eigen3

    eigen库使用_eigen3Eigen库的安装1.VisualStudio2017安装eigen库1.1下载eigen库1.2配置1.3运行测试1.VisualStudio2017安装eigen库1.1下载eigen库eigen官网下载地址找到自己需要的版本下载,我下载的是3.3.9,箭头指向的zip。解压缩得到文件eigen-3.3.9,放到自己想放置的路径下(后面会引用此处的路径)。1.2配置在VS2017中新建一个空项目,取名为“eigen_demo”。输入以下测试

    2022年10月19日
    1
  • datagrip激活码2020 mac_通用破解码

    datagrip激活码2020 mac_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月17日
    146
  • 树莓派 gpio usb_树莓派gpio编程

    树莓派 gpio usb_树莓派gpio编程概览树莓派最令人兴奋的特点之一是它有一个GPIO连接器可以用来接其他的硬件设备。GPIO连接器实际上是由许多不同类型的接口组成的:真正的GPIO(GeneralPurposeInputOutput,通用输入/输出)针脚,你可以用来控制LED灯的开和关。I2C(Inter-IntegratedCircuit)接口针脚,使你能够仅使用2个控制针脚连接硬件模块。SPI(SerialPeriph…

    2022年10月14日
    0
  • KNN与SVM对比&SVM与逻辑回归的对比

    KNN与SVM对比&SVM与逻辑回归的对比

    2021年11月19日
    51
  • 深度学习—3.Pytorch基础

    深度学习—3.Pytorch基础

    2021年10月6日
    39
  • 项目中更新Stimulsoft组件的方法

    项目中更新Stimulsoft组件的方法我们正在不断开发我们的软件。我们的主要目标是成为软件工程的前沿。每个版本均包含新功能,组件优化和错误修复。这就是为什么新发行版始终是先前版本的产品改进的原因。但是,并非所有用户都知道在他们的项目中更新

    2022年7月4日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号