搭建机器人电控系统——如何从零开发主控?——文件下载、工程配置、烧录

搭建机器人电控系统——如何从零开发主控?

编译环境的选择
主控的从零开发


如何开发主控?(软件实现烧录控制功能)


确定你选择的MCU参数

我以最常见的STM32F407ZET6为例:

①首先去ST官网上找到对应的单片机型号:

在这里插入图片描述

②找到对应型号的三大关键参数:

在该页面的底部,有STM32F407系列下所有型号的参数:
在这里插入图片描述

我们得到三大关键参数,三大关键参数是我们之后配置软件时非常重要的东西:

  • 产品型号:STM32F407ZET6
  • 内核:Cortex-M4
  • 主频:168MHZ

③找到官网上的例程(固件库),找出里面需要的文件

在这里插入图片描述
点击设计资源——固件和软件
在这里插入图片描述
找到STM32MCU——STM32F4——下载次数(降序)
在这里插入图片描述

找到STM32F4开发板固件,点进去下载这个压缩包,里面就有我们需要的文件,当然你也可以在这里找到某些例程的压缩包,例程里一定也包含对应的文件。
在这里插入图片描述

解压后一般如上图所示,找到Libraries文件夹
在这里插入图片描述

在Libraries文件夹下,我们主要关注CMSIS和STM32F4xx_StdPeriph_Driver文件,从名字也可以看出:CMSIS当时在主控的选择部分介绍是说ARM和芯片生产商共同提出的一套标准;而STM32F4xx_StdPeriph_Driver意为STM32F4系列的标准外设的驱动。

CMSIS——include文件夹:
在这里插入图片描述

在CMSIS的include文件夹下,我们能找到如下文件:
在这里插入图片描述

红色区域内是我们三大关键参数中的内核,我们查到的STM32F4内核是Cortex-M4,所以core_cm4.h是我们需要的文件,而core_cm4_simd.h、core_cmFunc.h、core_cmInstr.h是支撑起CMSIS中Cortex-M4标准的文件。另外绿色框内的文件arm_math.h你也可以考虑进去,这是在当你编程需要用到某些数学运算时需要的头文件,不需要可以不考虑。

CMSIS——ST文件夹:

在这里插入图片描述
在这里插入图片描述

在ST文件夹下找到include文件,里面有两个文件stm32f4xx.h和system_stm32f4xx.h,这两个文件都很重要,之后配置会用到。
在这里插入图片描述
在这里插入图片描述

在ST文件夹的另外两个路径下找到system_stm32f4xx.c和startup_stm32f4xx.s文件。其中
startup_stm32f4xx.s是汇编启动文件,和你的编译环境有关,如果你是IAR的,那么就找IAR路径下的那个,如果你用KEIL那么就找到arm下的那个。

除此之外还需要一个stm32f4xx_conf.h的文件,这个文件里面声明了所有的外设头文件:
在这里插入图片描述

这个文件在压缩包里没有找到,所以将代码贴在此处,复制即可用。
stm32f4xx_conf.h:

/**
  ******************************************************************************
  * @file    Project/STM32F4xx_StdPeriph_Templates/stm32f4xx_conf.h  
  * @author  MCD Application Team
  * @version V1.5.0
  * @date    06-March-2015
  * @brief   Library configuration file.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __STM32F4xx_CONF_H
#define __STM32F4xx_CONF_H

/* Includes ------------------------------------------------------------------*/
/* Uncomment the line below to enable peripheral header file inclusion */
#include "stm32f4xx_adc.h"
#include "stm32f4xx_crc.h"
#include "stm32f4xx_dbgmcu.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_flash.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_i2c.h"
#include "stm32f4xx_iwdg.h"
#include "stm32f4xx_pwr.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_rtc.h"
#include "stm32f4xx_sdio.h"
#include "stm32f4xx_spi.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_wwdg.h"
#include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */

#if defined (STM32F429_439xx) || defined(STM32F446xx)
#include "stm32f4xx_cryp.h"
#include "stm32f4xx_hash.h"
#include "stm32f4xx_rng.h"
#include "stm32f4xx_can.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_dcmi.h"
#include "stm32f4xx_dma2d.h"
#include "stm32f4xx_fmc.h"
#include "stm32f4xx_ltdc.h"
#include "stm32f4xx_sai.h"
#endif /* STM32F429_439xx || STM32F446xx */

#if defined (STM32F427_437xx)
#include "stm32f4xx_cryp.h"
#include "stm32f4xx_hash.h"
#include "stm32f4xx_rng.h"
#include "stm32f4xx_can.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_dcmi.h"
#include "stm32f4xx_dma2d.h"
//#include "stm32f4xx_fmc.h"
#include "stm32f4xx_sai.h"
#endif /* STM32F427_437xx */

#if defined (STM32F40_41xxx)
#include "stm32f4xx_cryp.h"
#include "stm32f4xx_hash.h"
#include "stm32f4xx_rng.h"
#include "stm32f4xx_can.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_dcmi.h"
#include "stm32f4xx_fsmc.h"
#endif /* STM32F40_41xxx */

#if defined (STM32F411xE)
#include "stm32f4xx_flash_ramfunc.h"
#endif /* STM32F411xE */

#if defined (STM32F446xx)
#include "stm32f4xx_qspi.h"
#include "stm32f4xx_fmpi2c.h"
#include "stm32f4xx_spdifrx.h"
#include "stm32f4xx_cec.h"
#endif /* STM32F446xx */


/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/* If an external clock source is used, then the value of the following define 
   should be set to the value of the external clock source, else, if no external 
   clock is used, keep this define commented */
/*#define I2S_EXTERNAL_CLOCK_VAL   12288000 */ /* Value of the external clock in Hz */


/* Uncomment the line below to expanse the "assert_param" macro in the 
   Standard Peripheral Library drivers code */
/* #define USE_FULL_ASSERT    1 */

/* Exported macro ------------------------------------------------------------*/
#ifdef  USE_FULL_ASSERT

/**
  * @brief  The assert_param macro is used for function's parameters check.
  * @param  expr: If expr is false, it calls assert_failed function
  *   which reports the name of the source file and the source
  *   line number of the call that failed. 
  *   If expr is true, it returns no value.
  * @retval None
  */
  #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
/* Exported functions ------------------------------------------------------- */
  void assert_failed(uint8_t* file, uint32_t line);
#else
  #define assert_param(expr) ((void)0)
#endif /* USE_FULL_ASSERT */

#endif /* __STM32F4xx_CONF_H */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

在这里插入图片描述

以上,就是我们需要用到的启动文件,接下来就是有关STM32F4系列的驱动文件。
为了方便起见,最好将他们放在一个文件夹下。(这里命名为了System文件夹)

STM32F4xx_StdPeriph_Driver文件夹:
在这里插入图片描述

在该文件夹下,有两个文件,其中inc是驱动外设的头文件,src是驱动外设对应的函数文件。
在这里插入图片描述
在这里插入图片描述

也就是说,你只需要用到什么外设再把对应的.c和.h文件加到你的工程中即可,并不需要全复制进去。

最后,整理好你的文件夹如下(都是为了之后方便整理文件):
在这里插入图片描述

(文件名字任意取)

  • APP用来放主函数.c和.h文件
  • BSP用来放你之后自己写的.c文件和.h文件
  • ST用来放外设驱动的.c和.h文件
  • System用来放之前的启动文件

新建工程,选择型号,编译运行

在这里插入图片描述
进入界面,首先在右上角点击Project新建工程

在这里插入图片描述
这个时候会有三种选择,一种是汇编ASM,一种是C++,一种是C,通常,我们选择C,以C建议主函数文件main.c,并选择一个路径保存。
在这里插入图片描述

右键点击左侧工程栏中刚建立的工程,选择Option进入工程设置
在这里插入图片描述

然后在Target中选择三大参数中的型号的内核。
在这里插入图片描述

依次在工程栏添加之前新建好的文件夹APP、BSP、ST、System,并将main.c文件放到APP中。

添加.c和.s函数文件:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

依次将对应文件夹中的.c或.s文件添加到对应工程栏文件夹中

添加.h头文件路径:
在这里插入图片描述

来到工程的option栏,在Additional include directories中添加头文件路径
在这里插入图片描述

路径添加到文件夹为止,采用相对路径($$形式,这样的目的是保证你的项目能够在另外一台计算机上打开)
在这里插入图片描述

在Defined symbols中定义:

  • 单片机型号
  • 采用外设驱动
  • 外部晶振HSE_VALUE

他们共同决定了之前我所说开发的三大参数——单片机型号、内核、主频

定义单片机型号的写法和当时ST官网上的系列有关,准确的说,你在system_stm32f4xx.c文件中可以找到(在这个.c文件中有很多英文的说明,看懂了之后对你配置有很大用处):
在这里插入图片描述

关于定义外部晶振的具体数值怎么找,在我的博客时钟配置中有提到,具体数值和单片机原理图有关,对于正点原子的开发板来说,外部晶振就是8MHZ(8000000)。
在这里插入图片描述

最后把整个工程编译一遍,没有问题
在这里插入图片描述

最后在option中选择你的烧录工具(JLINK或者CMSIS【USB烧录】)
在这里插入图片描述


工程烧录检验

编一个最简单的点亮LED灯的程序出来,测试你画好或买好的主控是否有问题:

void LED_Init(void)
{
//初始化PE9 (开发板上与LED灯相连)
	GPIO_InitTypeDef GPIO_InitStruct;
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
	GPIO_InitStruct.GPIO_Mode =	GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_OType =	GPIO_OType_OD;  
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOF,&GPIO_InitStruct);
    GPIO_ResetBits(GPIOF,GPIO_Pin_9);
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值