Advertisement
Guest User

Untitled

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