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

📄 ds1337.h

📁 时钟芯片ds1337在嵌入式处理器nios上的底层驱动程序
💻 H
字号:
#include "excalibur.h"

#define	ADDRTC	0xd0	
typedef  unsigned char	uchar;

np_pio *clk1337=na_clk1337;
np_pio *dio1337=na_dio1337;

uchar	sec, min, hr, dy, dt, mn, yr;
uchar display1337[6];
uchar displaycalendar[6];

void delay1337(uchar a)
{
	int i=0;
	for(i=0;i<a;i++){;}
}
void I2C_start()
{
	dio1337->np_piodata=1;
	clk1337->np_piodata=1;
	dio1337->np_piodata=0;
}

void I2C_stop()		
{
		 dio1337->np_piodata=0;
		 clk1337->np_piodata=1;
		 dio1337->np_piodata=1;
}

void ack()
{
clk1337->np_piodata=0;
dio1337->np_piodata=0;
clk1337->np_piodata=1;
clk1337->np_piodata=0;
}

void I2C_write(uchar dat)		
{
	uchar i;
	for(i=0;i<8;i++)
   {
	delay1337(1);
   clk1337->np_piodata=0;
   dio1337->np_piodata=dat>>7;
   dat=dat<<1;
	delay1337(1);
   clk1337->np_piodata=1;
   }
    ack();
}

uchar I2C_read()	
{
	uchar d=0, i;
	dio1337->np_piodirection=0;
	for (i = 1; i <= 8; i++)	
	{
			delay1337(1);
             clk1337->np_piodata = 0;
            delay1337(1);
            clk1337->np_piodata = 1;
		d = d << 1;
		d |=dio1337->np_piodata;
	}
	dio1337->np_piodirection = 1;
	return d;
}
void settime(uchar hour,uchar minute,uchar second)	// -- initialize the time and date using entries from stdin -- 
{
	uchar a=0;
	//I2C_start();		// The following Enables the Oscillator 
	//I2C_write(ADDRTC);	// address the part to write 
	//I2C_write(0x00);	// position the address pointer to 0 
	//I2C_write(0x00);	// write 0 to the seconds register, clear the CH bit 
	//I2C_stop();

	I2C_start();
	I2C_write(ADDRTC);	// write slave address + write
	I2C_write(0x00);	// write register address, 1st clock register
	a|=(second%10)&0x0f;
	a|=(second/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(minute%10)&0x0f;
	a|=(minute/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(hour%10)&0x0f;
	a|=(hour/10)<<4;	
	I2C_write(a);
	

	//I2C_start();
	//I2C_write(ADDRTC);	
	//I2C_write(0x0e);	
	//I2C_write(0x20);	
	//I2C_write(0);		
	I2C_stop();

}
void set(uchar year,uchar month,uchar date,uchar day,uchar hour,uchar minute,uchar second)	// -- initialize the time and date using entries from stdin -- 
{
	uchar a=0;
	//I2C_start();		// The following Enables the Oscillator 
	//I2C_write(ADDRTC);	// address the part to write 
	//I2C_write(0x00);	// position the address pointer to 0 
	//I2C_write(0x00);	// write 0 to the seconds register, clear the CH bit 
	//I2C_stop();

	I2C_start();
	I2C_write(ADDRTC);	// write slave address + write
	I2C_write(0x00);
	a|=(second%10)&0x0f;
	a|=(second/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(minute%10)&0x0f;
	a|=(minute/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(hour%10)&0x0f;
	a|=(hour/10)<<4;	
	I2C_write(a);
	
	
	I2C_write(day);
	a=0;
	a|=(date%10)&0x0f;
	a|=(date/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(month%10)&0x0f;
	a|=(month/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(year%10)&0x0f;
	a|=(year/10)<<4;	
	I2C_write(a);

	//I2C_start();
	//I2C_write(ADDRTC);	
	//I2C_write(0x0e);	
	//I2C_write(0x20);	
	//I2C_write(0);		
	I2C_stop();
}
void setcalendar(uchar year,uchar month,uchar day)	// -- initialize the time and date using entries from stdin -- 
{
	uchar a=0;
	//I2C_start();		// The following Enables the Oscillator 
	//I2C_write(ADDRTC);	// address the part to write 
	//I2C_write(0x00);	// position the address pointer to 0 
	//I2C_write(0x00);	// write 0 to the seconds register, clear the CH bit 
	//I2C_stop();

	I2C_start();
	I2C_write(ADDRTC);	// write slave address + write
	I2C_write(4);
	a=0;
	a|=(day%10)&0x0f;
	a|=(day/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(month%10)&0x0f;
	a|=(month/10)<<4;	
	I2C_write(a);
	a=0;
	a|=(year%10)&0x0f;
	a|=(year/10)<<4;	
	I2C_write(a);

	//I2C_start();
	//I2C_write(ADDRTC);	
	//I2C_write(0x0e);	
	//I2C_write(0x20);	
	//I2C_write(0);		
	I2C_stop();

}

unsigned char readany(unsigned char b)//b为0,1,2时分别读取秒。分时
{
	unsigned char a;
	    I2C_start();
		I2C_write(ADDRTC);	
		I2C_write(b);	 
		I2C_start();
		I2C_write(ADDRTC | 1);
		a=I2C_read();
		I2C_stop();
		return a;
}
void initial1337()
{
	dio1337->np_piodirection=1;
	}
	

⌨️ 快捷键说明

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