kopia lustrzana https://github.com/Schildkroet/GRBL-Advanced
106 wiersze
3.3 KiB
C
106 wiersze
3.3 KiB
C
![]() |
/*
|
||
|
TIM.c - Timer Implementation
|
||
|
Part of STM32F4_HAL
|
||
|
|
||
|
Copyright (c) 2017 Patrick F.
|
||
|
|
||
|
STM32F4_HAL is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 3 of the License, or
|
||
|
(at your option) any later version.
|
||
|
STM32F4_HAL is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with STM32F4_HAL. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
#include "stm32f4xx_tim.h"
|
||
|
#include "stm32f4xx_gpio.h"
|
||
|
#include "TIM.h"
|
||
|
|
||
|
|
||
|
void Timer1_Init(void);
|
||
|
void Timer9_Init(void);
|
||
|
|
||
|
|
||
|
void TIM_Init(void)
|
||
|
{
|
||
|
Timer1_Init();
|
||
|
Timer9_Init();
|
||
|
}
|
||
|
|
||
|
|
||
|
void Timer1_Init(void)
|
||
|
{
|
||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||
|
TIM_OCInitTypeDef TIM_OCInitStructure;
|
||
|
|
||
|
/* TIM1 clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
||
|
|
||
|
/* Time base configuration */
|
||
|
TIM_TimeBaseStructure.TIM_Period = 100-1; // ~10 KHz
|
||
|
TIM_TimeBaseStructure.TIM_Prescaler = 50; // 2 MHz
|
||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||
|
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
|
||
|
|
||
|
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
|
||
|
|
||
|
/* PWM1 Mode configuration: Channel1 */
|
||
|
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
|
||
|
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
|
||
|
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
|
||
|
TIM_OCInitStructure.TIM_Pulse = 0;
|
||
|
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
|
||
|
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
|
||
|
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
|
||
|
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
|
||
|
|
||
|
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
|
||
|
|
||
|
/* TIM1 Main Output Enable */
|
||
|
TIM_CtrlPWMOutputs(TIM1, ENABLE);
|
||
|
}
|
||
|
|
||
|
void Timer9_Init(void)
|
||
|
{
|
||
|
NVIC_InitTypeDef NVIC_InitStructure;
|
||
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||
|
TIM_OCInitTypeDef TIM_OCInitStructure;
|
||
|
|
||
|
/* TIM9 clock enable */
|
||
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, ENABLE);
|
||
|
|
||
|
/* Time base configuration */
|
||
|
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
|
||
|
TIM_TimeBaseStructure.TIM_Prescaler = 5-1; // 20 MHz
|
||
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
||
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||
|
|
||
|
TIM_TimeBaseInit(TIM9, &TIM_TimeBaseStructure);
|
||
|
|
||
|
/* Output Compare Toggle Mode configuration: Channel1 */
|
||
|
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Active;
|
||
|
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;
|
||
|
TIM_OCInitStructure.TIM_Pulse = 0x0FFF;
|
||
|
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
|
||
|
TIM_OC1Init(TIM9, &TIM_OCInitStructure);
|
||
|
|
||
|
TIM_OC1PreloadConfig(TIM9, TIM_OCPreload_Disable);
|
||
|
|
||
|
/* Enable the TIM9 global Interrupt */
|
||
|
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_TIM9_IRQn;
|
||
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
||
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||
|
NVIC_Init(&NVIC_InitStructure);
|
||
|
|
||
|
/* TIM IT enable */
|
||
|
TIM_ITConfig(TIM9, TIM_IT_CC1 | TIM_IT_Update, ENABLE);
|
||
|
|
||
|
/* TIM disable counter */
|
||
|
TIM_Cmd(TIM9, DISABLE);
|
||
|
}
|