void InitI2CGpio() { EALLOW; /* Enable internal pull-up for the selected pins */ // Pull-ups can be enabled or disabled disabled by the user. // This will enable the pullups for the specified pins. // Comment out other unwanted lines. // GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0; // Enable pull-up for GPIO28 (SDAA) // GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0; // Enable pull-up for GPIO29 (SCLA) GpioCtrlRegs.GPBPUD.bit.GPIO32 = 0; // Enable pull-up for GPIO32 (SDAA) GpioCtrlRegs.GPBPUD.bit.GPIO33 = 0; // Enable pull-up for GPIO33 (SCLA) /* Set qualification for selected pins to asynch only */ // This will select asynch (no qualification) for the selected pins. // Comment out other unwanted lines. // GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3; // Asynch input GPIO28 (SDAA) // GpioCtrlRegs.GPAQSEL2.bit.GPIO29 = 3; // Asynch input GPIO29 (SCLA) GpioCtrlRegs.GPBQSEL1.bit.GPIO32 = 3; // Asynch input GPIO32 (SDAA) GpioCtrlRegs.GPBQSEL1.bit.GPIO33 = 3; // Asynch input GPIO33 (SCLA) /* Configure I2C pins using GPIO regs*/ // This specifies which of the possible GPIO pins will be I2C functional pins. // Comment out other unwanted lines. // GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 2; // Configure GPIO28 for SDAA operation // GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 2; // Configure GPIO29 for SCLA operation GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 1; // Configure GPIO32 for SDAA operation GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 1; // Configure GPIO33 for SCLA operation EDIS; } void i2c_init(void){ /************I2C LOOPBACK INIT****************/ EALLOW; SysCtrlRegs.PCLKCR0.bit.I2CAENCLK = 1; I2caRegs.I2COAR=0x003F; //7-bit addressing I2caRegs.I2CMDR.bit.IRS = 0; I2caRegs.I2CMDR.bit.DLB=1; I2caRegs.I2CPSC.all = 5; // 7M <= 60M/(PSC+1) <=12M I2caRegs.I2CCLKL = 7; I2caRegs.I2CCLKH = 8; I2caRegs.I2CMDR.bit.IRS = 1; EDIS; /************I2C LOOPBACK INIT***************END*/ } void I2C_WriteData(unsigned char data) { I2caRegs.I2CSAR = 0x003F; I2caRegs.I2CMDR.bit.MST = 1; I2caRegs.I2CMDR.bit.TRX = 1; I2caRegs.I2CMDR.bit.STT = 1; I2caRegs.I2CMDR.bit.STP = 1; while(I2caRegs.I2CSTR.bit.BB!=1); I2caRegs.I2CDXR = data; } unsigned char I2C_ReadData() { unsigned char data; I2caRegs.I2CMDR.bit.TRX = 0; I2caRegs.I2CMDR.bit.STT = 1; I2caRegs.I2CMDR.bit.STP = 1; while(I2caRegs.I2CSTR.bit.RRDY!=1); data = I2caRegs.I2CDRR&0x00ff; return data; } void main(void) {//主程式 Hardware(); //硬體初始化 firmware(); //韌體初始化 DELAY_US(20000); while(1){ switch(TestCounter){ case 1: { I2C_WriteData('A'); testdata=I2C_ReadData(); TestCounter=0; break; } case 2: { I2C_WriteData('5'); testdata=I2C_ReadData(); TestCounter=0; break; } } }