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

📄 ds1302.c

📁 51单片机开发与应用技术详解(珍藏版)PPT及源码
💻 C
字号:
#include	<reg52.h>								//头文件
#include	<stdio.h>

#define uchar unsigned char						//宏定义

sbit SCLK = P1^3;								//声明接口引脚
sbit I_O   = P1^4;
sbit RSTB = P1^5;

void		ResetDS1302();						//复位函数声明
void		InitDS1302();							// DS1302初始化函数声明
void		WriteClkByte();							//时钟字节写入函数声明
void		WriteRamByte();						// RAM字节写入函数声明
uchar	ReadByteDS1302();						//字节读取函数声明
void		WriteByteDS1302(uchar); 				//字节写入函数声明
void		Display(uchar);							//显示时钟寄存器内容函数声明
void		BurstReadRAM();						//多字节突发方式读取RAM函数声明
void		BurstWriteRAM();						//多字节突发方式写入RAM函数声明

void main (void)								//主函数
{
uchar Key, Key1,Key2;
    SCON  = 0x50;								//设置串行口: 方式 1, 8位UART, 允许接收
    TMOD  |= 0x20;       						//设置定时器T1, 方式 2, 8位自动重装
PCON  =0x80;								//设置SMOD=1
TL1    =0xF4;								//波特率4800bit/s,初值
TH1   =0xF4;
    TR1   = 1;								//开启定时器1
    TI     = 1;

	printf("***********AT89S52 CONTROL DS1302***********\n\n");
	printf("Initialize the DS1302.\n");
	InitDS1302();								//初始化DS1302
	while (1) 
	{ 
	printf("\nEnter DS1302 Menu Selection:");
	printf("c or C: Run the Clock Routine\n");
	printf("i or I: Initialize the DS1302\n");
	printf("r or R: Run the RAM Routine\n");

	Key = _getkey();							//获取输入字符
	switch(Key) 
	{
		case 'c':								//执行时钟操作
		case 'C':
			printf("\rRun the Clock Routine\n");

			Key1 = _getkey();					//获取输入字符
			switch(Key1) 
			{
				case 'd':						//显示
				case 'D':
					Display(1);			
					break;
          		case 'w':						//时钟字节写入
      			case 'W':	
					WriteClkByte();	
					break;
			}
			break;

		case 'i':								//初始化DS1302
		case 'I':	
			InitDS1302();
			break;

		case 'r':								//RAM操作
		case 'R':
			printf("\rRun the RAM Routine\n");

			Key2 = _getkey();					//获取输入字符
			switch(Key2) 
			{
				case 'b':						//RAM字节写入
				case 'B':	
					WriteRamByte();	
					break;
				case 'r':						//多字节突发方式读取RAM
				case 'R':
					BurstReadRAM();	
					break;
				case 'w':						//多字节突发方式写入RAM
				case 'W':	
					BurstWriteRAM();	
					break;
			}
		break;
	}
   }
}

void ResetDS1302() 
{
   SCLK = 0;
   RSTB = 0;
   RSTB = 1;
}

uchar ReadByteDS1302()
{
uchar i;
uchar RByte;
uchar TempByte;

	RByte = 0x00;
	I_O = 1;
	for(i=0; i<8; ++i) 							//逐位读取字节数据
{
		SCLK = 1;								//时钟操作
		SCLK = 0;
		TempByte = (uchar)I_O;
		TempByte <<= 7;						//移位
		RByte >>= 1;
		RByte |= TempByte; 
	}
	return RByte;								//返回结果
}



void WriteByteDS1302(uchar W_Byte) 
{
uchar i;
	for(i = 0; i < 8; ++i) 							//循环逐位写入
{
		I_O = 0;
		if(W_Byte & 0x01) I_O = 1;
		SCLK = 0;								//时钟操作
		SCLK = 1;
		W_Byte >>= 1;							//移位
    }
}



void WriteClkByte() 
{
uchar Address;
uchar Data;	

	printf("\nWrite Clock Function:");  
printf("\nWrite Clock ADDRESS:");  
	scanf("%bu", &Address); 						//获取地址
	printf("\nWrite Clock DATA:");
	scanf("%bx", &Data); 						//获取数据
	
	Address = ((Address * 2) | 0x80); 				//时钟数据写入命令
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(Address);					//写地址
	WriteByteDS1302(Data);						//写数据
	ResetDS1302();							//复位DS1302
}


void WriteRamByte() 
{
uchar Address;
uchar Data;

	printf("\nWrite RAM Function:");  
	printf("\nWrite Ram ADDRESS (HEX):");
	scanf("%bx", &Address);						//获取RAM地址
	printf("\nWrite Ram DATA (HEX):");
	scanf("%bx", &Data); 						//获取RAM数据

	Address = ((Address * 2) | 0xC0);				//RAM数据写入命令
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(Address);					//写RAM地址
	WriteByteDS1302(Data);						//写RAM数据
	ResetDS1302();							//复位DS1302
}


