⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds1302.c

📁 我做的畢業設計,用AT89S51 控制LCD1602作為顯示. DS1302時鐘芯片顯示時間,DS18B20測量溫度,還有4X4的鍵盤驅動.實現了一個計算功能.可以用PROTUES 仿真軟件仿真,當時
💻 C
字号:
/**************************************************************************
  
                 
  
   File Name:       DS1302.h
   Author:          liao xu ming
   Created:         2007/3/23
   Modified:		2007/4/8
   Revision: 		1.0
  
***************************************************************************/

#include"DS1302.h"
#include"clock.h"

sbit  DS1302_CLK = P3^1;              //实时时钟时钟线引脚 
sbit  DS1302_IO  = P3^2;              //实时时钟数据线引脚 
sbit  DS1302_RST = P3^3;              //实时时钟复位线引脚
sbit  ACC0 = ACC^0;
sbit  ACC7 = ACC^7;



#define AM(X)	X
#define PM(X)	(X+12)            	  // 转成24小时制
#define DS1302_SECOND	0x80
#define DS1302_MINUTE	0x82
#define DS1302_HOUR		0x84 
#define DS1302_WEEK		0x8A
#define DS1302_DAY		0x86
#define DS1302_MONTH	0x88
#define DS1302_YEAR		0x8C
#define DS1302_RAM(X)	(0xC0+(X)*2)   	//用于计算 DS1302_RAM 地址的宏 

void DS1302InputByte(unsigned char d) 	//实时时钟写入一字节(内部函数)
{ 
    unsigned char i;
    ACC = d;
    for(i=8; i>0; i--)
    {
        DS1302_IO = ACC0;           	//相当于汇编中的 RRC
        DS1302_CLK = 1;
        DS1302_CLK = 0;
        ACC = ACC >> 1; 
    } 
}

unsigned char DS1302OutputByte(void) 	//实时时钟读取一字节(内部函数)
{ 
    unsigned char i;
    for(i=8; i>0; i--)
    {
        ACC = ACC >>1;         			//相当于汇编中的 RRC 
        ACC7 = DS1302_IO;
        DS1302_CLK = 1;
        DS1302_CLK = 0;
    } 
    return(ACC); 
}

void Write1302(unsigned char ucAddr, unsigned char ucDa)	//ucAddr: DS1302地址, ucData: 要写的数据
{
    DS1302_RST = 0;
    DS1302_CLK = 0;
    DS1302_RST = 1;
    DS1302InputByte(ucAddr);       	// 地址,命令 
    DS1302InputByte(ucDa);       	// 写1Byte数据
    DS1302_CLK = 1;
    DS1302_RST = 0;
} 

unsigned char Read1302(unsigned char ucAddr)	//读取DS1302某地址的数据
{
    unsigned char ucData;
    DS1302_RST = 0;
    DS1302_CLK = 0;
    DS1302_RST = 1;
    DS1302InputByte(ucAddr|0x01);        // 地址,命令 
    ucData = DS1302OutputByte();         // 读1Byte数据
    DS1302_CLK = 1;
    DS1302_RST = 0;
    return(ucData);
}

void DS1302_SetProtect(bit flag)        //是否写保护
{
	if(flag)
		Write1302(0x8E,0x10);   //Bit 7 of the control register is the write-protect bit.
	else                        //Before any write operation to the clock or RAM, bit 7 must be 0.
		Write1302(0x8E,0x00);   //When high,the write protect bit prevents a write operation to any other register.
}

void DS1302_SetTime(unsigned char Address, unsigned char Value)        // 设置时间函数
{
	DS1302_SetProtect(0);
	Write1302(Address, ((Value/10)<<4 | (Value%10))); 
}


