玩这个片子一年啦,可没找到战友呀!我的qq是8814405,有搞这个加我,最近有其他项目,暂时没搞,逛到这里,str9还是冷冷清清,悲哀呀。。。。看到有人问I2C问题,贴上我以前的代码,看能不能借鉴?算是推广一下了!
/******************************************************************************* * Function Name : SendI2C1Data * Description : 发送I2c1的数据 * Input1 : I2c1地址 * Input2 : I2c1数据指针 * Input3 : I2c1数据长度 * Output : None * Return : 1:成功 0: 失败 *******************************************************************************/ unsigned char SendI2C1Data(unsigned char I2cAddress, unsigned char *p, unsigned char len) { unsigned char i=0; unsigned long j=0,timeup=500; //I2C_Cmd (I2C0, ENABLE); I2C_GenerateStart (I2C1, ENABLE); /* Test on EV5 and clear it */ do { j++; if( I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) break; // EV5 }while(j<timeup); if(j==timeup) return 0; /* Send I2C1 Address for write */ I2C_Send7bitAddress (I2C1, I2cAddress, I2C_MODE_TRANSMITTER); /* Test on EV6 and clear it */ j=0; do { j++; if( I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECTED) ) break; // EV6 }while(j<timeup); if(j==timeup) return 0; // while(!I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //EV8 /* Clear EV6 by set again the PE bit */ I2C_Cmd (I2C1, ENABLE); /* Send Data to write on I2C1 slave */ while (i < len) { /* Send Master data */ I2C_SendData(I2C1, p[i++]); /* Test on EV8 and clear it */ j=0; do { j++; if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) break; //EV8 }while(j<timeup); if(j==timeup) return 0; }
/* Generate STOP condition to close communication */ I2C_GenerateSTOP (I2C1, ENABLE); //I2C_Cmd(I2C0, DISABLE); return 1; }
/******************************************************************************* * Function Name : ReadI2C1Data * Description : 接收I2c1的数据 * Input1 : I2c1地址 * Input2 : I2c1数据指针 * Input3 : I2c1数据长度 * Output : None * Return : 1:成功 0: 失败 *******************************************************************************/ unsigned char ReadI2C1Data(unsigned char I2cAddress, unsigned char *p, unsigned char len) { unsigned char i=0; unsigned long j=0,timeup=500; //I2C_Cmd (I2C0, ENABLE); I2C_GenerateStart (I2C1, ENABLE); do { j++; if( I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) break; // EV5 }while(j<timeup); if(j==timeup) return 0;
I2C_Send7bitAddress (I2C1, I2cAddress, I2C_MODE_RECEIVER); j=0; do { j++; if( I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECTED) ) break; // EV6 }while(j<timeup); if(j==timeup) return 0;
I2C_Cmd (I2C1, ENABLE); I2C_AcknowledgeConfig (I2C1, ENABLE); if(len==1) { I2C_AcknowledgeConfig (I2C1, DISABLE); I2C_GenerateSTOP (I2C1, ENABLE); } for(i=0;i<len;i++) { j=0; do { j++; if( I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED) ) break; // EV6 }while(j<timeup); if(j==timeup) return 0; p = I2C_ReceiveData(I2C1); if(i==len-2) { I2C_AcknowledgeConfig (I2C1, DISABLE); I2C_GenerateSTOP (I2C1, ENABLE); } } //I2C_Cmd(I2C0, DISABLE); return 1; }
|