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

📄 1.c

📁 SD2000时钟芯片读写程序
💻 C
字号:
#include <intrins.h>
#include "reg51.h"
#include "sd2000.h"
#define uchar unsigned char
#define uint unsigned char
sbit	SDA=P0^6;
sbit	SCL=P0^7;
#define true  1
#define false 0

/*********I2C延时***********/
void Wait(void)
{
    unsigned char n;
	for(n=0;n<6;n++)
  {	
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();
       _nop_();	
   }
}
//********开启SD2001C的I2C总线***********//
void Start(void)
{
	SDA=1;
	SCL=1;
	Wait();
	SDA=0;
	Wait();
	SCL=0;
}
/********关闭SD2001C的I2C总线*******/
void Stop(void)
{
	SDA=0;
	SCL=0;
	Wait();
	SCL=1;
	Wait();
	SDA=1;
}
/*********确认信号ACK*********/
void Ack(void)
{	
	SDA=0;
	Wait();
	SCL=1;
	Wait();
	SCL=0;
}

void NoAck(void)
{	
	SDA=1;
	Wait();
	SCL=1;
	Wait();
	SCL=0;
}

bit WaitAck(void)
{
	uchar delay_time=255;
	SDA=1;
	Wait();
	SCL=1;
	Wait();
	while(SDA)
	{
		delay_time--;
		if(!delay_time)
			return (false);
	}
	SCL=0;
	return (true);
}
/************向SD2001C发送一个字节*************/
void SD_SendByte(uchar date,bit order)  //order=1,发送命令,=0发数据
{
	uchar i=8;                            
	if(order)
	{
		while(i--)
		{
			SCL=0;
			_nop_();
			SDA=(bit)(date&0x80);
			date<<=1;
			Wait();
			SCL=1;
			Wait();
		}
		SCL=0;
	}
     else
	{
		while(i--)
		 {
			SCL=0;
			_nop_();
			SDA=(bit)(date&0x01);
			date>>=1;
			Wait();
			SCL=1;
			Wait();
		 }
		 SCL=0;
	}
}
/************从SD2000读出一字节*********/ 
uchar SD_ReceiveByte(void)
{
	uchar i=8;
	uchar temp=0;
	SDA=1;
	while(i--)
	{
		temp>>=1;    //数据从低位开始读取
		SCL=0;
		Wait();
		SCL=1;
		Wait();	   //从高位开始 ddata|=SDA;ddata<<=1
		if(SDA)
		{
			temp|=0x80;
		}
	}
	SCL=0;
	return (temp);
}
////////////串写SD2000/////////////
void SD_Write(uchar * time)
{
    uchar n;
    Start();
	SD_SendByte(0x64,1);   //从年开始读取数据
	WaitAck();
    //time[4]=time[4]&0x7f;  //24小时制下,将小时的最高位置为0;
	for(n=0;n<7;n++)
	{
    	SD_SendByte(time[n],0);	
		if (n!=6)     //最后一个数据不应答
		{
			Ack();
		}
	}
	NoAck();
	Stop();
}
////////////串读SD2000/////////////
void SD_ReadTime(unsigned char *time)
{ 
unsigned char temp;
	Start();
    SD_SendByte(0x65,1);   //从年开始读取数据
	WaitAck();
	for(temp=0;temp<7;temp++)
	{
     	time[temp]=SD_ReceiveByte();
		if (temp!=6)     //最后一个数据不应答
		{
			Ack();
		}
	}		
    Stop();
}
/*************设置时间************/
void SD_SetTime(uchar  *time)
{
	uchar  temp[7];
	temp[0]=time[0];
	temp[1]=time[1];
	temp[2]=time[2];
	temp[3]=time[3];
	temp[4]=time[4];
	temp[5]=time[5];
	temp[6]=time[6];
	SD_Write(&temp[0]);

}
/***********SD2000复位 ******************/
void SD_Rest(void)
{
	Start();
	SD_SendByte(0x60,1);  //发送SD20001C复位命令 00.01.01.01.00.00.00
	Wait();
	Stop();

	Start();
	SD_SendByte(0x62,1);  //发送SD20001C状态寄存器命令,从年开始读
	WaitAck();   
	SD_SendByte(0x80,1);  //24小时制,关所有中断
	WaitAck();
	Stop();        
}
void main()
{
uchar a,b,c,d,e,f,g;
uchar time[7];
uchar pp[7]={0x04,0x08,0x26,0x04,0x82,0x29,0x00};
//SD_Rest();
SD_SetTime(pp);
while(1)
{
SD_ReadTime(time);
a=time[0];
b=time[1];
c=time[2];
d=time[3];
e=time[4];
f=time[5];
g=time[6];
}
}

⌨️ 快捷键说明

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