bigbat 发表于 2024-3-12 18:40

【STM32U5A5ZJ开发板】I2C总线设备的兼容性测试及SHT20温湿度传感器测试

<p>SHT20是一款高质量的空气温湿度传感器,传感器使用I2C总线接口和MCU进行通讯,本次通过STM32U5A5ZJ的I2C设备接口进行通讯。</p>

<div style="text-align: center;"></div>

<p>1、I2C设备设置</p>

<p>本次使用I2C1接口进行测试,STM32U5A5ZJ开发板的PB9和PB8接口连接到接口CN7的SCL和SDA引脚。</p>

<p> &nbsp;</p>

<p>板子的引脚可以提供I2C通讯。使用STM32CUB进行I2C设置</p>

<p>&nbsp; 除了开启I2C1设备外,还需要注意设置I2C的通讯速率,100K的标准速率。其它的设置默认。</p>

<p>2、设备驱动程序</p>

<pre>
<code class="language-cpp">/* An STM32 HAL library written for the SHT2x temperature/humidity sensor series. */
/* Libraries by @eepj www.github.com/eepj */
#include "sht2x_for_stm32_hal.h"
#include "main.h"
#ifdef __cplusplus
extern "C"{
#endif

I2C_HandleTypeDef *_sht2x_ui2c;
       
/**
* @brief Initializes the SHT2x temperature/humidity sensor.
* @param hi2c User I2C handle pointer.
*/
void SHT2x_Init(I2C_HandleTypeDef *hi2c) {
        _sht2x_ui2c = hi2c;
}

/**
*@brief Performs a soft reset.
*/
void SHT2x_SoftReset(void){
        uint8_t cmd = SHT2x_SOFT_RESET;
        HAL_I2C_Master_Transmit(_sht2x_ui2c, SHT2x_I2C_ADDR &lt;&lt; 1, &amp;cmd, 1, SHT2x_TIMEOUT);
}

/**
* @brief Gets the value stored in user register.
* @return 8-bit value stored in user register, 0 to 255.
*/
uint8_t SHT2x_ReadUserReg(void) {
        uint8_t val;
        uint8_t cmd = SHT2x_READ_REG;
        HAL_I2C_Master_Transmit(_sht2x_ui2c, SHT2x_I2C_ADDR &lt;&lt; 1, &amp;cmd, 1, SHT2x_TIMEOUT);
        HAL_I2C_Master_Receive(_sht2x_ui2c, SHT2x_I2C_ADDR &lt;&lt; 1, &amp;val, 1, SHT2x_TIMEOUT);
        return val;
}

/**
* @brief Sends the designated command to sensor and read a 16-bit raw value.
* @param cmd Command to send to sensor.
* @return 16-bit raw value, 0 to 65535.
*/
uint16_t SHT2x_GetRaw(uint8_t cmd) {
        uint8_t val = { 0 };
        HAL_I2C_Master_Transmit(_sht2x_ui2c, SHT2x_I2C_ADDR &lt;&lt; 1, &amp;cmd, 1, SHT2x_TIMEOUT);
        HAL_I2C_Master_Receive(_sht2x_ui2c, SHT2x_I2C_ADDR &lt;&lt; 1, val, 3, SHT2x_TIMEOUT);
        return val &lt;&lt; 8 | val;
}

/**
* @brief Measures and gets the current temperature.
* @param hold Holding mode, 0 for no hold master, 1 for hold master.
* @return Floating point temperature value.
*/
float SHT2x_GetTemperature(uint8_t hold) {
        uint8_t cmd = (hold ? SHT2x_READ_TEMP_HOLD : SHT2x_READ_TEMP_NOHOLD);
        return -46.85 + 175.72 * (SHT2x_GetRaw(cmd) / 65536.0);
}

/**
* @brief Measures and gets the current relative humidity.
* @param hold Holding mode, 0 for no hold master, 1 for hold master.
* @return Floating point relative humidity value.
*/
float SHT2x_GetRelativeHumidity(uint8_t hold) {
        uint8_t cmd = (hold ? SHT2x_READ_RH_HOLD : SHT2x_READ_RH_NOHOLD);
        return -6 + 125.00 * (SHT2x_GetRaw(cmd) / 65536.0);
}

/**
* @brief Sets the measurement resolution.
* @param res Enum resolution.
* @note Available resolutions: RES_14_12, RES_12_8, RES_13_10, RES_11_11.
* @note RES_14_12 = 14-bit temperature and 12-bit RH resolution, etc.
*/
void SHT2x_SetResolution(SHT2x_Resolution res) {
        uint8_t val = SHT2x_ReadUserReg();
        val = (val &amp; 0x7e) | res;
        uint8_t temp = { SHT2x_WRITE_REG, val };
        HAL_I2C_Master_Transmit(_sht2x_ui2c, SHT2x_I2C_ADDR &lt;&lt; 1, temp, 2, SHT2x_TIMEOUT);
}

/**
* @brief Converts degrees Celsius to degrees Fahrenheit.
* @param celsius Floating point temperature in degrees Celsius.
* @return Floating point temperature in degrees Fahrenheit.
*/
float SHT2x_CelsiusToFahrenheit(float celsius) {
        return (9.0 / 5.0) * celsius + 32;
}

/**
* @brief Converts degrees Celsius to Kelvin.
* @param celsius Floating point temperature in degrees Celsius.
* @return Floating point temperature in Kelvin.
*/
float SHT2x_CelsiusToKelvin(float celsius) {
        return celsius + 273;
}

/**
* @brief Gets the integer part of a floating point number.
* @note Avoids the use of sprinf floating point formatting.
* @param num Floating point number.
* @return Integer part of floating point number.
*/
int32_t SHT2x_GetInteger(float num) {
        return num / 1;
}

/**
* @brief Gets the decimal part of a floating point number.
* @note Avoids the use of sprinf floating point formatting.
* @param num Floating point number.
* @return Decimal part of floating point number.
*/
uint32_t SHT2x_GetDecimal(float num, int digits) {
        float postDec = num - SHT2x_GetInteger(num);
        return postDec * SHT2x_Ipow(10, digits);
}

/**
* @brief Integer equivalent of pow() in math.h.
* @param base Base.
* @param power Power.
* @return
*/
uint32_t SHT2x_Ipow(uint32_t base, uint32_t power) {
        uint32_t temp = base;
        for (uint32_t i = 1; i &lt; power; i++)
                temp *= base;
        return temp;
}

#ifdef __cplusplus
}
#endif
</code></pre>

