Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2019 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "adc.h"
- #include "dma.h"
- #include "tim.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- uint8_t Received;
- /* USER CODE BEGIN PV */
- //ADC_HandleTypeDef hadc1;
- volatile uint16_t num = 5;
- volatile uint16_t disp = 0;
- volatile uint16_t command = 0;
- volatile uint8_t pause = 0;
- volatile uint8_t start = 0;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim);
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin);
- void newNum(uint16_t number, uint16_t display);
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_TIM2_Init();
- MX_TIM4_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim2);
- HAL_TIM_Base_Start_IT(&htim4);
- HAL_UART_Received_IT(&huart1, &Received, 1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- newNum(PomiarADC, disp);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV4;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.Prediv1Source = RCC_PREDIV1_SOURCE_PLL2;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL5;
- RCC_OscInitStruct.PLL2.PLL2State = RCC_PLL2_ON;
- RCC_OscInitStruct.PLL2.PLL2MUL = RCC_PLL2_MUL8;
- RCC_OscInitStruct.PLL2.HSEPrediv2Value = RCC_HSE_PREDIV2_DIV5;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
- {
- Error_Handler();
- }
- /** Configure the Systick interrupt time
- */
- __HAL_RCC_PLLI2S_ENABLE();
- }
- /* USER CODE BEGIN 4 */
- void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin){
- if(GPIO_Pin == Pause_Pin && pause == 0){
- pause = 1;
- }
- else{
- pause = 0;
- }
- if(GPIO_Pin == Start_Pin && start == 0){
- start = 1;
- }
- else{
- start = 0;
- }
- }
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
- if(htim->Instance == TIM2 && pause != 1){
- num == 999 ? num = 0 : num++;
- }
- if(htim->Instance == TIM4){
- HAL_GPIO_TogglePin(led_GPIO_Port, led_Pin);
- //HAL_GPIO_TogglePin(green_GPIO_Port,green_Pin);
- if(disp == 0){
- disp = 1;
- //GPIOC->ODR = 0xF7;
- HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_SET);
- }
- else if(disp == 1){
- disp = 2;
- //GPIOC->ODR = 0xFB;
- HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_SET);
- }
- else if(disp == 2){
- disp = 3;
- //GPIOC->ODR = 0xDF;
- HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_SET);
- }
- else if(disp == 3){
- disp = 0;
- //GPIOC->ODR = 0x7F;
- HAL_GPIO_WritePin(DIG1_GPIO_Port,DIG1_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG2_GPIO_Port,DIG2_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG3_GPIO_Port,DIG3_Pin,GPIO_PIN_SET);
- HAL_GPIO_WritePin(DIG4_GPIO_Port,DIG4_Pin,GPIO_PIN_RESET);
- HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_RESET);
- }
- }
- }
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
- uint8_t Data[50];
- uint16_t size = 0;
- switch (atoi(&Received)) {
- case 's':
- size = sprintf(Data, "START/STOP\n\r");
- if(command == 0 || command == 2){command = 1;}
- else{command = 0;}
- break;
- case 'p':
- if(command == 1 || command = 0){command = 2;}
- else{command = 2;)}
- size = sprintf(Data, "PAUSE\n\r");
- break;
- default:
- size = sprintf(Data, "Odebrano nieznany znak: %c\n\r", Received);
- break;
- }
- HAL_UART_Transmit_IT(&huart1, Data, size);
- HAL_UART_Receive_IT(&huart1, &Received, 1);
- }
- void newNum(uint16_t number, uint16_t display)
- {
- if(HAL_GPIO_ReadPin(DIG4_GPIO_Port,DIG4_Pin)==GPIO_PIN_RESET){
- number = number%10;
- }
- else if(HAL_GPIO_ReadPin(DIG3_GPIO_Port,DIG3_Pin)==GPIO_PIN_RESET){
- number = (number/10)%10;
- }
- else if(HAL_GPIO_ReadPin(DIG2_GPIO_Port,DIG2_Pin)==GPIO_PIN_RESET){
- number = (number/100)%10;
- }
- else if(HAL_GPIO_ReadPin(DIG1_GPIO_Port,DIG1_Pin)==GPIO_PIN_RESET){
- number = (number/1000)%10;
- }
- switch(number){
- case 0: GPIOE->ODR = 0xC0<<8; break;
- case 1: GPIOE->ODR = 0xF9<<8; break;
- case 2: GPIOE->ODR = 0xA4<<8; break;
- case 3: GPIOE->ODR = 0xB0<<8; break;
- case 4: GPIOE->ODR = 0x99<<8; break;
- case 5: GPIOE->ODR = 0x92<<8; break;
- case 6: GPIOE->ODR = 0x82<<8; break;
- case 7: GPIOE->ODR = 0xF8<<8; break;
- case 8: GPIOE->ODR = 0x80<<8; break;
- case 9: GPIOE->ODR = 0x90<<8; break;
- }
- if(HAL_GPIO_ReadPin(DIG2_GPIO_Port,DIG2_Pin)==GPIO_PIN_RESET){
- HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_RESET);
- }
- else{
- HAL_GPIO_WritePin(DP_GPIO_Port,DP_Pin,GPIO_PIN_SET);
- }
- }
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement