Skip to main content
RBrou.1595
Associate
October 30, 2020
Solved

I want to know if I have this right for the STM32 with the HAL library. I have this working on an Atmega. And Arduino has also a lot more examples to find then STM. So due lack of examples I have wrote this.

  • October 30, 2020
  • 3 replies
  • 1413 views

1) Set output high

2) When input is low wait till it is high

3) When input is high read 6 characters

4) When having read 6 characters then output low and return

(or leave while loop)

void Get_Instruction()

{

   HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET);

    while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_11)==0)

          {

          if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_11)==1)

             {

             for (;;)

                 {

                 HAL_UART_Receive(&huart1, RXbuffer, 6, 1000);

                 }

             }

          HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET);

          return;

          }

}

///////////////////////////////////////////////////////////////////////////////////////

Except the input/output this is how I have it with the Atmega.

void ReceiveData()

{

    ByteCount = 0;

    while (ByteCount <6)

    {

          if (STM32.available()> 1)

          {

          ByteData[ByteCount] = (STM32.read());

          ByteCount++;

          }

    }

}

    This topic has been closed for replies.
    Best answer by RBrou.1595

    Thank you both. It is working now like I ment.

    Using the same kind of routine for 'wait till input is high' (don't know why, but inputs are inverted detected).

    void Get1PPS()                  

       while (PPSstate == 1)

       {

            PPSstate = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15);

           {

                        if (PPSstate == 0)

                        {

                         return; // just exit the loop

                        }

             }

       }

       PPSstate = 1;

    }

    3 replies

    Tesla DeLorean
    Guru
    October 31, 2020

    Ignore the fact there's not a exact example to copy, the loop logic is very confused. It is more likely to exit the loop than it is to wait for the input data.

    The equivalent to your ReceiveData() is simply  HAL_UART_Receive(&huart1, RXbuffer, 6, 1000), perhaps might want to push the time out further.

    This is more like what you actually described

    void Get_Instruction()

    {

       HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_SET); // Pin out high

       while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_11)==0) {}; // Wait while low

    HAL_UART_Receive(&huart1, RXbuffer, 6, 100000); // Collect 6 characters, blocking

    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, GPIO_PIN_RESET); // Pin out low

    }

    Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
    TDK
    October 31, 2020

    If you want it blocking, make it actually blocking. Agree with everything else.

    HAL_UART_Receive(&huart1, RXbuffer, 6, HAL_MAX_DELAY);

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    RBrou.1595
    RBrou.1595AuthorBest answer
    Associate
    November 1, 2020

    Thank you both. It is working now like I ment.

    Using the same kind of routine for 'wait till input is high' (don't know why, but inputs are inverted detected).

    void Get1PPS()                  

       while (PPSstate == 1)

       {

            PPSstate = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15);

           {

                        if (PPSstate == 0)

                        {

                         return; // just exit the loop

                        }

             }

       }

       PPSstate = 1;

    }

    Piranha
    Principal III
    November 6, 2020

    For capturing PPS signals better use hardware timer input capture feature.