<p>sht2x_for_stm32_hal.C程序为设备的驱动程序,程序除了I2C设备外还有传感器的数字转数值的计算程序。</p>

<p>程序的输出和测试程序</p>

<pre>
<code class="language-cpp">/* USER CODE BEGIN Header */
/**
******************************************************************************
* @File : main.c
* @brief          : Main program body
******************************************************************************
* @attention *
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "sht2x_for_stm32_hal.h"
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

I2C_HandleTypeDef hi2c1;

UART_HandleTypeDef hlpuart1;
UART_HandleTypeDef huart1;
DMA_HandleTypeDef handle_LPDMA1_Channel0;

TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim8;
DMA_NodeTypeDef Node_GPDMA1_Channel0;
DMA_QListTypeDef List_GPDMA1_Channel0;
DMA_HandleTypeDef handle_GPDMA1_Channel0;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void SystemPower_Config(void);
static void MX_GPIO_Init(void);
static void MX_GPDMA1_Init(void);
static void MX_LPDMA1_Init(void);
static void MX_ICACHE_Init(void);
static void MX_LPUART1_UART_Init(void);
static void MX_TIM8_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_I2C1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
* @briefThe application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
unsigned char buffer = { 0 };
/* USER CODE END 1 */

/* MCU Configuration--------------------------------------------------------*/

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */
SystemClock_Config();

