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

📄 mxc62020m.c

📁 该代码是我编写的用于读去传感器MXC62020的代码.
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************************
*
*	The following variable and define will be used to communicate with MXC6202G.
*******************************************************************************/
#define	MXC62020M_MACRO_DEFINE	
//MXC62020M_MACRO_DEFINE
#ifdef MXC62020M_MACRO_DEFINE

//James Lee
#define   	SCL_LINE		8		//Use the GPIO08 as SCL_LINE for I2C
#define		SDA_LINE		9		//Use the GPIO09 as SDA_LINE for I2C
#define		High			1		//Output High
#define   	Low       			0		//Output Low
#define		WBit			0		//Write Operation for SLave Device
#define 		RBit				1		//Read Operation for Slave Device
#define		INPUT			0		//Set the GPIO as INPUT
#define		OUTPUT			1		//Set the GPIO as OUTPUT
#define		SAddress		0x20	//Should be assigned by customer

/************************************************************/
unsigned char 	Filter_Count;
unsigned short	XAxis_Data,YAxis_Data,RcvData,Temp_Data;
unsigned short	XAxis_TData,YAxis_TData;
unsigned int		Test_RcvData;
unsigned int		MXC6202G_Return_Value;
unsigned char 	ModeBit=0x0;
unsigned char		XX_Data=0x0;
kal_bool			ret_value;

typedef struct
{
   kal_uint8         	gpthandle;
   kal_uint32        	timeout;
} MXC62020M_IIC_struct;

MXC62020M_IIC_struct  IIC_OBT;

kal_bool 			MXC62020M_PwrDN(void);
kal_bool  		MXC62020M_PwrON(void);
unsigned short  	MXC62020M_ReadData(unsigned char data_index);
kal_bool			MXC62020M_ModeSetup(unsigned char Mode,unsigned char XData);
kal_bool			WRData_Set(unsigned char XXX_Data);
void 			MXC62020M_TimeOutHandler(void *parameter);

//End of the Declare of the variable for MXC6202G
#endif //MXC62020M_MACRO_DEFINE



/*******************************************************************************
*	The following code will be used to simulate communication with MXC6202G.
*
*	We use FM menu to Operate the Communication with MXC6202G.
 *******************************************************************************/
 #ifdef MXC62020M_MACRO_DEFINE
/*******************************************************************************/
//Beginning of the Code for Communication with MXC6202G
//James Lee
//This will be called to Power Down the MXC6202G.
//It will be called at mmi_fmrdo_press_step_up_button function.

kal_bool MXC62020M_PwrDN(void)
{	
	unsigned char i,BitMask,SLAddr;

	//Master Generate the Start condition
	//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();

	//The Address of the 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,SCL_LINE);	
	GPIO_WriteIO(High,SDA_LINE);	//Release the SDA 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();
		MXC6202G_Return_Value=0xFF07;
		return KAL_FALSE;

	}
	GPIO_InitIO(OUTPUT,SDA_LINE);
	GPIO_WriteIO(Low,SCL_LINE);
	Short_Nop();
	SLAddr=0x0;
	BitMask=0x80;
	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,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SDA_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();
		MXC6202G_Return_Value=0xFF08;
		return KAL_FALSE;	
	}
	GPIO_InitIO(OUTPUT,SDA_LINE);
	GPIO_WriteIO(Low,SCL_LINE);
	BitMask=0x80;
	SLAddr=0x1;	//Power Down 
	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,SCL_LINE);
	GPIO_WriteIO(High,SDA_LINE);	//Release the SDA 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);
		MXC6202G_Return_Value=0xFF08;
		return KAL_FALSE;
	}
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_InitIO(OUTPUT,SDA_LINE);

	//Generate Stop Contion
	GPIO_WriteIO(Low,SDA_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SCL_LINE);
	Short_Nop();
	GPIO_WriteIO(High,SDA_LINE);
	Short_Nop();
	
	MXC6202G_Return_Value=0xFF00;
	return KAL_TRUE;
}

