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

📄 iic.txt

📁 富士通单片机的iic源码 原创! 精华
💻 TXT
字号:

/* THIS SAMPLE CODE IS PROVIDED AS IS AND IS SUBJECT TO ALTERATIONS. FUJITSU */
/* MICROELECTRONICS ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR */
/* ELIGIBILITY FOR ANY PURPOSES.                                             */
/*                 (C) Fujitsu Microelectronics Europe GmbH                  */
/*---------------------------------------------------------------------------
  MAIN.C
  - description
  - See README.TXT for project description and disclaimer.

/*---------------------------------------------------------------------------*/



#include "mb90350.h"



unsigned char EEPROM_ERROR;
unsigned char EEpromErrFlag;

#define SCL PDR4_P45
#define SDA PDR4_P44
#define SCL_DIRE DDR4_D45
#define SDA_DIRE DDR4_D44 

#define I2CBUS_TIMEOUT 3000
void UART_sendbyte (char ch);

extern struct
{	
	unsigned char Byte;
	union
	{
		unsigned char item0: 1;
		unsigned char item1: 1;
		unsigned char item2: 1;
		unsigned char item3: 1;
		unsigned char item4: 1;
		unsigned char item5: 1;
		unsigned char item6: 1;
		unsigned char item7: 1;		
	}bit;	
}HardWareFault;



/*===========================================================================*/
/*========== I2C BUS========================================================*/
/*===========================================================================*/
void I2C_Init(void)
{
  ICCR0_EN  = 0;              // stop I2C interface
  ICCR0_NSF = 1;              // Enable noise filter
  ICCR0_CS4 = 0;              // CS4..0 : set prescaler
  ICCR0_CS3 = 0;              // 16MHZ n=12.CS3=1&CS2=1//Baud Rate=100Kbits
  ICCR0_CS2 = 0;              // CS1 = 1----400Kbit
  ICCR0_CS1 = 1;
  ICCR0_CS0 = 0;
  ICCR0_EN  = 1;              // enable I2C interface
  
  IDAR0 = 0;                  // clear data register
  
  IBCR0_BER  = 0;             // clear bus error interrupt flag
  IBCR0_BEIE = 0;             // bus error interrupt disabled
  IBCR0_ACK  = 0;             // no acknowledge generated
  IBCR0_GCAA = 0;             // no call acknowledge is generated
  IBCR0_INTE = 0;             // disable interrupt
  IBCR0_INT  = 0;             // clear transfer interrupt request flag
  IBCR0_MSS = 0;              // Set Slave Mode.
}	

void I2C_Acknowlegde(void)
{	
	unsigned int i = 0;	
	while(IBSR0_LRB == 1)
	{
		i += 1;
		if(i == I2CBUS_TIMEOUT)
		{
			EEPROM_ERROR = 1;
			// Error Report!
			break;
		}	
	}                               
}	
void I2C_Start(unsigned char slave_address)
{
	unsigned int i = 0;	
  do
  {
    IBCR0_BER = 0;                // clear bus error interrupt flag
    ICCR0_EN  = 1;                // enable I2C interface
    IDAR0 = slave_address;        // slave_address is sent out with start condition
    
    IBCR0_MSS = 1;                // set master mode and set start condition
    IBCR0_INT = 0;                // clear transfer end interrupt flag    
   // UART_sendbyte (0x10);
    while(IBCR0_INT == 0);        // look if transfer is in process
	i += 1;
  	if(i == I2CBUS_TIMEOUT)
  	{
  		EEPROM_ERROR = 1;
  		// Error Report!
  		break;
  	}
  	//UART_sendbyte(0x10);
  }
  while (IBCR0_BER == 1);         // retry if Bus-Error detected ////Bus Error
  i = 0;
  while(IBSR0_LRB == 1)           // no acknowledge means device not ready/// No answering.
  {                               // maybe last write cycle not ended yet
    IBCR0_SCC = 1;                // try restart (= continue)   	
    while (IBCR0_INT == 0);       // wait that transfer is finished
    i += 1;
  	if(i == I2CBUS_TIMEOUT)
  	{
  		EEPROM_ERROR = 1;
  		// Error Report!
  		break;
  	}    
  }
}
// Using For Read Operation
void I2C_Restart(unsigned char slave_address)
{
  IDAR0 = slave_address;          // slave_address is sent out with start condition
  IBCR0_SCC = 1;                  // restart (= continue)
  while (IBCR0_INT == 0);         // wait that transfer is finished
}