/* Configure the System Power */
SystemPower_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_GPDMA1_Init();
MX_LPDMA1_Init();
MX_ICACHE_Init();
MX_LPUART1_UART_Init();
MX_TIM8_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
/* Initializes SHT2x temperature/humidity sensor and sets the resolution. */
        SHT2x_Init(&amp;hi2c1);
        SHT2x_SetResolution(RES_14_12);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
    /* USER CODE END WHILE */
   
                /* Gets current temperature &amp; relative humidity. */
                float cel = SHT2x_GetTemperature(1);
                /* Converts temperature to degrees Fahrenheit and Kelvin */
                float fah = SHT2x_CelsiusToFahrenheit(cel);
                float kel = SHT2x_CelsiusToKelvin(cel);
                float rh = SHT2x_GetRelativeHumidity(1);
                /* May show warning below. Ignore and proceed. */
                sprintf(buffer,"%d.%dC, %d.%dF, %d.%d K, %d.%d%% RH\n",
                SHT2x_GetInteger(cel), SHT2x_GetDecimal(cel, 1),
                SHT2x_GetInteger(fah), SHT2x_GetDecimal(fah, 1),
                SHT2x_GetInteger(kel), SHT2x_GetDecimal(kel, 1),
                SHT2x_GetInteger(rh), SHT2x_GetDecimal(rh, 1));
                HAL_UART_Transmit(&amp;huart1, buffer, strlen(buffer), 1000);
                HAL_Delay(1000);
    /* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}

/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
{
    Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
RCC_OscInitStruct.PLL.PLLM = 1;
RCC_OscInitStruct.PLL.PLLN = 10;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 1;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;
RCC_OscInitStruct.PLL.PLLFRACN = 0;
if (HAL_RCC_OscConfig(&amp;RCC_OscInitStruct) != HAL_OK)
{
    Error_Handler();
}

/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&amp;RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
    Error_Handler();
}
}

/**
* @brief Power Configuration
* @retval None
*/
static void SystemPower_Config(void)
{

/*
   * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
   */
HAL_PWREx_DisableUCPDDeadBattery();

/*
   * Switch to SMPS regulator instead of LDO
   */
if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN PWR */
/* USER CODE END PWR */
}

/**
* @brief GPDMA1 Initialization Function
* @param None
* @retval None
*/
static void MX_GPDMA1_Init(void)
{

/* USER CODE BEGIN GPDMA1_Init 0 */

/* USER CODE END GPDMA1_Init 0 */

/* Peripheral clock enable */
__HAL_RCC_GPDMA1_CLK_ENABLE();

/* GPDMA1 interrupt Init */
    HAL_NVIC_SetPriority(GPDMA1_Channel0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(GPDMA1_Channel0_IRQn);

/* USER CODE BEGIN GPDMA1_Init 1 */

/* USER CODE END GPDMA1_Init 1 */
/* USER CODE BEGIN GPDMA1_Init 2 */

/* USER CODE END GPDMA1_Init 2 */

}

/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{

/* USER CODE BEGIN I2C1_Init 0 */

/* USER CODE END I2C1_Init 0 */

/* USER CODE BEGIN I2C1_Init 1 */

/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x30909DEC;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&amp;hi2c1) != HAL_OK)
{
    Error_Handler();
}

/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&amp;hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
    Error_Handler();
}

/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&amp;hi2c1, 0) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */

/* USER CODE END I2C1_Init 2 */

}

/**
* @brief ICACHE Initialization Function
* @param None
* @retval None
*/
static void MX_ICACHE_Init(void)
{

/* USER CODE BEGIN ICACHE_Init 0 */

/* USER CODE END ICACHE_Init 0 */

/* USER CODE BEGIN ICACHE_Init 1 */

/* USER CODE END ICACHE_Init 1 */

/** Enable instruction cache in 1-way (direct mapped cache)
*/
if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK)
{
    Error_Handler();
}
if (HAL_ICACHE_Enable() != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN ICACHE_Init 2 */

/* USER CODE END ICACHE_Init 2 */

}

