AN5047_STM8S001J3微控制器的入门指南
AN5047 Application note
Getting started with the STM8S001J3 microcontroller
https://www.stmcu.com.cn/Designresource/design_resource_detail/file/389585/lang/EN/token/f2b78686f2802f8ea6c27a20489d8c24
在文件的第 7 章节对于 STM8S001J3 器件的启动代码配置有 3 点建议:
1. 将芯片内部未拉出的 GPIOs 配置在合适的状态
片上有多支 GPIO 未拉到器件的引脚上,如 PA2, PB0, PB1, PB2, PB3, PB6, PB7, PC1, PC2, PC7, PD0, PD2, PD4, PD7, PE5, PF4。在器件 RESET 之后这些引脚自动会被配置成悬浮输入模式,建议将这些 GPIO 配置成输出模式与低电平状态,这对于减少 EMC 与降低功耗是有帮助的。
2. SWIM 改变引脚复用功能前要先延迟
在前一篇帖子提到过假如不这样做的话,STM8S001J3 编程烧录过一次之后就可能变砖了。我认为这一点是最重要的,在开发的初期手上的器件数量不会太多颗,或是使用开发板调试功能,万一不小心将 STM8S001J3 变砖后就要找热风枪想办法将器件换下来了!
3. 正确的 GPIO 引脚模式设定
STM8S001J3 器件有多支引脚提供了与多个 GPIO 的连接,为某个 GPIO 配置的模式也会影响到相同引脚上的其他 GPIO,因此正确设置 GPIO 模式对于避免 GPIO 之间的冲突是非常重要的。
比如说 PD1,PC6,PD3.PD5 这些 GPIO 都在引脚 8上,在 PD1 做上拉设置后对于 PC6,PD3,PD5 同样是有影响的。
文件里头的启动代码例程:
/* MAIN.C file */
#include "stm8s.h"
#include "stm8s_gpio.h"
#include "stm8s_clk.h"
#ifdef _COSMIC_
#define ASM _asm
#endif
#ifdef _IAR_
#define ASM asm
#endif
// 5秒的延迟
/* This delay should be added just after reset to have access to SWIM pin
and to be able to reprogram the device after power on (otherwise the device
will be locked) */
#define STARTUP_SWIM_DELAY_5S \
{ \
ASM(" PUSHW X \n" \
" PUSH A \n" \
" LDW X, #0xFFFF \n" \
"loop1: LD A, #50 \n" \
\
"loop2: DEC A \n" \
" JRNE loop2 \n" \
\
" DECW X \n" \
" JRNE loop1 \n" \
\
" POP A \n" \
" POPW X " ); \
}
// 将芯片内部未拉出的 GPIOs 配置在输出模式
/* not connected pins as output low state (the best EMC immunity)
(PA2, PB0, PB1, PB2, PB3, PB6, PB7, PC1, PC2, PC7, PD0, PD2, PD4, PD7, PE5, PF4) */
#define CONFIG_UNUSED_PINS_STM8S001 \
{ \
GPIOA->DDR |= GPIO_PIN_2; \
GPIOB->DDR |= GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_6 | GPIO_PIN_7; \
GPIOC->DDR |= GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7; \
GPIOD->DDR |= GPIO_PIN_0 | GPIO_PIN_2 | GPIO_PIN_4 | GPIO_PIN_7; \
GPIOE->DDR |= GPIO_PIN_5; \
GPIOF->DDR |= GPIO_PIN_4; \
}
/* pin for testing */
#define TEST_PORT GPIOA
#define TEST_PIN GPIO_PIN_3
//#define HSE_TEST
/* Example of firmware for STM8S001: recommended startup + test of pins
functionality */
main()
{
/* -------------STM8S001 startup-------------- */
/* configure unbonded pins */
CONFIG_UNUSED_PINS_STM8S001;
/* delay for SWIM connection: ~5seconds */
STARTUP_SWIM_DELAY_5S;
/* ------------------------------------------- */
/* configure all STM8S001 pins as input with pull up */
GPIO_Init(GPIOA, GPIO_PIN_1, GPIO_MODE_IN_PU_NO_IT); // pin 1
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_PU_NO_IT); // pin 5
GPIO_Init(GPIOB, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST); // pin 6 (PB4 has no pull-up - configure it as output low)
GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_IN_PU_NO_IT); // pin 7
GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT); // pin 8
/* disable peripherals clocks to decrease consumption */
CLK->PCKENR1 = 0x00;
CLK->PCKENR2 = 0x00;
/* for testing external HSE clock */
/* make sure that option bit EXTCLK=1 */
#ifdef HSE_TEST
/* test HSE (external clock) - apply input clock on PA1 (pin 1) */
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST); // CCO is on PC4 (pin 7)
CLK_CCOConfig(CLK_OUTPUT_CPU); // clock out on PC4/CCO (pin 7)
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE,
CLK_CURRENTCLOCKSTATE_DISABLE); //set HSE as clock
#endif //HSE_TEST
/* initialize tested pin */
GPIO_Init(TEST_PORT, TEST_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
while (1)
{
/* toggle with tested pin */
GPIO_WriteReverse(TEST_PORT, TEST_PIN);
}
}
[学习笔记] 8引脚的STM8微控制器初步认识-2
[学习笔记] 8引脚的STM8微控制器初步认识
[学习笔记]《使用mbed进行STM32开发及STM32F0的时钟问题》
|
|