2012-03-09 07:41 AM
Want to read my LIS302 sensor with software I2C (bit banging)
Hardware i2c works.Here is some code.Please help me with software i2c...2012-03-12 12:37 PM
with software I2C (bit banging)
WHY?Here is some code.
I have no intention of accessing ''some strange site'' what is wrong with posting code here? Erik
2012-03-13 07:37 AM
static void I2C1_GPIO_Config(void)
{ GPIO_InitTypeDef GPIO_InitStructure; //GPIO_DeInit(GPIOB); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Configure I2C1 pins: SCL and SDA GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); }void I2C_delay(void){ unsigned int i=150; while(i) { i--; } }bool I2C1_Start(void){ SDAH; SCLH; I2C_delay(); if(!SDAread)return FALSE; SDAL; I2C_delay(); if(SDAread) return FALSE; SDAL; I2C_delay(); return TRUE;}void I2C1_Stop(void){ SCLL; I2C_delay(); SDAL; I2C_delay(); SCLH; I2C_delay(); SDAH; I2C_delay();}void I2C1_Ack(void){ SCLL; I2C_delay(); SDAL; I2C_delay(); SCLH; I2C_delay(); SCLL; I2C_delay();}void I2C1_NoAck(void){ SCLL; I2C_delay(); SDAH; I2C_delay(); SCLH; I2C_delay(); SCLL; I2C_delay();}bool I2C1_WaitAck(void) { SCLL; I2C_delay(); SDAH; I2C_delay(); SCLH; I2C_delay(); if(SDAread) { SCLL; return FALSE; } SCLL; return TRUE;}void I2C1_SendByte(unsigned char SendByte) { unsigned char i=8; while(i--) { SCLL; I2C_delay(); if(SendByte&0x80) SDAH; else SDAL; SendByte<<=1; I2C_delay(); SCLH; I2C_delay(); } SCLL;}unsigned char I2C1_ReceiveByte(void) { unsigned char i=8; unsigned char ReceiveByte=0; SDAH; while(i--) { ReceiveByte<<=1; SCLL; I2C_delay(); SCLH; I2C_delay(); if(SDAread) { ReceiveByte|=0x01; } } SCLL; return ReceiveByte;} unsigned char I2C1_ReadByte( unsigned char DeviceAddress, int ReadAddress){ unsigned char temp; if(!I2C1_Start())return FALSE; I2C1_SendByte((DeviceAddress & 0xFE)); if(!I2C1_WaitAck()){I2C1_Stop(); return FALSE;} I2C1_SendByte((unsigned char)((ReadAddress>>8) & 0xFF)); I2C1_WaitAck(); I2C1_SendByte((unsigned char)((ReadAddress) & 0xFF)); I2C1_WaitAck(); I2C1_Start(); I2C1_SendByte((DeviceAddress & 0xFE)|0x01); I2C1_WaitAck(); temp = I2C1_ReceiveByte(); I2C1_NoAck(); I2C1_Stop(); return temp;}/************************************MAIN****************************/int main( void ){ prvSetupHardware(); I2C1_GPIO_Config(); initDisplay(); itoc(I2C1_ReadByte(I2C_ADDRESS, 0x0F)); GLCD_displayStringLn(Line3, string);}2012-03-13 08:58 AM
ah.. Forget to mention this, you can ask if you need some more info..:
#define SCLH GPIOB->BSRR = GPIO_Pin_6 #define SCLL GPIOB->BRR = GPIO_Pin_6 #define SDAH GPIOB->BSRR = GPIO_Pin_7 #define SDAL GPIOB->BRR = GPIO_Pin_7#define SCLread GPIOB->IDR & GPIO_Pin_6
#define SDAread GPIOB->IDR & GPIO_Pin_72012-03-13 09:56 AM
still no answer
WHY? Erik2012-03-13 11:43 AM
For learning purposes.. I am student..
Later I need to launch the camera which uses SCCB bus (something like i2c but a bit different)..2012-03-13 12:11 PM
1) is the device you access I²C or SMB (SMB has timeouits,I²C does not, otherwise they are, for all practical purposes identical.
2) you are using an arbitrary delay (all delays in C are, by definition, arbitrary) put a scope on SCL and verify it is OK Erik2012-03-15 01:32 AM
1. Its I2C.
2. So the code is right? What do you mean arbitary?2012-03-15 08:47 AM
arbitrary Timing loops in C:
there is no rule whatsoever for how a compiler implement a C construct. I recall an incident version x of a '51 compiler implemented for (time = 0; time !=47; time++)this way: mov r7,time loop: cjne r7, #47, continue ret continue: mov acc, r7 inc a mov r7,a sjmp loop then the compiler maker go smarter and in rev y it looked like this mov r7,#47 loop: djnz r7,loop ret and everybodys code with delays in C fell apart. The above is as best I recall and should be accurate. Erik
2012-03-15 08:49 AM
I guess your code, faulty or not, will generate a few SCL pulses, put a sope on SCL and make sure the timing is at or below what your external devices datasheet specify
Erik