设置中断标志
可以通过代码设置中断标志,如下例所示,还是该行只是一个思维错误?这只是主要功能。下面的代码片段是中断本身,在代码末尾清除中断标志是否正确且必要?
if(duty != (uint8_t) (SOFT_PWM_PERIOD - 1))
{
// Request an immediate interrupt if the timer counter has
// already the initial period. This helps minimize glitches
// when changing duty cycles
if(duty < TMR4)
PIR3bits.TMR4IF = 1;
// Finally (re-)start the timer
T4CON =
0 << 3 | // 1x post-scaler
1 << 2 | // Active
2 /* << 0 */; // 16x pre-scaler
IPR3bits.TMR4IP = 1; // TMR4 Overflow Interrupt Priority bit High
PIE3bits.TMR4IE = 1; // TMR4 Overflow Interrupt Enable bit
}
中断代码->
// Deal with PWM timer interrupts. Add this to the high-priority interrupt handler.
void SoftPWM_Interrupt(void)
{
volatile uint8_t _SoftPWM_Toggle; // Is this variable really accessed by both the ISR and mainline functions? (C.G)
/* Has a flank been reached yet? */
if(PIR3bits.TMR4IF)
{
/* Alternate between the low and high periods */
PR4 ^= _SoftPWM_Toggle;
/* Try to deal gracefully with the new period already having been reached. */
/* The hardware timer works by checking if TMR4 = PR4 when it is time to increase */
/* counter, in which case TMR4 is reset instead. Thus if is already TMR4 > PR4 due to */
/* interrupt latency then we've missed the period and an extra interrupt is needed. */
/* First acknowledging the flag and then conditionally setting it is necessary to */
/* avoid a race between reading TMR4 and changing the flag. */
/* Finally the the TMR4 > PR4 test is actually implemented as skip if TMR4 < PR4 + 1 */
/* but the increment cannot overflow since the interrupt won't be used for 0% or 100% */
/* duty cycles */
PIR3bits.TMR4IF = 0;
_asm
INCF PR4,0,ACCESS
CPFSLT TMR4,ACCESS
_endasm
/* if(TMR4 > PR4) */
PIR3bits.TMR4IF = 1; // Cant only the harware set this flag? (C.G)
/* Finally toggle the output pin */
SOFT_PWM_PIN ^= 1;
/*Important?*/
PIR3bits.TMR4IF = 0;
}
}
Can a interrupt flag be set by the code as in the example below or is that line just an error of thinking? This is just the main function. Below this code snipet are the interrupt it self, is it correct and nessasary to clear the interrupt flag in the end of the code?
if(duty != (uint8_t) (SOFT_PWM_PERIOD - 1))
{
// Request an immediate interrupt if the timer counter has
// already the initial period. This helps minimize glitches
// when changing duty cycles
if(duty < TMR4)
PIR3bits.TMR4IF = 1;
// Finally (re-)start the timer
T4CON =
0 << 3 | // 1x post-scaler
1 << 2 | // Active
2 /* << 0 */; // 16x pre-scaler
IPR3bits.TMR4IP = 1; // TMR4 Overflow Interrupt Priority bit High
PIE3bits.TMR4IE = 1; // TMR4 Overflow Interrupt Enable bit
}
The Intrrupt code ->
// Deal with PWM timer interrupts. Add this to the high-priority interrupt handler.
void SoftPWM_Interrupt(void)
{
volatile uint8_t _SoftPWM_Toggle; // Is this variable really accessed by both the ISR and mainline functions? (C.G)
/* Has a flank been reached yet? */
if(PIR3bits.TMR4IF)
{
/* Alternate between the low and high periods */
PR4 ^= _SoftPWM_Toggle;
/* Try to deal gracefully with the new period already having been reached. */
/* The hardware timer works by checking if TMR4 = PR4 when it is time to increase */
/* counter, in which case TMR4 is reset instead. Thus if is already TMR4 > PR4 due to */
/* interrupt latency then we've missed the period and an extra interrupt is needed. */
/* First acknowledging the flag and then conditionally setting it is necessary to */
/* avoid a race between reading TMR4 and changing the flag. */
/* Finally the the TMR4 > PR4 test is actually implemented as skip if TMR4 < PR4 + 1 */
/* but the increment cannot overflow since the interrupt won't be used for 0% or 100% */
/* duty cycles */
PIR3bits.TMR4IF = 0;
_asm
INCF PR4,0,ACCESS
CPFSLT TMR4,ACCESS
_endasm
/* if(TMR4 > PR4) */
PIR3bits.TMR4IF = 1; // Cant only the harware set this flag? (C.G)
/* Finally toggle the output pin */
SOFT_PWM_PIN ^= 1;
/*Important?*/
PIR3bits.TMR4IF = 0;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以通过软件设置中断标志。但在我看来,这样做并不是一个好的做法...
如果您确实希望 ISR 的行为在正常上下文中执行,为什么不将 ISR 代码外部化在一个可以在您的函数中调用的函数中呢?主要功能?
关于中断标志,如果不清除它,ISR将循环执行,并且永远不会回到主程序。
Yes you can set an interrupt flag by soft. But IMO it is not really a good practice to do it...
If you really want the behavior of your ISR to be executed in the normal context, why don't you externalize your ISR code in a function that you can call in your main function?
About the interrupt flag, if you don't clear it, the ISR will be executed in loop and you will never go back to your main program.