void I2C_Stop(void)
{
	unsigned int i = 0;	
  while (IBCR0_INT == 0);         // wait that transfer is finished
  IBCR0_MSS = 0;                  // change to slave and release stop condition
  IBCR0_INT = 0;                  // clear transfer end interrupt flag
  while (IBSR0_BB)               // wait till bus free
  {
  	i += 1;
  	if(i == I2CBUS_TIMEOUT)
  	{
  		EEPROM_ERROR = 1;
  		// Error Report!
  		break;
  	}     	
  }
}	

void I2C_Write(unsigned char value)
{	
  IDAR0 = value;                  // load data or address in to register
  IBCR0_INT = 0;                  // clear transfer end intrerupt flag
  while (IBCR0_INT == 0);         // look if transfer is in process
  I2C_Acknowlegde();              // wait for Acknowledge
}	

unsigned char I2C_Read(void)
{
  IBCR0_ACK = 1;                  // no acknowledge has to be send
  IBCR0_INT = 0;                  // clear transfer end interrupt flag
  while (IBCR0_INT == 0);         // wait that transfer is finished
  return(IDAR0);                  // read received data out
} 

unsigned char I2C_LastRead(void)
{ 
  IBCR0_ACK = 0;                  // acknowledge has to be sent after last byte
  IBCR0_INT = 0;                  // clear transfer end interrupt flag
  while(IBCR0_INT == 0);          // wait that transfer is finished
  return(IDAR0);                  // read received data out
} 