/**************************************************************************
//GPIO08 as SCL_LINE
//GPIO09 as SDA_LINE
//The following function will be called by mmi_fmrdo_press_step_down_button
//	
//kal_bool	 MXC62020M_PwrON(void)
//
//MXC62020M Will be Powered On
//The sequence is the following :
//IIC_Start
//Send the 0x20 (The Address of the MXC62020M)
//Receiving the ACK Signal from the MXC62020M
//Send the 0x01 to the MXC62020M
//Receiving the ACK Signal from the MXC62020M
//IIC_Stop
//The following code is okay.
**************************************************************************/
kal_bool	 MXC62020M_PwrON(void)
{	

	unsigned char BitMask,i,SLAddr;

	//Set the GPIO08 and GPIO09 as General Purpose IO
	//GPIO08 is SCL_LINE
	//GPIO09 is SDA_LINE
	GPIO_ModeSetup(SCL_LINE,0x0);	
	GPIO_ModeSetup(SDA_LINE,0x0);	

	//Set the SDA & SCL as OUTPUT	
	GPIO_InitIO(OUTPUT,SCL_LINE);
	GPIO_InitIO(OUTPUT,SDA_LINE);

	//Make the SDA & SCL Line at free
	GPIO_WriteIO(High,SCL_LINE);	
	GPIO_WriteIO(High,SDA_LINE);	
	Short_Nop();
	Short_Nop();

	//START
	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();
	
	//Send the Address of the Slave Device.
	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;
	}

	//Receiving the ACK from MXC6202G
	GPIO_WriteIO(High,SCL_LINE);
	GPIO_WriteIO(High,SDA_LINE);
	
	GPIO_InitIO(INPUT,SDA_LINE);	
	Short_Nop();
	if(GPIO_ReadIO(SDA_LINE))
	{
		GPIO_InitIO(OUTPUT,SDA_LINE);
		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();
		MXC6202G_Return_Value=0xFF01;	//Just For Debugging,0xFF01 means the Slave Address has been written successfully
		return KAL_FALSE;
	}	
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_InitIO(OUTPUT,SDA_LINE);
	Short_Nop();
	
	//The First Internal Register.
	//SLAddr=0x0;
	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();
	}
	//Receiving the ACK from MXC6202G
	GPIO_WriteIO(High,SCL_LINE);
	GPIO_WriteIO(High,SDA_LINE);
	
	GPIO_InitIO(INPUT,SDA_LINE);
	Short_Nop();
	
	if(GPIO_ReadIO(SDA_LINE))
	{
		GPIO_InitIO(OUTPUT,SDA_LINE);
		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();
		MXC6202G_Return_Value=0xFF02;	//Just For Debugging,0xFF01 means the Slave Address has been written successfully
		return KAL_FALSE;
	}	
	GPIO_WriteIO(Low,SCL_LINE);
	GPIO_InitIO(OUTPUT,SDA_LINE);

	//The Data will be sent to the first Internal Register
	//0x00 means Power On, 0x1 means Power Down
	BitMask=0x80;
	SLAddr=0x00;	//0x00 means Power On with output are XAxis Data And YAxis Data,
				//0x08 means Power ON with Output are Temp data & YAxis Data.
	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 ACK
	GPIO_WriteIO(High,SCL_LINE);
	GPIO_WriteIO(High,SDA_LINE);
	GPIO_InitIO(INPUT,SDA_LINE);
	
	Short_Nop();
	if(GPIO_ReadIO(SDA_LINE))
	{
		GPIO_InitIO(OUTPUT,SDA_LINE);
		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();
		MXC6202G_Return_Value=0xFF03;
		return KAL_FALSE;
	}
	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=0xFFFF;

	XAxis_Data=YAxis_Data=RcvData=XAxis_TData=YAxis_TData=0x0;
	Filter_Count=0x0;
	//========================================================
	//The Following code will Set the timer for Data Reading.
	//Set the IIC_OBT and Get the GPT handler for IIC Communication
//	GPTI_GetHandle(&IIC_OBT.gpthandle);	//Get the Handle for MXC6202G from General Purpose Timer2.
//	IIC_OBT.timeout=20;	//Set the Time period is 50ms .

	//Start the GPTI handler for MXC6202M	
//	GPTI_StartItem(IIC_OBT.gpthandle,IIC_OBT.timeout,MXC62020M_TimeOutHandler,NULL);
	
	//GPTI_StopItem(KBD.gpthandle); Stop Handle for MXC6202G from the General Purpose Timer2
	//GPTI_StartItem(KBD.gpthandle,KBD.longpress_timeout,MXC62020M_TimeOutHandler, NULL);
	//GPTI_StartItem(IIC_OBT.gpthandle,IIC_OBT.long_timeout,MXC6202G_TimeOutHandler,NULL);
	//========================================================
	return KAL_TRUE;
}

/******************************************************
 *	
 *	kal_bool  MXC62020M_ReadData(char data_index)
 *
 *
 *	Description:
 *
 * The Upper Application can call the function to read the XAxis 
 * Data and YAxis Data.
 * The customer Should input 0x1 for XAxis Data,
 *  0x4 for YAXis Data
 * and the temperature should be read by inputing 0x1.
 *
 *INPUT:
 *	for Temperature:	0x01,0x02
 *	for XAxis Data:		0x01,0x02
 *  for YAXis Data:		0x03,0x4
 *  for Internal Register 0: 0x00
 ******************************************************/
unsigned short  MXC62020M_ReadData(unsigned char data_index)
{	
	unsigned char i,BitMask,SLAddr;

	//MT6219 Generated the I2C Start Condition
	
	/*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.
	BitMask=0x80;
	SLAddr=0x20;
	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))
	{

⌨️ 快捷键说明

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