Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Timer Controlled For Loop

Status
Not open for further replies.

uranyumx

Advanced Member level 4
Joined
Mar 5, 2011
Messages
102
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,091
Hello,

I have tried to implement a for-loop with timer interrupt controlled. Because, I expect that the loop would run in a certain amount of time. For doing this purpose, I set a timer,

Code:
static void MX_TIM4_Init(void)
{

  /* USER CODE BEGIN TIM4_Init 0 */

  /* USER CODE END TIM4_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  /* USER CODE BEGIN TIM4_Init 1 */

  /* USER CODE END TIM4_Init 1 */
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 96-1; // The processor core clock freq is 96 MHz
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = 1000-1; // 1kHz expected
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim4) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim4, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM4_Init 2 */

  /* USER CODE END TIM4_Init 2 */

}

Then, in the "void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){ " function, I set a flag,

Code:
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
    if (htim == &htim4 && Burst == 1)
        {
            Flag = 1;                           
        }
}

Lastly, in the while loop, I run my main code and reset the flag,

Code:
if (Flag){
    HAL_GPIO_WritePin(Electrode_1st_Stim_GPIO_Port, Electrode_1st_Stim_Pin, GPIO_PIN_SET);
        if (CFirstPol == 1){
            HAL_GPIO_WritePin(Phase_2nd_GPIO_Port, Phase_2nd_Pin, GPIO_PIN_SET);
            DWT_Delay_us(pulse_width);
            HAL_GPIO_WritePin(Phase_2nd_GPIO_Port, Phase_2nd_Pin, GPIO_PIN_RESET);
            DWT_Delay_us(interphase);
            HAL_GPIO_WritePin(Phase_1st_GPIO_Port, Phase_1st_Pin, GPIO_PIN_SET);
            DWT_Delay_us(pulse_width2);
            HAL_GPIO_WritePin(Phase_1st_GPIO_Port, Phase_1st_Pin, GPIO_PIN_RESET);
        }
        else if (AFirstPol ==1){
            HAL_GPIO_WritePin(Phase_1st_GPIO_Port, Phase_1st_Pin, GPIO_PIN_SET);
            DWT_Delay_us(pulse_width);
            HAL_GPIO_WritePin(Phase_1st_GPIO_Port, Phase_1st_Pin, GPIO_PIN_RESET);                                                               
            DWT_Delay_us(interphase);
            HAL_GPIO_WritePin(Phase_2nd_GPIO_Port, Phase_2nd_Pin, GPIO_PIN_SET);
            DWT_Delay_us(pulse_width2);
            HAL_GPIO_WritePin(Phase_2nd_GPIO_Port, Phase_2nd_Pin, GPIO_PIN_RESET);
        }
        HAL_GPIO_WritePin(Electrode_1st_Stim_GPIO_Port, Electrode_1st_Stim_Pin, GPIO_PIN_RESET);
        DWT_Delay_us(off_per);
        DWT_Delay_us(IntBurstInterval);   
        Flag = 0;                                                                                   
}

Then, I got a this signal. I expected that signal generate during 10 ms and it should have 200 us intervals. The parameters are pulse widths 200us, interphase is 100us, and frequency is 1500 Hz.
 

Attachments

  • Signal1.PNG
    Signal1.PNG
    9.1 KB · Views: 103

Hello!

Sorry but what you are explaining is very unclear, at least to me.
Apparently you want to generate a burst at regular intervals, right?

Then, I got a this signal. I expected that signal generate during 10 ms and it should
have 200 us intervals. The parameters are pulse widths 200us, interphase is 100us, and
frequency is 1500 Hz.

You could consider actually drawing what you want. You give us a bunch of data but
one image would explain a lot more. Not sure what you are trying to do.

Generate during 10 ms

So your signal length is 10 ms?

it should have 200 us intervals

So if I start from 0 (all the numbers at the left are in µs)

000 000 Start, burst is set on
010 000 Burst is set off after 10 ms, beginning of the 200 µs interval
010 200 End of the 200 µs interval

