Keil下建立STM32库函数工程
 2015.03.09    |      STM32    |     AilsonJack    |     暂无评论    |     2719 views
广告位C招租:1
By: Ailson Jack
Date: 2015-03-09
个人博客: http://www.only2fire.com/
<p style="text-indent: 2em;">今天给大家讲讲如何在Keil下建立STM32的库函数工程,还不会的博友们,可以来看看,带图教程哦。。。<img src="/UEditor/dialogs/emotion/images/face/i_f29.gif"/>好了,废话不多说,直接进入今天的主题吧。<br/><span style="color: rgb(255, 0, 0);">注:</span>在看这个教程之前,先到教程底部把文件下载了,待会要用的哟<img src="/UEditor/dialogs/emotion/images/face/i_f01.gif"/>。</p><p class="artical_littlestyle1">1、在桌面上创建Test文件夹,并且在Test文件夹下面创建如下图所示文件<br/></p><p style="text-indent: 0em;"><span style="color: rgb(255, 0, 0);">注: </span><span style="color: rgb(0, 112, 192);">CORE</span>用来存放核心文件和启动文件;<br/>&nbsp;&nbsp; <span style="color: rgb(0, 112, 192);">OBJ</span>是用来存放编译过程文件以及hex文件;<br/>&nbsp;&nbsp; <span style="color: rgb(0, 112, 192);">LIB</span>用来存放 ST 官方提供的库函数源码文件;<br/>&nbsp;&nbsp; <span style="color: rgb(0, 112, 192);">USER</span>目录除了用来放工程文件外,还用来存放主函数文件main.c,以及其他包括 system_stm32f10x.c等等。<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087375918122.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087375918122.png&#39;)"/></p><p class="artical_littlestyle2">2、打开Keil创建工程</p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(1)、</span>点击 Keil的菜单:Project–&gt;New Uvision Project,然后将目录定位到刚才建立的文件夹Test之下的USR目录,工程命名为Test,点击保存:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087372705659.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087372705659.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373973537.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373973537.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(2)、</span>接下来会出现一个选择Device的界面,就是选择我们的芯片型号,这里我们定位到STMicroelectronics下面的STM32F103ZE(这是我使用的STM32芯片的型号,如果你是其他芯片,请选择对应的型号即可),点击OK按钮:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373208030.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373208030.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(3)、</span>弹出如下图对话框,我们选择”否”:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373543305.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373543305.png&#39;)"/></p><p class="artical_littlestyle3">3、将官方的固件库包里的源码文件复制到我们的工程目录文件夹下面</p><p style="text-indent: 2em;">上面已经创建好了一个工程,接下来就需要将将官方的固件库包里的源码文件复制到我们的工程目录文件夹下面。</p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(1)、</span>打开官方固件库包,定位到我们准备好的固件库包的目录 STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\STM32F10x_StdPeriph_Driver下面,将目录下面的src,inc文件夹拷贝到我们刚才建立的LIB文件夹下面。</p><p style="text-indent: 2em;"><span style="background-color: rgb(255, 255, 255); color: rgb(255, 0, 0);">注:</span><span style="color: rgb(0, 112, 192);">src</span>存放的是固件库的.c文件,<span style="color: rgb(0, 112, 192);">inc</span>存放的是对应的.h文件。</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373787531.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373787531.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087372137543.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087372137543.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(2)、</span>打开官方固件库包,定位到目录STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\CoreSupport下面,将文件<span style="color: rgb(0, 112, 192);">core_cm3.c</span>和文件<span style="color: rgb(0, 112, 192);">core_cm3.h</span>复制到CORE下面去。然后定位到目录STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\arm下面,将里面<span style="color: rgb(0, 112, 192);">startup_stm32f10x_hd.s</span>文件复制到CORE下面。<br/>现在我们的CORE文件夹下面的文件如下:<span style="color: rgb(255, 0, 0);"><br/></span></p><p style="text-indent: 0em;"><span style="color: rgb(255, 0, 0);">注:</span>对于103系列,主要是用其中3个启动文件(请根据你的芯片容量选择):<br/></p><p><span style="color: rgb(0, 112, 192);">startup_stm32f10x_ld.s</span>:适用于小容量产品(flash≤32K)<br/></p><p style="text-indent: 0em;"><span style="color: rgb(0, 112, 192);">startup_stm32f10x_md.s</span>:适用于中等容量产品(64K≤flash≤128K)<br/><span style="color: rgb(0, 112, 192);">startup_stm32f10x_hd.s</span>:适用于大容量产品(flash≥256K)</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373441492.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373441492.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(3)、</span>打开官方固件库包,定位到目录STM32F10x_StdPeriph_Lib_V3.5.0\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x下面,将里面的三个文件<span style="color: rgb(0, 112, 192);">stm32f10x.h</span>,<span style="color: rgb(0, 112, 192);">system_stm32f10x.c</span>,<span style="color: rgb(0, 112, 192);">system_stm32f10x.h</span>,复制到我们的USER目录之下。然后将<br/>STM32F10x_StdPeriph_Lib_V3.5.0\Project\STM32F10x_StdPeriph_Template下面的 4 个文件<span style="color: rgb(0, 112, 192);">main.c</span>,<span style="color: rgb(0, 112, 192);">stm32f10x_conf.h</span>,<span style="color: rgb(0, 112, 192);">stm32f10x_it.c</span>,<span style="color: rgb(0, 112, 192);">stm32f10x_it.h</span>复制到USER目录下面<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087374183096.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087374183096.png&#39;)"/></p><p style="text-indent: 2em;">至此我们将需要的固件库相关文件复制到了我们的工程目录下面,下面我们将这些文件加入我们的工程中去。<br/></p><p class="artical_littlestyle4">4、工程相关配置</p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(1)、</span>右击Source Group 1,点击Remove Group &#39;Source Group 1’ and its Files,弹出的对话框选择”是”,如下图:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373213553.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373213553.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373133382.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373133382.png&#39;)"/></p><p style="text-indent: 2em;">右击Target,点击Add Group,设置其名为USR,如下图:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087373950917.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087373950917.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087398586141.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087398586141.png&#39;)"/></p><p style="text-indent: 2em;">按照上述步骤,添加LIB,CORE,如下图:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087398204633.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087398204633.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(2)、</span>下面我们往Group里面添加我们需要的文件</p><p style="text-indent: 2em;">右击USR,点击Add Existing Files to Group‘USR&#39;...,在USR目录下,添加所有的.c文件:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087398760401.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087398760401.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087398955324.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087398955324.png&#39;)"/></p><p style="text-indent: 2em;">按照上述操作,将CORE目录下的.c文件与.s文件添加到CORE组中;将LIB目录下的src文件中你用得到的.c文件添加到LIB组中,这里我都添加进去:</p><p style="text-indent: 2em;">添加完成后,各个组中的文件如下图:<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087398627002.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087398627002.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087398708148.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087398708148.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087399579838.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399579838.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(3)、</span>点击Keil中的<img src="/uploads/AilsonJack/2018.08.12/1534087399775907.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399775907.png&#39;)"/>图标,然后点击“Output”选项下面的“Select folder for objects...”,然后选择目录为我们上面新建的OBJ目录,并且勾选Create HEX File;接着点击“Listing”选项下面的“Select folder for Listings...”,然后也选择我们上面新建的OBJ目录:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087399296725.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399296725.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461778411.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461778411.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087399133742.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399133742.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087399536544.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399536544.png&#39;)"/></p><p style="text-indent: 2em;"><span style="background-color: rgb(195, 214, 155);">(4)、</span>点击Keil中的<img src="/uploads/AilsonJack/2018.08.12/1534087399775907.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399775907.png&#39;)"/>图标,然后点击c/c++选项.然后点击Include Paths右边的按钮。弹出一个添加path的对话框,然后我们将上面的3个目录添加进去,将”STM32F10X_HD,USE_STDPERIPH_DRIVER”(逗号是英文状态下的逗号)输入到Define 输入框里面,具体如下图:</p><p style="text-indent: 2em;"><span style="color: rgb(255, 0, 0);">注:</span>你用的是中容量那么STM32F10X_HD修改为STM32F10X_MD,小容量修改为STM32F10X_LD<br/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087399313604.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087399313604.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461310470.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461310470.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461784387.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461784387.png&#39;)"/></p><p style="text-indent: 2em;">然后点击OK退出。</p><p class="artical_littlestyle1">5、编译程序</p><pre class="brush:cpp;toolbar:false PrismJs">#include&nbsp;&quot;stm32f10x.h&quot; void&nbsp;Delay(u32&nbsp;count) { &nbsp;&nbsp;&nbsp;&nbsp;u32&nbsp;i=0; &nbsp;&nbsp;&nbsp;&nbsp;for(;i&lt;count;i++); } int&nbsp;main(void) { &nbsp;&nbsp;&nbsp;&nbsp;GPIO_InitTypeDef&nbsp;GPIO_Pin; &nbsp;&nbsp;&nbsp;&nbsp;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,&nbsp;ENABLE);//使能PA端口时钟 &nbsp;&nbsp;&nbsp;&nbsp;GPIO_Pin.GPIO_Pin=GPIO_Pin_0; &nbsp;&nbsp;&nbsp;&nbsp;GPIO_Pin.GPIO_Mode=GPIO_Mode_Out_PP;//IO口为推挽输出模式 &nbsp;&nbsp;&nbsp;&nbsp;GPIO_Pin.GPIO_Speed=GPIO_Speed_50MHz;//IO口速度为50MHz &nbsp;&nbsp;&nbsp;&nbsp;GPIO_Init(GPIOA,&amp;GPIO_Pin);//初始化PA口 &nbsp;&nbsp;&nbsp;&nbsp;while(1) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO_SetBits(GPIOA,GPIO_Pin_0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Delay(3000000); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GPIO_ResetBits(GPIOA,GPIO_Pin_0); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Delay(3000000); &nbsp;&nbsp;&nbsp;&nbsp;} }</pre><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461441518.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461441518.png&#39;)"/></p><p style="text-indent: 2em;">点击下图所示的编译图标,编译程序:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461596727.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461596727.png&#39;)"/></p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461322255.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461322255.png&#39;)"/></p><p style="text-indent: 2em;">上述程序实现了PA0高低电平的切换,其仿真结果如下图:</p><p style="text-align:center"><img src="/uploads/AilsonJack/2018.08.12/1534087461379567.png" onclick="preview_image(&#39;/uploads/AilsonJack/2018.08.12/1534087461379567.png&#39;)"/></p><p style="text-indent: 0em;">&nbsp;&nbsp; 好了至此,基于Keil的STM32的库函数工程建立完了,大家如果还有什么不明白的,可以留言提问。</p><p style="text-indent: 0em;">&nbsp;&nbsp; 这里是我建立的工程及官方库函数,请下载:<a class="btn btn-success" href="https://pan.baidu.com/s/1jGpDPrC#list/path=%2F" target="_blank">点此下载</a></p>
欢迎关注博主的公众号呀,精彩内容随时掌握:
热情邀请仔细浏览下博客中的广告,万一有对自己有用或感兴趣的呢。◕ᴗ◕。。
如果这篇文章对你有帮助,记得点赞和关注博主就行了^_^,当然了能够赞赏博主,那就非常感谢啦!
注: 转载请注明出处,谢谢!^_^
广告位C招租:2
广告位C招租:3
转载请注明来源: 本文链接:  By: AilsonJack
Keil下建立STM32库函数工程  |  说好一起走
广告位D招租:1
广告位D招租:2
暂无评论,要不要来个沙发
发表评论

 
广告位E招租
Copyright © 2015~2021  说好一起走   保留所有权利   |  百度统计  蜀ICP备15004292号