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

📄 ds1302.c

📁 零基础单片机C语言设计全套教程
💻 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();						//复位子函数
uchar ReadByteDS1302();							//字节读取子函数
void WriteByteDS1302(uchar W_Byte) ;				//字节写入子函数
void SetYear() ;									//年设置子函数
void SetMonth(); 								//月设置子函数
void SetDate(); 									//日设置子函数
void SetDay(); 									//星期设置子函数
void SetHour(); 									//小时设置子函数
void SetMinute (); 								//分钟设置子函数
void SetSecond (); 								//秒设置子函数
void DisableWrite (); 								//写保护子函数
void EnableWrite (); 								//写允许子函数
void Charge (); 									//充电控制子函数
void InitDS1302(); 								//初始化子函数
void WriteRamByte(); 		   						//RAM字节写入子函数
void Display(uchar loop); 					   		//时钟显示子函数
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:\n");
	printf("d or D: Display Clock Routine\n");
	printf("s or S: Set DS1302\n");
	printf("i or I: Run the Initialize Routine\n");
	printf("r or R: Run the RAM Routine\n");
	printf("e or E: Exit\n");

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

		case 's':								//初始化DS1302
		case 'S':	
			printf("\rRun Set DS1302\n");
			printf("\nEnter DS1302 Time Set Menu Selection:");
			printf("y or Y: Set year\n");
			printf("M : Set Month\n");
			printf("D:Set date\n");
			printf("d: Set day\n");
			printf("h or H: Set hour\n");
			printf("m: Set minute\n");
			printf("s or S: Set second\n");

			Key1 = _getkey();					//获取输入字符
			switch(Key1) 
			{
				case 'y':						//设置年
				case 'Y':
					SetYear();			
					break;
				case 'M':						//设置月
					SetMonth();			
					break;
				case 'D':						//设置星期
					SetDate();			
					break;
				case 'd':						//设置日
					SetDay();			
					break;
				case 'h':						//设置小时
				case 'H':
					SetHour();			
					break;
				case 'm':						//设置分钟示
					SetMinute();			
					break;
				case 's':						//设置秒
				case 'S':
					SetSecond();			
					break;
				default:
					printf("Please Input the Right Option");
			}
			break;

		case 'i':								//初始化DS1302
		case 'I':	
			printf("Run the Initialize Routine\n");
			InitDS1302();
			break;

		case 'r':								//RAM操作
		case 'R':
			printf("\rRun the RAM Routine\n");
			printf("\nEnter DS1302 RAM Menu Selection:");
			printf("b or B: Write RAM Byte\n");
			printf("r or R: Burst Read RAM\n");
			printf("w or W: Burst Write RAM \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;
				default:
					printf("Please Input the Right Option");
			}
			break;

		case 'e':
		case 'E':
			goto exit;
			break;

		default:
			printf("Please Input the Right Option");
	}
   }
exit:	printf("Exit the program");	  
}





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 = TempByte <<7;				//移位
		RByte = 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 = W_Byte >>1;					//移位
    }
}

void SetYear() 									//年设置子函数
{
	uchar	year;
	printf("\nPlease Enter the year (0-99): ");			//输入年
	scanf("%bx", &year);
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x06);						//年寄存器地址
	WriteByteDS1302(year);						//写入年
	ResetDS1302();							//复位DS1302
}

void SetMonth() 								//月设置子函数
{
	uchar	month;
	printf("\n Please Enter the month (1-12): ");		//输入月
	scanf("%bx", &month);
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x04);						//月寄存器地址
	WriteByteDS1302(month);					//写入月
	ResetDS1302();							//复位DS1302
}

void SetDate() 									//日设置子函数
{
	uchar	date;
	printf("\n Please Enter the date (1-31): ");			//输入日
	scanf("%bx", &date);	
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x03);						//日寄存器地址
	WriteByteDS1302(date);						//写入日
	ResetDS1302();							//复位DS1302
}

void SetDay() 									//星期设置子函数
{
	uchar	day;
	printf("\n Please Enter the day (1-7): ");			//输入星期
	scanf("%bx", &day);
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x05);						//星期寄存器地址
	WriteByteDS1302(day);						//写入星期
	ResetDS1302();							//复位DS1302
}

void SetHour() 									//小时设置子函数
{
	uchar	hour;
	printf("\n Please Enter the hour (1-24): ");			//输入小时
	scanf("%bx", &hour);
	hour =hour & 0x3f;							//设置时钟为24小时方式
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x02);						//小时寄存器地址
	WriteByteDS1302(hour);						//写入小时
	ResetDS1302();							//复位DS1302
}

void SetMinute () 								//分钟设置子函数
{
	uchar	minute;
	printf("\n Please Enter the minute (0-59): ");		//输入分钟
	scanf("%bx", &minute);
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x01);						//分钟寄存器地址
	WriteByteDS1302(minute);					//写入分钟
	ResetDS1302();							//复位DS1302
}

void SetSecond () 								//秒设置子函数
{
	uchar	second;
	printf("\n Please Enter the second (0-59): ");		//输入秒
	scanf("%bx", &second);
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x00);						//秒寄存器地址
	WriteByteDS1302(second);					//写入秒
	ResetDS1302();							//复位DS1302
}

void DisableWrite () 								//写保护子函数
{
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x8e);						//写保护控制寄存器
	WriteByteDS1302(0x80);						//禁止写入
	ResetDS1302();							//复位DS1302
}

void EnableWrite () 								//写允许子函数
{
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x8e);						//写保护控制寄存器
	WriteByteDS1302(0);						//允许写入
	ResetDS1302();							//复位DS1302
}

void Charge () 									//充电控制子函数
{
	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0x90);						//涓流充电寄存器
	WriteByteDS1302(0xab);						//允许充电
	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);

	EnableWrite ();								//写允许子函数
	Charge ();									//允许充电

	ResetDS1302();							//复位DS1302
	WriteByteDS1302(0xbe);						//以多字节突发方式写入8个字节时钟数据
	WriteByteDS1302(second);					//写入秒
	WriteByteDS1302(minute);					//写入分钟
	WriteByteDS1302(hour);						//写入小时
	WriteByteDS1302(date);						//写入日
	WriteByteDS1302(month);					//写入月
	WriteByteDS1302(day);						//写入星期
	WriteByteDS1302(year);						//写入年
	WriteByteDS1302(0);						//对写保护控制寄存器写入0
	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[31];
uchar	i;

	for(i=0;i<31;i++)
	{
		printf("\nWrite Ram DATA (HEX):");
		scanf("%bx", &Data[i]);					//获取RAM数据
	}

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

⌨️ 快捷键说明

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