【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码

stm32f103c6t6蓝牙车,从硬件设计到代码编写手把手教学,附源码

文章目录

  • 前言
  • 一、STM32CUBEMX配置
  • 二、Fusion360(工业设计)
  • 三、立创EDA(硬件设计)
  • 四、控制算法(源代码)
    • 1.引入GPIO库,小车motor端
    • 2.引入UART库,小车蓝牙端
    • 3.主函数
  • 总结


前言

STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码

很少有STM32F103C6T6芯片的操作说明,最近芯片大涨,我用STM32F103C6T6简单的做了个蓝牙小车,供大家参考使用。源码在后面。走过的朋友一键三连哦。

使用工具:
工业设计:Fusion360设计外壳
PCB设计: 立创EDA+嘉立创
控制器:STM32CUBEMX
驱动器:DRV8833
电机:普通的直流电机
控制算法:无,简单的IO口打开关断

【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第1张图片设计出来的总体是这样,

stm32f103c6t6蓝牙小车车


提示:以下是本篇文章正文内容,下面案例可供参考

一、STM32CUBEMX配置

用stm32cubemx玩新的芯片是比较友好的,以下是设置步骤

【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第2张图片【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第3张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第4张图片【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第5张图片

【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第6张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第7张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第8张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第9张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第10张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第11张图片
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第12张图片

二、Fusion360(工业设计)

【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第13张图片车身
车轮胎

三、立创EDA(硬件设计)

【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第14张图片原理图
【DIY】STM32F103C6T6蓝牙车,从硬件设计到代码编写手把手教学,附源码_第15张图片3D图

四、控制算法(源代码)

1.引入GPIO库,小车motor端

代码如下(示例):

void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, car0_Pin|car1_Pin|car2_Pin|car3_Pin, GPIO_PIN_SET);

  /*Configure GPIO pin : PtPin */
  GPIO_InitStruct.Pin = LED1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LED1_GPIO_Port, &GPIO_InitStruct);

  /*Configure GPIO pins : PBPin PBPin PBPin PBPin */
  GPIO_InitStruct.Pin = car0_Pin|car1_Pin|car2_Pin|car3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

GPIO端口配置

2.引入UART库,小车蓝牙端

代码如下(示例):

/**
  ******************************************************************************
  * @file    usart.c
  * @brief   This file provides code for the configuration
  *          of the USART instances.
  ******************************************************************************
  * @attention
  *
  * 

© Copyright (c) 2022 STMicroelectronics. * All rights reserved.

* * 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 * ****************************************************************************** */
/* Includes ------------------------------------------------------------------*/ #include "usart.h" /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ UART_HandleTypeDef huart1; /* USART1 init function */ void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(uartHandle->Instance==USART1) { /* USER CODE BEGIN USART1_MspInit 0 */ /* USER CODE END USART1_MspInit 0 */ /* USART1 clock enable */ __HAL_RCC_USART1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USART1 interrupt Init */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_MspInit 1 */ } } void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) { if(uartHandle->Instance==USART1) { /* USER CODE BEGIN USART1_MspDeInit 0 */ /* USER CODE END USART1_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_USART1_CLK_DISABLE(); /**USART1 GPIO Configuration PA9 ------> USART1_TX PA10 ------> USART1_RX */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); /* USART1 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspDeInit 1 */ /* USER CODE END USART1_MspDeInit 1 */ } } /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

UART串口通信配置

3.主函数

代码如下(示例):

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * 

© Copyright (c) 2022 STMicroelectronics. * All rights reserved.

* * 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 "usart.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 */ #define LED_Pin GPIO_PIN_13 #define LED_GPIO GPIOC //#define LED_Pin0 GPIO_PIN_12 //#define LED_GPIO0 GPIOB //#define LED_Pin1 GPIO_PIN_13 //#define LED_GPIO1 GPIOB //#define LED_Pin2 GPIO_PIN_14 //#define LED_GPIO2 GPIOB //#define LED_Pin3 GPIO_PIN_15 //#define LED_GPIO3 GPIOB //#define LED_Pin4 GPIO_PIN_11 //#define LED_GPIO4 GPIOA //#define LED_Pin5 GPIO_PIN_12 //#define LED_GPIO5 GPIOA //#define LED_Pin6 GPIO_PIN_15 //#define LED_GPIO6 GPIOA //#define LED_Pin7 GPIO_PIN_18 //#define LED_GPIO7 GPIOA /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ uint8_t aTxStartMessages[] = "\r\n******Please enter your choic******\r\nLED_ON:1,LED_OFF:0:\r\n"; uint8_t LEDON[]="\r\nLED_ON\r\n"; uint8_t LEDOFF[]="\r\nLED_OFF\r\n"; uint8_t aRxBuffer[1]; void SystemClock_Config(void); /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* 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_USART1_UART_Init(); /* USER CODE BEGIN 2 */ HAL_UART_Receive_IT(&huart1,(uint8_t*)aRxBuffer,1); HAL_UART_Transmit_IT(&huart1 ,(uint8_t*)aTxStartMessages,sizeof(aTxStartMessages)); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // HAL_Delay(300); // HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET); // HAL_Delay(300); // HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET); // // HAL_UART_Transmit(&huart1, (uint8_t *)"hello windows!!!\r\n", 16 , HAL_MAX_DELAY); // HAL_Delay(100); // HAL_UART_Receive_IT(&huart1,(uint8_t*)aRxBuffer,1); } /* USER CODE END 3 */ } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { /* Prevent unused argument(s) compilation warning */ UNUSED(huart); /* NOTE: This function Should not be modified, when the callback is needed, the HAL_UART_TxCpltCallback could be implemented in the user file */ if(aRxBuffer[0]=='1')//Í£Ö¹ { HAL_GPIO_WritePin(GPIOB,car0_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,car1_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,car2_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,car3_Pin,GPIO_PIN_SET); } else if(aRxBuffer[0]=='2')//Ç°½ø { HAL_GPIO_WritePin(GPIOB,car0_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,car1_Pin,GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOB,car2_Pin,GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,car3_Pin,GPIO_PIN_RESET); } else if(aRxBuffer[0]=='3') { HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_RESET); HAL_UART_Transmit(&huart1,(uint8_t*)LEDON,sizeof(LEDON),0xFFFF); } else if(aRxBuffer[0]=='4') { HAL_GPIO_WritePin(LED1_GPIO_Port,LED1_Pin,GPIO_PIN_SET); HAL_UART_Transmit(&huart1,(uint8_t*)LEDOFF,sizeof(LEDOFF),0xFFFF); } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses 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_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* 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 */ __disable_irq(); while (1) { } /* 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, ex: 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****/

总结

本文仅仅简单介绍了STM32F103C6T6芯片控制蓝牙车的使用,评论区欢迎讨论。

你可能感兴趣的:(stm32,c语言,嵌入式硬件)