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

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

DS1302:

蓝桥杯单片机必备知识-----(10)DS1302时钟_第1张图片

写保护:

在这里插入图片描述

ds1302芯片:

蓝桥杯单片机必备知识-----(10)DS1302时钟_第2张图片

ds1302.h添加代码

void ds1302_write();
void ds1302_read();

ds1302.c

#include 
#include 

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[];//标示变量或者函数的定义在别的文件中,提示编译器遇到此变量和函数时在其他模块中寻找其定义

测试结果:

蓝桥杯单片机必备知识-----(10)DS1302时钟_第3张图片

整个代码粘贴

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 
#include 

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

你可能感兴趣的:(蓝桥杯单片机)