187 lines
5.7 KiB
C
187 lines
5.7 KiB
C
/* USER CODE BEGIN Header */
|
||
/**
|
||
******************************************************************************
|
||
* @file tim.c
|
||
* @brief This file provides code for the configuration
|
||
* of the TIM instances.
|
||
******************************************************************************
|
||
* @attention
|
||
*
|
||
* Copyright (c) 2024 STMicroelectronics.
|
||
* All rights reserved.
|
||
*
|
||
* This software is licensed under terms that can be found in the LICENSE file
|
||
* in the root directory of this software component.
|
||
* If no LICENSE file comes with this software, it is provided AS-IS.
|
||
*
|
||
******************************************************************************
|
||
*/
|
||
/* USER CODE END Header */
|
||
/* Includes ------------------------------------------------------------------*/
|
||
#include "tim.h"
|
||
|
||
/* USER CODE BEGIN 0 */
|
||
#include "usart.h"
|
||
/* USER CODE END 0 */
|
||
|
||
/* TIM3 init function */
|
||
void MX_TIM3_Init(void)
|
||
{
|
||
|
||
/* USER CODE BEGIN TIM3_Init 0 */
|
||
|
||
/* USER CODE END TIM3_Init 0 */
|
||
|
||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||
|
||
/* Peripheral clock enable */
|
||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
||
|
||
/* TIM3 interrupt Init */
|
||
NVIC_SetPriority(TIM3_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),0, 0));
|
||
NVIC_EnableIRQ(TIM3_IRQn);
|
||
|
||
/* USER CODE BEGIN TIM3_Init 1 */
|
||
|
||
/* USER CODE END TIM3_Init 1 */
|
||
TIM_InitStruct.Prescaler = 83;
|
||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||
TIM_InitStruct.Autoreload = 999;
|
||
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
||
LL_TIM_Init(TIM3, &TIM_InitStruct);
|
||
LL_TIM_EnableARRPreload(TIM3);
|
||
LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);
|
||
LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
|
||
LL_TIM_DisableMasterSlaveMode(TIM3);
|
||
/* USER CODE BEGIN TIM3_Init 2 */
|
||
|
||
/* USER CODE END TIM3_Init 2 */
|
||
|
||
}
|
||
/* TIM4 init function */
|
||
void MX_TIM4_Init(void)
|
||
{
|
||
|
||
/* USER CODE BEGIN TIM4_Init 0 */
|
||
|
||
/* USER CODE END TIM4_Init 0 */
|
||
|
||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||
|
||
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||
|
||
/* Peripheral clock enable */
|
||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
|
||
|
||
LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOB);
|
||
/**TIM4 GPIO Configuration
|
||
PB6 ------> TIM4_CH1
|
||
PB7 ------> TIM4_CH2
|
||
PB8 ------> TIM4_CH3
|
||
*/
|
||
GPIO_InitStruct.Pin = ENCODER_A_Pin|ENCODER_B_Pin|LL_GPIO_PIN_8;
|
||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
||
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
||
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||
GPIO_InitStruct.Alternate = LL_GPIO_AF_2;
|
||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||
|
||
/* USER CODE BEGIN TIM4_Init 1 */
|
||
|
||
/* USER CODE END TIM4_Init 1 */
|
||
TIM_InitStruct.Prescaler = 0;
|
||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||
TIM_InitStruct.Autoreload = 65535;
|
||
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
||
LL_TIM_Init(TIM4, &TIM_InitStruct);
|
||
LL_TIM_DisableARRPreload(TIM4);
|
||
LL_TIM_SetEncoderMode(TIM4, LL_TIM_ENCODERMODE_X4_TI12);
|
||
LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
||
LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
|
||
LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV1);
|
||
LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
|
||
LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
||
LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
|
||
LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV1);
|
||
LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
|
||
LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
|
||
LL_TIM_DisableMasterSlaveMode(TIM4);
|
||
LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH3, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
||
LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH3, LL_TIM_ICPSC_DIV1);
|
||
LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH3, LL_TIM_IC_FILTER_FDIV1);
|
||
LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH3, LL_TIM_IC_POLARITY_RISING);
|
||
/* USER CODE BEGIN TIM4_Init 2 */
|
||
|
||
/* USER CODE END TIM4_Init 2 */
|
||
|
||
}
|
||
/* TIM7 init function */
|
||
void MX_TIM7_Init(void)
|
||
{
|
||
|
||
/* USER CODE BEGIN TIM7_Init 0 */
|
||
|
||
/* USER CODE END TIM7_Init 0 */
|
||
|
||
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
||
|
||
/* Peripheral clock enable */
|
||
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM7);
|
||
|
||
/* TIM7 interrupt Init */
|
||
NVIC_SetPriority(TIM7_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),2, 1));
|
||
NVIC_EnableIRQ(TIM7_IRQn);
|
||
|
||
/* USER CODE BEGIN TIM7_Init 1 */
|
||
|
||
/* USER CODE END TIM7_Init 1 */
|
||
TIM_InitStruct.Prescaler = 16799;
|
||
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
||
TIM_InitStruct.Autoreload = 39;
|
||
LL_TIM_Init(TIM7, &TIM_InitStruct);
|
||
LL_TIM_EnableARRPreload(TIM7);
|
||
LL_TIM_SetTriggerOutput(TIM7, LL_TIM_TRGO_RESET);
|
||
LL_TIM_DisableMasterSlaveMode(TIM7);
|
||
/* USER CODE BEGIN TIM7_Init 2 */
|
||
|
||
/* USER CODE END TIM7_Init 2 */
|
||
|
||
}
|
||
|
||
/* USER CODE BEGIN 1 */
|
||
|
||
void encode_init(void)
|
||
{
|
||
LL_TIM_CC_EnableChannel(TIM4,LL_TIM_CHANNEL_CH1);
|
||
LL_TIM_CC_EnableChannel(TIM4,LL_TIM_CHANNEL_CH2);
|
||
|
||
LL_TIM_EnableCounter(TIM4);
|
||
}
|
||
|
||
|
||
/*****************************<2A><>дʱ<D0B4><CAB1><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>**********************/
|
||
void modbus_tim_init(void)
|
||
{
|
||
LL_TIM_EnableIT_UPDATE(TIM7);//TIM6<4D><36><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
||
LL_TIM_EnableCounter(TIM7);//TIM6<4D><36><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
||
|
||
}
|
||
|
||
void modbus_tim_callback(void)
|
||
{
|
||
LL_TIM_ClearFlag_UPDATE(TIM7); //<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>֤<EFBFBD><D6A4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4msû<73>н<EFBFBD><D0BD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD>һ֡<D2BB><D6A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
LL_TIM_DisableCounter(TIM7); //<2F>ж<EFBFBD>֮<EFBFBD><D6AE>ֹͣ<CDA3><D6B9>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ν<EFBFBD><CEBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD>ݿ<EFBFBD>ʼ
|
||
usart3.rx_size = usart3.rx_buf_cnt; //<2F><><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
||
usart3.rx_buf_cnt = 0; //<2F><><EFBFBD><EFBFBD>
|
||
usart3.rx_flag = 1; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD>1
|
||
//485ͨ<35><CDA8>
|
||
if(usart3.rx_flag == 1) //<2F><><EFBFBD><EFBFBD>һ֡<D2BB><D6A1><EFBFBD>ݽ<EFBFBD><DDBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><C9A3><EFBFBD><EFBFBD><EFBFBD>tim7<6D>Ļص<C4BB><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>λ
|
||
{
|
||
Modbus_Process();
|
||
usart3.rx_flag=0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɱ<EFBFBD>־λ<D6BE><CEBB><EFBFBD><EFBFBD>
|
||
}
|
||
}
|
||
|
||
/* USER CODE END 1 */
|