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

📄 eeprom.c

📁 硬件I2C通信程序。。。 大家参考一下
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************
                      EEPROM.C
                 Read and write the 24c02 EEPROM
********************************************************************/

#include "EEPROM.h"
#include "Moto.h"

/*============================================================================
** PROJECT: = -
** MODULE = EEPROM.c
** SHORT DESC. = EEPROM read and write
** DEVICE = uPD78F0822B
** VERSION = SW-Dragon-T1.0
** DATE = 08/31/06
** LAST CHANGE = -
** ===========================================================================
** Description: -This program includes functions that read and write eeprom.
**		
** ===========================================================================
** Environment: Device: uPD78F0822B
** Assembler: RA78k0 Version 3.8
** C-Compiler: CC78K0 Version 3.7
** IDE: PM plus
**
** ===========================================================================
** By: tian hua ,Novalux Shanghai
**
** ===========================================================================
** Changes: - 
**============================================================================
*/

#define	RUNFLAG	0x55		// indictor if the system is running for the first time.

                                    
#define SDA             P11.1       
#define SCL             P11.2
#define SDADir          PM11.1
#define SCLDir          PM11.2
#define SDAFuction      PF11.1
#define SCLFuction      PF11.2

#define Write24c02      0xA0
#define Read24c02       0xA1

                              
bit bACK;

UINT uiPulsePerMile=4840;///5850;

UCHAR ucPulsePerRotation=20;

UINT	 uiFuelWarnHigh=105;
UINT 	 uiFuelWarnLow=102;
UINT 	uiWaterWarnHigh=37;
UINT	 uiWaterWarnLow=34;

UCHAR 	ucversion;

UCHAR ucMeterName[5];


UCHAR ucSParas[SPARANUM];
UCHAR ucRParas[RPARANUM];
UCHAR ucWParas[WPARANUM];
UCHAR ucFParas[FPARANUM];

void delayI(UINT i)
 {
	while(--i);
 }
 

	      
/************************************************************************************************************************
## function name: I2C_Delay()	
## parameter : NULL
## return  : NULL
## description:  Delay I2C bus clock ,so the data on the bus can be stable
*************************************************************************************************************************/
 void I2C_Delay()
 {
 #ifndef I2C_FAST
 	NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
 #endif
 	NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
 //	NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
 }  

/************************************************************************************************************************
## function name: I2C_Init()	
## parameter : NULL
## return  : NULL
## description: Initialize the I2C bus state
*************************************************************************************************************************/
void I2C_Init()
{
	SDAFuction = 0; 		//set SDA to port mode
	SCLFuction = 0; 		//set SCL to port mode
	SCLDir = OUTPUT; 			//set SCL to output mode
	SCL    = 0; 			//set SCL to low level
	SDADir = OUTPUT;     		//set SDA to output mode
	SDA    = 0; 			//set SDA to low level
	bACK = 0;
	PM3.0=1;
}
/************************************************************************************************************************
## function name: I2C_Start()	
## parameter : NULL
## return  : NULL
## description: generate the bus start signal
*************************************************************************************************************************/
void I2C_Start()
{
	SDA = 1;   
	I2C_Delay();
	SCL = 1;
	I2C_Delay();
	SDA = 0;  
	I2C_Delay();
	SCL  =0;  
	I2C_Delay();
}
/************************************************************************************************************************
## function name: I2C_Stop()	
## parameter : NULL
## return  : NULL
## description: generate the bus stop signal
*************************************************************************************************************************/
void I2C_Stop()
{
	SDA = 0; 
	I2C_Delay();  
	SCL = 1; 
	I2C_Delay();
	SDA = 1; 
	I2C_Delay();
	SCL = 0;
}
/************************************************************************************************************************
## function name: I2C_SendByte()	
## parameter : c: the data to send
## return  : NULL
## description: send  8 bits  to bus
*************************************************************************************************************************/
void  I2C_SendByte(UCHAR c)
{
	UCHAR _ucBitCount;
 
 	for(_ucBitCount=0;_ucBitCount<8;_ucBitCount++) 
    {
	     if((c<<_ucBitCount)&0x80)
	     	SDA=1;  
	     else 
	     	SDA=0;                
	     I2C_Delay();
	     SCL=1;               
		 I2C_Delay();
	     SCL=0; 
    }
    
	I2C_Delay();
    SDADir = INPUT;                         //change the SDA to input mode              
	I2C_Delay();
    SCL = 1;
	I2C_Delay();
    if(SDA==1)
    	bACK = 0;     
    else
    	bACK = 1;       
    SCL = 0;
	I2C_Delay();
	SDADir = OUTPUT;                        //change the SDA to output mode
}