From there:
- What happens after the 200µs interval?
- Does it repeat every 666 µs? (because of 1500 Hz)
- Where does the "interphase" of 100µs happen and what does it do?

Etc...

Dora
 

The shape of the pulses you show tells me that the GPIO pin is being loaded down and/or you are actually trying to transition the pin up and down too quickly.
What GPIO pin is the signal you are showing us?
There are a lot of variables that you use that we can't see their values - 'pulse_width', 'interphase' etc.
Also is 'flag' being declared as volatile?
Where are 'CFirstPol' and 'FirstCol' being set?
Are you sure that the timer ISR is being called correctly?
Are you sure that the code in the 'for' loop is being entered with the correct values in the various variables?
Susan
 

Hi,

Yes, I also don't understand. --> a picture, even hand drawn could clarify.

You say "timer controlled for loop"
What exactly should be controlled by a timer?
* starting the loop
* stopping the loop
* delay anything
?

Klaus
 

Hello @doraemon , @Aussie Susan , and @KlausST ,

First of all, thank you for your interest. I am sorry for making confusion. Let me simplify the main code to avoid multiple variables. So simplified code in the main,

Code:
if (Flag){
    HAL_GPIO_WritePin(Phase_2nd_GPIO_Port, Phase_2nd_Pin, GPIO_PIN_SET);
    DWT_Delay_us(pulse_width);
    HAL_GPIO_WritePin(Phase_2nd_GPIO_Port, Phase_2nd_Pin, GPIO_PIN_RESET);
    DWT_Delay_us(off_per); // off_per is pulse period - pulse width
    DWT_Delay_us(PulseInterval);   
    Flag = 0;                                                                                   
}

So, with this code, I want to generate square pulses with a certain frequencies. Duration of the pulse would be timer controlled, and the pulses woud need to start after every interval time.

I attached my idea with a sketch.
 

Attachments

  • Idea_Pulse.png
    Idea_Pulse.png
    18.7 KB · Views: 82

Hi,

I don´t see any "timer controlled", I just see busy_wait_delay.
This consumes 100% of processing power and is not precise.

Why don´t you smply use a true timer hardware generated PWM?
It consumes no processing power (besides setup) and is extremely precise.

Klaus
 

Hi @KlausST,

Actually, I didn't use the PWM before but let me try it first.

Thank you,
--- Updated ---

@KlausST

To make sure your proposed idea, I drew it, is that correct?
--- Updated ---

I have one more question. In the PWM applications, people get the WPM out from one of the pin of the processor. But in my case, the pulses need to generate during ON period of time, the OFF period will be equivalent of pulse interval time.

More precisely, the ON period of control pulse (which is the red pulse in the previous thread attachment) will be equal to the multiples of period of each pulse.
@KlausST
 

Attachments

  • Idea_Pulse_V1.png
    Idea_Pulse_V1.png
    23.4 KB · Views: 92
Last edited:

Perhaps if you show us more than 1 complete cycle it will make it clearer what you are trying to do.
It looks like you are using one of the STM MCS and they generally have quote capable PWM modules. Read up on them and see what other capabilities they have (interrupts, triggers, fault pins etc.) as these might be of use in whatever you are doing.
You might also be able to use a timer to control the PWM or even have two PWMs that you (carefully) synchronise - it really all depends on which MCU you have, the PWM module capabilities and that you really want to do.
Susan
 

@Aussie Susan
In the attachment, you can imagine that after the second group of pulses, there is a pulse interval. I use stm32f767 processor. In the PWM, I didn't clarify how a PWM signal controls the group of pulses. Because in the applications, people measure the PWM signal from one of the pin. But in my case, square pulses will generate during ON period of another control pulse, and pulse interval will be equivalent of the OFF period of the control signal (red signal in my last thread). Thank you.
 

In that case, how about using a timer to toggle the control line and also start the PWM. The use the timer again to toggle the control line at the end of whatever interval you want and then stop the PWM.
Susan.
 

Hi,

So basically one PWM is ORed with onother PWM?
What frequency range and what duty cycle range does the lower frequency have?
...and the same for the higher frequency?

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top