Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.86 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : 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) 2017 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 "stm32f1xx_hal.h"
  41. #include "dma.h"
  42. #include "rtc.h"
  43. #include "tim.h"
  44. #include "usart.h"
  45. #include "gpio.h"
  46.  
  47. /* USER CODE BEGIN Includes */
  48.  
  49. /* USER CODE END Includes */
  50.  
  51. /* Private variables ---------------------------------------------------------*/
  52.  
  53. /* USER CODE BEGIN PV */
  54. /* Private variables ---------------------------------------------------------*/
  55.  
  56. volatile uint32_t miliseconds           = 0;
  57. volatile uint16_t LED_BYTE_Buffer[100]   = {0};
  58. /* USER CODE END PV */
  59.  
  60. /* Private function prototypes -----------------------------------------------*/
  61. void SystemClock_Config(void);
  62.  
  63. /* USER CODE BEGIN PFP */
  64. /* Private function prototypes -----------------------------------------------*/
  65.  
  66. /* USER CODE END PFP */
  67.  
  68. /* USER CODE BEGIN 0 */
  69.  
  70. /* USER CODE END 0 */
  71.  
  72. int main(void)
  73. {
  74.  
  75.   /* USER CODE BEGIN 1 */
  76.  
  77.   /* USER CODE END 1 */
  78.  
  79.   /* MCU Configuration----------------------------------------------------------*/
  80.  
  81.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  82.   HAL_Init();
  83.  
  84.   /* USER CODE BEGIN Init */
  85.  
  86.   /* USER CODE END Init */
  87.  
  88.   /* Configure the system clock */
  89.   SystemClock_Config();
  90.  
  91.   /* USER CODE BEGIN SysInit */
  92.  
  93.   /* USER CODE END SysInit */
  94.  
  95.   /* Initialize all configured peripherals */
  96.   MX_GPIO_Init();
  97.   MX_DMA_Init();
  98.   MX_RTC_Init();
  99.   MX_TIM1_Init();
  100.   MX_USART2_UART_Init();
  101.   MX_TIM2_Init();
  102.  
  103.   /* USER CODE BEGIN 2 */
  104.     // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 26);
  105.    
  106.  
  107.   volatile uint8_t sw = 0;
  108.   uint16_t i;
  109.   for(i =0; i<100; i++)
  110.   {
  111.         if(sw == 0)
  112.         {
  113.             LED_BYTE_Buffer[i] = 26;
  114.             sw = 1;
  115.         }
  116.         else if(sw == 1)
  117.         {
  118.             LED_BYTE_Buffer[i] = 13;
  119.             sw = 2;
  120.         }
  121.         else
  122.         {
  123.             LED_BYTE_Buffer[i] = 0;
  124.             sw = 0;
  125.         }
  126.   }
  127.  
  128.  
  129.     HAL_TIM_PWM_Start_DMA(&htim1, TIM_CHANNEL_1, (uint32_t*)LED_BYTE_Buffer, sizeof(LED_BYTE_Buffer) / sizeof(LED_BYTE_Buffer[0]));
  130.  
  131.     HAL_Delay(10);
  132.  
  133.   /* USER CODE END 2 */
  134.  
  135.   /* Infinite loop */
  136.   /* USER CODE BEGIN WHILE */
  137.   while (1)
  138.   {
  139.  
  140.     // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 26);
  141.     HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  142.     HAL_Delay(1000);
  143.     // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 0);
  144.     // HAL_TIM_PWM_Stop(&htim1, TIM_CHANNEL_1);
  145.     // HAL_Delay(1000);
  146.     // __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, 13);
  147.     // HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);    //starts PWM on CH1 pin
  148.     // HAL_Delay(5000);
  149.  
  150.   /* USER CODE END WHILE */
  151.  
  152.   /* USER CODE BEGIN 3 */
  153.  
  154.   }
  155.   /* USER CODE END 3 */
  156.  
  157. }
  158.  
  159. /** System Clock Configuration
  160. */
  161. void SystemClock_Config(void)
  162. {
  163.  
  164.   RCC_OscInitTypeDef RCC_OscInitStruct;
  165.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  166.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  167.  
  168.     /**Initializes the CPU, AHB and APB busses clocks
  169.     */
  170.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
  171.   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  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_DIV2;
  176.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  177.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  178.   {
  179.     _Error_Handler(__FILE__, __LINE__);
  180.   }
  181.  
  182.     /**Initializes the CPU, AHB and APB busses clocks
  183.     */
  184.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  185.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  186.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  187.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  188.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  189.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  190.  
  191.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  192.   {
  193.     _Error_Handler(__FILE__, __LINE__);
  194.   }
  195.  
  196.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  197.   PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  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. }
  214.  
  215. /* USER CODE BEGIN 4 */
  216.  
  217. void HAL_SYSTICK_Callback(void)
  218. {
  219.     miliseconds++;
  220. }
  221.  
  222. /* USER CODE END 4 */
  223.  
  224. /**
  225.   * @brief  This function is executed in case of error occurrence.
  226.   * @param  None
  227.   * @retval None
  228.   */
  229. void _Error_Handler(char * file, int line)
  230. {
  231.   /* USER CODE BEGIN Error_Handler_Debug */
  232.   /* User can add his own implementation to report the HAL error return state */
  233.   while(1)
  234.   {
  235.   }
  236.   /* USER CODE END Error_Handler_Debug */
  237. }
  238.  
  239. #ifdef USE_FULL_ASSERT
  240.  
  241. /**
  242.    * @brief Reports the name of the source file and the source line number
  243.    * where the assert_param error has occurred.
  244.    * @param file: pointer to the source file name
  245.    * @param line: assert_param error line source number
  246.    * @retval None
  247.    */
  248. void assert_failed(uint8_t* file, uint32_t line)
  249. {
  250.   /* USER CODE BEGIN 6 */
  251.   /* User can add his own implementation to report the file name and line number,
  252.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  253.   /* USER CODE END 6 */
  254.  
  255. }
  256.  
  257. #endif
  258.  
  259. /**
  260.   * @}
  261.   */
  262.  
  263. /**
  264.   * @}
  265. */
  266.  
  267. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement