Advertisement
Guest User

Untitled

a guest
Sep 16th, 2021
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.82 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_tx;
  45.  
  46. /* USER CODE BEGIN PV */
  47.  
  48. /* USER CODE END PV */
  49.  
  50. /* Private function prototypes -----------------------------------------------*/
  51. void SystemClock_Config(void);
  52. static void MX_GPIO_Init(void);
  53. static void MX_DMA_Init(void);
  54. static void MX_I2C1_Init(void);
  55. /* USER CODE BEGIN PFP */
  56.  
  57. /* USER CODE END PFP */
  58.  
  59. /* Private user code ---------------------------------------------------------*/
  60. /* USER CODE BEGIN 0 */
  61.  
  62. /* Uncomment this line to use the board as master, if not it is used as slave */
  63. #define MASTER_BOARD
  64. /* I2C SPEEDCLOCK define to max value: 400 KHz on STM32F1xx*/
  65. //#define I2C_SPEEDCLOCK   400000
  66. //#define I2C_DUTYCYCLE    I2C_DUTYCYCLE_2
  67.  
  68. /* USER CODE END 0 */
  69.  
  70. /**
  71.   * @brief  The application entry point.
  72.   * @retval int
  73.   */
  74. int main(void)
  75. {
  76.   /* USER CODE BEGIN 1 */
  77.  
  78.   /* USER CODE END 1 */
  79.  
  80.   /* MCU Configuration--------------------------------------------------------*/
  81.  
  82.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  83.   HAL_Init();
  84.  
  85.   /* USER CODE BEGIN Init */
  86.  
  87.   /* USER CODE END Init */
  88.  
  89.   /* Configure the system clock */
  90.   SystemClock_Config();
  91.  
  92.   /* USER CODE BEGIN SysInit */
  93.  
  94.   /* USER CODE END SysInit */
  95.  
  96.   /* Initialize all configured peripherals */
  97.   MX_GPIO_Init();
  98.   MX_DMA_Init();
  99.   MX_I2C1_Init();
  100.   /* USER CODE BEGIN 2 */
  101.  
  102.   /* --------------------------------------------------------*/
  103.   //extern I2C_HandleTypeDef hi2c1;  // change your handler here accordingly
  104.  
  105.   #define SLAVE_ADDRESS_LCD 0x4E // change this according to ur setup
  106.  
  107.     void lcd_send_cmd(char cmd) {
  108.         char data_u, data_l;
  109.         uint8_t data_t[4];
  110.         data_u = (cmd & 0xf0);
  111.         data_l = ((cmd << 4) & 0xf0);
  112.         data_t[0] = data_u | 0x0C;  //en=1, rs=0
  113.         data_t[1] = data_u | 0x08;  //en=0, rs=0
  114.         data_t[2] = data_l | 0x0C;  //en=1, rs=0
  115.         data_t[3] = data_l | 0x08;  //en=0, rs=0
  116.         /*  Before starting a new communication transfer, you need to check the current
  117.          state of the peripheral; if it�s busy you need to wait for the end of current
  118.          transfer before starting a new one.
  119.          For simplicity reasons, this example is just waiting till the end of the
  120.          transfer, but application may perform other tasks while transfer operation
  121.          is ongoing. */
  122.  
  123.         while (HAL_I2C_Master_Transmit_DMA(&hi2c1, SLAVE_ADDRESS_LCD,
  124.                 (uint8_t*) data_t, 4) != HAL_OK) {
  125.             /* Error_Handler() function is called when Timeout error occurs.
  126.              When Acknowledge failure occurs (Slave don't acknowledge its address)
  127.              Master restarts communication */
  128.             if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF) {
  129.                 Error_Handler();
  130.             }
  131.         }
  132.         while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) {
  133.             //HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
  134.  
  135.             /*##-2- Start the transmission process #####################################*/
  136.             /* While the I2C in reception process, user can transmit data through
  137.              "aTxBuffer" buffer */
  138.         }
  139.     }
  140.   void lcd_send_data (char data)
  141.   {
  142.     char data_u, data_l;
  143.     uint8_t data_t[4];
  144.     data_u = (data&0xf0);
  145.     data_l = ((data<<4)&0xf0);
  146.     data_t[0] = data_u|0x0D;  //en=1, rs=0
  147.     data_t[1] = data_u|0x09;  //en=0, rs=0
  148.     data_t[2] = data_l|0x0D;  //en=1, rs=0
  149.     data_t[3] = data_l|0x09;  //en=0, rs=0
  150.     /*  Before starting a new communication transfer, you need to check the current
  151.      state of the peripheral; if it�s busy you need to wait for the end of current
  152.      transfer before starting a new one.
  153.      For simplicity reasons, this example is just waiting till the end of the
  154.      transfer, but application may perform other tasks while transfer operation
  155.      is ongoing. */
  156.     while (HAL_I2C_Master_Transmit_DMA(&hi2c1, SLAVE_ADDRESS_LCD,
  157.             (uint8_t*) data_t, 4) != HAL_OK) {
  158.         /* Error_Handler() function is called when Timeout error occurs.
  159.          When Acknowledge failure occurs (Slave don't acknowledge its address)
  160.          Master restarts communication */
  161.         if (HAL_I2C_GetError(&hi2c1) != HAL_I2C_ERROR_AF) {
  162.             Error_Handler();
  163.         }
  164.     }
  165.     while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) {
  166.         //HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *) data_t, 4, 100);
  167.  
  168.         /*##-2- Start the transmission process #####################################*/
  169.         /* While the I2C in reception process, user can transmit data through
  170.          "aTxBuffer" buffer */
  171.     }
  172. }
  173.  
  174.   void lcd_clear (void)
  175.   {
  176.     lcd_send_cmd (0x80);
  177.     for (int i=0; i<70; i++)
  178.     {
  179.         lcd_send_data (' ');
  180.     }
  181.   }
  182.  
  183.   void lcd_put_cur(int row, int col)
  184.   {
  185.       switch (row)
  186.       {
  187.           case 0:
  188.               col |= 0x80;
  189.               break;
  190.           case 1:
  191.               col |= 0xC0;
  192.               break;
  193.       }
  194.  
  195.       lcd_send_cmd (col);
  196.   }
  197.  
  198.  
  199.   void lcd_init (void)
  200.   {
  201.     // 4 bit initialisation
  202.     HAL_Delay(50);  // wait for >40ms
  203.     lcd_send_cmd (0x30);
  204.     HAL_Delay(5);  // wait for >4.1ms
  205.     lcd_send_cmd (0x30);
  206.     HAL_Delay(1);  // wait for >100us
  207.     lcd_send_cmd (0x30);
  208.     HAL_Delay(10);
  209.     lcd_send_cmd (0x20);  // 4bit mode
  210.     HAL_Delay(10);
  211.  
  212.     // dislay initialisation
  213.     lcd_send_cmd (0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2 line display) F = 0 (5x8 characters)
  214.     HAL_Delay(1);
  215.     lcd_send_cmd (0x08); //Display on/off control --> D=0,C=0, B=0  ---> display off
  216.     HAL_Delay(1);
  217.     lcd_send_cmd (0x01);  // clear display
  218.     HAL_Delay(1);
  219.     HAL_Delay(1);
  220.     lcd_send_cmd (0x06); //Entry mode set --> I/D = 1 (increment cursor) & S = 0 (no shift)
  221.     HAL_Delay(1);
  222.     lcd_send_cmd (0x0C); //Display on/off control --> D = 1, C and B = 0. (Cursor and blink, last two bits)
  223.   }
  224.  
  225.   void lcd_send_string (char *str)
  226.   {
  227.     while (*str) lcd_send_data (*str++);
  228.   }
  229.  
  230.   /* --------------------------------------------------------*/
  231.    lcd_init ();
  232.    lcd_clear(); // clear LCD
  233.    lcd_put_cur(0,0); // Put are cursor in the right place
  234.    lcd_send_string ("Hello World!");
  235.    HAL_Delay(5000);
  236.    lcd_clear(); // clear LCD
  237.  
  238.    lcd_put_cur(1,0); // Put are cursor in the right place
  239.    lcd_send_string ("Cool, It works!");
  240.  
  241.   /* USER CODE END 2 */
  242.  
  243.   /* Infinite loop */
  244.   /* USER CODE BEGIN WHILE */
  245.   while (1)
  246.   {
  247.     /* USER CODE END WHILE */
  248.  
  249.     /* USER CODE BEGIN 3 */
  250.           HAL_Delay(1500);
  251.           lcd_put_cur(0,6);
  252.           lcd_send_string ("HELLO");
  253.           lcd_put_cur(1,6);
  254.           lcd_send_string ("WORLD!");
  255.           HAL_Delay(1500);
  256.           lcd_clear();
  257.   }
  258.   /* USER CODE END 3 */
  259. }
  260.  
  261. /**
  262.   * @brief System Clock Configuration
  263.   * @retval None
  264.   */
  265. void SystemClock_Config(void)
  266. {
  267.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  268.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  269.  
  270.   /** Initializes the RCC Oscillators according to the specified parameters
  271.   * in the RCC_OscInitTypeDef structure.
  272.   */
  273.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  274.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  275.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  276.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  277.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  278.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL11;
  279.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  280.   {
  281.     Error_Handler();
  282.   }
  283.   /** Initializes the CPU, AHB and APB buses clocks
  284.   */
  285.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  286.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  287.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  288.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  289.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  290.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  291.  
  292.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  293.   {
  294.     Error_Handler();
  295.   }
  296. }
  297.  
  298. /**
  299.   * @brief I2C1 Initialization Function
  300.   * @param None
  301.   * @retval None
  302.   */
  303. static void MX_I2C1_Init(void)
  304. {
  305.  
  306.   /* USER CODE BEGIN I2C1_Init 0 */
  307.  
  308.   /* USER CODE END I2C1_Init 0 */
  309.  
  310.   /* USER CODE BEGIN I2C1_Init 1 */
  311.  
  312.   /* USER CODE END I2C1_Init 1 */
  313.   hi2c1.Instance = I2C1;
  314.   hi2c1.Init.ClockSpeed = 100000;
  315.   hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  316.   hi2c1.Init.OwnAddress1 = 0;
  317.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  318.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  319.   hi2c1.Init.OwnAddress2 = 0;
  320.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  321.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  322.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  323.   {
  324.     Error_Handler();
  325.   }
  326.   /* USER CODE BEGIN I2C1_Init 2 */
  327.  
  328.   /* USER CODE END I2C1_Init 2 */
  329.  
  330. }
  331.  
  332. /**
  333.   * Enable DMA controller clock
  334.   */
  335. static void MX_DMA_Init(void)
  336. {
  337.  
  338.   /* DMA controller clock enable */
  339.   __HAL_RCC_DMA1_CLK_ENABLE();
  340.  
  341.   /* DMA interrupt init */
  342.   /* DMA1_Channel6_IRQn interrupt configuration */
  343.   HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  344.   HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  345.  
  346. }
  347.  
  348. /**
  349.   * @brief GPIO Initialization Function
  350.   * @param None
  351.   * @retval None
  352.   */
  353. static void MX_GPIO_Init(void)
  354. {
  355.  
  356.   /* GPIO Ports Clock Enable */
  357.   __HAL_RCC_GPIOD_CLK_ENABLE();
  358.   __HAL_RCC_GPIOA_CLK_ENABLE();
  359.   __HAL_RCC_GPIOB_CLK_ENABLE();
  360.  
  361. }
  362.  
  363. /* USER CODE BEGIN 4 */
  364. void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c)
  365. {
  366.     printf("Dane TxCpltCallback \r\n");
  367. }
  368. /*
  369. void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c)
  370. {
  371.     printf("Dane RxCpltCallback \r\n");
  372. }
  373. */
  374. /* USER CODE END 4 */
  375.  
  376. /**
  377.   * @brief  This function is executed in case of error occurrence.
  378.   * @retval None
  379.   */
  380. void Error_Handler(void)
  381. {
  382.   /* USER CODE BEGIN Error_Handler_Debug */
  383.   /* User can add his own implementation to report the HAL error return state */
  384.   __disable_irq();
  385.   while (1)
  386.   {
  387.   }
  388.   /* USER CODE END Error_Handler_Debug */
  389. }
  390.  
  391. #ifdef  USE_FULL_ASSERT
  392. /**
  393.   * @brief  Reports the name of the source file and the source line number
  394.   *         where the assert_param error has occurred.
  395.   * @param  file: pointer to the source file name
  396.   * @param  line: assert_param error line source number
  397.   * @retval None
  398.   */
  399. void assert_failed(uint8_t *file, uint32_t line)
  400. {
  401.   /* USER CODE BEGIN 6 */
  402.   /* User can add his own implementation to report the file name and line number,
  403.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  404.   /* USER CODE END 6 */
  405. }
  406. #endif /* USE_FULL_ASSERT */
  407.  
  408. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  409.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement