转贴自:
https://www.reddit.com/r/STM8/comments/jbq023/interrupts_on_stm8_with_sduino/
亲测好用,以下是原文:
I did it! With some googling, sleuthing, and trial-and-error, I figured it out.
With STM8 sduino, attachInterrupt works but it's not quite like on a normal arduino.
Interrupts are separate per PORT, not per pin. Each port has multiple pins. PA1, PA2 are on port A, PC3-PC7 are on port C etc.
Use pinMode to designate the pin as input, so that digitalRead can keep working in case you need it.
After that, you must call GPIO_Init (an STM8-specific API function) to actually enable interrupts for a particular pin.
Then, you must disable interrupts (as per the official documentation, and it really doesn't work otherwise!) and call EXTI_SetExtIntSensitivity to set the interrupt trigger type on that entire port.
The you can re-enable interrupts and finally call attachInterrupt(). See my example below, this code works for me on my STM8S003F3.
Hope this saves someone some time.
void ISR()
{ //your code here }
void setup()
{
pinMode(2,INPUT);
GPIO_Init(GPIOA, GPIO_PIN_3, GPIO_MODE_IN_FL_IT);
disableInterrupts();
EXTI_SetExtIntSensitivity( EXTI_PORT_GPIOA, EXTI_SENSITIVITY_RISE_ONLY);
enableInterrupts();
attachInterrupt(INT_PORTA & 0xFF,ISR,0);
}