STMF32 truestudio example code

Thread Starter

skyr6546

Joined Mar 22, 2019
73
Hello

I have atollic truestudio and I have started working on STM32F407 discovery board

I am looking sample code for counter and external hardware interrput

I got one example code in truestudio but I think its software interrupt

Code:
/* Includes ------------------------------------------------------------------*/
#include "stm32f4_discovery.h"

/** @addtogroup STM32F4_Discovery_Peripheral_Examples
  * @{
  */

void EXTILine0_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f4xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
        system_stm32f4xx.c file
     */

  /* Initialize LEDs mounted on STM32F4-Discovery board */
  STM_EVAL_LEDInit(LED4);
  STM_EVAL_LEDInit(LED3);

  /* Configure EXTI Line0 (connected to PA0 pin) in interrupt mode */
  EXTILine0_Config();

  /* Generate software interrupt: simulate a rising edge applied on EXTI0 line */
  EXTI_GenerateSWInterrupt(EXTI_Line0);

  while (1)
  {
  }
}

/**
  * @brief  Configures EXTI Line0 (connected to PA0 pin) in interrupt mode
  * @param  None
  * @retval None
  */
void EXTILine0_Config(void)
{
 
  GPIO_InitTypeDef   GPIO_InitStructure;
  NVIC_InitTypeDef   NVIC_InitStructure;

  /* Enable GPIOA clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  /* Enable SYSCFG clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
 
  /* Configure PA0 pin as input floating */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Connect EXTI Line0 to PA0 pin */
  SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);

  /* Configure EXTI Line0 */
  EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; 
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);

  /* Enable and set EXTI Line0 Interrupt to the lowest priority */
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

#ifdef  USE_FULL_ASSERT


  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif
does anyone have a working code of counter and hardware interrupt?
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
What is your definition of software vs hardware interrupt?
@MrChips

Hardware interrupt use the pin of a microcontroller

software interrupts, they use flag bits that are built into registers of the microcontroller, meaning that we can not physically access them from outside

I don't have much knowledge about this board so I think it's better to run the example first and later I can try to modify code according to the requirement.

Do you have a working code for counter and external interrupt?
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
No, but I can show you how to accomplish whatever it is that you are trying to do.

What are you trying to do?
Hello @MrChips

I have a proximity sensor which will only output high when an object is within a certain range. counter increments each time when an object detected. I want to ring alarm (Buzzer) When the object detected 2000 times. I want to repeat this process continuously
 

MrChips

Joined Oct 2, 2009
34,810
You still have not given us the big picture.
What is the object? What is the distance for detection?
2000 times seems a bit odd. Why 2000 times? Why not one time?
How long will the buzzer be on?
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
You still have not given us the big picture.
What is the object? What is the distance for detection?
2000 times seems a bit odd. Why 2000 times? Why not one time?
How long will the buzzer be on?
Hello @MrChips

I don't have specific requirements You can see the first post I just wanted to run a counter program on the board. you can assume anything

I had sensor and buzzer so I can test code with sensor and buzzer
 

MrChips

Joined Oct 2, 2009
34,810
That is not the way to approach Systems Engineering.
Sure, you can learn various techniques for hypothetical applications but each application calls for different solutions.

You can have the following situations:
  • detect an intruder entering restricted space
  • count widgets going by on a conveyor belt
  • count how many cars enter and exit a parking lot
  • measure human heart rate
  • measure frequency of RF sine wave
The approach to each application is different. Pick one.
 

MrChips

Joined Oct 2, 2009
34,810
Hardware interrupt use the pin of a microcontroller

software interrupts, they use flag bits that are built into registers of the microcontroller, meaning that we can not physically access them from outside
What you have described are both hardware interrupts.
The first is external hardware interrupt.
The second is internal hardware interrupt.
 

Thread Starter

skyr6546

Joined Mar 22, 2019
73
What you have described are both hardware interrupts.
The first is external hardware interrupt.
The second is internal hardware interrupt.
so what is your definition for hardware and software interrupt?

You can have the following situations:
  • detect an intruder entering restricted space
  • count widgets going by on a conveyor belt
  • count how many cars enter and exit a parking lot
  • measure human heart rate
  • measure frequency of RF sine wave
The approach to each application is different. Pick one.
I would like to count how many cars enter and exit a parking lot
 

MrChips

Joined Oct 2, 2009
34,810
Software interrupts are triggered by code.
Some MCUs have a special instruction SWI that causes interrupt when executed.
 
Top