cancel
Showing results for 
Search instead for 
Did you mean: 

BASIC GPIO

xtian
Associate II
Posted on January 17, 2013 at 09:34

Hi Guys,

I am 1week old to stm32 programming I came from PIC

1ST QUESTION

// FROM Pic Basic Code normally I do this

TrisC = %11111111

int data = PortC

the return is the decimal equivalent of the binary or port C example 1000001 data will contain 65 then that is very easy to manipulate if you want to send via USART / LCD

//00: General purpose output push-pull

//01: Output mode, max speed 10 MHz.

 GPIOC->CRL = 0x11111111;

 GPIOC->CRH = 0x11111111;

//10: Input with pull-up / pull-down

//00: Input mode (reset state)

// set as pulldown

  GPIOA->CRL = 0x88888888;

  GPIOA->CRH = 0x88888888;

  GPIOA->ODR = 0x0000;

while(1)

{

    GPIOC->BSRR |= GPIO_ReadInputData(GPIOA);  //read pins and set PORTC from data of PORTA

    GPIOC->BRR |= GPIO_ReadInputData(GPIOA);  //reset portC based from portA

}

this is working but i want to convert the data to 8 bit to represent an ascii

i want a concept that is similar to this

int data = GPIO_ReadInputData(GPIOA);

instead of doing this:

if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6) == 1)

    {

     data +=64

   }

(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_5) == 1)

    {

     data +=32

   }

(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_4) == 1)

    {

     data +=16

   }

2ND QUESTION

usually in PIC to declare a input and output

I DO THIS

TRISA = %11110000 //1111input 0000output so port0-port3(output) port4-port7(input)

so in STM32VL for example i want port A0 - portA7 as output and the rest A8-AX as input how do i do that?

7 REPLIES 7
Posted on January 17, 2013 at 23:21

int data = GPIO_ReadInputData(GPIOA) & 0xFF; // Will mask low order bits

int data = GPIOA->IDR & 0xFF; // A more direct form

The LCD code is done in a bitwise fashion to allow an agnostic implementation, where the pins are scattered, or in reverse order, or whatever.

  /* GPIOA Periph clock enable */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

  /* Configure PA0 .. PA7 in output pushpull mode */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure PA8 .. PA15 in input, pulled up mode */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIOA->ODR = GPIOA->IDR >> 8; // Mirror high ordered inputs to low ordered outputs

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtian
Associate II
Posted on January 18, 2013 at 01:48

Hi Clide,

Thank you very much! your the best:) This gave me a good start to understand configuring pins in C. may I ask if you have a spare time this weekend and if you could give me a crash course just teach me how to start and  that's it the rest I'll manage...:)

I am trying to study on my own but it may stil take 2-4 weeks and my project is due this january 31. I hope you could be my mentor for at least 3-4 hours or for the basics of C in STM programming

xtian
Associate II
Posted on January 18, 2013 at 02:18

typedef struct

{

  uint16_t GPIO_Pin;         

  GPIOSpeed_TypeDef GPIO_Speed;

  GPIOMode_TypeDef GPIO_Mode;

}GPIO_InitTypeDef;

is this one

GPIO_InitTypeDef GPIO_InitStructure; like creating an object of a class which allows me to use its variable or methods?

GPIO_InitTypeDef - class

GPIO_InitStructure - object / variable

so I can make it something like GPIO_Initialization

xtian
Associate II
Posted on January 18, 2013 at 02:28

Please see if my understanding are correct

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;

//will put implement the succeeding lines on the following pins 0, 1, 2,3,4,5,6,7

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

//will put implement the a 50 mhz cycle on pins 0, 1, 2,3,4,5,6,7

 

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

//will put implement a push pull on pins 0, 1, 2,3,4,5,6,7

  GPIO_Init(GPIOA, &GPIO_InitStructure);

//will implement all the setting above on portA

//so if i have some thing likeGPIO_Init(GPIOC, &GPIO_InitStructure); settings above will //also be implemented to portC pins 0-7

Posted on January 18, 2013 at 02:55

type

structure

GPIO_Init() is a function that takes a pointer to the structure (array in memory if you will) that contains the setting we want to apply.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
xtian
Associate II
Posted on January 18, 2013 at 02:57

thanks clide!

xtian
Associate II
Posted on January 18, 2013 at 03:04

Thanks clive! how I wish you are the Philippines so I could treat you :) how about it clive do you have some spare time? could I have you as a mentor for few hours?