Advertisement
Guest User

Untitled

a guest
Sep 16th, 2021
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.95 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2021 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.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include <stdio.h> /* for printf */
  26. /* USER CODE END Includes */
  27.  
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30.  
  31. /* USER CODE END PTD */
  32.  
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36.  
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39.  
  40. /* USER CODE END PM */
  41.  
  42. /* Private variables ---------------------------------------------------------*/
  43. I2C_HandleTypeDef hi2c1;
  44. DMA_HandleTypeDef hdma_i2c1_rx;
  45. DMA_HandleTypeDef hdma_i2c1_tx;
  46.  
  47. /* USER CODE BEGIN PV */
  48.  
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. static void MX_GPIO_Init(void);
  54. static void MX_I2C1_Init(void);
  55. static void MX_DMA_Init(void);
  56. /* USER CODE BEGIN PFP */
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62.  
  63. /* USER CODE END 0 */
  64.  
  65. /**
  66.   * @brief  The application entry point.
  67.   * @retval int
  68.   */
  69. int main(void)
  70. {
  71.   /* USER CODE BEGIN 1 */
  72.  
  73.   /* USER CODE END 1 */
  74.  
  75.   /* MCU Configuration--------------------------------------------------------*/
  76.  
  77.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78.   HAL_Init();
  79.  
  80.   /* USER CODE BEGIN Init */
  81.  
  82.   /* USER CODE END Init */
  83.  
  84.   /* Configure the system clock */
  85.   SystemClock_Config();
  86.  
  87.   /* USER CODE BEGIN SysInit */
  88.  
  89.   /* USER CODE END SysInit */
  90.  
  91.   /* Initialize all configured peripherals */
  92.   MX_GPIO_Init();
  93.   MX_I2C1_Init();
  94.   MX_DMA_Init();
  95.   /* USER CODE BEGIN 2 */
  96.   /* --------------------------------------------------------*/
  97.   extern I2C_HandleTypeDef hi2c1;  // change your handler here accordingly
  98.  
  99.   #define SLAVE_ADDRESS_LCD 0x4E // change this according to ur setup
  100.  
  101.   void lcd_send_cmd (char cmd)
  102.   {
  103.     char data_u, data_l;
  104.     uint8_t data_t[4];
  105.     data_u = (cmd&0xf0);
  106.     data_l = ((cmd<<4)&0xf0);
  107.     data_t[0] = data_u|0x0C;  //en=1, rs=0
  108.     data_t[1] = data_u|0x08;  //en=0, rs=0
  109.     data_t[2] = data_l|0x0C;  //en=1, rs=0
  110.     data_t[3] = data_l|0x08;  //en=0, rs=0
  111.     //HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
  112.     HAL_I2C_Master_Transmit_DMA(&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4);
  113.   }
  114.  
  115.   void lcd_send_data (char data)
  116.   {
  117.     char data_u, data_l;
  118.     uint8_t data_t[4];
  119.     data_u = (data&0xf0);
  120.     data_l = ((data<<4)&0xf0);
  121.     data_t[0] = data_u|0x0D;  //en=1, rs=0
  122.     data_t[1] = data_u|0x09;  //en=0, rs=0
  123.     data_t[2] = data_l|0x0D;  //en=1, rs=0
  124.     data_t[3] = data_l|0x09;  //en=0, rs=0
  125.     //HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
  126.     HAL_I2C_Master_Transmit_DMA(&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4);
  127.   }
  128.  
  129.   void lcd_clear (void)
  130.   {
  131.     lcd_send_cmd (0x80);
  132.     for (int i=0; i<70; i++)
  133.     {
  134.         lcd_send_data (' ');
  135.     }
  136.   }
  137.  
  138.   void lcd_put_cur(int row, int col)
  139.   {
  140.       switch (row)
  141.       {
  142.           case 0:
  143.               col |= 0x80;
  144.               break;
  145.           case 1:
  146.               col |= 0xC0;
  147.               break;
  148.       }
  149.  
  150.       lcd_send_cmd (col);
  151.   }
  152.  
  153.  
  154.   void lcd_init (void)
  155.   {
  156.     // 4 bit initialisation
  157.     HAL_Delay(50);  // wait for >40ms
  158.     lcd_send_cmd (0x30);
  159.     HAL_Delay(5);  // wait for >4.1ms
  160.     lcd_send_cmd (0x30);
  161.     HAL_Delay(1);  // wait for >100us
  162.     lcd_send_cmd (0x30);
  163.     HAL_Delay(10);
  164.     lcd_send_cmd (0x20);  // 4bit mode
  165.     HAL_Delay(10);
  166.  
  167.     // dislay initialisation
  168.     lcd_send_cmd (0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
  169.     HAL_Delay(1);
  170.     lcd_send_cmd (0x08); //Display on/off control --> D=0,C=0, B=0  ---> display off
  171.     HAL_Delay(1);
  172.     lcd_send_cmd (0x01);  // clear display
  173.     HAL_Delay(1);
  174.     HAL_Delay(1);
  175.     lcd_send_cmd (0x06); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)
  176.     HAL_Delay(1);
  177.     lcd_send_cmd (0x0C); //Display on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)
  178.   }
  179.  
  180.   void lcd_send_string (char *str)
  181.   {
  182.     while (*str) lcd_send_data (*str++);
  183.   }
  184.  
  185.   /* --------------------------------------------------------*/
  186.    lcd_init ();
  187.    lcd_clear(); // clear LCD
  188.    lcd_put_cur(0,0); // Put are cursor in the right place
  189.    lcd_send_string ("Hello World!");
  190.    HAL_Delay(5000);
  191.    lcd_clear(); // clear LCD
  192.  
  193.    lcd_put_cur(1,0); // Put are cursor in the right place
  194.    lcd_send_string ("Cool, It works!");
  195.   /* USER CODE END 2 */
  196.  
  197.   /* Infinite loop */
  198.   /* USER CODE BEGIN WHILE */
  199.   while (1)
  200.   {
  201.     /* USER CODE END WHILE */
  202.  
  203.     /* USER CODE BEGIN 3 */
  204.       HAL_Delay(1500);
  205.       lcd_put_cur(0,6);
  206.       lcd_send_string ("HELLO");
  207.       lcd_put_cur(1,6);
  208.       lcd_send_string ("WORLD!");
  209.       HAL_Delay(1500);
  210.       lcd_clear();
  211.   }
  212.   /* USER CODE END 3 */
  213. }
  214.  
  215. /**
  216.   * @brief System Clock Configuration
  217.   * @retval None
  218.   */
  219. void SystemClock_Config(void)
  220. {
  221.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  222.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  223.  
  224.   /** Initializes the RCC Oscillators according to the specified parameters
  225.   * in the RCC_OscInitTypeDef structure.
  226.   */
  227.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  228.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  229.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  230.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  231.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  232.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  233.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  234.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  235.   {
  236.     Error_Handler();
  237.   }
  238.   /** Initializes the CPU, AHB and APB buses clocks
  239.   */
  240.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  241.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  242.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  243.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  244.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  245.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  246.  
  247.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  248.   {
  249.     Error_Handler();
  250.   }
  251. }
  252.  
  253. /**
  254.   * @brief I2C1 Initialization Function
  255.   * @param None
  256.   * @retval None
  257.   */
  258. static void MX_I2C1_Init(void)
  259. {
  260.  
  261.   /* USER CODE BEGIN I2C1_Init 0 */
  262.  
  263.   /* USER CODE END I2C1_Init 0 */
  264.  
  265.   /* USER CODE BEGIN I2C1_Init 1 */
  266.  
  267.   /* USER CODE END I2C1_Init 1 */
  268.   hi2c1.Instance = I2C1;
  269.   hi2c1.Init.ClockSpeed = 100000;
  270.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  271.   hi2c1.Init.OwnAddress1 = 0;
  272.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  273.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  274.   hi2c1.Init.OwnAddress2 = 0;
  275.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  276.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  277.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  278.   {
  279.     Error_Handler();
  280.   }
  281.   /* USER CODE BEGIN I2C1_Init 2 */
  282.  
  283.   /* USER CODE END I2C1_Init 2 */
  284.  
  285. }
  286.  
  287. /**
  288.   * Enable DMA controller clock
  289.   */
  290. static void MX_DMA_Init(void)
  291. {
  292.  
  293.   /* DMA controller clock enable */
  294.   __HAL_RCC_DMA1_CLK_ENABLE();
  295.  
  296.   /* DMA interrupt init */
  297.   /* DMA1_Channel6_IRQn interrupt configuration */
  298.   HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  299.   HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  300.   /* DMA1_Channel7_IRQn interrupt configuration */
  301.   HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
  302.   HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);
  303.  
  304. }
  305.  
  306. /**
  307.   * @brief GPIO Initialization Function
  308.   * @param None
  309.   * @retval None
  310.   */
  311. static void MX_GPIO_Init(void)
  312. {
  313.  
  314.   /* GPIO Ports Clock Enable */
  315.   __HAL_RCC_GPIOD_CLK_ENABLE();
  316.   __HAL_RCC_GPIOA_CLK_ENABLE();
  317.   __HAL_RCC_GPIOB_CLK_ENABLE();
  318.  
  319. }
  320.  
  321. /* USER CODE BEGIN 4 */
  322. void HAL_I2C_TxCpltCallback(I2C_HandleTypeDef *hi2c){
  323.     printf("Dane TxCpltCallback \r\n");
  324. }
  325.  
  326. void HAL_I2C_RxCpltCallback(I2C_HandleTypeDef *hi2c){
  327.     printf("Dane RxCpltCallback \r\n");
  328. }
  329. /* USER CODE END 4 */
  330.  
  331. /**
  332.   * @brief  This function is executed in case of error occurrence.
  333.   * @retval None
  334.   */
  335. void Error_Handler(void)
  336. {
  337.   /* USER CODE BEGIN Error_Handler_Debug */
  338.   /* User can add his own implementation to report the HAL error return state */
  339.   __disable_irq();
  340.   while (1)
  341.   {
  342.   }
  343.   /* USER CODE END Error_Handler_Debug */
  344. }
  345.  
  346. #ifdef  USE_FULL_ASSERT
  347. /**
  348.   * @brief  Reports the name of the source file and the source line number
  349.   *         where the assert_param error has occurred.
  350.   * @param  file: pointer to the source file name
  351.   * @param  line: assert_param error line source number
  352.   * @retval None
  353.   */
  354. void assert_failed(uint8_t *file, uint32_t line)
  355. {
  356.   /* USER CODE BEGIN 6 */
  357.   /* User can add his own implementation to report the file name and line number,
  358.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  359.   /* USER CODE END 6 */
  360. }
  361. #endif /* USE_FULL_ASSERT */
  362.  
  363. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  364.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement