TA的每日心情 | 开心 5 小时前 |
---|
签到天数: 952 天 连续签到: 68 天 [LV.10]以坛为家III
|
作为显示部件,本来是打算使用I2C接口的OLED,[size=12.0000pt]和板载的传感器使用相同的接口。它们基本上都是用PH4作为SCL,PH5作为SDA,使用I2C接口协议。为此查看PH4和PH5是否对外开放了插头座之类的接口。可以看到在CN2中已经开放了,在第7脚和第10脚刚好就是对应PH4和PH5。
CN2和CN3都是2*10的2.0mm输出插座。为了方便连接OLED,买了转接板,将2.0mm转为2.54mm间距,方便用杜邦线连接。
为此简单修改下GPIO_TOGGLE程序,测试PH4和PH5的输出。
[size=12.0000pt]在原来的程序基础上,
[size=12.0000pt]1、在main.h中加入以下代码:
[size=10.0000pt]#define I2C_GPIO_PORT GPIOH
[size=10.0000pt]#define I2C_GPIO_CLK_ENABLE() __HAL_RCC_GPIOH_CLK_ENABLE()
[size=10.0000pt]#define I2C_SCK_GPIO_CLK_DISABLE() __HAL_RCC_GPIOH_CLK_DISABLE()
[size=10.0000pt]#define I2C_SCL_PIN GPIO_PIN_4
[size=10.0000pt]#define I2C_SDA_PIN GPIO_PIN_5
[size=12.0000pt]2、在主程序main中加入以下代码:
[size=10.0000pt]// 允许使用PH4和PH5作为I2C的SCL和SDA。因为同样使用PH口,所以不再设置Mode,Pull,Speed
GPIO_InitStruct.[size=10.0000pt]Pin = I2C_SCL_PIN | I2C_SDA_PIN;
[size=10.0000pt]HAL_GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStruct);
[size=12.0000pt]运行能看到PH4和PH5的正常输出。至此完成 GPIO口的的设置。下面是加入OLED驱动的相关代码。是以软件模拟方式操作的(虽然PH4和PH5是可以复用为I2C2的SCL和SDA,但是用I2C外设方式操作I2C从设备,实在是很麻烦)。由于OLED的电压是3.3V,而CN2和CN3并没有这个电压的输出,幸好这个我开发板的反面提供了Arduino的输出支持,其中有3.3V可用。
以下是main.c的代码,其它代码就不粘贴了,后面附上整个工程的压缩包。
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file GPIO/GPIO_IOToggle/Src/main.c
- * @author MCD Application Team
- * @brief This example describes how to configure and use GPIOs through
- * the STM32U5xx HAL API.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2021 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 "gui.h"
- #include "bmp.h"
- /* 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 ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- static GPIO_InitTypeDef GPIO_InitStruct;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void SystemPower_Config(void);
- static void MX_ICACHE_Init(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- void TEST_Para(void) {
- uint8_t i;
- srand(123456789);
- GUI_ShowString(0,0, (unsigned char *)"Para1:",8,1);
- GUI_ShowString(64,0,(unsigned char *)"8888",8,1);
- GUI_ShowString(0,8,(unsigned char *)"Para2:",8,1);
- GUI_ShowString(64,8,(unsigned char *)"888",8,1);
- GUI_ShowString(0,16,(unsigned char *)"Para3:",8,1);
- GUI_ShowString(64,16,(unsigned char *)"888",8,1);
- GUI_ShowString(0,24,(unsigned char *)"Para4:",8,1);
- GUI_ShowString(64,24,(unsigned char *)"888",8,1);
- for(i=0;i<15;i++) {
- GUI_ShowNum(64,0,rand()%10+1,1,8,1);
- GUI_ShowNum(72,0,rand()%10,1,8,1);
- GUI_ShowNum(80,0,rand()%10,1,8,1);
- GUI_ShowNum(88,0,rand()%10,1,8,1);
- GUI_ShowNum(64,8,rand()%10+1,1,8,1);
- GUI_ShowNum(72,8,rand()%10,1,8,1);
- GUI_ShowNum(80,8,rand()%10,1,8,1);
- GUI_ShowNum(64,16,rand()%10+1,1,8,1);
- GUI_ShowNum(72,16,rand()%10,1,8,1);
- GUI_ShowNum(80,16,rand()%10,1,8,1);
- GUI_ShowNum(64,24,rand()%10+1,1,8,1);
- GUI_ShowNum(72,24,rand()%10,1,8,1);
- GUI_ShowNum(80,24,rand()%10,1,8,1);
- HAL_Delay(500);
- }
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void) {
- /* USER CODE BEGIN 1 */
- /* STM32U5xx HAL library initialization:
- - Configure the Flash prefetch
- - Configure the Systick to generate an interrupt each 1 msec
- - Set NVIC Group Priority to 3
- - Low Level Initialization
- */
- /* 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_ICACHE_Init();
- /* USER CODE BEGIN 2 */
- /* -1- Enable GPIO Clock (to be able to program the configuration registers) */
- LED7_GPIO_CLK_ENABLE();
- LED6_GPIO_CLK_ENABLE();
- /* -2- Configure IO in output push-pull mode to drive external LEDs */
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
- GPIO_InitStruct.Pin = LED7_PIN;
- HAL_GPIO_Init(LED7_GPIO_PORT, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = LED6_PIN;
- HAL_GPIO_Init(LED6_GPIO_PORT, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = I2C_SCL_PIN | I2C_SDA_PIN;
- HAL_GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStruct);
- OLED_Init(); //初始化OLED
- OLED_Clear(0); //清屏(全黑)
- TEST_Menu();
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1) {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- //HAL_GPIO_TogglePin(LED7_GPIO_PORT, LED7_PIN);
- HAL_GPIO_WritePin(LED7_GPIO_PORT, LED7_PIN, GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED7_GPIO_PORT, LED6_PIN, GPIO_PIN_RESET);
- HAL_Delay(500);
- //HAL_GPIO_TogglePin(LED6_GPIO_PORT, LED6_PIN);
- HAL_GPIO_WritePin(LED7_GPIO_PORT, LED6_PIN, GPIO_PIN_SET);
- HAL_GPIO_WritePin(LED7_GPIO_PORT, LED7_PIN, GPIO_PIN_RESET);
- /* Insert delay 100 ms */
- HAL_Delay(500);
- TEST_Para();
- /* 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_MSI;
- RCC_OscInitStruct.MSIState = RCC_MSI_ON;
- RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
- RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
- RCC_OscInitStruct.PLL.PLLM = 1;
- RCC_OscInitStruct.PLL.PLLN = 80;
- RCC_OscInitStruct.PLL.PLLP = 2;
- RCC_OscInitStruct.PLL.PLLQ = 2;
- RCC_OscInitStruct.PLL.PLLR = 2;
- RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
- RCC_OscInitStruct.PLL.PLLFRACN = 0;
- if (HAL_RCC_OscConfig(&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(&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 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 */
- }
- /* USER CODE BEGIN 4 */
- // 初始化GPIO口用于板载传感器的接口,PH4(SCK),PH5(SDA)
- void Gpio_Init_I2c_ForSensor(void) {
- }
- /**
- * 读取I2C设备的数据
- */
- uint32_t Read_i2c_sensor(uint8_t devAddr, uint8_t cmd, uint8_t dat) {
- }
- /* USER CODE END 4 */
- /**
- * @brief This 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 */
- while (1) {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: 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) */
- /* Infinite loop */
- while (1)
- {
- }
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
复制代码 终于调试通了OLED的显示,接下来是对板载的一些传感器的数据读取。
工程包:
GPIO_IOToggle.zip
(14.04 MB, 下载次数: 0)
|
|