查看: 1113|回复: 0

[项目提交] 《2023DigiKey汽车应用创意挑战赛》---驱动OLED显示屏

[复制链接]
  • TA的每日心情
    开心
    5 小时前
  • 签到天数: 952 天

    连续签到: 68 天

    [LV.10]以坛为家III

    发表于 2023-12-3 22:27:23 | 显示全部楼层 |阅读模式
    分享到:

    作为显示部件,本来是打算使用I2C接口的OLED,[size=12.0000pt]和板载的传感器使用相同的接口。它们基本上都是用PH4作为SCL,PH5作为SDA,使用I2C接口协议。为此查看PH4和PH5是否对外开放了插头座之类的接口。可以看到在CN2中已经开放了,在第7脚和第10脚刚好就是对应PH4和PH5。
    图片4.png
    CN2和CN3都是2*10的2.0mm输出插座。为了方便连接OLED,买了转接板,将2.0mm转为2.54mm间距,方便用杜邦线连接。
    转接板.jpg
    为此简单修改下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的代码,其它代码就不粘贴了,后面附上整个工程的压缩包。
    1. /* USER CODE BEGIN Header */
    2. /**
    3. ******************************************************************************
    4. * @file    GPIO/GPIO_IOToggle/Src/main.c
    5. * @author  MCD Application Team
    6. * @brief   This example describes how to configure and use GPIOs through
    7. *          the STM32U5xx HAL API.
    8. ******************************************************************************
    9. * @attention
    10. *
    11. * Copyright (c) 2021 STMicroelectronics.
    12. * All rights reserved.
    13. *
    14. * This software is licensed under terms that can be found in the LICENSE file
    15. * in the root directory of this software component.
    16. * If no LICENSE file comes with this software, it is provided AS-IS.
    17. *
    18. ******************************************************************************
    19. */
    20. /* USER CODE END Header */
    21. /* Includes ------------------------------------------------------------------*/
    22. #include "main.h"

    23. /* Private includes ----------------------------------------------------------*/
    24. /* USER CODE BEGIN Includes */
    25. #include "gui.h"
    26. #include "bmp.h"
    27. /* USER CODE END Includes */

    28. /* Private typedef -----------------------------------------------------------*/
    29. /* USER CODE BEGIN PTD */

    30. /* USER CODE END PTD */

    31. /* Private define ------------------------------------------------------------*/
    32. /* USER CODE BEGIN PD */

    33. /* USER CODE END PD */

    34. /* Private macro -------------------------------------------------------------*/
    35. /* USER CODE BEGIN PM */

    36. /* USER CODE END PM */

    37. /* Private variables ---------------------------------------------------------*/

    38. /* USER CODE BEGIN PV */
    39. static GPIO_InitTypeDef GPIO_InitStruct;

    40. /* USER CODE END PV */

    41. /* Private function prototypes -----------------------------------------------*/
    42. void SystemClock_Config(void);
    43. static void SystemPower_Config(void);
    44. static void MX_ICACHE_Init(void);
    45. /* USER CODE BEGIN PFP */

    46. /* USER CODE END PFP */

    47. /* Private user code ---------------------------------------------------------*/
    48. /* USER CODE BEGIN 0 */
    49. void TEST_Para(void) {
    50.         uint8_t i;
    51.         srand(123456789);
    52.         GUI_ShowString(0,0, (unsigned char *)"Para1:",8,1);
    53.         GUI_ShowString(64,0,(unsigned char *)"8888",8,1);

    54.         GUI_ShowString(0,8,(unsigned char *)"Para2:",8,1);
    55.         GUI_ShowString(64,8,(unsigned char *)"888",8,1);

    56.         GUI_ShowString(0,16,(unsigned char *)"Para3:",8,1);
    57.         GUI_ShowString(64,16,(unsigned char *)"888",8,1);

    58.         GUI_ShowString(0,24,(unsigned char *)"Para4:",8,1);
    59.         GUI_ShowString(64,24,(unsigned char *)"888",8,1);

    60.         for(i=0;i<15;i++) {
    61.                 GUI_ShowNum(64,0,rand()%10+1,1,8,1);
    62.                 GUI_ShowNum(72,0,rand()%10,1,8,1);
    63.                 GUI_ShowNum(80,0,rand()%10,1,8,1);
    64.                 GUI_ShowNum(88,0,rand()%10,1,8,1);

    65.                 GUI_ShowNum(64,8,rand()%10+1,1,8,1);
    66.                 GUI_ShowNum(72,8,rand()%10,1,8,1);
    67.                 GUI_ShowNum(80,8,rand()%10,1,8,1);

    68.                 GUI_ShowNum(64,16,rand()%10+1,1,8,1);
    69.                 GUI_ShowNum(72,16,rand()%10,1,8,1);
    70.                 GUI_ShowNum(80,16,rand()%10,1,8,1);

    71.                 GUI_ShowNum(64,24,rand()%10+1,1,8,1);
    72.                 GUI_ShowNum(72,24,rand()%10,1,8,1);
    73.                 GUI_ShowNum(80,24,rand()%10,1,8,1);

    74.                 HAL_Delay(500);
    75.         }
    76. }
    77. /* USER CODE END 0 */


    78. /**
    79. * @brief  The application entry point.
    80. * @retval int
    81. */
    82. int main(void) {
    83.         /* USER CODE BEGIN 1 */
    84.         /* STM32U5xx HAL library initialization:
    85.          - Configure the Flash prefetch
    86.          - Configure the Systick to generate an interrupt each 1 msec
    87.          - Set NVIC Group Priority to 3
    88.          - Low Level Initialization
    89.          */
    90.         /* USER CODE END 1 */

    91.         /* MCU Configuration--------------------------------------------------------*/

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

    94.         /* USER CODE BEGIN Init */

    95.         /* USER CODE END Init */

    96.         /* Configure the system clock */
    97.         SystemClock_Config();

    98.         /* Configure the System Power */
    99.         SystemPower_Config();

    100.         /* USER CODE BEGIN SysInit */

    101.         /* USER CODE END SysInit */

    102.         /* Initialize all configured peripherals */
    103.         MX_ICACHE_Init();
    104.         /* USER CODE BEGIN 2 */

    105.         /* -1- Enable GPIO Clock (to be able to program the configuration registers) */
    106.         LED7_GPIO_CLK_ENABLE();
    107.         LED6_GPIO_CLK_ENABLE();

    108.         /* -2- Configure IO in output push-pull mode to drive external LEDs */
    109.         GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    110.         GPIO_InitStruct.Pull = GPIO_NOPULL;
    111.         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    112.         GPIO_InitStruct.Pin = LED7_PIN;
    113.         HAL_GPIO_Init(LED7_GPIO_PORT, &GPIO_InitStruct);
    114.         GPIO_InitStruct.Pin = LED6_PIN;
    115.         HAL_GPIO_Init(LED6_GPIO_PORT, &GPIO_InitStruct);

    116.         GPIO_InitStruct.Pin = I2C_SCL_PIN | I2C_SDA_PIN;
    117.         HAL_GPIO_Init(I2C_GPIO_PORT, &GPIO_InitStruct);

    118.         OLED_Init();                //初始化OLED
    119.         OLED_Clear(0);      //清屏(全黑)

    120.         TEST_Menu();
    121.         /* USER CODE END 2 */

    122.         /* Infinite loop */
    123.         /* USER CODE BEGIN WHILE */
    124.         while (1) {
    125.                 /* USER CODE END WHILE */

    126.                 /* USER CODE BEGIN 3 */
    127.                 //HAL_GPIO_TogglePin(LED7_GPIO_PORT, LED7_PIN);
    128.                 HAL_GPIO_WritePin(LED7_GPIO_PORT, LED7_PIN, GPIO_PIN_SET);
    129.                 HAL_GPIO_WritePin(LED7_GPIO_PORT, LED6_PIN, GPIO_PIN_RESET);



    130.                 HAL_Delay(500);
    131.                 //HAL_GPIO_TogglePin(LED6_GPIO_PORT, LED6_PIN);
    132.                 HAL_GPIO_WritePin(LED7_GPIO_PORT, LED6_PIN, GPIO_PIN_SET);
    133.                 HAL_GPIO_WritePin(LED7_GPIO_PORT, LED7_PIN, GPIO_PIN_RESET);



    134.                 /* Insert delay 100 ms */
    135.                 HAL_Delay(500);

    136.                 TEST_Para();
    137.                 /* USER CODE END 3 */

    138.         }
    139. }

    140. /**
    141. * @brief System Clock Configuration
    142. * @retval None
    143. */
    144. void SystemClock_Config(void) {
    145.         RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
    146.         RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };

    147.         /** Configure the main internal regulator output voltage
    148.          */
    149.         if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1)
    150.                         != HAL_OK) {
    151.                 Error_Handler();
    152.         }

    153.         /** Initializes the CPU, AHB and APB buses clocks
    154.          */
    155.         RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
    156.         RCC_OscInitStruct.MSIState = RCC_MSI_ON;
    157.         RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
    158.         RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_4;
    159.         RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    160.         RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI;
    161.         RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
    162.         RCC_OscInitStruct.PLL.PLLM = 1;
    163.         RCC_OscInitStruct.PLL.PLLN = 80;
    164.         RCC_OscInitStruct.PLL.PLLP = 2;
    165.         RCC_OscInitStruct.PLL.PLLQ = 2;
    166.         RCC_OscInitStruct.PLL.PLLR = 2;
    167.         RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_0;
    168.         RCC_OscInitStruct.PLL.PLLFRACN = 0;
    169.         if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
    170.                 Error_Handler();
    171.         }

    172.         /** Initializes the CPU, AHB and APB buses clocks
    173.          */
    174.         RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
    175.                         | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_PCLK3;
    176.         RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    177.         RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    178.         RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    179.         RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    180.         RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

    181.         if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK) {
    182.                 Error_Handler();
    183.         }
    184. }

    185. /**
    186. * @brief Power Configuration
    187. * @retval None
    188. */
    189. static void SystemPower_Config(void) {

    190.         /*
    191.          * Disable the internal Pull-Up in Dead Battery pins of UCPD peripheral
    192.          */
    193.         HAL_PWREx_DisableUCPDDeadBattery();

    194.         /*
    195.          * Switch to SMPS regulator instead of LDO
    196.          */
    197.         if (HAL_PWREx_ConfigSupply(PWR_SMPS_SUPPLY) != HAL_OK) {
    198.                 Error_Handler();
    199.         }
    200.         /* USER CODE BEGIN PWR */
    201.         /* USER CODE END PWR */
    202. }

    203. /**
    204. * @brief ICACHE Initialization Function
    205. * @param None
    206. * @retval None
    207. */
    208. static void MX_ICACHE_Init(void) {

    209.         /* USER CODE BEGIN ICACHE_Init 0 */

    210.         /* USER CODE END ICACHE_Init 0 */

    211.         /* USER CODE BEGIN ICACHE_Init 1 */

    212.         /* USER CODE END ICACHE_Init 1 */

    213.         /** Enable instruction cache in 1-way (direct mapped cache)
    214.          */
    215.         if (HAL_ICACHE_ConfigAssociativityMode(ICACHE_1WAY) != HAL_OK) {
    216.                 Error_Handler();
    217.         }
    218.         if (HAL_ICACHE_Enable() != HAL_OK) {
    219.                 Error_Handler();
    220.         }
    221.         /* USER CODE BEGIN ICACHE_Init 2 */

    222.         /* USER CODE END ICACHE_Init 2 */

    223. }

    224. /* USER CODE BEGIN 4 */
    225. // 初始化GPIO口用于板载传感器的接口,PH4(SCK),PH5(SDA)
    226. void Gpio_Init_I2c_ForSensor(void) {

    227. }

    228. /**
    229. * 读取I2C设备的数据
    230. */
    231. uint32_t Read_i2c_sensor(uint8_t devAddr, uint8_t cmd, uint8_t dat) {


    232. }

    233. /* USER CODE END 4 */

    234. /**
    235. * @brief  This function is executed in case of error occurrence.
    236. * @retval None
    237. */
    238. void Error_Handler(void) {
    239.         /* USER CODE BEGIN Error_Handler_Debug */
    240.         /* User can add his own implementation to report the HAL error return state */
    241.         while (1) {
    242.         }
    243.         /* USER CODE END Error_Handler_Debug */
    244. }

    245. #ifdef  USE_FULL_ASSERT
    246. /**
    247.   * @brief  Reports the name of the source file and the source line number
    248.   *         where the assert_param error has occurred.
    249.   * @param  file: pointer to the source file name
    250.   * @param  line: assert_param error line source number
    251.   * @retval None
    252.   */
    253. void assert_failed(uint8_t *file, uint32_t line)
    254. {
    255.   /* USER CODE BEGIN 6 */
    256.   /* User can add his own implementation to report the file name and line number,
    257.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    258.   /* Infinite loop */
    259.   while (1)
    260.   {
    261.   }

    262.   /* USER CODE END 6 */
    263. }
    264. #endif /* USE_FULL_ASSERT */
    复制代码
    终于调试通了OLED的显示,接下来是对板载的一些传感器的数据读取。
    demo.jpg
    工程包: GPIO_IOToggle.zip (14.04 MB, 下载次数: 0)
    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-11-23 15:40 , Processed in 0.120856 second(s), 17 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.