【问题标题】:set interrupt flag设置中断标志
【发布时间】:2012-01-06 13:53:10
【问题描述】:

可以通过代码设置中断标志,如下例所示,还是该行只是一个错误的思考?这只是主要功能。这段代码下面是它自己的中断,在代码末尾清除中断标志是否正确和必要?

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
    }

Intrrupt 代码 ->

      // 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;
}
     }

【问题讨论】:

  • 看代码是Microchip PIC单片机

标签: c interrupt flags microchip


【解决方案1】:

是的,您可以通过软设置中断标志。但是 IMO 这样做并不是一个好习惯......

如果您真的希望 ISR 的行为在正常上下文中执行,为什么不将您的 ISR 代码外部化到可以在主函数中调用的函数中?

关于中断标志,如果你不清除它,ISR会循环执行,你永远不会回到你的主程序。

【讨论】:

  • 啊..谢谢!这种情况偶尔会发生(它只是循环)。此代码之前没有 clr 标志。所以我正要调试它,但我的调试器不会去那里,因为代码在像#define SoftPwm()这样的标题中......它被放在那里是因为有人虽然它会更快。跨度>
  • 为什么我不将其外部化是因为我有更多这样的高 ISR:#ifdef FLOW_H #ifdef SOFT_PWM_H #pragma interrupt Microchip_InterruptHook void Microchip_InterruptHook(void) { Flow_Interrupt(); SoftPWM_Interrupt(); } #endif #endif
猜你喜欢
  • 2020-03-29
  • 1970-01-01
  • 1970-01-01
  • 2015-11-28
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 2018-08-10
  • 2023-03-26
相关资源
最近更新 更多