cancel
Showing results for 
Search instead for 
Did you mean: 

Delay, multi milliseconds, on the STM32F0, again..

XooM
Associate III

where I wrote it among the codes.. // Here I have to wait 500ms.
Can you help me to prevent the timers from being affected by this wait?
Can you give me the codes that I will add. Because I am very confused..

I tried many things but they all stopped the interrupts..

 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{

	if(htim->Instance==TIM1)
	{
		
	if(input1==0) 
	{
		HAL_GPIO_WritePin(GPIOA, GPIO_Pin_1, GPIO_PIN_SET);
		// Here I have to wait 500ms.
		HAL_GPIO_WritePin(GPIOA, GPIO_Pin_1, GPIO_PIN_RESET);
	}

}

 

 

 

 

 

70 REPLIES 70

You now have Input 1-7, 9-12 and 14. So do those inputs have to match as well? If not, then you need to mark them as don't care using "X" instead of "1" or "0". We need to know exactly which inputs you are concentrating on for Output3 to pulse high for 500ms..

 

Also you wrote... 

 

When C13=1 and A2=1, A10=0
When C13=0 and A2 =0, A10 =1
500ms after C13=0 and A2 =1, A10 =0

 

Where A10 is turning on for 500ms, but your table shows PB14. So which is correct?

 

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

Try simple words "It drops to 0 not when the input arrives, but after the input disappears."

your info is chaos 

Signals and event can be based on states or edges. Then for edge reaction write code , you have all required info.

1. variant input drops to 0 in time output is ready and start 500ms

2. variant next input drops to 0 between 0-500 ... what to do?

3. variant input drops to 0 and stay here more as 500ms ... what to do 

...

XooM
Associate III

Thanks. Other friends are more helpful.

I guess it doesn't even occur to you that they could be Pull-Ups and Pull-Downs. Just because we asked questions doesn't mean we don't know anything.


Not everything can be solved with words. Simple code examples are more helpful.
No one asked you to write a full program.
If it could be possible to code what was said verbally, why would we ask here?
Please, I don't want your help.

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  // I turn on/off 24 LEDs according to the inputs.
  if (htim->Instance==TIM1) // Timer2 Update Event'i 250us (333us)
  {
  }

  // I manage the outputs according to the incoming inputs.
  if (htim->Instance==TIM16) // Timer16 Update Event'i 1 ms
  {
    static int PB14_On = 0;
    static uint32_t PB14_Start;

    if (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2) == 0) // INPUT8
    {
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // OUTPUT1

      if (PB14_On == 0)
      {
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_SET); // OUTPUT3
        PB14_Start = HAL_GetTick();
        PB14_On = 1;
      }
    }
    else
    {
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // OUTPUT1
    }

    // Check Every 1ms
    if (PB14_On && ((HAL_GetTick() - PB14_Start) >= 500))
    {
      PB14_On = 0;
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, GPIO_PIN_RESET); // OUTPUT3
    }
  } // TIM16
}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Let's assume you're only looking a 3 inputs and controlling 2 outputs. 

Create a data structure to represent the look-up table

typedef union
{
	struct
	{
		uint8_t data[2]; // byte 0 is current state, byte 1 is last state
	}Byte;
	struct
	{
		uint8_t pa1:1;
		uint8_t pa2:1;
		uint8_t pc13:1;
		uint8_t :5;
	}Status;
}InputStatus_t;

 

Using EXTI for the input pins, we can use the HAL GPIO callbacks to set the input pin states. I've tested on a Nucleo-G071RB which uses a Rising and Falling callback where some STM's use 1 callback, but concept is the same.  

/*
 * Description: Update pin status for specific input.
 * Input: pin data structure, pin to update, the pin state
 */
void GPIO_UpdatePinStatus(InputStatus_t *input, uint8_t pin, GPIO_PinState pinStatus)
{
	switch(pin)
	{
	case 0:
		input->Status.pa1 = pinStatus;
		break;
	case 1:
		input->Status.pa2 = pinStatus;
		break;
	case 2:
		input->Status.pc13 = pinStatus;
		break;
	default:
		// invalid pin
		break;
	}
}

/*
 * Description: Functions as a HAL GPIO callback.
 * Note: no sw debounce incorporated.
 * See YouTube video to learn how to debounce push buttons that use EXTI
 * https://www.youtube.com/watch?v=o0qhmXR5LD0
 */
void GPIO_Callback(uint16_t GPIO_Pin)
{
	if(GPIO_Pin == PA1_Pin)
	{
		GPIO_UpdatePinStatus(&inputs, 0, HAL_GPIO_ReadPin(PA1_GPIO_Port, PA1_Pin));
	}
	else if(GPIO_Pin == PA2_Pin)
	{
		GPIO_UpdatePinStatus(&inputs, 1, HAL_GPIO_ReadPin(PA2_GPIO_Port, PA2_Pin));
	}
	else if(GPIO_Pin == PC13_Pin)
	{
		GPIO_UpdatePinStatus(&inputs, 2, HAL_GPIO_ReadPin(PC13_GPIO_Port, PC13_Pin));
	}
}

/*
 * STM32G071 HAL driver uses Rising and Falling callbacks where other STM32's use 1 callback.
 */
void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
	GPIO_Callback(GPIO_Pin);
}

void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
	GPIO_Callback(GPIO_Pin);
}

 

By using a data structure to represent the look-up table, we can use switch-case to determine what state the input pins are. By checking for current pin state versus the last pin state, you can avoid having case 0 being called again. For case 0, PB12 is low, and PA10 is high. A timer callback is started and after 500ms, it turns Off PA10.

void GPIO_Check(InputStatus_t *input)
{
	if(input->Byte.data[1] != input->Byte.data[0]) // check for change, else do nothing.
	{
		input->Byte.data[1] = input->Byte.data[0]; // copy current state to last state

		switch(input->Byte.data[0]) // index of truth table
		{
		case 0:
			PB12_Off(); // pin is low
			PA10_On(); // pin is high
			TimerCallbackTimerStart(&timerCallback, PA10_Off, 500, TIMER_NO_REPEAT); // start timer to turn off PA10
			break;
		case 1: // all other case, PA12 is On (high state)
		case 2:
		case 3:
		case 4:
		case 5:
		case 6:
		case 7:
		default:
			PB12_On(); // pin is high
			break;
		}
	}
}

 

And the code for the GPIO outputs

void PA10_On(void)
{
	HAL_GPIO_WritePin(PA10_GPIO_Port, PA10_Pin, GPIO_PIN_SET);
}

void PA10_Off(void)
{
	HAL_GPIO_WritePin(PA10_GPIO_Port, PA10_Pin, GPIO_PIN_RESET);
}

void PB12_On(void)
{
	HAL_GPIO_WritePin(PB12_GPIO_Port, PB12_Pin, GPIO_PIN_SET);
}

void PB12_Off(void)
{
	HAL_GPIO_WritePin(PB12_GPIO_Port, PB12_Pin, GPIO_PIN_RESET);
}

 

The working code is on Github https://github.com/karlyamashita/Nucleo-G071RB_GPIO_Delay500/wiki

 

 

 

 

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

Thank you very much. It's a good thing that people like you exist here. I will examine the code immediately. I need to work on a typedef structure that I don't know at all. I didn't understand that much. But you have broadened my horizons and perspective. I will try the code in a moment. I have some questions. Can I continue asking here?

You can ask a question, but if it's a different topic, then it's better that you post a new topic so others can benefit when searching.

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.

I am a bit of a novice, I want to say that from the beginning.
I like your pin change control mechanism here.
As you can see in the table I gave you, there are 14 inputs and 8 outputs.
Can I add what I want to do to my outputs according to other situations that will come from the inputs to the structure you built?
I am asking to learn. Because it seems like this control structure will make my job easier.
Example:

When PA4 , PA2 , PA0 , PC13 are "0" PB13 will be "1".
When PA3 is "0";

PB13 becomes "0" and then
PB14 becomes "1" and then 500ms later becomes "0".


Should I completely break your code structure to add this addition?

Where am I making a mistake?

Note: link removed.

Where am I making a mistake?
I couldn't convert it to my own STM32 MCU. ( STM32F100C6T6B)

I won't download files from an unknown website. Explain in more detail what is wrong? Screen shots?

Tips and Tricks with TimerCallback https://www.youtube.com/@eebykarl
If you find my solution useful, please click the Accept as Solution so others see the solution.