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

📄 max7301driver.c

📁 Max7301 SPI IO Expander Driver
💻 C
📖 第 1 页 / 共 2 页
字号:
//Type              : User defined
//Return Type       : void
//Arguments         : unsigned char Address,unsigned char data,unsigned char IOPort
//Details           : Write Data and command to the IO Expander 
//Autor             : Nibu
//******************************************************************************


void WriteToMax7301(unsigned char Address,unsigned char data,unsigned char IOPort)
{
	unsigned int SPISendData;
	unsigned char *SPITemp;
				// compain the given two character values in to an int variable
	SPITemp=(unsigned char *)&SPISendData;
	*SPITemp=data;
	SPITemp++;
	*SPITemp=Address;
	switch (IOPort)	
	{
		case 0x01:				// if io expander 1
		IOCS1=0;				// enable IO expander 1
		WriteSPI(SPISendData);	// Write 16 bit Command+Data
		DelayCycle(1500);		// delay of 1500 cycles
		IOCS1=1;				// Disable IO Expander 1
		break;
		case 0x02:				//if IO expander 2
		IOCS2=0;				// Enable IO Expander 2
		WriteSPI(SPISendData);	// Write 16 bit Command+Data
		DelayCycle(1500);		//delay of 1500 cycles
		IOCS2=1;				//disable IO Expander 1
		break;
		case 0x03:				//if IO expander 3
		IOCS3=0;				// Enable IO Expander 3
		WriteSPI(SPISendData);	// Write 16 bit Command+Data
		DelayCycle(1500);		//delay of 1500 cycles
		IOCS3=1;				//disable IO Expaner 1
		break;
		default:
		IOCS1=1;				// disable all IO Expander
		IOCS2=1;
		IOCS3=1;
		break;
	}
}

//******************************************************************************
//Function Name     : unsigned int ReadFromMax7301(unsigned char Address,unsigned char IOPort)
//Type              : User defined
//Return Type       : unsigned int
//Arguments         : unsigned char Address,unsigned char IOPort
//Details           : Read Data From The IO Expander
//Autor             : Nibu
//******************************************************************************

unsigned int ReadFromMax7301(unsigned char Address,unsigned char IOPort)
{
	unsigned int PortValue=0;
	unsigned int SPISendData;
	unsigned char *SPITemp;
			// compain the given two character values in to an int variable
	SPITemp=(unsigned char *)&SPISendData;
	*SPITemp=0x00;
	SPITemp++;
	*SPITemp=Address;

 	switch (IOPort)	
	{
		case 0x01:			// if IO Expander 1
		IOCS1=0;			// Enable IO Expander 1
		WriteSPI(SPISendData);	// Write Read address 
		DelayCycle(1500);		//1500 Cycle delay
		IOCS1=1;				//Disable IO Expander 1
		DelayCycle(100);
		IOCS1=0;
//		PortValue = ReadSPI(PortValue);	// Read the 16 bit Port expander buffer
		ReadSPI(PortValue);
		break;
		case 0x02:				// if IO Expander 2
		IOCS2=0;				// Enable IO Expander 2
		WriteSPI(SPISendData);	// Write Read address 
		DelayCycle(1500);		//1500 Cycle delay
		IOCS2=1;				//Disable IO Expander 2
		DelayCycle(100);
		IOCS2=0;
//		PortValue = ReadSPI(PortValue);	// Read the 16 bit Port expander buffer
		ReadSPI(PortValue);
		break;
		case 0x03:				// if IO Expander 3
		IOCS3=0;				// Enable IO Expander 3
		WriteSPI(SPISendData);	// Write Read address 
		DelayCycle(1500);		//1500 Cycle delay
		IOCS3=1;				//Disable IO Expander 3
		DelayCycle(100);
		IOCS3=0;
//		PortValue = ReadSPI(PortValue);	// Read the 16 bit Port expander buffer
		ReadSPI(PortValue);
		break;
		default:
		IOCS1=1;			//Disable IO Expander
		IOCS2=1;
		IOCS3=1;
		break;

	}
return PortValue;			// Return the Port Status

}

//******************************************************************************
//Function Name     : void DelayCycle(unsigned int Cycle)
//Type              : User defined
//Return Type       : void
//Arguments         : unsigned int Cycle
//Details           : Delay in Instruction Cycles
//Autor             : Nibu
//******************************************************************************


void DelayCycle(unsigned int Cycle)
{
	unsigned int i;
	for(i=0;i<Cycle;i++)
	{
		asm("nop");
	}	
}

//******************************************************************************
//Function Name     : void WriteSPI(unsigned int SPIData)
//Type              : User defined
//Return Type       : void
//Arguments         : unsigned int SPIData
//Details           : Write data in to the SPI
//Autor             : Nibu
//******************************************************************************


void WriteSPI(unsigned int SPIData)
{
	unsigned char SPI_Count;		// SPI Bit count
	Max7301CLK   =0;				// Pull down the clock pin
    DelayCycle(100);
					// Send 16 bit Data to the SPI DO pin
 	for (SPI_Count = 0; SPI_Count < 16; SPI_Count++)                  
  	{
    	if (SPIData & 0x8000)     //                                     
      	Max7301DO = 1;                                            
    	else
      	Max7301DO = 0;
	Max7301CLK = 0; 		// SPI Clock
    DelayCycle(100);
	Max7301CLK= 1;
	DelayCycle(100);
    SPIData <<= 1;                                              
  } 
// 	Max7301CS = 1;
 //   Max7301DO = 0; 

	Max7301CLK   =0; 			// Pull Down the Clock Pin
}

//******************************************************************************
//Function Name     : unsigned int ReadSPI()
//Type              : User defined
//Return Type       : unsigned int
//Arguments         : none
//Details           : Read SPI Data
//Autor             : Nibu
//******************************************************************************


unsigned int ReadSPI(unsigned int ReadPort)
{
	unsigned char SPI_Count=0;	// Spi Count
	unsigned int spi_data=0xD800;	// Set data
	unsigned int SPIData=0;
	unsigned char ReadBit=0;
//	Max7301CS	 =1;
	Max7301CLK   =0;			// Pull down the clock pin
    DelayCycle(100);			
//	Max7301CS	 =0;
    Max7301DO = 1; 
					//Reading the 16 bit SPI Data
  	for (SPI_Count = 0; SPI_Count < 16; SPI_Count++)                  
 	{   
		SPIData <<=1; 
		if (spi_data & 0x8000)                                         
      	Max7301DO = 1;                                            
    	else
      	Max7301DO = 0;
                                             
		Max7301CLK   =1;   	//SPI Clock
    	DelayCycle(50);
 //   	SPIData += _LATF7; 	//Read The DOUT Pin
//		printf("%x", _LATF7);
		Max7301CLK   =0; 
//    	DelayCycle(10);

		ReadBit=PORTDbits.RD7;//*************************
		printf("%d",PORTDbits.RD7);
    	SPIData += ReadBit;       
//		Max7301CLK   =0; 
	   	DelayCycle(10); 
     	spi_data <<= 1;    
 // 		DelayCycle(100);                                               
//    	SPIData += _LATF7; 	//Read The DOUT Pin
//    	spi_data <<= 1; 		
  	} 
//	Max7301CS	 =0;
	Max7301CLK   =0;		//Pull down the clock
	ReadPort=SPIData;
	printf("%d",SPIData);
	return SPIData;			// Return The read Value

}

⌨️ 快捷键说明

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