3663 views|14 replies

2865

Posts

4

Resources
The OP
 

Will the timed interrupt enter the interruption immediately after it is turned on? [Copy link]

 

I wrote an application using the GD32F303 microcontroller. My goal is to turn on the TIMER0 timer output after a delay of 1000us after turning on the interrupt.

main
{

   .......

     
	/* enable outq-timer to delaying */
	timer_interrupt_enable(TIMER6,TIMER_INT_FLAG_UP);

   .......


   .......

}



void TIMER6_IRQHandler(void)
{
    if(SET == timer_interrupt_flag_get(TIMER6,TIMER_INT_FLAG_UP)){
			  
			  timer_enable(TIMER0); 
        /* clear channel 0 interrupt bit */
        timer_interrupt_flag_clear(TIMER6,TIMER_INT_FLAG_UP);
			  timer_interrupt_disable(TIMER6,TIMER_INT_FLAG_UP);
        //work_counter++;cur_counter++;   
    }
}


However, after timer_interrupt_enable(TIMER6,TIMER_INT_FLAG_UP); is executed, the program enters the interrupt immediately instead of waiting for 1000uS before entering the interrupt.

May I ask the experts, does it mean that as long as the interrupt is enabled, the interrupt will be entered and wait for the next cycle to interrupt again?

This post is from GD32 MCU

Latest reply

It is timer_enable(TIMER), which executes the timer first and then turns on the interrupt to enter the interrupt state.   Details Published on 2023-2-18 08:55
 

2w

Posts

0

Resources
2
 

"When the timed interrupt is turned on, will it enter the interrupt immediately?"

No. The timer will generate an interrupt only when it counts to a certain value.

This post is from GD32 MCU

Comments

I have already known the answer through experiments. After the timer is initialized, that is, timer_enable(TIMER0); after this sentence is executed, the timer has already started to execute, but the interrupt flag is not turned on at this time, so the interrupt function cannot enter. So as long as the interrupt is turned on, you can enter the interrupt at this time  Details Published on 2022-9-20 19:15
 
 
 

2865

Posts

4

Resources
3
 
maychang posted on 2022-9-20 18:26 "Will the timer interrupt enter the interrupt immediately after it is turned on?" No. The timer will only generate an interrupt when it counts to a certain value.

I have learned the answer through experiments. After the timer is initialized, that is, timer_enable(TIMER0); is executed, the timer has already started to execute, but the interrupt flag is not turned on at this time, so the interrupt function cannot enter. So as long as the interrupt is turned on, the interrupt can be entered at this time. Because the timer is working at this time, the timer_interrupt_enable(TIMER6,TIMER_INT_FLAG_UP); method cannot be used, but timer_enable(TIMER0); is used to turn on the interrupt, and then the counting starts.

This post is from GD32 MCU

Comments

timer_enable(TIMER0) may trigger the automatic reload. You can reload the value before the interrupt is enabled or disable and then enable the timer.  Details Published on 2022-9-21 09:00
 
 
 

4764

Posts

12

Resources
4
 

They are all program jumps. Whichever statement is jumped to will be executed. It will definitely enter the interrupt function.

This post is from GD32 MCU
 
 
 

5998

Posts

6

Resources
5
 

1000us? You can observe this too? However, the timer starts working after it is turned on. When the timer reaches the specified time, the hardware will set the flag. However, if you do not turn on the interrupt, it will not be triggered. At this time, if the interrupt is turned on, the interrupt will be triggered, which means that the delay between turning on the timer and turning on the interrupt is greater than the timing time of your timer.

This post is from GD32 MCU
 
 
 

5998

Posts

6

Resources
6
 

And there is also a problem with your design. Timer 6 will repeatedly interrupt. At this time, it is meaningless to repeatedly open timer 0. You can't design it this way.

This post is from GD32 MCU
 
 
 

6062

Posts

4

Resources
7
 
bigbat published on 2022-9-20 19:15 I have learned this answer through experiments. After the timer is initialized, that is, timer_enable(TIMER0); after this sentence is executed, the timer is already...

The statement timer_enable(TIMER0) may have triggered the update of the automatic reload value.

You can update the auto-reload value or disable and then enable the timer before the interrupt is enabled.

This post is from GD32 MCU
 
 
 

6062

Posts

4

Resources
8
 