void DateToStr()
{   BurstRead1302(ClockRtc);
    DateString[0]=(((ClockRtc[6]&0X70)>>4)*10+(ClockRtc[6]&0X0F))/10+'0';
    DateString[1]=(((ClockRtc[6]&0X70)>>4)*10+(ClockRtc[6]&0X0F))%10+'0';
    DateString[2]='/'; //可否省掉

    DateString[3]=(((ClockRtc[4]&0X70)>>4)*10+(ClockRtc[4]&0X0F))/10+'0';
    DateString[4]=(((ClockRtc[4]&0X70)>>4)*10+(ClockRtc[4]&0X0F))%10+'0';
    DateString[5]='/';

    DateString[6]=(((ClockRtc[3]&0X70)>>4)*10+(ClockRtc[3]&0X0F))/10+'0';
    DateString[7]=(((ClockRtc[3]&0X70)>>4)*10+(ClockRtc[3]&0X0F))%10+'0';
    DateString[8]='\0';;
    
    TimeString[0]=(((ClockRtc[2]&0X70)>>4)*10+(ClockRtc[2]&0X0F))/10+'0';
    TimeString[1]=(((ClockRtc[2]&0X70)>>4)*10+(ClockRtc[2]&0X0F))%10+'0';
    TimeString[2]=':';

    TimeString[3]=(((ClockRtc[1]&0X70)>>4)*10+(ClockRtc[1]&0X0F))/10+'0';
    TimeString[4]=(((ClockRtc[1]&0X70)>>4)*10+(ClockRtc[1]&0X0F))%10+'0';
    TimeString[5]=':';

    TimeString[6]=(((ClockRtc[0]&0X70)>>4)*10+(ClockRtc[0]&0X0F))/10+'0';
    TimeString[7]=(((ClockRtc[0]&0X70)>>4)*10+(ClockRtc[0]&0X0F))%10+'0';
    TimeString[8]='\0';
}


void Initial_DS1302(void)
{
	unsigned char Second=Read1302(DS1302_SECOND);
	if(Second&0x80)		  //Bit 7 of the seconds register is defined as the clock halt flag.
		DS1302_SetTime(DS1302_SECOND,0);//When this bit is written to logic 0, the clock will start.
}                                       //When this bit is set to logic 1, the clock
										//oscillator is stopped and 
										//the DS1302 is placed into a low-power standby 
										//mode with a current drain of less
										//than 100 nanoamps.

/*******************************************************************************/
void BurstWrite1302(unsigned char ClockRtc[])	//往DS1302写入时钟数据(多字节方式)
{
    unsigned char i;
    unsigned char * pWClock;
    pWClock=ClockRtc;
   DS1302_SetProtect(0);
   // Write1302(0x8e,0x00);         	// 控制命令,WP=0,写操作 Before any write operation to the clock or RAM, bit 7 must be 0.
    DS1302_RST = 0;					//
    DS1302_CLK = 0;
    DS1302_RST = 1;
    DS1302InputByte(0xbe);        	// 0xbe:时钟多字节写命令
    for (i = 8; i>0; i--)     		//8Byte = 7Byte 时钟数据 + 1Byte 控制
    {
        DS1302InputByte(*pWClock); 	// 写1Byte数据
        pWClock++;
    }
    DS1302_CLK = 1;
    DS1302_RST = 0;
}/***/

void BurstRead1302(unsigned char ClockRtc[])	//读取DS1302时钟数据(时钟多字节方式)
{
    unsigned char i;                    //
    unsigned char *pWClock;
    pWClock=ClockRtc;
    DS1302_RST = 0;
    DS1302_CLK = 0;
    DS1302_RST = 1;                      
    DS1302InputByte(0xbf);             	// 0xbf:时钟多字节读命令 
    for (i=8; i>0; i--) 
    {
       *pWClock = DS1302OutputByte();   // 读1Byte数据 
       pWClock++;
    }
    DS1302_CLK = 1;
    DS1302_RST = 0;
}


void Delay1ms(unsigned int count)
{
	unsigned int i,j;
	for(i=0;i<count;i++)
	for(j=0;j<120;j++);
}

/*
void DS1302_TimeStop(bit flag)           // 是否将时钟停止
{
	unsigned char Data;
	Data=Read1302(DS1302_SECOND);
	DS1302_SetProtect(0);
	if(flag)
		Write1302(DS1302_SECOND, Data|0x80);//Bit 7 of the seconds register is defined as the clock halt flag.
	else                                    //When this bit is set to logic 1, the clock
        Write1302(DS1302_SECOND, Data&0x7F);// oscillator is stopped and the DS1302 is placed 
                                            // into a low-power standby mode with a current drain of less
                                            //than 100 nanoamps
                                         	//When this bit is written to logic 0, the clock will start.
}
********************************************************************************/
//#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -