|
由于我刚刚从51转过来STM8,遇到很多问题,同时也得到了群里的前辈帮助,所以分享些经验让后面的人碰到类似问题时可以有个参考。 我现在用的低功耗的STM8L151F2,20PIN、Flash只有4k,对于这种资源缺乏的芯片,对很多基本的东西心里面要有底(有低功耗要求当然要用最低功耗的芯片): 首先,我也是从“标准外设库”开始的。请大家注意标准库版本,我用的开发环境是IAR官网下载的7.3版(限制编译代码大小的许可证),在安装目录下面查找“STM8L15x_StdPeriph_Driver”这个目录就是上面说的“标准外设库”。注意这个库里面最基本的文件“stm8l15x.h”(日期是2015年11月26日)里面提到: #if !defined (STM8L15X_MD) && !defined (STM8L15X_MDP) && !defined (STM8L15X_HD) #error "Please select first the target STM8L device used in your application (in stm8l15x.h file)" #endif 也就是说这个库只支持“中密度”、“中密度+”、“高密度”三种芯片,简单来说就是“只支持flash >=16K的芯片”。如果要支持芯片末尾数字是2或3(flash<16k)的芯片,必须要支持“低密度”的标准外设库。后来我找到2011年6月的库就可以支持低密度设备,头文件里有“LD”的标志。 然后,我就基于这个2011年的库写了一个最简单的循环从串口发出字符串的小程序,结果编译不出来提示说“超过编译器的代码限制”只能先解决许可证的问题,再编译出来的.hex有5k大小当然写不进F2的芯片,然后各种优化,各种剪裁终于编译出4K的.hex文件,烧进芯片里果然能收到串口定时发出的字符串。 但问题来了:这个只实现基本功能的应用已经占去了所有的Flash空间,根本没空间再容纳其他功能的代码。幸好根据我在51上面的经验,只需要直接操作,就能节省大量的代码空间,比如初始化系统时钟只需要这样: CLK_ICKCR=0x01; //打开内部高速时钟HSI(16MHz); CLK_SWR =0x01; //系统时钟源使用内部高速时钟HSI;0x01=HSI,0x02=LSI,0x04=HSE,0x08=LSE其余保留 CLK_CKDIVR=0x03; //系统8分频,使之工作在2MHz的频率下(0=1,1=2,2=4,3=8,4=16) 最后如果有低功耗的要求,参考手册中的一段话应该留意: Unused I/O pins must not be left floating to avoid extra current consumption. They must be put into one of the following configurations: • connected to VDD or VSS by external pull-up or pull-down resistor and kept as input floating (reset state), • configured as input with internal pull-up/down resistor, • configured as output push-pull low. 如果要省电,没有使用的管脚要设置为下面三种模式之一: *外部接上拉或下拉,内部保持在输入模式; *设为输入模式,内部设为上拉或下拉 *设为上拉输出,并置为低。 |
STM8 的MCU有四个供电单元
【STM8-SO8】08-STM8L001J3的点灯
STM8单片机如何实现Bootloader
基于STM8的DALI (数字可寻址调光协议)
开源基于STM32的STM8脱机编程器
【ST MCU实战经验】之STM8中UART奇偶校验的使用方法
【思修电子STM8集合贴】龙顺宇STM8理论/实战视频/书籍/软件/
初次尝试STM8S001J3
分享STM8 风驰光盘的资料,是完整的(包括原理图+例程+PDF注释)
基于STM8的实验代码汇总分享
微信公众号
手机版
但对我来说却是过五关斩六将之后才知道的。
在修改日期为2011-05-16的stm8l15x.h文件中有解释:
/* #define STM8L15X_LD */ /*!< STM8L15X_LD: STM8L15x Low density devices */
/* #define STM8L15X_MD */ /*!< STM8L15X_MD: STM8L15x Medium density devices */
/* #define STM8L15X_MDP */ /*!< STM8L15X_MDP: STM8L15x Medium density plus devices */
/* #define STM8L15X_HD */ /*!< STM8L15X_HD: STM8L15x/16x High density devices */
/* Tip: To avoid modifying this file each time you need to switch between these
devices, you can define the device in your toolchain compiler preprocessor.
- Low density STM8L15x devices are STM8L151C3, STM8L151K3, STM8L151G3, STM8L151F3,
STM8L151C2, STM8L151K2, STM8L151G2 and STM8L151F2 microcontrollers where the
Flash memory density ranges between 4 and 8 Kbytes.
- Medium density STM8L15x devices are STM8L151C4, STM8L151C6, STM8L152C4,
STM8L152C6, STM8L151K4, STM8L151K6, STM8L152K4, STM8L152K6, STM8L151G4,
STM8L151G6, STM8L152G4 and STM8L152G6 microcontrollers where the Flash memory
density ranges between 16 and 32 Kbytes.
- Medium density Plus devices are STM8L151R6, STM8L152R6 microcontrollers where
the Flash memory density is fixed and equal to 32 Kbytes and a wider range of
peripheral than the medium density devices.
- High density STM8L15x devices are STM8L151x8, STM8L152x8, STM8L162R8 and STM8L162M8
microcontrollers where the Flash memory density is fixed and equal to 64 Kbytes with
the same peripheral set than Medium Density Plus devices.
*/
应该是指flash记忆体的密度是4K和8K的芯片。
在修改日期为2015-11-26的stm8l15x.h的支持设备表里面就没有“LD”设备了,这里就不贴出来了。
用这个库写出来的东西很容易就超过4K了,就算用8K的设备也没多少空间放代码,建议使用小flash芯片的还是老老实实研究各个寄存器的用法。
是代码密度吗?