相信很多朋友在STM8的开发中很多是使用官方的固件库来进行开发的,这样开发速度快,并且程序不易出错。
前面的博客讲到了如何在STVD中新建一个新的STM8L151的工程,最后新建成功后,出现下面的工程目录:
在STM8_interrupt_vector.c 文件中,代码如下:
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices * Copyright (c) 2007 STMicroelectronics */ typedef void @far (*interrupt_handler_t)(void); struct interrupt_vector { unsigned char interrupt_instruction; interrupt_handler_t interrupt_handler; }; @far @interrupt void NonHandledInterrupt (void) { /* in order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction */ return; } extern void _stext(); /* startup routine */ struct interrupt_vector const _vectab[] = { {0x82, (interrupt_handler_t)_stext}, /* reset */ {0x82, NonHandledInterrupt}, /* trap */ {0x82, NonHandledInterrupt}, /* irq0 */ {0x82, NonHandledInterrupt}, /* irq1 */ {0x82, NonHandledInterrupt}, /* irq2 */ {0x82, NonHandledInterrupt}, /* irq3 */ {0x82, NonHandledInterrupt}, /* irq4 */ {0x82, NonHandledInterrupt}, /* irq5 */ {0x82, NonHandledInterrupt}, /* irq6 */ {0x82, NonHandledInterrupt}, /* irq7 */ {0x82, NonHandledInterrupt}, /* irq8 */ {0x82, NonHandledInterrupt}, /* irq9 */ {0x82, NonHandledInterrupt}, /* irq10 */ {0x82, NonHandledInterrupt}, /* irq11 */ {0x82, NonHandledInterrupt}, /* irq12 */ {0x82, NonHandledInterrupt}, /* irq13 */ {0x82, NonHandledInterrupt}, /* irq14 */ {0x82, NonHandledInterrupt}, /* irq15 */ {0x82, NonHandledInterrupt}, /* irq16 */ {0x82, NonHandledInterrupt}, /* irq17 */ {0x82, NonHandledInterrupt}, /* irq18 */ {0x82, NonHandledInterrupt}, /* irq19 */ {0x82, NonHandledInterrupt}, /* irq20 */ {0x82, NonHandledInterrupt}, /* irq21 */ {0x82, NonHandledInterrupt}, /* irq22 */ {0x82, NonHandledInterrupt}, /* irq23 */ {0x82, NonHandledInterrupt}, /* irq24 */ {0x82, NonHandledInterrupt}, /* irq25 */ {0x82, NonHandledInterrupt}, /* irq26 */ {0x82, NonHandledInterrupt}, /* irq27 */ {0x82, NonHandledInterrupt}, /* irq28 */ {0x82, NonHandledInterrupt}, /* irq29 */ };代码中的中断向量表需要我们自己修改,至于如何修改在STM8L151的官方固件库里有,下面看一下官方固件库里的stm8_interrupt_vector.c文件,代码如下:
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices * Copyright (c) 2013 STMicroelectronics */ #include "stm8l15x_it.h" typedef void @far (*interrupt_handler_t)(void); struct interrupt_vector { uint8_t interrupt_instruction; interrupt_handler_t interrupt_handler; }; struct interrupt_vector const _vectab[] = { {0x82, (interrupt_handler_t)_stext}, /* Reset */ {0x82, (interrupt_handler_t)TRAP_IRQHandler}, /* TRAP - Software interrupt */ {0x82, (interrupt_handler_t)NonHandledInterrupt}, /* Reserved */ {0x82, (interrupt_handler_t)FLASH_IRQHandler}, /* irq1 - FLASH interrupt */ {0x82, (interrupt_handler_t)DMA1_CHANNEL0_1_IRQHandler}, /* irq2 - DMA1 channel0 and channel1 interrupt */ {0x82, (interrupt_handler_t)DMA1_CHANNEL2_3_IRQHandler}, /* irq3 - DMA1 channel2 and channel3 interrupt */ {0x82, (interrupt_handler_t)RTC_CSSLSE_IRQHandler}, /* irq4 - RTC/ CSS on LSE interrupt */ {0x82, (interrupt_handler_t)EXTIE_F_PVD_IRQHandler}, /* irq5 - External IT PORTE/F interrupt /PVD interrupt*/ {0x82, (interrupt_handler_t)EXTIB_G_IRQHandler}, /* irq6 - External IT PORTB / PORTG interrupt */ {0x82, (interrupt_handler_t)EXTID_H_IRQHandler}, /* irq7 - External IT PORTD / PORTH interrupt */ {0x82, (interrupt_handler_t)EXTI0_IRQHandler}, /* irq8 - External IT PIN0 interrupt */ {0x82, (interrupt_handler_t)EXTI1_IRQHandler}, /* irq9 - External IT PIN1 interrupt */ {0x82, (interrupt_handler_t)EXTI2_IRQHandler}, /* irq10 - External IT PIN2 interrupt */ {0x82, (interrupt_handler_t)EXTI3_IRQHandler}, /* irq11 - External IT PIN3 interrupt */ {0x82, (interrupt_handler_t)EXTI4_IRQHandler}, /* irq12 - External IT PIN4 interrupt */ {0x82, (interrupt_handler_t)EXTI5_IRQHandler}, /* irq13 - External IT PIN5 interrupt */ {0x82, (interrupt_handler_t)EXTI6_IRQHandler}, /* irq14 - External IT PIN6 interrupt */ {0x82, (interrupt_handler_t)EXTI7_IRQHandler}, /* irq15 - External IT PIN7 interrupt */ {0x82, (interrupt_handler_t)LCD_AES_IRQHandler}, /* irq16 - LCD / AES interrupt */ {0x82, (interrupt_handler_t)SWITCH_CSS_BREAK_DAC_IRQHandler}, /* irq17 - CLK switch/CSS interrupt/ TIM1 Break interrupt / DAC */ {0x82, (interrupt_handler_t)ADC1_COMP_IRQHandler}, /* irq18 - ADC1 and Comparator interrupt */ {0x82, (interrupt_handler_t)TIM2_UPD_OVF_TRG_BRK_USART2_TX_IRQHandler}, /* irq19 - TIM2 Update/Overflow/Trigger/Break / USART2 TX interrupt */ {0x82, (interrupt_handler_t)TIM2_CC_USART2_RX_IRQHandler}, /* irq20 - TIM2 Capture/Compare / USART2 RX interrupt */ {0x82, (interrupt_handler_t)TIM3_UPD_OVF_TRG_BRK_USART3_TX_IRQHandler}, /* irq21 - TIM3 Update/Overflow/Trigger/Break / USART3 TX interrupt */ {0x82, (interrupt_handler_t)TIM3_CC_USART3_RX_IRQHandler}, /* irq22 - TIM3 Capture/Compare /USART3 RX interrupt */ {0x82, (interrupt_handler_t)TIM1_UPD_OVF_TRG_COM_IRQHandler}, /* irq23 - TIM1 Update/Overflow/Trigger/Commutation interrupt */ {0x82, (interrupt_handler_t)TIM1_CC_IRQHandler}, /* irq24 - TIM1 Capture/Compare interrupt */ {0x82, (interrupt_handler_t)TIM4_UPD_OVF_TRG_IRQHandler}, /* irq25 - TIM4 Update/Overflow/Trigger interrupt */ {0x82, (interrupt_handler_t)SPI1_IRQHandler}, /* irq26 - SPI1 interrupt */ {0x82, (interrupt_handler_t)USART1_TX_TIM5_UPD_OVF_TRG_BRK_IRQHandler}, /* irq27 - USART1 TX / TIM5 Update/Overflow/Trigger/Break interrupt */ {0x82, (interrupt_handler_t)USART1_RX_TIM5_CC_IRQHandler}, /* irq28 - USART1 RX / TIM1 Capture/Compare interrupt */ {0x82, (interrupt_handler_t)I2C1_SPI2_IRQHandler}, /* irq29 - I2C1 / SPI2 interrupt */ };直接将这个文件替换原来的文件即可。
另外需要将固件库里的libraries的inc、src文件夹的文件放到工程里面。还有stm8l15x_conf.h、stm8l15x_it.c、stm8l15x_it.h放到工程里。并配置stm8l15x_conf.h里的MCU选择,最后的工程文件架构大概如下:
到这里,就可以使用官方的固件库来进行开发了。