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

📄 max7301driver.c

📁 Max7301 SPI IO Expander Driver
💻 C
📖 第 1 页 / 共 2 页
字号:
#include<p24hj256GP210.h>
#include <delay.h>

//***********************************************************************************

//							   		MACRO

//***********************************************************************************

#define ENABLE_PULLUP

#define IOCS1	PORTFbits.RF0			// Port expaneder 1 Chip Select pin
#define IOCS2	PORTFbits.RF1			// Port expaneder 2 Chip Select pin
#define IOCS3	PORTGbits.RG1			// Port expaneder 3 Chip Select pin

#define IOEXP1	0x01			// IO expander 1	
#define IOEXP2	0x02			// IO expander 1	
#define IOEXP3	0x03			// IO expander 1	

#define STATUSCONFIGREG		0x04	// 
#define INPUTMASKREG		0x06

//Bit wise command address of the IO Expander

#define STATUSBIT1	0x2C
#define STATUSBIT2	0x2D
#define STATUSBIT3	0x2E
#define STATUSBIT4	0x2F
#define STATUSBIT5	0x30
#define STATUSBIT6	0x31
#define STATUSBIT7	0x32
#define STATUSBIT8	0x33
#define STATUSBIT9	0x34
#define STATUSBIT10	0x35
#define STATUSBIT11	0x36
#define STATUSBIT12	0x37
#define STATUSBIT13	0x38
#define STATUSBIT14	0x39
#define STATUSBIT15	0x3A
#define STATUSBIT16	0x3B
#define STATUSBIT17	0x3C
#define STATUSBIT18	0x3D
#define STATUSBIT19	0x3E
#define STATUSBIT20	0x3F

// Byte wise command address of the IO Expander

#define STATUSBYTE12_19	0x4C			//first eight bit
#define STATUSBYTE13_20	0x4D
#define STATUSBYTE14_21	0x4E
#define STATUSBYTE15_22	0x4F
#define STATUSBYTE16_23	0x50
#define STATUSBYTE17_24	0x51
#define STATUSBYTE18_25	0x52
#define STATUSBYTE19_26	0x53			// second eight bit
#define STATUSBYTE20_27	0x54
#define STATUSBYTE21_28	0x55
#define STATUSBYTE22_29	0x56
#define STATUSBYTE23_30	0x57
#define STATUSBYTE24_31	0x58
#define STATUSBYTE27_31	0x5B			//final 4 bytes
// Mode Definitions

#define Mode8bit 0x00
#define Mode16bit 0x01

// Pin Definitions of the SPI PORT

#define Max7301CLK    PORTFbits.RF6
#define Max7301DI     PORTFbits.RF7
#define Max7301DO     PORTFbits.RF8

//***********************************************************************************

//							    FUNCTION DECLARATION

//***********************************************************************************

void InitSPI();				// Initialize SPI Function
void ConfigureMax7301();	// Configure theIO Expander
unsigned int ReadSPI(unsigned int ReadPort);		// Read SPI 
void WriteSPI(unsigned int SPIData);	//Write SPI
void WriteToMax7301(unsigned char Address,unsigned char data,unsigned char IOPort); //Write Address and Data to the IO expander
unsigned int ReadFromMax7301(unsigned char Address,unsigned char IOPort);		//	Read the POrt Value from the IO expander
void DelayCycle(unsigned int Cycle);
 

//******************************************************************************
//Function Name     : InitSPI()
//Type              : User defined
//Return Type       : void
//Arguments         : none
//Details           : Initialize SPI and all CS pin
//Autor             : Nibu
//******************************************************************************

