cancel
Showing results for 
Search instead for 
Did you mean: 

Defining GPIO ports as inputs

jjhopkotcha
Associate II
Posted on July 22, 2011 at 11:07

Hi there, I am fairly new to programming these chips, I am currently coding using the IAR Embedded Workspace and loading my program onto an STM32F100 LQFP48 chip.

Currently I have pins PB8, PB9, PB10, PB11, PB12, PA11, PA12, PB15 set up as outputs which power 8 LED's. 

I am using to of the outputs to control a small DC motor using a H-Bridge. In order to control the movement of my DC motor I have a couple of micro-switches which limit the rotation of my motor. I need my program to be able to respond to signals from these micro-switches which requires defining some of my GPIO's on my chip as inputs. 

I have a small PCB board for programming my chip and there are lots of test pins which I could solder onto, many of these are GPIO's such as PB0-PB2, presumably these would be fine. 

Any help would be seriously appreciated. 

Thanks

#lqfp48 #gpio #stm32f100
6 REPLIES 6
infoinfo966
Associate II
Posted on July 22, 2011 at 11:39

What exactly do you want to know?

Do you use the ST Standard Peripheral Libraries?

Best

Tom

jjhopkotcha
Associate II
Posted on July 22, 2011 at 12:27

Yes as far as I am aware I do. There are so many functions containing GPIO such as GPIO_Remap or GPIO_typedef etc. 

I am wondering what sort of line of code would set up, say for example PB0 and PB1 as a logical input enabling my microprocessor to respond to my motor's position. 

Thanks

infoinfo966
Associate II
Posted on July 22, 2011 at 13:19

That's pretty simple:

  GPIO_InitTypeDef GPIO_InitStructure;

  //IMportant, you always have to enable the Peripheral Clock before you can use a device

For you:

 

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;

  // now it depends if you want to use the internal PUSH UP/DOWN resistors, if so use

   //  GPIO_Mode_IPD = 0x28,

   // GPIO_Mode_IPU = 0x48,

  // if you have external pus ups/downs just

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;

      GPIO_Init(GPIOB, &GPIO_InitStructure);

 

Now you can poll the state of your PIN with

GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

I hope this helps

Best

Tom

jjhopkotcha
Associate II
Posted on July 22, 2011 at 14:44

Thanks Tom, i'm definitely getting closer. Forgive me if i sound a little inexperienced with my questions. I believe in my code that GPIO_Pin_1 is being used to define PA0_WKUP and this is used as an interrupt to shut down my chip. 

I intend to use external pull down resistors so would this bit of code work for me,

GPIO_InitTypeDef GPIO_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // enable's the peripheral clock. 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; // this sets up input to be used with external push up/pull down resistors.

  GPIO_Init(GPIOB, &GPIO_InitStructure);

Thanks for your help thus far, I am struggling to realise where in this code i can know which pin of my chip is being defined and then when it comes to polling my input presumably i refer to the desired pin at this point (''__'') in the read data line and the x following GPIO would be B if im using PB1 or PB2...???

GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin ''__'' ); 

Thanks again, 

John

jjhopkotcha
Associate II
Posted on July 22, 2011 at 18:36

Hi there,

I think i've cracked how to define any of my GPIO's as inputs or outputs, i am now struggling to poll a value for an input. 

i am using the following code to hopefully store a value or 1, or 0 into either of the variables M1_Status or M2_Status. 

int M1_Status (void)

{

  return (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) ? 1:0);

}

int M2_Status (void)

{

  return (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) ? 1:0);

}

Any help on how to extract a number from these inputs would be great. 

Thanks

John

infoinfo966
Associate II
Posted on July 22, 2011 at 20:51

Hmmm...

M1_Status and M2_Status are not variables but functions as you defined them.

I have no idea what you want to achieve with your code, as GPIO_ReadInputDataBit

already returns an int with the result.