/**
* @brief LPDMA1 Initialization Function
* @param None
* @retval None
*/
static void MX_LPDMA1_Init(void)
{

/* USER CODE BEGIN LPDMA1_Init 0 */

/* USER CODE END LPDMA1_Init 0 */

/* Peripheral clock enable */
__HAL_RCC_LPDMA1_CLK_ENABLE();

/* LPDMA1 interrupt Init */
    HAL_NVIC_SetPriority(LPDMA1_Channel0_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(LPDMA1_Channel0_IRQn);

/* USER CODE BEGIN LPDMA1_Init 1 */

/* USER CODE END LPDMA1_Init 1 */
/* USER CODE BEGIN LPDMA1_Init 2 */

/* USER CODE END LPDMA1_Init 2 */

}

/**
* @brief LPUART1 Initialization Function
* @param None
* @retval None
*/
static void MX_LPUART1_UART_Init(void)
{

/* USER CODE BEGIN LPUART1_Init 0 */

/* USER CODE END LPUART1_Init 0 */

/* USER CODE BEGIN LPUART1_Init 1 */

/* USER CODE END LPUART1_Init 1 */
hlpuart1.Instance = LPUART1;
hlpuart1.Init.BaudRate = 115200;
hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
hlpuart1.Init.StopBits = UART_STOPBITS_1;
hlpuart1.Init.Parity = UART_PARITY_NONE;
hlpuart1.Init.Mode = UART_MODE_TX_RX;
hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;
if (HAL_UART_Init(&amp;hlpuart1) != HAL_OK)
{
    Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&amp;hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
    Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&amp;hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
    Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&amp;hlpuart1) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN LPUART1_Init 2 */

/* USER CODE END LPUART1_Init 2 */

}

/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{

/* USER CODE BEGIN USART1_Init 0 */

/* USER CODE END USART1_Init 0 */

/* USER CODE BEGIN USART1_Init 1 */

/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&amp;huart1) != HAL_OK)
{
    Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&amp;huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
    Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&amp;huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
    Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&amp;huart1) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */

/* USER CODE END USART1_Init 2 */

}

/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{

/* USER CODE BEGIN TIM2_Init 0 */

/* USER CODE END TIM2_Init 0 */

TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};

/* USER CODE BEGIN TIM2_Init 1 */

/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 159;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 2000000;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&amp;htim2) != HAL_OK)
{
    Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&amp;htim2, &amp;sMasterConfig) != HAL_OK)
{
    Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 1000000;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&amp;htim2, &amp;sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */

/* USER CODE END TIM2_Init 2 */
HAL_TIM_MspPostInit(&amp;htim2);

}

/**
* @brief TIM8 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM8_Init(void)
{

/* USER CODE BEGIN TIM8_Init 0 */

/* USER CODE END TIM8_Init 0 */

TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

/* USER CODE BEGIN TIM8_Init 1 */

/* USER CODE END TIM8_Init 1 */
htim8.Instance = TIM8;
htim8.Init.Prescaler = 159;
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
htim8.Init.Period = 1000;
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim8.Init.RepetitionCounter = 0;
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&amp;htim8) != HAL_OK)
{
    Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&amp;htim8, &amp;sMasterConfig) != HAL_OK)
{
    Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 500;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
if (HAL_TIM_PWM_ConfigChannel(&amp;htim8, &amp;sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
    Error_Handler();
}
sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
sBreakDeadTimeConfig.DeadTime = 0;
sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
sBreakDeadTimeConfig.BreakFilter = 0;
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
sBreakDeadTimeConfig.Break2Filter = 0;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
if (HAL_TIMEx_ConfigBreakDeadTime(&amp;htim8, &amp;sBreakDeadTimeConfig) != HAL_OK)
{
    Error_Handler();
}
/* USER CODE BEGIN TIM8_Init 2 */

/* USER CODE END TIM8_Init 2 */
HAL_TIM_MspPostInit(&amp;htim8);

}

/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
* @briefThis function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}

#ifdefUSE_FULL_ASSERT
/**
* @briefReports the name of the source file and the source line number
*         where the assert_param error has occurred.
* @paramfile: pointer to the source file name
* @paramline: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
</code></pre>

<p>main.c程序是驱动的测试程序。</p>

<p>3、程序运行如下:</p>

<p> &nbsp;</p>

<p>程序输出主要有三种温度显示和温湿度值显示。</p>

<p> &nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp;</p>
页: [1]
查看完整版本: 【STM32U5A5ZJ开发板】I2C总线设备的兼容性测试及SHT20温湿度传感器测试