/*===========================================================================*/
/*========== I2C BUS========================================================*/
/*===========================================================================*/
/*void I2C_Init(void)
{
  ICCR0_EN  = 0;              // stop I2C interface
 // ICCR0_NSF = 1;              // Enable noise filter
  ICCR0_CS4 = 0;              // CS4..0 : set prescaler
  ICCR0_CS3 = 0;              // 16MHZ n=12.CS3=1&CS2=1//Baud Rate=100Kbits
  ICCR0_CS2 = 1;              // CS1 = 1----400Kbit
  ICCR0_CS1 = 0;
  ICCR0_CS0 = 0;
  ICCR0_EN  = 1;              // enable I2C interface
  
  IDAR0 = 0;                  // clear data register
  
  IBCR0_BER  = 0;             // clear bus error interrupt flag
  IBCR0_BEIE = 0;             // bus error interrupt disabled
  IBCR0_ACK  = 0;             // no acknowledge generated
  IBCR0_GCAA = 0;             // no call acknowledge is generated
  IBCR0_INTE = 0;             // disable interrupt
  IBCR0_INT  = 0;             // clear transfer interrupt request flag
//  IBCR0_MSS = 0;              // Set Slave Mode.
}	

void I2C_Acknowlegde(void)
{	
	volatile unsigned int ti_TimeOut = 0;	
	while(IBSR0_LRB == 1)
	{
		ti_TimeOut = ti_TimeOut +1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}	
	}                               
}	
void I2C_Start(unsigned char slave_address)
{
	volatile unsigned int ti_TimeOut1 = 0;
	volatile unsigned int ti_TimeOut2 = 0;
	
	// do while start
  	do
  	{
    	IBCR0_BER = 0;             	   									// clear bus error interrupt flag
    	ICCR0_EN  = 1;             	  									// enable I2C interface
    	IDAR0 = slave_address;     	   									// slave_address is sent out with start condition
    
    	IBCR0_MSS = 1;             	   									// set master mode and set start condition
    	IBCR0_INT = 0;            	   									// clear transfer end interrupt flag  
    	while(IBCR0_INT == 0)	       									// look if transfer is in process
    	{
    		ti_TimeOut1 = ti_TimeOut1 +1;
			if(ti_TimeOut1 >= I2CBUS_TIMEOUT)
			{
				EEpromErrFlag = 1;
				// Error Report!
				break;
			}	
    	}
    
		ti_TimeOut2 = ti_TimeOut2 +1;
  		if(ti_TimeOut2 >= I2CBUS_TIMEOUT)
  		{
  			EEpromErrFlag = 1;
  			// Error Report!
  			break;
  		}
  	}
  	while (IBCR0_BER == 1);											// retry if Bus-Error detected ////Bus Error
  	// do while end 
  
  	ti_TimeOut2 = 0;
  	ti_TimeOut1 = 0;
  	while(IBSR0_LRB == 1)												// no acknowledge means device not ready/// No answering.
  	{              													// maybe last write cycle not ended yet
    	IBCR0_SCC = 1;													// try restart (= continue)   	
    	while (IBCR0_INT == 0)											// wait that transfer is finished
    	{
    		ti_TimeOut1 = ti_TimeOut1 +1;
			if(ti_TimeOut1 >= I2CBUS_TIMEOUT)
			{
				EEpromErrFlag = 1;
				// Error Report!
				break;
			}
    	}
    	ti_TimeOut2 = ti_TimeOut2 + 1;
  		if(ti_TimeOut2 >= I2CBUS_TIMEOUT)
  		{
  			EEpromErrFlag = 1;
  			// Error Report!
  			break;
  		}    
  	}
}
// Using For Read Operation
void I2C_Restart(unsigned char slave_address)
{
	volatile unsigned int ti_TimeOut = 0;	
	
	IDAR0 = slave_address;												// slave_address is sent out with start condition
	IBCR0_SCC = 1;														// restart (= continue)
	while (IBCR0_INT == 0)												// wait that transfer is finished
	{
		ti_TimeOut = ti_TimeOut +1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}
	}// end while
}

void I2C_Stop(void)
{
	volatile unsigned int ti_TimeOut = 0;
	while (IBCR0_INT == 0)												// wait that transfer is finished
	{
		ti_TimeOut = ti_TimeOut +1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}  
	}
	IBCR0_MSS = 0;														// change to slave and release stop condition
	IBCR0_INT = 0;	
	ti_TimeOut = 0;													// clear transfer end interrupt flag
	while (IBSR0_BB)													// wait till bus free
	{
		ti_TimeOut = ti_TimeOut +1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}     	
	}
}	

void I2C_Write(unsigned char value)
{	
	volatile unsigned int ti_TimeOut = 0;
	
	IDAR0 = value;														// load data or address in to register
	IBCR0_INT = 0;														// clear transfer end intrerupt flag
	while (IBCR0_INT == 0)												// look if transfer is in process
	{
		ti_TimeOut = ti_TimeOut +1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}   
	}
	I2C_Acknowlegde();													// wait for Acknowledge
}	

unsigned char I2C_Read(void)
{
	volatile unsigned int ti_TimeOut = 0;
	
	IBCR0_ACK = 1;														// no acknowledge has to be send
	IBCR0_INT = 0;														// clear transfer end interrupt flag
	while (IBCR0_INT == 0)												// wait that transfer is finished
	{
		ti_TimeOut = ti_TimeOut +1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}   
	}
	return(IDAR0);														// read received data out
} 

unsigned char I2C_LastRead(void)
{ 
	volatile unsigned int ti_TimeOut = 0;
	
	IBCR0_ACK = 0;														// acknowledge has to be sent after last byte
	IBCR0_INT = 0;														// clear transfer end interrupt flag
	while(IBCR0_INT == 0)												// wait that transfer is finished
	{
		ti_TimeOut = ti_TimeOut + 1;
		if(ti_TimeOut >= I2CBUS_TIMEOUT)
		{
			EEpromErrFlag = 1;
			// Error Report!
			break;
		}   
	}
	return(IDAR0);														// read received data out
} 
*/






/*****************************************************************************/
/* UART                                                                      */
/*****************************************************************************/

void InitUart3(void)
{
  // Initialize UART3 asynchronous mode
  BGR3 = 1666; 										// 9600 Baud @ 16MHz
  SCR3 = 0x17; 										// 8N1
  SMR3 = 0x0d; 										// enable SOT3, Reset, normal mode
  SSR3 = 0x02; 										// LSB first		Enables Reception Interrupt
  DDR1_D12 = 0;										// enable  SIN3  SET  MOD-> IN
}//end InitUart3



void UART_sendbyte (char ch)
{
  while (!SSR3_TDRE); 
  TDR3 = ch;		      
}

char UART_readbyte_wait (void)
{
  while(!SSR3_RDRF);  								// wait, until byte is received
  return (RDR3);      								// return received byte
}

