2011-03-02 02:48 AM
2011-03-02 02:49 AM
My interrupt routine is as below:
@far @interrupt void I2C_IRQHandler(void) /* I2C Interrupt handler*/ { //Handle I2C interrupts here I2C_HandleEvents(); return; } void I2C_HandleEvents() { char tmp1, tmp2; #ifdef _MASTER_MODE_ /*Need to remove repeting events*/ /* MASTER EVENTS*/ /*MASTER TRANSMITTER, read-write byte=0*/ /*Start bit sent from Master*/ /* EV5: SB=1, cleared by reading SR1 register followed by writing DR register with Address */ if(I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT)) { I2C_ClearFlag(I2C_FLAG_STARTDETECTION); I2C.DR = I2C.DR;//Dummy write } /* master address acknowledged */ /*EV6:ADDR=1, cleared by reading SR1 register followed by reading SR3*/ else if(I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED)) { I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED); } /*Master byte transmitted*/ /*EV8_1: TxE=1, shift register empty, data register empty, write DR register*/ else if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { I2C.DR = I2C.DR;//Dummy write I2C_ClearFlag(I2C_FLAG_TRANSFERFINISHED); } /*Master byte being transmitted now*/ /*EV8: TxE=1, shift register not empty, data register empty, cleared by writing DR register.*/ else if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTING)) { I2C.DR = I2C.DR;//Dummy write I2C_ClearFlag(I2C_FLAG_TRANSFERFINISHED); } /*Stop bit sent*/ /*EV8_2: TxE=1, BTF = 1, Program STOP request. TxE and BTF are cleared by HW by stop condition*/ else if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { I2C_GenerateSTOP(ENABLE); } /*MASTER RECEIVER, read-write byte=1*/ /*Start bit sent from Master*/ /* EV5: SB=1, cleared by reading SR1 register followed by WRITING DR register with Address */ /*else if(I2C_CheckEvent(I2C_EVENT_MASTER_START_SENT)) { I2C_ClearFlag(I2C_FLAG_STARTDETECTION); I2C.DR = I2C.DR;//Dummy write }*/ /*EV6:ADDR=1, cleared by reading SR1 register followed by reading SR3*/ /*else if(I2C_CheckEvent(I2C_EVENT_MASTER_ADDRESS_ACKED)) { I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED); } */ else if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED)) { /*EV7_1: RxNE=1, cleared by reading DR register, program ACK=0 and STOP request*/ if(I2C_GetFlagStatus(I2C_FLAG_ACKNOWLEDGEFAILURE)) { //I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED); tmp1 = I2C.DR; I2C.CR2 &=~(I2C_CR2_ACK); I2C_GenerateSTOP(ENABLE); } /*EV7: RxNE=1, cleared by reading DR register*/ else { //User different variable for read to care of compiler optimization tmp2 = I2C.DR; } } /* EV6_1: How is the single byte transfer info provided beforehand? */ /* EV6_1: used for 1 byte reception only. Program ACK=0 and STOP=1 after clearing ADDR.*/ else if(I2C_CheckEvent(I2C_EVENT_SLAVE_ACK_FAILURE)) { I2C_ClearFlag(I2C_FLAG_ACKNOWLEDGEFAILURE); I2C.SR1 &=~(I2C_SR1_ADDR); I2C.CR2 &=~(I2C_CR2_ACK); //I2C_GenerateSTOP(ENABLE); } /* slave detected stop command from master */ /*else if(I2C_CheckEvent(I2C_EVENT_SLAVE_STOP_DETECTED)) { } */ #else //Slave mode /* SLAVE EVENTS */ /* SLAVE RECEIVER */ /* Slave address matched with the address sent by master */ /* EV1: ADDR =1, cleared by reading SR1 register followed by reading SR3.*/ if(I2C_CheckEvent(I2C_EVENT_SLAVE_ADDRESS_MATCHED)) { I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED); } /* slave received the date byte from master */ /*EV2: RxNE=1, cleared by reading DR register*/ else if(I2C_CheckEvent(I2C_EVENT_SLAVE_BYTE_RECEIVED)) { I2C_ClearFlag(I2C_FLAG_TRANSFERFINISHED); } /* Slave received STOP byte from master */ /*EV4: STOPF=1, cleared by reading SR1 register followed by dummy writing CR2 register*/ else if(I2C_CheckEvent(I2C_EVENT_SLAVE_STOP_DETECTED)) { I2C_ClearFlag(I2C_FLAG_STOPDETECTION); }/*SLAVE TRANSMITTER*/
/*Slave address matched with the address sent by master*/ /* EV1: ADDR =1, cleared by reading SR1 register followed by reading SR3.*/ /*if(I2C_CheckEvent(I2C_EVENT_SLAVE_ADDRESS_MATCHED)) { I2C_ClearFlag(I2C_FLAG_ADDRESSSENTMATCHED); }*/ /*Data transmited from slave to master*/ /*EV3: TxE=1, cleared by writing DR; shift register not empty*/ else if(I2C_CheckEvent(I2C_EVENT_SLAVE_BYTE_TRANSMITTED)) { //I2C_ClearFlag(I2C_FLAG_TRANSFERFINISHED); //Dummy write I2C.DR = I2C.DR; } /*Acknowledge failure*/ /*EV3-2: AF=1, AF is cleared by writing ‘0’ in AF bit of SR2 register*/ else if(I2C_CheckEvent(I2C_EVENT_SLAVE_ACK_FAILURE)) { I2C_ClearFlag(I2C_FLAG_ACKNOWLEDGEFAILURE); } #endif return; }2011-03-02 02:52 AM
My Main routine is:
I2C_DeInit(); _asm(''rim\n''); I2C_Cmd(ENABLE); I2C_ITConfig((I2C_IT_EVT |I2C_IT_BUF | I2C_IT_ERR), ENABLE); I2C_Init(100000, 0x48, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, 2); while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY)); I2C_GenerateSTART(ENABLE); I2C_Send7bitAddress(0x40, I2C_DIRECTION_TX); I2C_SendData(0x22); I2C_GenerateSTOP(ENABLE);