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

📄 452_i2crtc.h

📁 It is a source code for access and program from PIC 18F452 to DS1307 Real Time Clock by I2C interfac
💻 H
字号:
/* *****************************************************************************************/
// Hardware I2C single master routines for PIC18F452
// for HI-TEC PIC C COMPILER.
//
// i2c_init  - initialize I2C functions
// i2c_start - issue Start condition
// i2c_repStart- issue Repeated Start condition
// i2c_stop  - issue Stop condition
// i2c_read(x) - receive unsigned char - x=0, don't acknowledge - x=1, acknowledge
// i2c_write - write unsigned char - returns ACK
//
// modified from CCS i2c_877 demo
//
// by
//
// Marcelo Molina
// My E-mail marcelo_moy@yahoo.com
//
/* *****************************************************************************************/
#define RUTINAS_I2C_CARGADAS 1
#define a1 1

#define DIREC_DOS_BYTES		 1	// Indica que se han de considerar dos bytes para las direcciones


// **************************************************************
// *     Rutina que genera un retardo en Ms.
// **************************************************************
/*void DelayMs(unsigned char cnt)
{
#if PIC_CLK <= 2000
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if PIC_CLK > 2000
	unsigned char	i;
	do {
		i = 4;
		do {
			DelayUs(250);
		} while(--i);
	} while(--cnt);
#endif

}

*/

// ********** Fin Rutina de retardo. ***************************

void i2c_init()
{
  TRISC3=1;           // set SCL and SDA pins as inputs
 TRISC4=1;

 SSPCON1 = 0x38;      // set I2C master mode
 SSPCON2 = 0x00;

// file://SSPADD = 0x6;     // 400kHz bus with 10MHz xtal - use 0x0C with 20MHz xtal
 //SSPADD = 10;            // 100k at 4Mhz clock
 SSPADD = 100;            // 100khz at 40Mhz clock
// SSPADD = 25;            // 400khz at 40Mhz clock

 CKE=1;     // use I2C levels      worked also with '0'
 SMP=1;     // disable slew rate control  worked also with '0'

 PSPIF=0;      // clear SSPIF interrupt flag
 BCLIF=0;      // clear bus collision flag
}

/* ****************************************************************************************/

void i2c_waitForIdle()
{
 while (( SSPCON2 & 0x1F ) | RW ){}; // wait for idle and not writing
}

/* ****************************************************************************************/

void i2c_start()
{

 i2c_waitForIdle();
 SEN=1;

}

/* ****************************************************************************************/

void i2c_repStart()
{
 i2c_waitForIdle();
 RSEN=1;
}

/* ****************************************************************************************/

void i2c_stop()
{
 i2c_waitForIdle();
 PEN=1;
}

/* ****************************************************************************************/

int i2c_read( unsigned char ack )
{
 unsigned char i2cReadData;

 i2c_waitForIdle();

 RCEN=1;

 i2c_waitForIdle();

 i2cReadData = SSPBUF;

 i2c_waitForIdle();

 if ( ack )
  {
  ACKDT=0;
  }
 else
  {
  ACKDT=1;
  }
  ACKEN=1;               // send acknowledge sequence

 return( i2cReadData );
}

/* ****************************************************************************************/

unsigned char i2c_write( unsigned char i2cWriteData )
{
 i2c_waitForIdle();
 SSPBUF = i2cWriteData;

 return ( ! ACKSTAT  ); // function returns '1' if transmission is acknowledged
}

/* ****************************************************************************************/


/* ****************************************************************************************/

void write_ext_eeprom(unsigned int address, unsigned char data)
{
   unsigned char Aux;

 /*
 i2c_start();
      Aux=0xa0 | ((address>>7) & 0x0E);
       i2c_write(Aux);
      Aux=address & 0xFF;
      i2c_write(Aux);
      i2c_write(data);
      i2c_stop();
   DelayMs(6);
 */

   i2c_start();
/*#if !defined(DIREC_DOS_BYTES)
   Aux=0xa0 | ((address>>7) & 0x0E);
   i2c_write(Aux);
#else
*/
   //Aux=0xa0; //stanard
  #if (a1==1)
    	Aux=0xa4+RTC;
  #else
   	 	Aux=0xa0+RTC;
  #endif
   i2c_write(Aux);
if (RTC==0){
   Aux=(address>>8) & 0xFF;
   i2c_write(Aux);
}
//#endif
   Aux=address & 0xFF;
   i2c_write(Aux);
   i2c_write(data);
   i2c_stop();
   if (RTC==0)  DelayMs(11); //prueba porque alenta el programa

 }

/* ****************************************************************************************/

unsigned char read_ext_eeprom(unsigned int address)
{
   unsigned char data;
   unsigned char Aux;
/*
  i2c_start();
   Aux=0xa0 | ((address>>7) & 0x0E);
   i2c_write(data);
   Aux=address & 0xFF;
   i2c_write(Aux);
   i2c_repStart();
   //i2c_write(0xa1); ERA ANTES PERO NO ANDUvO EN LAS MEMORIAS HOLTEK ht24lc04
   i2c_write(data|1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
 */

   i2c_start();
/*#if !defined(DIREC_DOS_BYTES)
   Aux=0xa0 | ((address>>7) & 0x0E);
   i2c_write(Aux);
#else
*/
   //Aux=0xa0; //stanard
  #if (a1==1)
 	 Aux=0xa4+RTC;
  #else
	 Aux=0xa0+RTC;
 #endif
   i2c_write(Aux);
  if (RTC==0){
   Aux=(address>>8) & 0xFF;
   i2c_write(Aux);
 }
//#endif
   Aux=address & 0xFF;
   i2c_write(Aux);
   i2c_repStart();
   //i2c_write(0xa1);//stanard
 	#if (a1==1)
   		Aux=0xa5+RTC;
	#else
  	 	Aux=0xa1+RTC;
  	#endif
   i2c_write(Aux);
   //i2c_write(0xa1); ERA ANTES PERO NO ANDUvO EN LAS MEMORIAS HOLTEK ht24lc04
   //i2c_write(data|1);Para holtek
   data=i2c_read(0);
   i2c_stop();
   return(data);

}

/* ****************************************************************************************/
unsigned int read_int_ext_eeprom(unsigned int addr)
{
	return ((unsigned int)(read_ext_eeprom(addr)) | ((unsigned int)((unsigned int)(read_ext_eeprom((unsigned int)(addr+1)))<<8)));
}

void write_int_ext_eeprom(unsigned int addr, unsigned int data)
{
	write_ext_eeprom(addr,(unsigned char)(data & 0xFF));
	write_ext_eeprom((unsigned int)(addr+1),(unsigned char)(data>>8));
}
unsigned long int read_long_ext_eeprom(unsigned int addr)
{
	if (RTC==44) addr=addr+8;
	return ((unsigned long int)(read_int_ext_eeprom(addr)) | ((unsigned long int)((unsigned long int)(read_int_ext_eeprom((unsigned int)(addr+2)))<<16)));
}
void write_long_ext_eeprom(unsigned int addr, unsigned long int data)
{
	if (RTC==44) addr=addr+8;
	write_int_ext_eeprom(addr,(unsigned int)(data & 0xFFFF));
	write_int_ext_eeprom((unsigned int)(addr+2),(unsigned int)(data>>16));
}

⌨️ 快捷键说明

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