void Display(uchar loop) 
{
uchar lsec = 99, sec, min, hrs, dte, mon, day, yr;

do										//主循环
{ 
		ResetDS1302();
		WriteByteDS1302(0xBF);					//以多字节方式读取时钟寄存器数据
		sec = ReadByteDS1302();					//读取秒
		min = ReadByteDS1302();				//读取分
		hrs = ReadByteDS1302();					//读取小时
		dte = ReadByteDS1302();					//读取日期
		mon = ReadByteDS1302();				//读取月份
		day = ReadByteDS1302();				//读取星期
		yr  = ReadByteDS1302();					//读取年
		ResetDS1302();						//复位DS1302
		if(sec != lsec || !loop)						//每秒显示一次
 {									//输出结果
			printf("\nYr   Day  Mon  Dte  Hrs  Min  Sec");
			printf("\n%2.bX   %2.bX   %2.bX   %2.bX", yr, day, mon, dte);
			printf("   %2.bX   %2.bX   %2.bX", hrs, min, sec);
			lsec = sec;
		}
		if(!loop)	break;
	}while(!RI);
	if(loop)   _getkey();
}



void BurstReadRAM() 
{
uchar DS1302RAM[31];							//RAM数组
uchar i;

	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0xFF);						//多字节方式读取RAM
	for (i=0; i<31; ++i) 
	{
		DS1302RAM[i] = ReadByteDS1302();		//保存数据到RAM数组
	}
	ResetDS1302();							//复位DS1302

	printf("\nDS1302 RAM Data;");				//输出片内RAM的数据

	printf("\n%2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX",
						DS1302RAM[0], DS1302RAM[1], DS1302RAM[2], DS1302RAM[3], 
						DS1302RAM[4], DS1302RAM[5], DS1302RAM[6], DS1302RAM[7]);
	printf("\n%2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX",
						DS1302RAM[8], DS1302RAM[9], DS1302RAM[10], DS1302RAM[11],
						DS1302RAM[12], DS1302RAM[13], DS1302RAM[14], DS1302RAM[15]);
	printf("\n%2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX",
						DS1302RAM[16], DS1302RAM[17], DS1302RAM[18], DS1302RAM[19],
						DS1302RAM[20], DS1302RAM[21], DS1302RAM[22], DS1302RAM[23]);
	printf("\n%2.bX %2.bX %2.bX %2.bX %2.bX %2.bX %2.bX",
						DS1302RAM[24], DS1302RAM[25], DS1302RAM[26], DS1302RAM[27], 
						DS1302RAM[28], DS1302RAM[29], DS1302RAM[30]);

	printf("\nEND RAM Data;");
}


void BurstWriteRAM() 
{
uchar Data;
uchar	i;

	printf("\nWrite Ram DATA (HEX):");
	scanf("%bx", &Data);							//获取RAM数据

	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0xfe);						//多字节方式写入RAM 
	for (i=0; i<31; ++i) 							//循环写入
{
		WriteByteDS1302(Data);
	}
	ResetDS1302();							//复位DS1302
}


void InitDS1302() 
{
uchar	year, month, date, day, hour, minute, second;

	printf("\nEnter the Clock information: ");
	printf("\nPlease Enter the year (0-99): ");			//输入年
	scanf("%bx", &year);
	printf("\n Please Enter the month (1-12): ");		//输入月
	scanf("%bx", &month);
	printf("\n Please Enter the date (1-31): ");			//输入日
	scanf("%bx", &date);	
	printf("\n Please Enter the day (1-7): ");			//输入星期
	scanf("%bx", &day);
	printf("\n Please Enter the hour (1-24): ");			//输入小时
	scanf("%bx", &hour);
	hour =hour & 0x3f;							//设置时钟为24小时方式
	printf("\n Please Enter the minute (0-59): ");		//输入分钟
	scanf("%bx", &minute);
	printf("\n Please Enter the second (0-59): ");		//输入秒
	scanf("%bx", &second);

	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x8e);						//写保护控制寄存器
	WriteByteDS1302(0);						//允许写入

	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x90);						//涓流充电寄存器
	WriteByteDS1302(0xab);						//允许充电

	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0xbe);						//以多字节突发方式写入8个字节时钟数据
	WriteByteDS1302(second);					//写入秒
	WriteByteDS1302(minute);					//写入分钟
	WriteByteDS1302(hour);						//写入小时
	WriteByteDS1302(date);						//写入星期
	WriteByteDS1302(month);					//写入月
	WriteByteDS1302(day);						//写入日
	WriteByteDS1302(year);						//写入年
	WriteByteDS1302(0);						//对写保护控制寄存器写入0
	ResetDS1302();							//复位DS1302
}

⌨️ 快捷键说明

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