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

📄 mxc62020m.c

📁 该代码是我编写的用于读去传感器MXC62020的代码.
💻 C
📖 第 1 页 / 共 2 页
字号:
		GPIO_InitIO(OUTPUT,SDA_LINE);
		GPIO_WriteIO(Low,SCL_LINE);
		GPIO_WriteIO(Low,SDA_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SDA_LINE);
		Short_Nop();
		MXC6202G_Return_Value=0xFF04;	//Just For Debugging,0xFF01 means the Slave Address has been written successfully
		return  MXC6202G_Return_Value ;
	}	
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_InitIO(OUTPUT,SDA_LINE);
	Short_Nop();
	
	//From the data_index register to read.
	//
	BitMask=0x80;
	SLAddr=data_index;	//
	for(i=0;i<8;i++)
	{
		if(SLAddr&BitMask)
		{
			GPIO_WriteIO(High,SDA_LINE);
		}else
		{
			GPIO_WriteIO(Low,SDA_LINE);
		}
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(Low,SCL_LINE);
		Short_Nop();
		BitMask>>=1;
	}
	
	//Receiving the ACK from MXC6202G
	GPIO_WriteIO(High,SDA_LINE);	//Release the SDA Line
	GPIO_WriteIO(High,SCL_LINE);
	GPIO_InitIO(INPUT,SDA_LINE);	
	Short_Nop();
	if(GPIO_ReadIO(SDA_LINE))
	{
		GPIO_InitIO(OUTPUT,SDA_LINE);
		GPIO_WriteIO(Low,SCL_LINE);
		GPIO_WriteIO(Low,SDA_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SDA_LINE);
		Short_Nop();
		MXC6202G_Return_Value=0xFF05;	//Just For Debugging,0xFF01 means the Slave Address has been written successfully
		return MXC6202G_Return_Value;
	}	
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_InitIO(OUTPUT,SDA_LINE);
	
	
	//Stop
	GPIO_WriteIO(Low,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SDA_LINE);
	Short_Nop();
	
	//There is some different from the datasheet.
	//Start Condition
	GPIO_WriteIO(High,SDA_LINE);
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SCL_LINE);
	Short_Nop();
	
	//Read Operation.
	BitMask=0x80;
	SLAddr=0x21;
	for(i=0;i<8;i++)
	{
		if(SLAddr&BitMask)
		{
			GPIO_WriteIO(High,SDA_LINE);
		}else
		{
			GPIO_WriteIO(Low,SDA_LINE);
		}
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(Low,SCL_LINE);
		Short_Nop();
		BitMask>>=1;
	}

	//Receiving the ACK from MXC6202G
	GPIO_WriteIO(High,SDA_LINE);	//Release the SDA Line
	GPIO_WriteIO(High,SCL_LINE);
	GPIO_InitIO(INPUT,SDA_LINE);	
	Short_Nop();
	if(GPIO_ReadIO(SDA_LINE))
	{
		GPIO_InitIO(OUTPUT,SDA_LINE);
		GPIO_WriteIO(Low,SCL_LINE);
		GPIO_WriteIO(Low,SDA_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SDA_LINE);
		Short_Nop();
		MXC6202G_Return_Value=0xFF06;	//Just For Debugging,0xFF01 means the Slave Address has been written successfully
		return MXC6202G_Return_Value;
	}	
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_InitIO(INPUT,SDA_LINE);
	GPIO_WriteIO(High,SDA_LINE);	//Release the SDA LINE
	Short_Nop();
		
	//Receiving the two bytes data
	RcvData=0x0;
	BitMask=0x1;
	SLAddr=0x0;
	for(i=0;i<8;i++)
	{
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		if(GPIO_ReadIO(SDA_LINE))
			SLAddr|=BitMask;
		else
			SLAddr|=0x0;
		if(i<7)
			SLAddr<<=1;
		
		GPIO_WriteIO(Low,SCL_LINE);
		Short_Nop();
			
	}
	
	//Send the ACK
	GPIO_InitIO(OUTPUT,SDA_LINE);
	GPIO_WriteIO(Low,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SCL_LINE);
	Short_Nop();
	GPIO_InitIO(INPUT,SDA_LINE);
	GPIO_WriteIO(High,SDA_LINE);	//Release the SDA LINE
	RcvData=SLAddr;
	RcvData<<=8;

	
	//The Second Bytes 
	BitMask=0x1;
	SLAddr=0x0;
	for(i=0;i<8;i++)
	{
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		if(GPIO_ReadIO(SDA_LINE))
			SLAddr|=BitMask;
		else
			SLAddr|=0x0;

		if(i<7)
			SLAddr<<=1;
		GPIO_WriteIO(Low,SCL_LINE);
		Short_Nop();
			
	}
	//Send NACK
	GPIO_InitIO(OUTPUT,SDA_LINE);
	GPIO_WriteIO(High,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SCL_LINE);
	Short_Nop();
		
	//Stop
	Short_Nop();
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_WriteIO(Low,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SDA_LINE);
	Short_Nop();
	RcvData|=SLAddr;

	return RcvData;
}

