找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3710|回复: 1
打印 上一主题 下一主题
收起左侧

分享一个HAL库STM32L151IIC的LCD1602代码

[复制链接]
跳转到指定楼层
楼主
ID:356403 发表于 2019-9-10 21:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2019 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */

  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"

  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "i2c-lcd.h"
  25. /* USER CODE END Includes */

  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */

  28. /* USER CODE END PTD */

  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */

  31. /* USER CODE END PD */

  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */

  34. /* USER CODE END PM */

  35. /* Private variables ---------------------------------------------------------*/
  36. I2C_HandleTypeDef hi2c1;

  37. UART_HandleTypeDef huart1;

  38. /* USER CODE BEGIN PV */

  39. /* USER CODE END PV */

  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. static void MX_I2C1_Init(void);
  44. static void MX_USART1_UART_Init(void);
  45. /* USER CODE BEGIN PFP */

  46. /* USER CODE END PFP */

  47. /* Private user code ---------------------------------------------------------*/
  48. /* USER CODE BEGIN 0 */


  49. /* USER CODE END 0 */

  50. /**
  51.   * @brief  The application entry point.
  52.   * @retval int
  53.   */
  54. int main(void)
  55. {
  56.   /* USER CODE BEGIN 1 */

  57.   /* USER CODE END 1 */
  58.   

  59.   /* MCU Configuration--------------------------------------------------------*/

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

  62.   /* USER CODE BEGIN Init */

  63.   /* USER CODE END Init */

  64.   /* Configure the system clock */
  65.   SystemClock_Config();

  66.   /* USER CODE BEGIN SysInit */

  67.   /* USER CODE END SysInit */

  68.   /* Initialize all configured peripherals */
  69.   MX_GPIO_Init();
  70.   MX_I2C1_Init();
  71.   MX_USART1_UART_Init();

  72.   /* USER CODE BEGIN 2 */
  73.   lcd_init();




  74.   /* USER CODE END 2 */

  75.   /* Infinite loop */
  76.   /* USER CODE BEGIN WHILE */
  77.   while (1)
  78.   {               

  79.     /* USER CODE END WHILE */
  80.                 lcd_send_cmd (0x80|0x00);
  81.                 lcd_send_string("HELLO WORLD");
  82.                
  83.                 HAL_Delay(1000);
  84.                 lcd_send_cmd (0x80|0x40);
  85.                 lcd_send_string("STM32");
  86.                 HAL_Delay(1000);
  87.                 lcd_send_cmd (0x01);
  88.                 HAL_Delay(1000);
  89.     /* USER CODE BEGIN 3 */
  90.   }
  91.   /* USER CODE END 3 */
  92. }

  93. /**
  94.   * @brief System Clock Configuration
  95.   * @retval None
  96.   */
  97. void SystemClock_Config(void)
  98. {
  99.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  100.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  101.   /** Configure the main internal regulator output voltage
  102.   */
  103.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  104.   /** Initializes the CPU, AHB and APB busses clocks
  105.   */
  106.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  107.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  108.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  109.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  110.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
  111.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLL_DIV3;
  112.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  113.   {
  114.     Error_Handler();
  115.   }
  116.   /** Initializes the CPU, AHB and APB busses clocks
  117.   */
  118.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  119.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  120.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  121.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  122.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  123.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

  124.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  125.   {
  126.     Error_Handler();
  127.   }
  128. }

  129. /**
  130.   * @brief I2C1 Initialization Function
  131.   * @param None
  132.   * @retval None
  133.   */
  134. static void MX_I2C1_Init(void)
  135. {

  136.   /* USER CODE BEGIN I2C1_Init 0 */

  137.   /* USER CODE END I2C1_Init 0 */

  138.   /* USER CODE BEGIN I2C1_Init 1 */

  139.   /* USER CODE END I2C1_Init 1 */
  140.   hi2c1.Instance = I2C1;
  141.   hi2c1.Init.ClockSpeed = 100000;
  142.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  143.   hi2c1.Init.OwnAddress1 = 0;
  144.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  145.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  146.   hi2c1.Init.OwnAddress2 = 0;
  147.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  148.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  149.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  150.   {
  151.     Error_Handler();
  152.   }
  153.   /* USER CODE BEGIN I2C1_Init 2 */

  154.   /* USER CODE END I2C1_Init 2 */

  155. }

  156. /**
  157.   * @brief USART1 Initialization Function
  158.   * @param None
  159.   * @retval None
  160.   */
  161. static void MX_USART1_UART_Init(void)
  162. {

  163.   /* USER CODE BEGIN USART1_Init 0 */

  164.   /* USER CODE END USART1_Init 0 */

  165.   /* USER CODE BEGIN USART1_Init 1 */

  166.   /* USER CODE END USART1_Init 1 */
  167.   huart1.Instance = USART1;
  168.   huart1.Init.BaudRate = 115200;
  169.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  170.   huart1.Init.StopBits = UART_STOPBITS_1;
  171.   huart1.Init.Parity = UART_PARITY_NONE;
  172.   huart1.Init.Mode = UART_MODE_TX_RX;
  173.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  174.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  175.   if (HAL_UART_Init(&huart1) != HAL_OK)
  176.   {
  177.     Error_Handler();
  178.   }
  179.   /* USER CODE BEGIN USART1_Init 2 */

  180.   /* USER CODE END USART1_Init 2 */

  181. }

  182. /**
  183.   * @brief GPIO Initialization Function
  184.   * @param None
  185.   * @retval None
  186.   */
  187. static void MX_GPIO_Init(void)
  188. {
  189.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  190.   /* GPIO Ports Clock Enable */
  191.   __HAL_RCC_GPIOH_CLK_ENABLE();
  192.   __HAL_RCC_GPIOA_CLK_ENABLE();
  193.   __HAL_RCC_GPIOB_CLK_ENABLE();

  194.   /*Configure GPIO pin Output Level */
  195.   HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

  196.   /*Configure GPIO pin : LED_Pin */
  197.   GPIO_InitStruct.Pin = LED_Pin;
  198.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  199.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  200.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  201.   HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);

  202. }

  203. /* USER CODE BEGIN 4 */

  204. /* USER CODE END 4 */

  205. /**
  206.   * @brief  This function is executed in case of error occurrence.
  207.   * @retval None
  208.   */
  209. void Error_Handler(void)
  210. {
  211.   /* USER CODE BEGIN Error_Handler_Debug */
  212.   /* User can add his own implementation to report the HAL error return state */

  213.   /* USER CODE END Error_Handler_Debug */
  214. }

  215. #ifdef  USE_FULL_ASSERT
  216. /**
  217.   * @brief  Reports the name of the source file and the source line number
  218.   *         where the assert_param error has occurred.
  219.   * @param  file: pointer to the source file name
  220.   * @param  line: assert_param error line source number
  221.   * @retval None
  222.   */
  223. void assert_failed(uint8_t *file, uint32_t line)
  224. {
  225.   /* USER CODE BEGIN 6 */
  226.   /* User can add his own implementation to report the file name and line number,
  227.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  228.   /* USER CODE END 6 */
  229. }
  230. #endif /* USE_FULL_ASSERT */

  231. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

LCD1602代码

LCDTEST.7z

5.26 MB, 下载次数: 72, 下载积分: 黑币 -5

STM32 LCD 1602 HAL

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

沙发
ID:473768 发表于 2020-2-24 10:30 | 只看该作者
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表