可以实现编码器输出同时输出时间单位毫秒

This commit is contained in:
wantong
2024-04-25 16:58:12 +08:00
parent 8d6d5c773d
commit 6fc86e91e9
44 changed files with 3677 additions and 7640 deletions

View File

@ -18,6 +18,7 @@
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
@ -45,14 +46,17 @@
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
volatile uint32_t currentMillis = 0;
int enc1;
volatile uint32_t G_StartMillis = 0;
volatile uint32_t G_CurrentMillis = 0;
int enc;
uint8_t buf[10];
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MPU_Config(void);
/* USER CODE BEGIN PFP */
void data_shift();
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
@ -70,6 +74,9 @@ int main(void)
/* USER CODE END 1 */
/* MPU Configuration--------------------------------------------------------*/
MPU_Config();
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
@ -94,6 +101,7 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
@ -110,10 +118,12 @@ int main(void)
/* USER CODE BEGIN 3 */
enc1 = __HAL_TIM_GET_COUNTER(&htim2);//获取定时器的值
printf("encoder:%d\r\n", enc1);
currentMillis = HAL_GetTick(); //获取当前系统时间
printf("start time:%lu\r\n", currentMillis);
enc = __HAL_TIM_GET_COUNTER(&htim2);//获取定时器的值
G_CurrentMillis = HAL_GetTick()-G_StartMillis;
data_shift();
HAL_UART_Transmit_DMA(&huart1, buf, sizeof(buf));
// printf("start time:%lu\r\n", G_StartMillis);
// printf("current time:%lu\r\n", G_CurrentMillis);
@ -186,8 +196,49 @@ void SystemClock_Config(void)
/* USER CODE BEGIN 4 */
void data_shift(){
buf[0] = (uint8_t)(enc & 0xFF);
buf[1] = (uint8_t)((enc >> 8) & 0xFF);
buf[2] = (uint8_t)((enc >> 16) & 0xFF);
buf[3] = (uint8_t)((enc >> 24) & 0xFF);
buf[4] = G_CurrentMillis & 0xFF;
buf[5] = (G_CurrentMillis >> 8) & 0xFF;
buf[6] = (G_CurrentMillis >> 16) & 0xFF;
buf[7] = (G_CurrentMillis >> 24) & 0xFF;
buf[8] = 0x0d;
buf[9] = 0xff;
}
/* USER CODE END 4 */
/* MPU Configuration */
void MPU_Config(void)
{
MPU_Region_InitTypeDef MPU_InitStruct = {0};
/* Disables the MPU */
HAL_MPU_Disable();
/** Initializes and configures the Region and the memory to be protected
*/
MPU_InitStruct.Enable = MPU_REGION_ENABLE;
MPU_InitStruct.Number = MPU_REGION_NUMBER0;
MPU_InitStruct.BaseAddress = 0x24000000;
MPU_InitStruct.Size = MPU_REGION_SIZE_512KB;
MPU_InitStruct.SubRegionDisable = 0x0;
MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_ENABLE;
MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
MPU_InitStruct.IsCacheable = MPU_ACCESS_CACHEABLE;
MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
HAL_MPU_ConfigRegion(&MPU_InitStruct);
/* Enables the MPU */
HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None