1. 程式人生 > >stm32之keil開發環境搭建

stm32之keil開發環境搭建

  1.     只要按照下面的一步步來,絕對能從0開始建立一個STM32工程。不僅包括工程建立過程,還有Jlink設定方法。本文使用晶片為STM32F103CB。

    1 下載stm32F10x的官方庫

  1. 2 新建工程

工程名設為stm32_demo,選擇晶片型號為STM32F103B,如圖,

因為下載的stm32庫中有啟動程式碼,所以這裡選擇"",不拷貝啟動程式碼。

在工程檔案下,新建Startup HeadersUserLibrariesCMSISListsOutput資料夾。

資料夾

用途

Startup

啟動檔案,Flash16~32Kb小容量,

64~128Kb中容量,256~512Kb大容量

CMSIS

Cortex微控制器軟體介面標準檔案,該目錄下檔案適用所有Cortex系列

Libraries

存放stm32的驅動庫檔案

Headers

自定義的全域性標頭檔案

User

使用者檔案,我們把main.c放在該目錄下

Lists

編譯過程中產生的檔案

Output

編譯後輸出檔案,hex/bin等可執行屬性的檔案將儲存在該目錄下

至此,stm32的工程檔案結構如下

  1. 3 庫檔案拷貝

把下載stm32庫中檔案拷貝到新建工程中

stm32F10x的官方庫

工程

Libraries\STM32F10x_StdPeriph_Driver\inc 庫標頭檔案

Libraries\STM32F10x_StdPeriph_Driver\src 庫原始檔

Libraries

Project\STM32F10x_StdPeriph_Template\main.c

Project\STM32F10x_StdPeriph_Template\stm32f10x_it.c 中斷函式檔案

User

Project\STM32F10x_StdPeriph_Template\stm32f10x_it.h 中斷函式標頭檔案

Project\STM32F10x_StdPeriph_Template\stm32f10x_conf.h 

配置檔案

Headers

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm\* 啟動檔案

Startup

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\stm32f10x.h

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\ system_stm32f10x.c

Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\system_stm32f10x.h

CMSIS

Libraries\CMSIS\CM3\CoreSupport\core_cm3.c Cortex-M3系統檔案

Libraries\CMSIS\CM3\CoreSupport\core_cm3.h

CMSIS

檔案拷貝完成後的工程檔案目錄結構如下:

  1. 4 將檔案新增到工程

點選Keil右上角的工程分組按鈕,在Group一列新增分組,分組和工程的檔名可以一一對應。

  1. 5 工程配置

點選右上角的工程配置按鈕,彈出對話方塊,有多個選項卡,按照下面截圖逐一配置。

  1. 6 編譯連結

原來的main.c從庫檔案中拷貝過來的,把其中的內容都刪除,新增最簡單的main函式:

#include "stm32f10x.h"
int main(void)
{
    while(1) {
 
    }
}

修改配置檔案stm32f10x_conf.h,通過註釋新增或取消註釋刪除需要的功能模組,這裡根據自己需要配置。

編譯

  1. 7 Jlink除錯配置

接上Jlink及開發板,

至此,STM32的工程搭建和Jlink除錯配置都設定好了。為了方便,在工程成中添加了includes.htypes.hgpio_bits.h等提供基本功能。

  1. 8 測試程式——LED流水燈程式

在工程檔案目錄下新建DriversDevices資料夾,

Drivers

存放stm32相關的驅動,比如:延時函式等

Devices

存放開發板上涉及的硬體裝置相關程式碼

工程中File->New,新建下面的一些檔案。

Drivers/delay.h

延時函式標頭檔案

Drivers/delay.c

延時函式原始檔

Devices/led.h

LED流水燈標頭檔案

Devices/led.c

LED流水燈原始檔

注:作為一種好的習慣,每個.c檔案都應該有一個對應的.h檔案。

新增程式碼:

led.c

/*
* LED example
* Author : xiahouzuoxin
* Date : 2013.08
*/
#include "LED.h"
 
GPIO_InitTypeDef LED_InitStructure;
 
/********************************************************************
Function Name: Init_LED
Author : 夏侯佐鑫
Date : 2011-09-28
Description :
Inputs : None
Outputs : None
Notes :
Revision :
********************************************************************/
void InitLED(void)
{
//使能PA埠時鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 
    //埠配置推輓輸出
    LED_InitStructure.GPIO_Pin = LED1 | LED2 | LED3 | LED4;
    LED_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    LED_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
    GPIO_Init(GPIOA, &LED_InitStructure);
 
    //初始輸出高電平
    GPIO_SetBits(GPIOA, LED1 | LED2 | LED3 | LED4);
}
 
/********************************************************************
Function Name: Flash_LED
Author : xiahouzuoxin
Date : 2011-09-28
Description :
Inputs : None
Outputs : None
Notes :
Revision :
********************************************************************/
void FlashLED(void)
{
GPIO_SetBits(GPIOA, LED2 | LED3 | LED4);
    GPIO_ResetBits(GPIOA, LED1);
    delay_ms(500);
GPIO_SetBits(GPIOA, LED1 | LED3 | LED4);
    GPIO_ResetBits(GPIOA, LED2);
    delay_ms(500);
GPIO_SetBits(GPIOA, LED1 | LED2 | LED4);
    GPIO_ResetBits(GPIOA, LED3);
    delay_ms(500);
GPIO_SetBits(GPIOA, LED1 | LED2 | LED3);
    GPIO_ResetBits(GPIOA, LED4);
    delay_ms(500);
}

led.h

/*
* LED example
* Author : xiahouzuoxin
* Date : 2013.08
*/
#ifndef __LED_H__
#define __LED_H__
 
#include "../Headers/includes.h"
 
#define LED1                      GPIO_Pin_4
#define LED2                      GPIO_Pin_5
#define LED3                      GPIO_Pin_6
#define LED4                      GPIO_Pin_7
 
extern void InitLED(void);
extern void FlashLED(void);
 
#endif

delay.h

#ifndef _DELAY_H
#define _DELAY_H
 
#include "includes.h"
 
extern void delay_ms(UINT32 ms);
 
#endif

delay.c

#include "delay.h"
 
/********************************************************************* DELAY_MS
* Discription : delay for 1 ms if ms=1, not accurate
* Author : xiahouzuoxin
* data : 2012-08-01
* inputs :    ms -- ms number
* outputs :
* Modified :
********************************************************************/
void delay_ms(UINT32 ms)
{
int i = 0;
    int j = 0;
 
for(i = 0; i < ms; i++)
{
for(j = 0; j < 8040; j++)
{
// Delay n ms
}
}
}

includes.h

#include "../Devices/LED.h"

main.c

#include "includes.h"
 
int main(void)
{
InitLED();
while(1) {
     FlashLED();
}
}

編譯下載,執行,OK