1

I tried to program an interrupt with S2 that is connected to PB1 on my stm32f103vet6 board, but I failed and I don't know why!

The codes here:

#include "stm32f10x.h"                  // Device header
#include "stm32f10x_exti.h"             // Keil::Device:StdPeriph Drivers:EXTI
#include "misc.h"                       // Keil::Device:StdPeriph Drivers:Framework
#include "stm32f10x_gpio.h"             // Keil::Device:StdPeriph Drivers:GPIO
#include "stm32f10x_rcc.h"              // Keil::Device:StdPeriph Drivers:RCC

#define D2_ON GPIO_ResetBits(GPIOB, GPIO_Pin_13)
#define D2_OFF GPIO_SetBits(GPIOB, GPIO_Pin_13)
#define D3_ON GPIO_ResetBits(GPIOB, GPIO_Pin_14)
#define D3_OFF GPIO_SetBits(GPIOB, GPIO_Pin_14)

#define PB9_ON GPIO_ResetBits(GPIOB, GPIO_Pin_9)
#define PB9_OFF GPIO_SetBits(GPIOB, GPIO_Pin_9)
#define PB8_ON GPIO_ResetBits(GPIOB, GPIO_Pin_8)
#define PB8_OFF GPIO_SetBits(GPIOB, GPIO_Pin_8)

#define S2_DOWN GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0
#define S2_UP GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 1
#define S3_DOWN GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15) == 0
#define S3_UP GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_15) == 1


void LED_Configure(void){
//only D2. D3, AND PB9, PB8
GPIO_InitTypeDef PB13;
GPIO_InitTypeDef PB14;
GPIO_InitTypeDef PB9;
GPIO_InitTypeDef PB8;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

PB13.GPIO_Pin = GPIO_Pin_13;
PB13.GPIO_Mode = GPIO_Mode_Out_PP;
PB13.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &PB13);
GPIO_SetBits(GPIOB, GPIO_Pin_13);

PB14.GPIO_Pin = GPIO_Pin_14;
PB14.GPIO_Mode = GPIO_Mode_Out_PP;
PB14.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &PB14);
GPIO_SetBits(GPIOB, GPIO_Pin_14);

PB9.GPIO_Pin = GPIO_Pin_9;
PB9.GPIO_Mode = GPIO_Mode_Out_PP;
PB9.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &PB9);
GPIO_SetBits(GPIOB, GPIO_Pin_9);

PB8.GPIO_Pin = GPIO_Pin_8;
PB8.GPIO_Mode = GPIO_Mode_Out_PP;
PB8.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &PB8);
GPIO_SetBits(GPIOB, GPIO_Pin_8);
}

//Microsecond delay
void delay_us(uint32_t delay_us){
  volatile unsigned int num;
  volatile unsigned int t;


  for (num = 0; num < delay_us; num++)
  {
t = 11;
while (t != 0)
{
  t--;
    }
  }
    }
//Millisecond delay
void delay_ms(uint16_t delay_ms){
  volatile unsigned int num;
  for (num = 0; num < delay_ms; num++)
  {
    delay_us(1000);
 }
}


void sysRST(void){

__set_FAULTMASK(1);
NVIC_SystemReset();
}

void Configure_PB1(void){

GPIO_InitTypeDef PB1_GPIOInitStruct;
EXTI_InitTypeDef PB1_EXTIInitStruct;
NVIC_InitTypeDef PB1_NVICInitStruct;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

PB1_GPIOInitStruct.GPIO_Mode = GPIO_Mode_IPU;
PB1_GPIOInitStruct.GPIO_Pin = GPIO_Pin_1;
PB1_GPIOInitStruct.GPIO_Speed = GPIO_Speed_50MHz;

PB1_EXTIInitStruct.EXTI_Line = EXTI_Line1;
PB1_EXTIInitStruct.EXTI_LineCmd = ENABLE;
PB1_EXTIInitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
PB1_EXTIInitStruct.EXTI_Trigger = EXTI_Trigger_Rising;

PB1_NVICInitStruct.NVIC_IRQChannel = EXTI1_IRQn;
PB1_NVICInitStruct.NVIC_IRQChannelCmd = ENABLE;
PB1_NVICInitStruct.NVIC_IRQChannelPreemptionPriority = 0x00;
PB1_NVICInitStruct.NVIC_IRQChannelSubPriority = 0x00;

GPIO_Init(GPIOB, &PB1_GPIOInitStruct);
EXTI_Init(&PB1_EXTIInitStruct);
NVIC_Init(&PB1_NVICInitStruct); 
}

void EXTI1_IRQHandler(void){

if (EXTI_GetITStatus(EXTI_Line1) != RESET){
    /*DO SOMETHING*/
    
    /*Clear Interrupt Flag*/
    EXTI_ClearITPendingBit(EXTI_Line1);
}
}

int main(){

SystemInit();
LED_Configure();
Configure_PB1();
EXTI1_IRQHandler();

while(1){

    D2_OFF;
    D3_OFF;
    PB9_OFF;
    PB8_OFF;
    delay_ms(80);
    D2_ON;
    delay_ms(80);
    D3_ON;
    delay_ms(80);
    PB9_ON;
    delay_ms(80);
    PB8_ON;
    delay_ms(80);
}
}

Anyone could tell me what's wrong with my codes here? I think I just programmed according to those libraries and some pdf files, nothing wrong. Thank you very much!

RedRabbit
  • 31
  • 3
  • I've never messed with this STM32 nor its C language. But how does the address of EXTI15_IRQHandler get placed anywhere so that the interrupt vector (or associated code) knows to call it? (Assuming that is what you want done.) – jonk Nov 08 '21 at 04:07
  • Do you have a debugger? Time to get serious and track down the problem. Note that putting an external interrupt on a pushbutton is bad juju. You don’t want sub- microsecond response on an input that is in the order of 10’s milliseconds. Use a timer tick to poll. – Kartman Nov 08 '21 at 05:17
  • 2
    Failed how? Program didnt compile? Pressing button didnt cause leds to blink? Leds didnt stop blinking? Leds are blinking even without pressing button? ... – Rokta Nov 08 '21 at 07:19
  • @Jonk the standard startup code will provide the labels for interrupt vectors. However that was an extremely good question as it made me realize what the issue is. – Justme Nov 08 '21 at 07:41
  • The failed thing is that the leds do not stop blinking! – RedRabbit Nov 08 '21 at 09:55
  • 3
    You upated the code so the original problem I answered is now solved, but it means you invalidated the answer and this is now a completely new question. Please don't change the question to a new one. Your code blinks the LEDs in the interrupt and main loop. There is nothing in the code to make LEDs to stop blinking. Why do you expect the LEDs to "stop" blinking? – Justme Nov 08 '21 at 10:10
  • 2
    BIG rule number one: **Never place delays in interrupt handler!** Whatever you want to do in interrupt must be as short as possible. What will happen with your system when there is interrupt called and another comes before previous is served (finished)? And another? – smajli Nov 08 '21 at 11:21
  • 1
    Start by fixing your non-existent code formatting if you want others to look at your code. – Lundin Nov 09 '21 at 11:12

0 Answers0