/*******************************************************************************
*
*	kal_bool	MXC62020M_ModeSetup(unsigned char Mode,unsigned char XData)
*
* Description:
*
* This Function will be used to Setup the Operation of the MXC62020M's Mode
*
*INPUT:
*	Mode---0 means MXC62020M	will output XAxis Data and YAxis Data
*	Mode---1 means MXC62020M	will output  Temperature and YAxis Data.
*	XData--0 means the upper application will get the XAxis Data
* XData--1 means the Upper applicaiton will get the YAxis Data
*******************************************************************************/
/*******************************************************************************
*
*	kal_bool	MXC62020M_ModeSetup(unsigned char *Mode)
*
* Description:
*
* This Function will be used to Setup the Operation of the MXC62020M's Mode
*INPUT:
*	0 means MXC62020M	will output XAxis Data and YAxis Data
*	1 means MXC62020M	will output  Temperature and YAxis Data.
*******************************************************************************/
kal_bool	MXC62020M_ModeSetup(unsigned char Mode,unsigned char XData)
{
	unsigned char BitMask,SLAddr,i;
	
	
	if(Mode==0x0)
	{
				ModeBit=0x0;	//means the XAxis & YAxis  Data will be valided.
	}else if(Mode==0x1)
	{
				ModeBit=0x1;	//means the Temperature & YAxis data will be valided.
	}else 	
	{			
				ModeBit=0x0;
				return KAL_FALSE;
	}
	//Start Condition
	GPIO_WriteIO(High,SDA_LINE);
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(Low,SCL_LINE);
	Short_Nop();
	
	//Slave Address
	SLAddr=0x20;
	BitMask=0x80;
	for(i=0;i<8;i++)
	{
		if(SLAddr&BitMask)
			GPIO_WriteIO(High,SDA_LINE);
		else
			GPIO_WriteIO(Low,SDA_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(Low,SCL_LINE);
		Short_Nop();
		BitMask>>=1;	
	}
		GPIO_WriteIO(High,SDA_LINE);//Release the SDA LINE
		GPIO_InitIO(INPUT,SDA_LINE);
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		if(GPIO_ReadIO(SDA_LINE))
		{
			
			GPIO_InitIO(OUTPUT,SDA_LINE);
			GPIO_WriteIO(Low,SDA_LINE);
			GPIO_WriteIO(Low,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SDA_LINE);
			Short_Nop();
			return KAL_FALSE;
		}	
		GPIO_WriteIO(Low,SCL_LINE);
		GPIO_InitIO(OUTPUT,SDA_LINE);
		
		//The First Internal Register
		//SLAddr=0x00
		Short_Nop();	
		for(i=0;i<8;i++)
		{
			GPIO_WriteIO(Low,SDA_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(Low,SCL_LINE);
			Short_Nop();
		}
		
		GPIO_WriteIO(High,SDA_LINE);	
		GPIO_WriteIO(High,SCL_LINE);
		GPIO_InitIO(INPUT,SDA_LINE);
		Short_Nop();
		if(GPIO_ReadIO(SDA_LINE))
		{
			GPIO_InitIO(OUTPUT,SDA_LINE);
			GPIO_WriteIO(Low,SDA_LINE);
			GPIO_WriteIO(Low,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SDA_LINE);
			Short_Nop();
			return KAL_FALSE;
		}
		GPIO_InitIO(OUTPUT,SDA_LINE);
		GPIO_WriteIO(Low,SCL_LINE);
		
		//The Mode data 
		Short_Nop();
		SLAddr=Mode;
		BitMask=0x80;		
		for(i=0;i<8;i++)
		{
			if(SLAddr&BitMask)
				GPIO_WriteIO(High,SDA_LINE);
			else
				GPIO_WriteIO(Low,SDA_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SCL_LINE);
			Short_Nop();			
			GPIO_WriteIO(Low,SCL_LINE);
			Short_Nop();
			BitMask>>=1;
		}
		GPIO_WriteIO(High,SDA_LINE);//Release the SDA LINE
		GPIO_WriteIO(High,SCL_LINE);
		GPIO_InitIO(INPUT,SDA_LINE);
		Short_Nop();
		if(GPIO_ReadIO(SDA_LINE))
		{
			GPIO_InitIO(OUTPUT,SDA_LINE);
			GPIO_WriteIO(Low,SDA_LINE);
			GPIO_WriteIO(Low,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SCL_LINE);
			Short_Nop();
			GPIO_WriteIO(High,SDA_LINE);
			Short_Nop();
			return KAL_FALSE;
		}
		GPIO_InitIO(OUTPUT,SDA_LINE);
		GPIO_WriteIO(Low,SCL_LINE);
		Short_Nop();
		//Stop Condition
		GPIO_WriteIO(Low,SDA_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SCL_LINE);
		Short_Nop();
		GPIO_WriteIO(High,SDA_LINE);
		Short_Nop();
			
		
		return KAL_TRUE;
}
/********************************************************************
*	kal_bool	WRData_Set(unsigned char XXX_Data)
*
*Description:
*
* This functill be used by upper application to set which data should be 
* read and sent to the upper applicaiton
*
*	XXX_Data=0x0 means the message will send the XAxis data to the upper app.
* XXX_Data=0x1 means the Message will send the YAxis Data to the upper app.
*	
 ********************************************************************/
kal_bool	WRData_Set(unsigned char XXX_Data)
{
	
	if(XXX_Data==0x0)
		XX_Data=0x0;	//means the message will send the XAxis data to the upper app.
	else if (XXX_Data==0x1)
		XX_Data=0x1;	//means the message will send the YAxis data to the upper app.
	else
	{
			XX_Data=0x0;
			return KAL_FALSE;
	}		
	return KAL_TRUE;
}
/********************************************************************
*
*		void MXC62020M_TimeOutHandler(void *parameter)
*	
*Description:
*	This function will be executed when the MXC62020M timer out.
*
*	The ModeBit will be selected by the customer.
*
*
*	ModeBit=0x0---The XAxis Data will be read and sent to the Upper APPlication
*	ModeBit=0x1---The YAxis Data will be read and sent to the upper applicaitoin
* ModeBit=0x2---The Temperature will be read and sent to the upper application.
* ModeBit=0x3---
********************************************************************/
void MXC62020M_TimeOutHandler(void *parameter)
{

   	if(ModeBit==0)	//setting the XAxis data and YAxis data 
   	{
   		if(XX_Data)
   			Temp_Data=MXC62020M_ReadData(0x1);	//Reading the XAxis Data
   		else
   			Temp_Data=MXC62020M_ReadData(0x3);//Reading the YAxis Data		
   	}
   
   	if(ModeBit==0x1)
   	{	
			if(XX_Data)
				Temp_Data=MXC62020M_ReadData(0x1);	//Reading the Temperature Data
			else   		
				Temp_Data=MXC62020M_ReadData(0x3);	//Reading the YAxis Data
  	}

	 Filter_Count++;
	 
	 if(Filter_Count<5)
	 {
		XAxis_Data+=Temp_Data;
	 }else
	 {		
	 		Filter_Count=0x0;
			XAxis_Data=(XAxis_Data/5);
			//mmi_fmrdo_set_frequency(XAxis_Data);

	 }	
	
   //Restart the GPTI Handler for MXC62020M
   GPTI_StartItem(IIC_OBT.gpthandle,IIC_OBT.timeout,MXC62020M_TimeOutHandler,NULL);
}

/*******************************************************************************/
//End of the Code for Communication with MXC6202G
#endif  //MXC62020M_MACRO_DEFINE

⌨️ 快捷键说明

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