cancel
Showing results for 
Search instead for 
Did you mean: 

Reading Output Open-Drain GPIO (Debugging I2C Bit Banging)

godmode
Associate II
Posted on December 29, 2009 at 12:28

Reading Output Open-Drain GPIO (Debugging I2C Bit Banging)

3 REPLIES 3
godmode
Associate II
Posted on May 17, 2011 at 13:35

Greetings to you all!

Recently Im trying my best to have my I2C ICs functioning.

First of all configuration:

* STM32F107

* Keil uVision v4.00 + compiler

* I2C temperature IC with 10k pull-up resistor on SCL and SDA,

software bit banging I2C protocol - I dont want to use built-in one, dont ask why.

I couldnt manage to make it working so I started to debug.

I was trying to read the state of output pin (SDA, output open-drain), which configuration is:

Code:

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_DeInit(GPIOB);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

also to make it sure that my code is working Iv configured another pin as input floating which is connected to a microswitch and a pull-up resistor:

Code:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

read procedure:

Code:

while (1)

{

if(GPIO_ReadOutputDataBit(I2C, SDA) == 1)

{

GPIO_ResetBits(LED, LED1);

}

else

{

GPIO_SetBits(LED, LED1);

}

if(GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_10) == 1)

GPIO_ResetBits(LED, LED2);

else

GPIO_SetBits(LED, LED2);

}

LED, LED1, LED2, I2C, SDA are #define of pins and ports.

GPIO_Pin_10 is a button.

Second 'if' turn on/off a LED whenever a button is pushed.

Unfortunately I have completely no idea why when Im reading my SDA pin it is always '1', even when I short this pin to the ground :(

Can anybody help me with this out?

Thanks! and sorry for my bad english.

Cheers!

tomas23
Associate II
Posted on May 17, 2011 at 13:35

The difference is, that first test reads OUTPUT data register, while the other test reads INPUT data register.

See in the Reference Manual structure of each GPIO bit => always read back the Input data register, if you want to know what happens on the pin. The reading of output data register reflects only the information you stored there previously.

godmode
Associate II
Posted on May 17, 2011 at 13:35

Oh sorry for that lame question, I was reading the manual but I was sure that registers for reading Outputs and Inputs are seperate.

Now everything works just fine.

Big thanks!

[ This message was edited by: godmode on 29-12-2009 16:58 ]