void UART_sendstring (const char *string)
{
  unsigned int i;
	
  for (i=0; i<strlen(string); i++) 
  {
    if (string[i] == 10)
      UART_sendbyte(13);
    UART_sendbyte(string[i]);
  }
}

void delay(unsigned int i)
{
	volatile unsigned int j;
	i = i * 120;
	for (j=0; j<i; j++);
}






void main(void)
{
 
 	unsigned char ch1;
 	unsigned char ch2;
 	unsigned char ch3;
 	unsigned char ch4;
 
  	InitIrqLevels();
  	__set_il(7);      // allow all IQR levels
  	__EI();           // globaly enable interrupts
  
  	

	InitUart3();
	I2C_Init();
	
	
	DDR1_D10 = 1;
	
	//SCL_DIRE = 1;
	//SDA_DIRE = 1;
	//SCL = 0;
	//SDA = 0;
	
	
	PDR1_P10 = 1;
	delay(1000);
	PDR1_P10 = 0;
	
	//UART_sendbyte(0x10);
	
	/*I2C_Start(0xA0);
	//UART_sendbyte(0x10);
	I2C_Write(0x10); 
	I2C_Write(0x11);
	I2C_Write(0xaa);
	I2C_Write(0x55);
	I2C_Stop();*/
	
	
	I2C_Start(0xA0);
	I2C_Write(0x10);
	I2C_Write(0x11);
	I2C_Restart(0xA1);  
	ch1 = I2C_Read();	 	
	//ch2 = I2C_Read();
	ch3 = I2C_LastRead();	
	I2C_Stop();
	

	UART_sendbyte(ch1);
	//UART_sendbyte(ch2); /* 将读出的数值发送出去 */
	UART_sendbyte(ch3);
	UART_sendbyte(0x10);
	
	
	/*I2C_Start(0xA0);
	I2C_Write(0x20); 
	I2C_Write(0x21);
	I2C_Write(0x22);
	I2C_Write(0x05);
	I2C_Write(0x03);
	I2C_Write(0x01);
	I2C_Stop();
	
	
	
	I2C_Start(0xA0);
	I2C_Write(0x30); 
	I2C_Write(0x31);
	I2C_Write(0x32);
	I2C_Write(0x05);
	I2C_Write(0x03);
	I2C_Write(0x01);
	I2C_Stop();*/
	
	/*I2C_Start(0xA0);
	I2C_Write(0x20); 
	I2C_Write(0x21);
	I2C_Write(0x22);
	I2C_Write(0x05);
	I2C_Write(0x03);
	I2C_Write(0x01);
	I2C_Stop();*/
	
	
					
    I2C_Start(0xA0);
	I2C_Write(0x20);
	I2C_Write(0x21);
	I2C_Write(0x22);
	//I2C_Write(0x23);
	I2C_Restart(0xA1);  
	ch1 = I2C_Read();	 	
	ch2 = I2C_Read();
	//ch3 = I2C_Read();
	ch4 = I2C_LastRead();	
	I2C_Stop();
	

	UART_sendbyte(ch1);
	UART_sendbyte(ch2); /* 将读出的数值发送出去 */
	//UART_sendbyte(ch3);
	UART_sendbyte(ch4);
	UART_sendbyte(0x10);
	
					
    I2C_Start(0xA0);
	I2C_Write(0x30);
	//I2C_Write(0x31);
	//I2C_Write(0x32);
	I2C_Restart(0xA1);  
	//ch1 = I2C_Read();	 	
	//ch2 = I2C_Read();
	ch1  = I2C_LastRead();	
	I2C_Stop();
	
	I2C_Start(0xA0);
	//I2C_Write(0x30);
	I2C_Write(0x31);
	//I2C_Write(0x32);
	I2C_Restart(0xA1);  
	//ch1 = I2C_Read();	 	
	//ch2 = I2C_Read();
	ch3  = I2C_LastRead();	
	I2C_Stop();
	
	

	UART_sendbyte(ch1);
	//UART_sendbyte(ch2); /* 将读出的数值发送出去 */
	UART_sendbyte(ch3);
	UART_sendbyte(0x10);
	UART_sendbyte(EEpromErrFlag);
	while(1);

}

⌨️ 快捷键说明

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