Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.49 KB | None | 0 0
  1.  
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   ** This notice applies to any and all portions of this file
  8.   * that are not between comment pairs USER CODE BEGIN and
  9.   * USER CODE END. Other portions of this file, whether
  10.   * inserted by the user or by software development tools
  11.   * are owned by their respective copyright owners.
  12.   *
  13.   * COPYRIGHT(c) 2019 STMicroelectronics
  14.   *
  15.   * Redistribution and use in source and binary forms, with or without modification,
  16.   * are permitted provided that the following conditions are met:
  17.   *   1. Redistributions of source code must retain the above copyright notice,
  18.   *      this list of conditions and the following disclaimer.
  19.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  20.   *      this list of conditions and the following disclaimer in the documentation
  21.   *      and/or other materials provided with the distribution.
  22.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  23.   *      may be used to endorse or promote products derived from this software
  24.   *      without specific prior written permission.
  25.   *
  26.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36.   *
  37.   ******************************************************************************
  38.   */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "main.h"
  41. #include "stm32f3xx_hal.h"
  42. #include "user_5110.h"
  43.  
  44. /* USER CODE BEGIN Includes */
  45.   GPIO_InitTypeDef GPIO_InitStruct;
  46. #include "string.h"
  47. /* USER CODE END Includes */
  48.  
  49. /* Private variables ---------------------------------------------------------*/
  50. SPI_HandleTypeDef hspi1;
  51.  
  52. UART_HandleTypeDef huart2;
  53.  
  54. /* USER CODE BEGIN PV */
  55. /* Private variables ---------------------------------------------------------*/
  56.  
  57. char dado[16], endereco[4], buffer[50];
  58. int escrita, leitura, chip;
  59.  
  60.  
  61. /* USER CODE END PV */
  62.  
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. static void MX_GPIO_Init(void);
  66. static void MX_USART2_UART_Init(void);
  67. static void MX_SPI1_Init(void);
  68.  
  69. /* USER CODE BEGIN PFP */
  70. /* Private function prototypes -----------------------------------------------*/
  71.  
  72. /* USER CODE END PFP */
  73.  
  74. /* USER CODE BEGIN 0 */
  75.  
  76. /* USER CODE END 0 */
  77.  
  78. /**
  79.   * @brief  The application entry point.
  80.   *
  81.   * @retval None
  82.   */
  83. int main(void)
  84. {
  85.   /* USER CODE BEGIN 1 */
  86.  
  87.   /* USER CODE END 1 */
  88.  
  89.   /* MCU Configuration----------------------------------------------------------*/
  90.  
  91.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  92.   HAL_Init();
  93.  
  94.   /* USER CODE BEGIN Init */
  95.  
  96.   /* USER CODE END Init */
  97.  
  98.   /* Configure the system clock */
  99.   SystemClock_Config();
  100.  
  101.   /* USER CODE BEGIN SysInit */
  102.  
  103.   /* USER CODE END SysInit */
  104.  
  105.   /* Initialize all configured peripherals */
  106.   MX_GPIO_Init();
  107.   MX_USART2_UART_Init();
  108.   MX_SPI1_Init();
  109.   /* USER CODE BEGIN 2 */
  110.  
  111.   LCD_Init();
  112.   LCD_Clear();
  113.  
  114.   /* USER CODE END 2 */
  115.  
  116.   /* Infinite loop */
  117.   /* USER CODE BEGIN WHILE */
  118.   while (1)
  119.   {
  120.  
  121.   /* USER CODE END WHILE */
  122.  
  123.   /* USER CODE BEGIN 3 */
  124.  
  125.       if (HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)==1)                        // Lê Chip select
  126.       {
  127.           if (HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_8)==1 && HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_10)==0) // Escrita == 1 && Leitura == 0
  128.           {
  129.               GPIO_InitStruct.Pin = DS0_Pin|DS1_Pin|DS2_Pin|DS3_Pin;    // Define como entrada os pinos
  130.               GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  131.               HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  132.  
  133.               // Lê dado enviado pelo master
  134.               char bitDado0 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8);  // Read state
  135.               char bitDado1 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_9);  // Read state
  136.               char bitDado2 = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_7);  // Read state
  137.               char bitDado3 = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_6);  // Read state
  138.               // Mescla os bits lidos
  139.               char ValorLido = bitDado0|(bitDado1<<1)|(bitDado2<<2)|(bitDado3<<3);
  140.  
  141.               // Lê dado enviado pelo master
  142.               char bitAdress0 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_7);  // Read state
  143.               char bitAdress1 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_6);  // Read state
  144.               char bitAdress2 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5);  // Read state
  145.               char bitAdress3 = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_9);  // Read state
  146.               // Mescla os bits lidos
  147.               char AdressLido = bitAdress0|(bitAdress1<<1)|(bitAdress2<<2)|(bitAdress3<<3);
  148.  
  149.               //dado[AdressLido] = ValorLido;
  150.  
  151.               sprintf(buffer,"\n\r %d",ValorLido);
  152.               HAL_UART_Transmit(&huart2,buffer,strlen(buffer),100);
  153.               HAL_Delay(100);
  154.  
  155.  
  156.  
  157.  
  158.               //dado[AdressLido] = ValorLido;
  159.  
  160.               //dado[0] 1;
  161.  
  162.  
  163.  
  164.           }
  165.  
  166.       }
  167.  
  168.       //sprintf(buffer,"teste display");
  169.       //LCD_Write_String(0, 0,buffer);
  170.  
  171.  
  172.   }
  173.   /* USER CODE END 3 */
  174.  
  175. }
  176.  
  177. /**
  178.   * @brief System Clock Configuration
  179.   * @retval None
  180.   */
  181. void SystemClock_Config(void)
  182. {
  183.  
  184.   RCC_OscInitTypeDef RCC_OscInitStruct;
  185.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  186.  
  187.     /**Initializes the CPU, AHB and APB busses clocks
  188.     */
  189.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  190.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  191.   RCC_OscInitStruct.HSICalibrationValue = 16;
  192.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  193.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  194.   {
  195.     _Error_Handler(__FILE__, __LINE__);
  196.   }
  197.  
  198.     /**Initializes the CPU, AHB and APB busses clocks
  199.     */
  200.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  201.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  202.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  203.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  204.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  205.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  206.  
  207.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  208.   {
  209.     _Error_Handler(__FILE__, __LINE__);
  210.   }
  211.  
  212.     /**Configure the Systick interrupt time
  213.     */
  214.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  215.  
  216.     /**Configure the Systick
  217.     */
  218.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  219.  
  220.   /* SysTick_IRQn interrupt configuration */
  221.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  222. }
  223.  
  224. /* SPI1 init function */
  225. static void MX_SPI1_Init(void)
  226. {
  227.  
  228.   /* SPI1 parameter configuration*/
  229.   hspi1.Instance = SPI1;
  230.   hspi1.Init.Mode = SPI_MODE_MASTER;
  231.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  232.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  233.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  234.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  235.   hspi1.Init.NSS = SPI_NSS_SOFT;
  236.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  237.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  238.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  239.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  240.   hspi1.Init.CRCPolynomial = 7;
  241.   hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  242.   hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  243.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  244.   {
  245.     _Error_Handler(__FILE__, __LINE__);
  246.   }
  247.  
  248. }
  249.  
  250. /* USART2 init function */
  251. static void MX_USART2_UART_Init(void)
  252. {
  253.  
  254.   huart2.Instance = USART2;
  255.   huart2.Init.BaudRate = 115200;
  256.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  257.   huart2.Init.StopBits = UART_STOPBITS_1;
  258.   huart2.Init.Parity = UART_PARITY_NONE;
  259.   huart2.Init.Mode = UART_MODE_TX_RX;
  260.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  261.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  262.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  263.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  264.   if (HAL_UART_Init(&huart2) != HAL_OK)
  265.   {
  266.     _Error_Handler(__FILE__, __LINE__);
  267.   }
  268.  
  269. }
  270.  
  271. /** Configure pins as
  272.         * Analog
  273.         * Input
  274.         * Output
  275.         * EVENT_OUT
  276.         * EXTI
  277. */
  278. static void MX_GPIO_Init(void)
  279. {
  280.  
  281.  
  282.  
  283.   /* GPIO Ports Clock Enable */
  284.   __HAL_RCC_GPIOA_CLK_ENABLE();
  285.   __HAL_RCC_GPIOB_CLK_ENABLE();
  286.   __HAL_RCC_GPIOC_CLK_ENABLE();
  287.  
  288.   /*Configure GPIO pin Output Level */
  289.   HAL_GPIO_WritePin(LCD_RST_GPIO_Port, LCD_RST_Pin, GPIO_PIN_RESET);
  290.  
  291.   /*Configure GPIO pin Output Level */
  292.   HAL_GPIO_WritePin(GPIOC, LCD_CE_Pin|LCD_DC_Pin, GPIO_PIN_RESET);
  293.  
  294.   /*Configure GPIO pins : CHI_SL_Pin ES2_Pin ES1_Pin ES0_Pin
  295.                            DS0_Pin DS1_Pin LEI_SL_Pin */
  296.   GPIO_InitStruct.Pin = CHI_SL_Pin|ES2_Pin|ES1_Pin|ES0_Pin
  297.                           |DS0_Pin|DS1_Pin|LEI_SL_Pin;
  298.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  299.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  300.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  301.  
  302.  
  303.   /*Configure GPIO pin : LCD_RST_Pin */
  304.   GPIO_InitStruct.Pin = LCD_RST_Pin;
  305.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  306.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  307.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  308.   HAL_GPIO_Init(LCD_RST_GPIO_Port, &GPIO_InitStruct);
  309.  
  310.   /*Configure GPIO pin : DS2_Pin */
  311.   GPIO_InitStruct.Pin = DS2_Pin;
  312.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  313.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  314.   HAL_GPIO_Init(DS2_GPIO_Port, &GPIO_InitStruct);
  315.  
  316.   /*Configure GPIO pins : LCD_CE_Pin LCD_DC_Pin */
  317.   GPIO_InitStruct.Pin = LCD_CE_Pin|LCD_DC_Pin;
  318.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  319.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  320.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  321.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  322.  
  323.   /*Configure GPIO pins : DS3_Pin ESC_SL_Pin ES3_Pin */
  324.   GPIO_InitStruct.Pin = DS3_Pin|ESC_SL_Pin|ES3_Pin;
  325.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  326.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  327.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  328.  
  329. }
  330.  
  331. /* USER CODE BEGIN 4 */
  332.  
  333. /* USER CODE END 4 */
  334.  
  335. /**
  336.   * @brief  This function is executed in case of error occurrence.
  337.   * @param  file: The file name as string.
  338.   * @param  line: The line in file as a number.
  339.   * @retval None
  340.   */
  341. void _Error_Handler(char *file, int line)
  342. {
  343.   /* USER CODE BEGIN Error_Handler_Debug */
  344.   /* User can add his own implementation to report the HAL error return state */
  345.   while(1)
  346.   {
  347.   }
  348.   /* USER CODE END Error_Handler_Debug */
  349. }
  350.  
  351. #ifdef  USE_FULL_ASSERT
  352. /**
  353.   * @brief  Reports the name of the source file and the source line number
  354.   *         where the assert_param error has occurred.
  355.   * @param  file: pointer to the source file name
  356.   * @param  line: assert_param error line source number
  357.   * @retval None
  358.   */
  359. void assert_failed(uint8_t* file, uint32_t line)
  360. {
  361.   /* USER CODE BEGIN 6 */
  362.   /* User can add his own implementation to report the file name and line number,
  363.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  364.   /* USER CODE END 6 */
  365. }
  366. #endif /* USE_FULL_ASSERT */
  367.  
  368. /**
  369.   * @}
  370.   */
  371.  
  372. /**
  373.   * @}
  374.   */
  375.  
  376. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement