ECE471/571 (RTOS) STM32 C Example - UART send [anything] and optionally receive 1 byte command

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. find PB0, PB7, PB14, PC13
  3. enable UART3
  4. disable Ethernet (optional)
  5. disable USB_OTG_FS (optional)

Adapt and compile a project (example 1)

  1. Open source/main.c
  2. Locate int main() function
  3. Inspect the GPIO initialization called from main()
  4. Locate the forever loop in main()
  5. Insert your code ** HERE **
        /* USER CODE BEGIN WHILE */
            ** HERE1 **
        while (1)
        {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
            ** HERE2 **
      }
      /* USER CODE END 3 */
    
  6. Code for ** HERE1 **
        GPIO_PinState state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
    
  7. Code for ** HERE2 **
          GPIO_PinState new_state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13);
          if ( state!=new_state ) {
            state = new_state;
            HAL_Delay(10); // delay for debouncing
            // sending text over fast 115200bps will cause delay of 1ms per 10 characters
            // sending text over slow   9600bps will cause delay of 1ms per character - set timeout accordingly
            if (state) {
              HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
              HAL_UART_Transmit(&huart3, "button pressed\r\n", 16, 2);
              if ( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_0) ) {
                HAL_UART_Transmit(&huart3, "LED is on\r\n", 11, 2);
              } else {
                HAL_UART_Transmit(&huart3, "LED is off\r\n", 12, 2);
              }
            } else {
              HAL_UART_Transmit(&huart3, "button released\r\n", 17, 2);
            }
          }
    
          unsigned char buffer[2];
          HAL_StatusTypeDef status = HAL_UART_Receive(&huart3, buffer, 1, 0);
          // Note: timeout of 0ms means that either a byte is already received and waiting or timeout is returned
          if ( HAL_OK == status ) {
            if ( buffer[0]=='0' ) {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
            } else if ( buffer[0]=='1' ) {
              HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
            } else{
              // nothing - unrecognized command
            }
          }
    

web site front local main page local list page general bookmarks software bookmarks go back copyright info