网上搜到很多这样的帖子,但只说到建立起一个工程,没说到怎么做出一个可以软件仿真调试的小程序;或者给了段程序代码,没说到怎么配置其他的库。我自己在RVMDK里编译的时候总是报各种各样的链接错误,摸不着头脑。摸索了一阵搞定了,步骤记录如下。
1. 在工程文件夹下添加"src", "lib", "obj", "list"子文件夹.
2. [project]----[new uVision project]---选择STM32F107VC---[ok]----[yes]
3. [Setup file extensions, books and environment] / [poject---manage---commponents, environment, books...]-----[poject commonents]----- change name in [project Targets]-----change to "startup", add "src","lib" in [groups] ----- [ok]
4. [options for target] ----- [output] ----- [select folder for objects] ----- 选为建立的obj子文件夹 ----- [listing] ----- [select folder for listings] ----- 选为建立的list文件夹 ----- [debug] ----- [use simulator] ----- [ok]
5. 找到keil安装目录下的stm32f10x_rcc.c,以及工程中需要用到的其他外设的C文件,拷贝到工程中lib文件夹下。新建main.c到工程文件夹的src文件夹下。我找的一个小例子用到了rcc, gpio和flash,于是把这三个.c拷贝到lib文件夹中。按照步骤3,把文件添加到RVMDK工程中。
6. 在main.c中添加#include "stm32f10x_lib.h"。添加代码。按F7编译。我用的代码是从网上搜的一个类似于跑马灯的小程序,将贴在本文最后。编译过后,每个文件需要链接的头文件会自动添加在该文件的下面。
7. ctrl+F5进入调试,F5至可运行。[peripherals] ----- [General purpose IO] ----- [GPIOB]。可以看到跑马灯的效果。ctrl+F5结束调试。
这个程序是可以跑通了,但是我有点怀疑这个步骤的正确性。按照视频教程的说法,如果做应用程序,需要修改的只有main.c, stm32f10x_conf.h, stm32f10x_it.c。但是上述步骤只有在编译完成之后才会链接到keil安装目录下的conf.g,没有自动链接it.c。而如果不先在conf.h里配置正确的外设,RVMDK是编译不过的。可能在实际工程中这两个文件也需要事先手动添加修改。
视频教程里有一张library的结构图,截不下来,就把其中几个文件的功能罗列如下。
stm32f10x_it.c: 中断处理函数
stm32f10x_lib.c: debug时指向外设结构体的指针
stm32f10x_lib.h: 全局头文件、变量
stm32f10x_map.h: 外设结构体,地址
stm32f10x_type.h: 公用类型、常量
stm32f10x_rcc.h: 时钟,复位外设,用外设时都要用到。
实验所用代码(感谢网上的好心人):
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Version : V1.0
* Date : 10/08/2007
* Description : Main program body
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Local includes ------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void delay(void);
void delay()
{
int i;
for (i=0; i<0xfffff; i++)
;
}
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
while(1)
{
// For STM3210B-LK1
GPIO_SetBits(GPIOB, GPIO_Pin_5);
delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
GPIO_SetBits(GPIOB, GPIO_Pin_6);
delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_6);
GPIO_SetBits(GPIOB, GPIO_Pin_7);
delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
GPIO_SetBits(GPIOB, GPIO_Pin_8);
delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_8);
GPIO_SetBits(GPIOB, GPIO_Pin_7);
delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_7);
GPIO_SetBits(GPIOB, GPIO_Pin_6);
delay();
GPIO_ResetBits(GPIOB, GPIO_Pin_6);
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit(); //复位RCC外部设备寄存器到默认值
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable peripheral clocks --------------------------------------------------*/
// for small target (48pin) in mini-kit
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// for target(48pin) of mini-kit
/* Configure PB.5/6/7/8 for LR3/4/5/6 ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// For STM3210B-LK1, configure PC.04-PC.07 instead
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* 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) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/