void InitSPI()
{
	SPI2STATbits.SPIEN = 0;
  	TRISFbits.TRISF8 = 0;	//Set DO As Output
    TRISFbits.TRISF7 = 1;	// Set SI as input
    TRISFbits.TRISF6 = 0;	// Set CLK As Output
					// Set all chip select pins are Output
    TRISFbits.TRISF0 = 0; 
	TRISFbits.TRISF1 = 0;
    TRISGbits.TRISG1 = 0;
  	TRISFbits.TRISF13 =0;
  	TRISFbits.TRISF12= 0;
  	TRISGbits.TRISG6 = 0;
  	TRISGbits.TRISG0 = 0;
  	TRISAbits.TRISA9 = 0;

//****************
	TRISDbits.TRISD8=1;
	TRISDbits.TRISD7=1;

//******************

	Max7301CLK  =0;			// pull down the clock
//  Max7301DO = 0; 
	IOCS1=1;			// disable IO Expander 1
	IOCS2=1;			// disable IO Expander 2
	IOCS3=1;			// disable IO Expander 3

	PORTFbits.RF13=0;	//Disable SPI RTC
	PORTFbits.RF12=1;	//Disable Data Flash 
	PORTGbits.RG6=1;	// Disable Buffer 1
	PORTGbits.RG0=1;	// Disable Buffer 2
	PORTAbits.RA9=1;	// Disable Buffer 3
}

//******************************************************************************
//Function Name     : ConfigureMax7301()
//Type              : User defined
//Return Type       : void
//Arguments         : none
//Details           : Configure IO Expander
//Autor             : Nibu
//******************************************************************************

void ConfigureMax7301()
{
//*******************************************************************
#if defined ENABLE_PULLUP
//********************************************************************
	WriteToMax7301(0x04,0x01,IOEXP1);	//Normal Operation,No Transition Detection
	WriteToMax7301(0x06,0x00,IOEXP1);	//No Input Mask
	WriteToMax7301(0x09,0xFF,IOEXP1);	//P4,P5,P6,P7 as Input
	WriteToMax7301(0x0A,0xFF,IOEXP1);	//P8,P9,P10,P11 as Input
	WriteToMax7301(0x0B,0x55,IOEXP1);	//P12,P13,P14,P15 as Output
	WriteToMax7301(0x0C,0xF5,IOEXP1);	//P16,P17 as output,P18,P19 as Input with pullup
	WriteToMax7301(0x0D,0xFF,IOEXP1);	//P20,P21,P22,P23 as Input with Pullup
	WriteToMax7301(0x0E,0xFF,IOEXP1);	//P24,P25,P26,P27 as Input with Pullup
	WriteToMax7301(0x0F,0xFF,IOEXP1);	//P28,P29,P30,P31 as Input with Pullup
//*******************************************************************
//********************************************************************
	WriteToMax7301(0x04,0x01,IOEXP2);	//Normal Operation,No Transition Detection
	WriteToMax7301(0x06,0x00,IOEXP2);	//No Input Mask
	WriteToMax7301(0x09,0xFF,IOEXP2);	//P4,P5,P6,P7 as Input
	WriteToMax7301(0x0A,0xFF,IOEXP2);	//P8,P9,P10,P11 as Input
	WriteToMax7301(0x0B,0xFF,IOEXP2);	//P12,P13,P14,P15 as input with Pullup
	WriteToMax7301(0x0C,0xFF,IOEXP2);	//P16,P17,P18,P19 as Input with pullup
	WriteToMax7301(0x0D,0xFF,IOEXP2);	//P20,P21,P22,P23 as Input with Pullup
	WriteToMax7301(0x0E,0xFF,IOEXP2);	//P24,P25,P26,P27 as Input with Pullup
	WriteToMax7301(0x0F,0xFF,IOEXP2);	//P28,P29,P30,P31 as Input with Pullup
//*******************************************************************
//********************************************************************
	WriteToMax7301(0x04,0x01,IOEXP3);	//Normal Operation,No Transition Detection
	WriteToMax7301(0x06,0x00,IOEXP3);	//No Input Mask
	WriteToMax7301(0x09,0xFF,IOEXP3);	//P4,P5,P6,P7 as Input
	WriteToMax7301(0x0A,0xFF,IOEXP3);	//P8,P9,P10,P11 as Input
	WriteToMax7301(0x0B,0xFF,IOEXP3);	//P12,P13,P14,P15 as input with Pullup
	WriteToMax7301(0x0C,0xFF,IOEXP3);	//P16,P17,P18,P19 as Input with pullup
	WriteToMax7301(0x0D,0xFF,IOEXP3);	//P20,P21,P22,P23 as Input with Pullup
	WriteToMax7301(0x0E,0xFF,IOEXP3);	//P24,P25,P26,P27 as Input with Pullup
	WriteToMax7301(0x0F,0xFF,IOEXP3);	//P28,P29,P30,P31 as Input with Pullup
//*******************************************************************
//printf("\n\r Pull up enabled");
#elif defined DISSABLE_PULLUP
//*******************************************************************
	WriteToMax7301(0x04,0x01,IOEXP1);	//Normal Operation,No Transition Detection
	WriteToMax7301(0x06,0x00,IOEXP1);	//No Input Mask
	WriteToMax7301(0x09,0xAA,IOEXP1);	//P4,P5,P6,P7 as Input
	WriteToMax7301(0x0A,0xAA,IOEXP1);	//P8,P9,P10,P11 as Input
	WriteToMax7301(0x0B,0x55,IOEXP1);	//P12,P13,P14,P15 as Output
	WriteToMax7301(0x0C,0xA5,IOEXP1);	//P16,P17 as output,P18,P19 as Input without pullup
	WriteToMax7301(0x0D,0xAA,IOEXP1);	//P20,P21,P22,P23 as Input without Pullup
	WriteToMax7301(0x0E,0xAA,IOEXP1);	//P24,P25,P26,P27 as Input without Pullup
	WriteToMax7301(0x0F,0xAA,IOEXP1);	//P28,P29,P30,P31 as Input without Pullup
//*******************************************************************
//*******************************************************************
	WriteToMax7301(0x04,0x01,IOEXP2);	//Normal Operation,No Transition Detection
	WriteToMax7301(0x06,0x00,IOEXP2);	//No Input Mask
	WriteToMax7301(0x09,0xAA,IOEXP2);	//P4,P5,P6,P7 as Input
	WriteToMax7301(0x0A,0xAA,IOEXP2);	//P8,P9,P10,P11 as Input
	WriteToMax7301(0x0B,0xAA,IOEXP2);	//P12,P13,P14,P15 as Input without pullup
	WriteToMax7301(0x0C,0xAA,IOEXP2);	//P16,P17,P18,P19 as Input without pullup
	WriteToMax7301(0x0D,0xAA,IOEXP2);	//P20,P21,P22,P23 as Input without Pullup
	WriteToMax7301(0x0E,0xAA,IOEXP2);	//P24,P25,P26,P27 as Input without Pullup
	WriteToMax7301(0x0F,0xAA,IOEXP2);	//P28,P29,P30,P31 as Input without Pullup
//*******************************************************************
//*******************************************************************
	WriteToMax7301(0x04,0x01,IOEXP3);	//Normal Operation,No Transition Detection
	WriteToMax7301(0x06,0x00,IOEXP3);	//No Input Mask
	WriteToMax7301(0x09,0xAA,IOEXP3);	//P4,P5,P6,P7 as Input
	WriteToMax7301(0x0A,0xAA,IOEXP3);	//P8,P9,P10,P11 as Input
	WriteToMax7301(0x0B,0xAA,IOEXP3);	//P12,P13,P14,P15 as Input without pullup
	WriteToMax7301(0x0C,0xAA,IOEXP3);	//P16,P17,P18,P19 as Input without pullup
	WriteToMax7301(0x0D,0xAA,IOEXP3);	//P20,P21,P22,P23 as Input without Pullup
	WriteToMax7301(0x0E,0xAA,IOEXP3);	//P24,P25,P26,P27 as Input without Pullup
	WriteToMax7301(0x0F,0xAA,IOEXP3);	//P28,P29,P30,P31 as Input without Pullup
//printf("\n\r Pull up disabled");
//*******************************************************************
#endif
}

//******************************************************************************
//Function Name     : WriteToMax7301(unsigned char Address,unsigned char data,unsigned char IOPort)

⌨️ 快捷键说明

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