ECE471/571 (RTOS) STM32 C Example - ADC multiple channels

In STM32CubeMX

  1. Initialize all peripherals with in default mode? - yes
  2. find PB0, PB7, PB14, PC13
  3. find ADC1
  4. Settings for multiple channel ADC
  5. enable UART3
  6. disable Ethernet (optional)
  7. 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 a good place to insert a custom include files
  5. Insert your code ** HERE **
    /* USER CODE BEGIN Includes */
        ** HERE **
    /* USER CODE END Includes */
    
  6. We are using the C-string library (strlen), and stdio library (sprintf) we need to include their headers
    #include <string.h>
    #include <stdio.h>
    
  7. Locate the forever loop in main()
  8. Insert your code ** HERE **
        /* USER CODE BEGIN WHILE */
        while (1)
        {
        /* USER CODE END WHILE */
    
        /* USER CODE BEGIN 3 */
            ** HERE **
        }
        /* USER CODE END 3 */
    
  9. Code for ** HERE **
        HAL_ADC_Start(&hadc1);
        HAL_ADC_PollForConversion(&hadc1, 10);
        uint32_t raw_result5 = HAL_ADC_GetValue(&hadc1);
        uint32_t ch5_voltage = (raw_result5 * 3300U) / 4095U;
        // Note: 4095 is the constant for 12bit right aligned mode
        HAL_ADC_Start(&hadc1);
        HAL_ADC_PollForConversion(&hadc1, 10);
        uint32_t raw_result6 = HAL_ADC_GetValue(&hadc1);
        uint32_t ch6_voltage = (raw_result6 * 3300U) / 4095U;
        HAL_ADC_Start(&hadc1);
        HAL_ADC_PollForConversion(&hadc1, 10);
        uint32_t raw_temp = HAL_ADC_GetValue(&hadc1);
        uint32_t cht_voltage = (raw_temp * 3300U) / 4095U;
        uint32_t temperature = 25 + (cht_voltage*10 - 7600U) /25; // V25 = 0.76V, avg slope = 2.5mV/C
    
        char buffer[80];
        sprintf(buffer, "%lu\t%lu\t\t%lu\t%lu\t\t%lu\t%lu\t%lu\r\n",
            raw_result5, ch5_voltage,
            raw_result6, ch6_voltage,
            raw_temp, cht_voltage, temperature);
        HAL_UART_Transmit(&huart3, buffer, strlen(buffer), 10);
        HAL_Delay(500);
    

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