This post was last edited by damiaa on 2022-9-21 09:37
/*以前做的一个*/
/*按周期启动定时器2*/
void TIM2_Start(uint32_t tim_period)
{
  uint16_t tmpcr1 = 0;
  tmpcr1 = TIM2->CR1;  
  /* Select the Counter Mode */
    tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS)));
    tmpcr1 |= (uint32_t)TIM_CounterMode_Up;
    /* Set the clock division */
    tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CR1_CKD));
    tmpcr1 |= 0;
  TIM2->CR1 = tmpcr1;
  /* Set the Autoreload value */
  TIM2->ARR = tim_period;;
  /* Set the Prescaler value */
  TIM2->PSC = 0;
  /* Generate an update event to reload the Prescaler and the Repetition counter
     values immediately */
  TIM2->EGR = TIM_PSCReloadMode_Immediate;  
  /* Prescaler configuration */
  TIM_PrescalerConfig(TIM2, 3, TIM_PSCReloadMode_Immediate);
  TIM2->SR = (uint16_t)~TIM_FLAG_Update;
  /* Enable the Interrupt sources */
  TIM2->DIER |= TIM_IT_Update;	
  /* TIM2 enable counter */
  TIM2->CR1 |= TIM_CR1_CEN;
}

This is one used in previous products. I don't know if it has any reference value. It is from stm32f0 standard library 1.5. It is a direct operation register. It is for higher efficiency.

This post is from GD32 MCU

Comments

I used an oscilloscope to see what the naked eye cannot do. My purpose is: there are two switches A and B. After A is turned on, B will delay for a period of time before starting, between 100uS and 5000us. So I use a timer as a control timing, but it seems that the two signals are synchronized. In fact, I want Timer6 to only interrupt each time it is turned on.  Details Published on 2022-9-21 09:40
 
 
 

2865

Posts

4

Resources
9
 
damiaa posted on 2022-9-21 09:23 /*Previous one*/ /*Start timer 2 according to period*/ void TIM2_Start(uint32_t tim_period) { uint16_t tmpcr ...

I used an oscilloscope to see what the naked eye cannot do. My purpose is: there are two switches A and B. After A is turned on, B will delay for a period of time before starting, between 100uS and 5000us. So I use a timer as a control timing, but it seems that the two signals are synchronized. In fact, I want Timer6 to interrupt only once each time it is turned on.

The current solution is to turn off Timer6 in the interrupt. I don't know if it is feasible.

This post is from GD32 MCU

Comments

It is OK to turn off timer 6 after interruption. However, you should check the auto-reload value and reload it every time you start timer 6 in the main program, then enable interruption and start the timer immediately. In this way, the time can be controlled accurately.  Details Published on 2022-9-21 09:55
 
 
 

6062

Posts

4

Resources
10
 
bigbat posted on 2022-9-21 09:40 I used an oscilloscope to see what the naked eye cannot do. My purpose is: there are two switches A and B. After A is turned on, B will delay for a period of time before starting. 100u ...

There is no problem with timer 6 shutting itself off after an interrupt.

However, next time you start timer 6 in the main program, you should check to make sure the auto-reload value is reloaded, then enable interrupts and start the timer immediately. In this way, the time can be accurately controlled.

This post is from GD32 MCU

Comments

Thank you, should I turn off the reload timer_auto_reload_shadow_enable(TIMER6); and load manually  Details Published on 2022-9-21 10:09
 
 
 

2865

Posts

4

Resources
11
 
damiaa posted on 2022-9-21 09:55 It is OK to shut down timer 6 after it is interrupted. However, you should handle it every time you start timer 6 in the main program to make sure the auto-reload value is reset...

Thank you, should I turn off the reload timer_auto_reload_shadow_enable(TIMER6); and load manually

This post is from GD32 MCU

Comments

You can modify and test it by referring to the initialization code. If it is only once, you should be able to install it manually. You can try it.  Details Published on 2022-9-21 10:34
 
 
 

6062

Posts

4

Resources
12
 
This post was last edited by damiaa on 2022-9-21 10:35
bigbat posted on 2022-9-21 10:09 Thank you, should I turn off the reload timer_auto_reload_shadow_enable(TIMER6); manual loading

You can modify and test it by referring to the initialization code. If it is only once, you should be able to install it manually. You can try it. The code on the 8th floor has also been tested and can be used.

This post is from GD32 MCU
 
 
 

10

Posts

0

Resources
13
 

Depending on the specific situation, you can configure it to enter later or immediately.

This post is from GD32 MCU
 
 
 

18

Posts

0

Resources
14
 

In fact, as long as the instruction sequence is correct, there should be no error. That is, first initialize the timer, then enable (allow) interrupts, and finally start counting. That's it.

This post is from GD32 MCU
 
 
 

115

Posts

0

Resources
15
 

It is timer_enable(TIMER), which executes the timer first and then turns on the interrupt to enter the interrupt state.

This post is from GD32 MCU
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list