/************************************************************************************************************************
## function name: I2C_ReadByte()	
## parameter : NULL
## return  : the received 8 bits data
## description: read 8 bits from the bus
*************************************************************************************************************************/
UCHAR  I2C_ReadByte()
{
	UCHAR _ucRetc;
    UCHAR _ucBitCount;
  
  	_ucRetc = 0;
  	SDADir = INPUT;                       // set SDA to input mode 
 	 NOP();             
  	for(_ucBitCount=0;_ucBitCount<8;_ucBitCount++)
    {
        I2C_Delay();           
        SCL = 0;      
		I2C_Delay();  
        SCL=1;      
		I2C_Delay();  
        _ucRetc = _ucRetc<<1;
        if(SDA==1)
        	_ucRetc |= 0x01;
		NOP();  
    }
	I2C_Delay();      
  	SCL = 0;
  	NOP();
  	SDADir = OUTPUT;                       //set SDA to output mode  
	I2C_Delay();      
  	   
    return(_ucRetc);
}
/************************************************************************************************************************
## function name: bACK_To_I2c()	
## parameter : the bACK signal to send
		   bACK=1: send NO_bACK signal to bus
		   bACK=0: send bACK signal to bus 
## return  : NULL
## description: send bACK to bus
*************************************************************************************************************************/
void ACK_To_I2c(UCHAR bACK)
{
  
	if(bACK == 0)
  		SDA = 0;     
    else 
    	SDA = 1;
	I2C_Delay();      
  	SCL = 1;
	I2C_Delay();      
	SCL =0;               
	I2C_Delay();      
}
/************************************************************************************************************************
## function name: WriteByteTo24c02()	
## parameter : 
		   Address: the address to write ;
		   Data: the data to write;
## return  : state
            1 : success ;
            0 : fail
## description: write a byte data to 24c02
*************************************************************************************************************************/
UCHAR WriteByteTo24c02(UCHAR Address,UCHAR Data)
{
	UCHAR _i=0;
	I2C_Start();
	do
	{  
		_i++;
		if(_i>10)
			return (FAIL);
		I2C_SendByte(Write24c02); 	
	}while(bACK==0);            
    I2C_SendByte(Address);              
   	if(bACK==0)
    	return(FAIL);
    I2C_SendByte(Data);
   	if(bACK==0)
    	return(FAIL);
  	I2C_Stop();                
  	return(SUCCESS);
}
/************************************************************************************************************************
## function name: ReadByteFrom24c02()	
## parameter : 
		   Address: the address to read ;		 
## return  : the state  1 : success
					   0 : fail 
## description: read a byte data form 2402
*************************************************************************************************************************/
UCHAR ReadByteFrom24c02(UCHAR Address,UCHAR *data)
{
	UCHAR _i=0;
	UCHAR	result;

	I2C_Start();
	do
	{   
		_i++;
		if(_i>10)
			return (FAIL);
		I2C_SendByte(Write24c02); 	
	}while(bACK==0); 
	           
	I2C_SendByte(Address);
   	if(bACK==0)
    	return(FAIL);

	I2C_Start();
	I2C_SendByte(Read24c02);
   	if(bACK==0)
    	return(FAIL);
	result = I2C_ReadByte();
	ACK_To_I2c(1);
 	I2C_Stop(); 
 	*data = result;                  
 	return(SUCCESS);
}
/************************************************************************************************************************
## function name: WriteNByteTo24c02()	
## parameter :
			Address: the address to write ; 
			Data: the pointer to the data to be wroten ; 
			Num: the number of data to write 
## return  : the state  1 : success
					   0 : fail

⌨️ 快捷键说明

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