void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { #ifdef USE_SCOPE if(htim_base->Instance==TIM3) { /* USER CODE BEGIN TIM3_MspInit 0 */ /* USER CODE END TIM3_MspInit 0 */ /* Peripheral clock enable */ __TIM3_CLK_ENABLE(); /* USER CODE BEGIN TIM3_MspInit 1 */ /* USER CODE END TIM3_MspInit 1 */ } #endif //USE_SCOPE #ifdef USE_GEN if(htim_base->Instance==TIM6){ __TIM6_CLK_ENABLE(); } if(htim_base->Instance==TIM7){ __TIM7_CLK_ENABLE(); } #endif //USE_GEN }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* Peripheral clock enable */ __TIM6_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 3, 0); HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } else if(htim_base->Instance==TIM7) { /* USER CODE BEGIN TIM7_MspInit 0 */ /* USER CODE END TIM7_MspInit 0 */ /* Peripheral clock enable */ __TIM7_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM7_IRQn, 4, 0); HAL_NVIC_EnableIRQ(TIM7_IRQn); /* USER CODE BEGIN TIM7_MspInit 1 */ /* USER CODE END TIM7_MspInit 1 */ } }
void Timer_For_Main_Loop_Init() { __TIM6_CLK_ENABLE(); /* Compute the prescaler value to have TIMx counter clock equal to 1 KHz */ uwPrescalerValue = (uint32_t) ((SystemCoreClock / 1000) - 1); /*##-1- Configure the TIM peripheral #######################################*/ /* Set TIMx instance */ TimHandle.Instance = TIM6; /* Initialize TIMx peripheral as follow: + Period = 10000 - 1 + Prescaler = SystemCoreClock/10000 Note that APB clock = TIMx clock if APB prescaler = 1. + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = 10000 - 1; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimHandle); /*##-2- Start the TIM Base generation in interrupt mode ####################*/ /* Start Channel1 */ HAL_TIM_Base_Start_IT(&TimHandle); HAL_NVIC_EnableIRQ(TIM6_IRQn); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* Peripheral clock enable */ __TIM6_CLK_ENABLE(); /* Peripheral DMA init*/ hdma_tim6_up.Instance = DMA1_Stream1; hdma_tim6_up.Init.Channel = DMA_CHANNEL_7; hdma_tim6_up.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_tim6_up.Init.PeriphInc = DMA_PINC_DISABLE; hdma_tim6_up.Init.MemInc = DMA_MINC_ENABLE; hdma_tim6_up.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD; hdma_tim6_up.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD; hdma_tim6_up.Init.Mode = DMA_NORMAL; hdma_tim6_up.Init.Priority = DMA_PRIORITY_LOW; hdma_tim6_up.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_tim6_up); __HAL_LINKDMA(htim_base,hdma[TIM_DMA_ID_UPDATE],hdma_tim6_up); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } }
/** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) { /*##-1- Enable peripherals and GPIO Clocks #################################*/ /* Enable TIM6 clock */ __TIM6_CLK_ENABLE(); /*##-2- Configure the NVIC for TIMx ########################################*/ /* Set the TIM6 priority */ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TICK_INT_PRIORITY, 0); /* Enable the TIM6 global Interrupt */ HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* Peripheral clock enable */ __TIM6_CLK_ENABLE(); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } }
void TIM6_init(void){ TIM_TimeBaseInitTypeDef TIM6_TimeBase; //RCC_AHB1PeriphClockCmd(RCC_APB1Periph_TIM6,ENABLE); __TIM6_CLK_ENABLE(); TIM_TimeBaseStructInit(&TIM6_TimeBase); TIM6_TimeBase.TIM_Period = (uint16_t)TIM_PERIOD; TIM6_TimeBase.TIM_Prescaler = 0; TIM6_TimeBase.TIM_ClockDivision = 0; TIM6_TimeBase.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM6, &TIM6_TimeBase); TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); TIM_Cmd(TIM6, ENABLE); }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); htim6.Instance = TIM6; htim6.Init.Prescaler = 31999; //32MHz/32000 = 1000Hz htim6.Init.Period = 499; //1000HZ / 500 = 2Hz = 0.5s __TIM6_CLK_ENABLE(); HAL_NVIC_SetPriority(TIM6_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM6_IRQn); HAL_TIM_Base_Init(&htim6); HAL_TIM_Base_Start_IT(&htim6); while (1); }
void Timer_Init(void) { #ifndef _MS_VS __TIM6_CLK_ENABLE(); // Для отсчёта миллисекунд __TIM2_CLK_ENABLE(); // Для тиков __TIM5_CLK_ENABLE(); #endif HAL_TIM_Base_Init((TIM_HandleTypeDef*)&handleTIM6); HAL_TIM_Base_Start_IT((TIM_HandleTypeDef*)&handleTIM6); TIM_HandleTypeDef handleTIM2 = { TIM2, { 0, TIM_COUNTERMODE_UP, 0xffffffff, TIM_CLOCKDIVISION_DIV4 } }; HAL_TIM_Base_Init((TIM_HandleTypeDef*)&handleTIM2); HAL_TIM_Base_Start((TIM_HandleTypeDef*)&handleTIM2); TIM_HandleTypeDef handleTIM5 = { TIM5, { 8999, // WARN Так и не разобрался, как настроить таймер на 1мс. При 8999 период счета - 10мс. TIM_COUNTERMODE_UP, 0xffffffff, TIM_CLOCKDIVISION_DIV1 } }; HAL_TIM_Base_Init((TIM_HandleTypeDef*)&handleTIM5); HAL_TIM_Base_Start((TIM_HandleTypeDef*)&handleTIM5); }
// Init TIM6 with a counter-overflow at the given frequency (given in Hz) // TIM6 is used by the DAC and ADC for auto sampling at a given frequency // This function inits but does not start the timer void timer_tim6_init(uint freq) { // TIM6 clock enable __TIM6_CLK_ENABLE(); // Timer runs at SystemCoreClock / 2 // Compute the prescaler value so TIM6 triggers at freq-Hz uint32_t period = MAX(1, (SystemCoreClock / 2) / freq); uint32_t prescaler = 1; while (period > 0xffff) { period >>= 1; prescaler <<= 1; } // Time base clock configuration TIM6_Handle.Instance = TIM6; TIM6_Handle.Init.Period = period - 1; TIM6_Handle.Init.Prescaler = prescaler - 1; TIM6_Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // unused for TIM6 TIM6_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // unused for TIM6 HAL_TIM_Base_Init(&TIM6_Handle); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { GPIO_InitTypeDef GPIO_InitStruct; if(htim_base->Instance==TIM1) { /* USER CODE BEGIN TIM1_MspInit 0 */ /* USER CODE END TIM1_MspInit 0 */ /* Peripheral clock enable */ __TIM1_CLK_ENABLE(); /**TIM1 GPIO Configuration PE9 ------> TIM1_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); /* USER CODE BEGIN TIM1_MspInit 1 */ /* USER CODE END TIM1_MspInit 1 */ } else if(htim_base->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* Peripheral clock enable */ __TIM6_CLK_ENABLE(); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } }
/** * @brief This function configures the TIM6 as a time base source. * The time source is configured to have 1ms time base with a dedicated * Tick interrupt priority. * @note This function is called automatically at the beginning of program after * reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig(). * @param TickPriority: Tick interrupt priorty. * @retval HAL status */ HAL_StatusTypeDef HAL_InitTick (uint32_t TickPriority) { RCC_ClkInitTypeDef sClokConfig; uint32_t uwTimclock, uwAPB1Prescaler = 0; uint32_t uwPrescalerValue = 0; uint32_t pFLatency; /*Configure the TIM6 IRQ priority */ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, TickPriority ,0); /* Enable the TIM6 global Interrupt */ HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); /* Enable TIM6 clock */ __TIM6_CLK_ENABLE(); /* Get clock configuration */ HAL_RCC_GetClockConfig(&sClokConfig, &pFLatency); /* Get APB1 prescaler */ uwAPB1Prescaler = sClokConfig.APB1CLKDivider; /* Compute TIM6 clock */ if (uwAPB1Prescaler == 0) { uwTimclock = HAL_RCC_GetPCLK1Freq(); } else { uwTimclock = 2*HAL_RCC_GetPCLK1Freq(); } /* Compute the prescaler value to have TIM6 counter clock equal to 1MHz */ uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1); /* Initialize TIM6 */ TimHandle.Instance = TIM6; /* Initialize TIMx peripheral as follow: + Period = [(TIM6CLK/1000) - 1]. to have a (1/1000) s time base. + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock. + ClockDivision = 0 + Counter direction = Up */ TimHandle.Init.Period = (1000000 / 1000) - 1; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_Base_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start the TIM time Base generation in interrupt mode */ if(HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Return function status */ return HAL_OK; }
void Hardware_Init() { HAL_Init(); SystemClock_Config(); __GPIOA_CLK_ENABLE(); __GPIOB_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); __GPIOD_CLK_ENABLE(); __GPIOE_CLK_ENABLE(); __GPIOF_CLK_ENABLE(); __GPIOG_CLK_ENABLE(); __DMA1_CLK_ENABLE(); // Для DAC1 (бикалка) __TIM6_CLK_ENABLE(); // Для отсчёта миллисекунд __TIM2_CLK_ENABLE(); // Для тиков __TIM7_CLK_ENABLE(); // Для DAC1 (бикалка) __DAC_CLK_ENABLE(); // Для бикалки __PWR_CLK_ENABLE(); __SYSCFG_CLK_ENABLE(); HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); // Timer ///////////////////////////////////////////////////////////////// //RCC_PCLK1Config(RCC_HCLK_Div1); // Таймер для мс HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 2, 0); HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); if (HAL_TIM_Base_Init(&handleTIM6forTimer) != HAL_OK) { HARDWARE_ERROR } if (HAL_TIM_Base_Start_IT(&handleTIM6forTimer) != HAL_OK) { HARDWARE_ERROR } // Таймер для тиков TIM_HandleTypeDef tim2handle = { TIM2, { 0, TIM_COUNTERMODE_UP, 0xffffffff, TIM_CLOCKDIVISION_DIV1 } }; if (HAL_TIM_Base_Init(&tim2handle) != HAL_OK) { HARDWARE_ERROR } if (HAL_TIM_Base_Start(&tim2handle) != HAL_OK) { HARDWARE_ERROR } Sound_Init(); #ifdef SPI_ENABLE Panel_Init(); #endif FSMC_Init(); ADC_Init(); FDrive_Init(); // Analog and DAC programmable SPI //////////////////////////////////////// GPIO_InitTypeDef isGPIOG = { GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_5 | GPIO_PIN_7, // GPIO_PIN_1 - для работы с дисплеем GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_SPEED_HIGH, GPIO_AF0_MCO }; HAL_GPIO_Init(GPIOG, &isGPIOG); isGPIOG.Pin = GPIO_PIN_1; isGPIOG.Mode = GPIO_MODE_OUTPUT_PP; isGPIOG.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOG, &isGPIOG); HAL_GPIO_WritePin(GPIOG, GPIO_PIN_1, GPIO_PIN_RESET); // PG1 - когда равен 1, чтение дисплея, в остальных случаях 0 RTC_Init(); }
/** * @brief TIM MSP Initialization * This function configures the hardware resources used in this example: * - Peripheral's clock enable * - Peripheral's GPIO Configuration * @param htim: TIM handle pointer * @retval None */ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) { /* TIM6 Periph clock enable */ __TIM6_CLK_ENABLE(); }
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) { if(htim_base->Instance==TIM1) { /* USER CODE BEGIN TIM1_MspInit 0 */ /* USER CODE END TIM1_MspInit 0 */ /* Peripheral clock enable */ __TIM1_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn); HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); HAL_NVIC_SetPriority(TIM1_CC_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM1_CC_IRQn); /* USER CODE BEGIN TIM1_MspInit 1 */ /* USER CODE END TIM1_MspInit 1 */ } else if(htim_base->Instance==TIM4) { /* USER CODE BEGIN TIM4_MspInit 0 */ /* USER CODE END TIM4_MspInit 0 */ /* Peripheral clock enable */ __TIM4_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM4_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM4_IRQn); /* USER CODE BEGIN TIM4_MspInit 1 */ /* USER CODE END TIM4_MspInit 1 */ } else if(htim_base->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* Peripheral clock enable */ __TIM6_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } else if(htim_base->Instance==TIM7) { /* USER CODE BEGIN TIM7_MspInit 0 */ /* USER CODE END TIM7_MspInit 0 */ /* Peripheral clock enable */ __TIM7_CLK_ENABLE(); /* USER CODE BEGIN TIM7_MspInit 1 */ /* USER CODE END TIM7_MspInit 1 */ } else if(htim_base->Instance==TIM9) { /* USER CODE BEGIN TIM9_MspInit 0 */ /* USER CODE END TIM9_MspInit 0 */ /* Peripheral clock enable */ __TIM9_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn); /* USER CODE BEGIN TIM9_MspInit 1 */ /* USER CODE END TIM9_MspInit 1 */ } else if(htim_base->Instance==TIM10) { /* USER CODE BEGIN TIM10_MspInit 0 */ /* USER CODE END TIM10_MspInit 0 */ /* Peripheral clock enable */ __TIM10_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); /* USER CODE BEGIN TIM10_MspInit 1 */ /* USER CODE END TIM10_MspInit 1 */ } else if(htim_base->Instance==TIM11) { /* USER CODE BEGIN TIM11_MspInit 0 */ /* USER CODE END TIM11_MspInit 0 */ /* Peripheral clock enable */ __TIM11_CLK_ENABLE(); /* USER CODE BEGIN TIM11_MspInit 1 */ /* USER CODE END TIM11_MspInit 1 */ } else if(htim_base->Instance==TIM13) { /* USER CODE BEGIN TIM13_MspInit 0 */ /* USER CODE END TIM13_MspInit 0 */ /* Peripheral clock enable */ __TIM13_CLK_ENABLE(); /* Peripheral interrupt init*/ HAL_NVIC_SetPriority(TIM8_UP_TIM13_IRQn, 5, 0); HAL_NVIC_EnableIRQ(TIM8_UP_TIM13_IRQn); /* USER CODE BEGIN TIM13_MspInit 1 */ /* USER CODE END TIM13_MspInit 1 */ } }