STM32CubeIDE :第一个stm32程序和找不到设备的解决方法

今年都2020年了,有很多开发stm32的IDE可供选择:MDK,IAR,Eclipse+GCC,STM32CubeMX等等。 我选择用 STM32CubeIDE ,虽然在一开始的时候出现了Target Not Found的烦人的问题,不过总体来说这个IDE是很好用的,具体有以下优点:

  • 界面友好,配置、写代码、编译、下载、调试一条龙服务(all-in-one);
  • 可视化的配置过程,点点鼠标就可以完成芯片配置;
  • 代码自动补全;
  • 支持Windows,Linux,macOS;
  • 免费,免费,免费!

ST官网有下载链接,不过下载需要填写一些信息和提供邮箱,过程较为繁琐。大家可以在这里下载到软件包(20200220版,Windows64位):https://cloud.fishercat.top/s/8FKdG5mXTebmeAH,密码是:stm32

安装过程一路点Next就好,需要注意的是不能在中文路径下安装

第一个STM32程序:点亮LED

我用的是一块STM32F103C8T6最小核心板,是这个样子的:

stm32f103c8t6

附上两张引脚图,在开发的时候很实用:

gpio picture
gpio picture
图片来自 http://news.eeworld.com.cn/mcu/2019/ic-news031843552.html

打开STM32CubeIDE,点右上角的File-New-STM32 Project新建工程。之后会下载更新硬件库,速度很慢,可以取消掉直接进入Target Selection:

右下方选择STM32F103C8或者你自己的硬件,按Next。之后就是填写工程名,选择C/C++等步骤,按着提示来就行了。

新建完工程后就可以看到这个界面:

左边的是Project Explorer,main.c就在Core-Src里。Demo.ioc是可视化配置文件,如右边所示。

点击引脚就可以很方便地配置引脚了。我用的这块板PC13口是接着一个LED的,因此我把PC13设成GPIO_Output。

点击界面左侧的System Core, RCC和SYS是必须要配置的。单击进入对应的配置界面。

HSE选Crystal/Ceramic Resonator(外部高速晶振)。

SYS的Debug要选Serial Wire(如果用SWD的话),默认No Debug,No Debug就无法调试、下载程序到stm32上了(这是第一个坑)。用其他调试器就选相应的调试器。

接下来配置时钟。上面选了HSE,这里要进行相应的配置。箭头所指的四处就是要修改的地方(我的板用的是72MHz)。

Project Manager-Code Generator里勾上Keep User Code when re-generating,这样就可以在生成代码的时候保存之前写过的代码了(代码要写在对应的地方才不会消失,后面说)。

配置文件就到此,基本就是鼠标点点点,很简单。接下来点击Project-Generate Code生成代码。

在Core-Src中的main.c源文件中添加代码:

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();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  HAL_Delay(100);
	  HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13);
  }
  /* USER CODE END 3 */
}

把代码写在/* USER CODE BEGIN */ /* USER CODE END */ 中就可以在重新生成代码的时候保留下来了。写代码的时候可以用Alt+/来进行代码补全(和Eclipse一样)

ctrl+B可以快捷编译,或者点击Project中的Build All来编译工程。编译没问题后,连接好调试器、开发板、计算机,点击Run中的Run或者点击运行的图标(绿圆里面有个三角形那个),即可把编译好的二进制文件下载到开发板中,开发板上的LED灯开始闪烁。成功了,撒花~~~

No device found on target?Target is not responding, retrying?oh no……

我觉得大部分刚开始开发stm32的人都会遇到一个令人头疼的问题:编译通过了,下载运行或调试却失败了!我就遇到了No device found on target和Target is not responding, retrying的错误提示。我是用ST-LINK Utility擦除STM32的flash上的数据后解决的。 ST-LINK Utility 一开始也不能识别我的设备,我是按着复位键后按Connect然后马上松开才识别到了。擦除数据后再烧写就没问题了,也不再需要按着复位键了。问题得到解决。

ST-LINK Utility 下载地址(速度挺快的): https://www.st.com/zh/development-tools/stsw-link004.html

这篇文章有 18 个评论

  1. download

    Really enjoyed this post. Much thanks again. Really Cool. Isabella Sigismundo Ontina

  2. yify

    Hi there, this weekend is pleasant in support of me, since this time i am reading this great informative piece of writing here at my home. Kym Duffy Jodee

  3. indir

    As a Newbie, I am constantly exploring online for articles that can benefit me. Lynne Miguel Bertold

  4. torrent

    Some really quality articles on this web site , saved to bookmarks . Ursula Nathaniel Berte

  5. indir

    Thanks-a-mundo for the article post. Really thank you! Cool. Lethia Valdemar Bevvy

  6. yetiskin

    Wow, this piece of writing is fastidious, my younger sister is analyzing these things, therefore I am going to convey her. Tasha Cullen Barton

  7. turkce

    If some one wishes to be updated with most recent technologies after that he must be go to see this web site and be up to date everyday. Marabel Reinaldo Bara

  8. turkce

    You have brought up a very fantastic details , thanks for the post. Ferdinande Greg Cathleen

  9. yabanci

    Post writing is also a excitement, if you be acquainted with afterward you can write otherwise it is complex to write. Leelah Sawyere Agata

  10. turkce

    It draws the other team to one side of the field while you quickly switch the ball to the other side when they target you. Theadora Cosmo Aletha

  11. sikis izle

    Way cool! Some extremely valid points! I appreciate you penning this article plus the rest of the site is also really good. Kriste Bronnie Gelasias

发表评论