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

📄 main.c

📁 DS1302读写程序
💻 C
字号:
#include <reg51.h>
#include <intrins.h>
#define uint unsigned int 
#define uchar unsigned char

sbit SCLK = P1^1;
sbit IO   = P1^2;
sbit RST  = P1^0;
sbit LED  = P1^3;
void Display( void );
void DelayMs( uchar n );

void WriteByte( uchar D );
uchar ReadByte( void );

void WriteDate( uchar Add , uchar Date );
uchar ReadDate( uchar Add );

void SetTime( uchar Hour , uchar Min , uchar Sec );
void GetTime( void );

uchar DispBuf[6]={1,2,3,4,5,6};
code uchar DispCode[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
main()
{
	uchar Cnt=0;
	SetTime( 0x15 , 0x23 ,0x50 );
	while(1)
	{
		GetTime();
		Display();	
		if( ++Cnt == 20)
		{	LED = ~LED;	
			Cnt = 0;
		}
	}
}

void Display( void )
{	
	uchar i,LedSelect,DispDate;
	LedSelect = 0xfe;
	
	for( i = 0 ; i < 6 ; i++ )
	{
		DispDate = DispBuf[i];
		P2 = DispCode[DispDate];
		P3 = LedSelect;
		LedSelect <<= 1;
		LedSelect += 1;
		DelayMs( 5 ); 
		P3 = 0xff;
	}
}

void DelayMs( uchar n )
{
	uchar i;
	while( n-- )
	{
		for( i = 0 ; i < 125 ; i++ )
			;
	}
}

void SetTime( uchar Hour , uchar Min , uchar Sec )
{
	WriteDate( 0x84 , Hour);
	WriteDate( 0x82 , Min);
	WriteDate( 0x80 , Sec);
}
void GetTime( void )
{	
	uchar Time;
	Time = ReadDate( 0x85 );
	DispBuf[0] = Time >> 4;
	DispBuf[1] = Time & 0x0f; 
	Time = ReadDate( 0x83 );
	DispBuf[2] = Time >> 4;
	DispBuf[3] = Time & 0x0f; 
	Time = ReadDate( 0x81 );
	DispBuf[4] = Time >> 4;
	DispBuf[5] = Time & 0x0f;
}

⌨️ 快捷键说明

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