stm8s interrupt fired twice - Page 1
Forum section: electronics / general

author: electros6
author_rank: Regular Contributor
timestamp: June 28, 2019, 04:39:23 pm
content: Hi guys,I am new to stm8. I used stm8s003 to create a program for external interrupt in PORTB7 . Everything works fine but when external interrupt occurred the interrrupt routine executive two times. I don't know what is wrong I double checked everything.here is the code#include <intrinsics.h>#include <iostm8s103f3.h>unsigned int led,status;#define on 1#define off 0//// Setup the system clock to run at 16MHz using the internal oscillator.//void InitialiseSystemClock(){CLK_ICKR = 0; // Reset the Internal Clock Register.CLK_ICKR_HSIEN = 1; // Enable the HSI.CLK_ECKR = 0; // Disable the external clock.while (CLK_ICKR_HSIRDY == 0); // Wait for the HSI to be ready for use.CLK_CKDIVR = 0; // Ensure the clocks are running at full speed.CLK_PCKENR1 = 0xff; // Enable all peripheral clocks.CLK_PCKENR2 = 0xff; // Ditto.CLK_CCOR = 0; // Turn off CCO.CLK_HSITRIMR = 0; // Turn off any HSIU trimming.CLK_SWIMCCR = 0; // Set SWIM to run at clock / 2.CLK_SWR = 0xe1; // Use HSI as the clock source.CLK_SWCR = 0; // Reset the clock switch control register.CLK_SWCR_SWEN = 1; // Enable switching.while (CLK_SWCR_SWBSY != 0); // Pause while the clock switch is busy.}//// initilize Timer 2 to a known state.//void timer1init(void){TIM1_CR1=0xA0; // set in up dowm count mode,counter not enabledTIM1_CCMR1=0x60; // pwm1 modeTIM1_BKR=0x8C;TIM1_ARRH=0x7D;TIM1_ARRL=0x00;TIM1_CCR1H=0x4E;TIM1_CCR1L=0x20;TIM1_CCER1_CC1E=1;TIM1_CR1_CEN=1;}//// Main program loop.//void portinit(void){//set PD0 for led on or offPD_DDR=0x01;PD_CR1=0x01;PD_CR2=0x00;//set PB7 for external interruptPB_DDR=0x00;PB_CR1=0x00;PB_CR2=0x00;}void led_on(void){PD_ODR=1;led=on;}void led_off(void){PD_ODR=0;led=off;}void externalinterrupt(void){EXTI_CR1_PBIS=2;EXTI_CR2=0x00;}/*#pragma vector = 6__interrupt void EXTI_PORTB_IRQHandler(void){if(led==on){led_off();led=off;}else{led_on();led=on;}}*/void main(){__disable_interrupt();InitialiseSystemClock();timer1init();portinit();externalinterrupt();led_off();__enable_interrupt();while (1){if(PB_IDR_IDR7==1){if(led==on){led_off();led=off;}else{led_on();led=on;}while(PB_IDR_IDR7==1);}}}can somebody tell me what is the problem.ThanksN.Baskaran

author: rhodges
author_rank: Frequent Contributor
timestamp: June 28, 2019, 06:53:51 pm
content: It looks like you tried using the interrupt first, then removed your interrupt in order to watch PB7 from main() code.In this code, your PB7 interrupt is not enabled (CR2 = 0x80). Was the CR2 interrupt bit set in your first code?How did you know that your interrupt was triggering twice? If you have a scope, you could put something in your interrupt function so you can get more clues. Maybe ringing on B7 input?I see that you set your interrupt on falling edge, and I presume that __disable_interrupt() sets I0 and I1 CCR flags to level 3. ("These bits can only be written when I1 and I0 are both set to 1 (level 3)"). That was my first thought, but the setup looks okay. Nothing obviously wrong.

author: rhodges
author_rank: Frequent Contributor
timestamp: June 29, 2019, 03:01:08 pm
content: I think this needs to be fixed:Code:void externalinterrupt(void){EXTI_CR1_PBIS=2;EXTI_CR2=0x00;}https://documentation.help/STM8S-STM8A/group__EXTI__Registers__Bits__Definition.html#ga26ba9a95d78d85d801f4f9f8e16b1198Code:#define EXTI_CR1_PBIS ((uint8_t)0x0C)Try something like this:Code:EXTI->CR1 = 8; /* trigger on falling edge */EXTI->CR2 = 0;

author: electros6
author_rank: Regular Contributor
timestamp: June 30, 2019, 04:23:03 pm
content: Thanks i will test and let you know

author: Nerull
author_rank: Frequent Contributor
timestamp: June 30, 2019, 07:05:24 pm
content: How are you triggering the external interrupt? Does it have sufficient debouncing?