本程序通过外部中断来控制LED亮灭。在STVD建立工程后,包含了main.c和stm8_interrupt_vector.c,外部中断程序除了要在main里面初始化以外还需要对stm8_interrupt_vector.c作一下改动。main程序如下:
/* MAIN.C file
*
* Copyright (c) 2002-2005 STMicroelectronics
*/
#include <iostm8s.h>
void Delay(unsigned int time)
{
volatile unsigned int i;
while(time--){
i=300;
while(i--);
}
}
void Init_Gpio(void)
{
PC_ODR |= (1<<1);//PC1输出1
PC_DDR |= (1<<1)|(1<<2);//将PC1和PC2设置为输出
}
void Init_Exti(void)
{
PD_DDR &= (1<<3);//PD3设置为输入
PD_CR1 |= (1<<3);//带上拉电阻输入
PD_CR2 |= (1<<3);//使能外部中断
EXTI_CR1 |= (2<<6);//仅下降沿触发
_asm("rim\n"); //使能全局中断
}
main()
{
Init_Gpio();
Init_Exti();
while (1)
{
Delay(200);
}
}
stm8_interrupt_vector.c如下,有颜色的字为修改过的内容
/* BASIC INTERRUPT VECTOR TABLE FOR STM8 devices
* Copyright (c) 2007 STMicroelectronics
*/
#include <iostm8s.h>
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;
}
@far @interrupt void EXTI_PORTD_IRQHandler (void)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
PC_ODR ^= (1<<1)|(1<<2);
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, EXTI_PORTD_IRQHandler}, /* 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 */
};