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

📄 i2c.c

📁 renasas m16c上实现iic通信的源代码.
💻 C
字号:

//
/*----------------------------------------------------------------------------------**
** 	Driver:			I2C Bus Device Driver Package for M16C/62 serires MCUs.         **
** 	(c) 1999 		Mitsubishi Electronics America, Inc.                            **
** 					Three Diamond Lane                                              **
** 					Durham, North Carolina 27704  USA                               **
** 					Ph 919-479-3333                                                 **
** 	File:			i2c.c                                                           **
** 	Discription:	This multi-master driver provides the software interface to the **
** 					I2C Bus hardware of the M3062x series of Mitsubishi's MCU.      **
**----------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------------------**
** Mitsubishi Electronics America, INC.                                                      **
** Design Engineering Center-East                                                            **
**                                                                                           **
** (c) 1999 Mitsubishi Electronics America, Inc                                              **
** By: Bruce Embry                                                                           **
** July, 1999                                                                                **
** Version 1.00                                                                              **
**                                                                                           **
**                                                                                           **
**-------------------------------------------------------------------------------------------*/

#define I2C_C
#include "i2c.h"
unsigned char lng_of_data_stream; // number of data bytes transfered by device driver. 
unsigned char I2C_bus_busy;		// Bus Busy Flag.  
unsigned char status;			// Status indormation return by device driver. 

void iic_mw_end(unsigned char MW_STATUS, unsigned char MW_LNG);
void iic_mr_end(unsigned char MR_STATUS,unsigned char MR_LNG);
void init_i2c_mas_func(void);
unsigned char check_i2c_bus_status(unsigned char i);
char far* Get_error_mass(void);
#if GET_ERROR_MES_RET == 0
const char STAT_MESSAGE[6][70]={
{"\nMaster transmission completed normally.\n"},
{"\nSlave returned NACK in the first byte.\n"},
{"\nSlave_returned_NACK_in_data_region.\n"},
{"\nBUS competition detected and master operation is not avalible.\n"},
{"\nImproper_start_condition_detected.\n"},
{"\nImproper stop condition_detected.\n"}};
#endif
// Static values used by assembly language.  13 bytes ram. 
unsigned int del_2usec;
unsigned int del_4usec;
unsigned int del_5usec;
unsigned int del_15usec;
unsigned int del_20usec;
unsigned char i2c_baudrate;


/***************************************************************************/
// Function called by assembly language, do not call from applicaiton. 
void iic_mw_end(unsigned char MW_STATUS, unsigned char MW_LNG)
{
	status = MW_STATUS;
	lng_of_data_stream = MW_LNG;	// Only good for 7bit addresses.
	// If using RTOS, add code to signal semaphore. 
}
// Function called by assembly language, do not call from application. 
void iic_mr_end(unsigned char MR_STATUS,unsigned char MR_LNG)
{
	
	status = MR_STATUS;
	lng_of_data_stream = MR_LNG;	// Only good for 7 bit addresses.
	// If using RTOS, add code to signal semaphore. 
} 
void init_i2c_mas_func(void)
{
	del_2usec = DELAY_2USEC;
	del_4usec = DELAY_4USEC;
	del_5usec = DELAY_5USEC;
	del_15usec = DELAY_15USEC;
	del_20usec = DELAY_20USEC;
	i2c_baudrate = I2C_BUS_BAUDRATE;
	I2C_bus_busy = Not_busy;
	lng_of_data_stream = 0;
	status = 0;
}	
unsigned char check_i2c_bus_status(unsigned char i)
{
 // checks status of I2C bus.
 // If using RTOS change code to protect resource.  When using RTOS, call this function after 
 // calling iic_mr_start or iic_mw_start and add code to wait for semaphore to be signaled when
 // assembly language routines calls either iic_mr_end or iic_mw_end. 

	if((i == Set_flag) && (I2C_bus_busy == Not_busy)){
		// if bus is not busy, set it busy. 
		lng_of_data_stream = 0;	// Set number of bytes received or transmitted to zero.
		status = 0; // Initialize status to "No Error."
		I2C_bus_busy = Busy; // make bus busy, causes other tasks to wait for bus 
		}
	else { 
		// We will have three other conditions.  Condition 1 Bus is not busy and
		// Set_flag == 0, we return not busy and do nothing else.  
		// Condition 2.  Bus is busy and number of bytes != lng, bus remains busy,
		// return busy.  
		// Condition 3. lng >= Num_of_bytes,  set flag to not busy but return busy.
		if(I2C_bus_busy == Not_busy)
			return(Not_busy);  // bus not busy.  	
		if((lng_of_data_stream > 0) || (status != 0)){
			I2C_bus_busy = Not_busy;
			lng_of_data_stream = 0;
			return(Busy);// Transmission complete continue, but force another call to set Set_flag.
			} 
		if(I2C_bus_busy == Busy){  
			return(Busy);
			}
		}
	return(Not_busy);
}
char far* Get_error_mass(void)
{
	// This function is used to return the address of the current error.  Use this function
	// in combination with device.c to debug and test the I2C bus device driver.
	// 
if( status == 0 )
	return(0);
else
#if GET_ERROR_MES_RET == 0
	return( &STAT_MESSAGE[status][0] );
#else
	return( &status );
#endif
}




⌨️ 快捷键说明

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