设置中断标志

发布于 2024-12-25 04:10:18 字数 2229 浏览 2 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘然心甜 2025-01-01 04:10:18

是的,您可以通过软件设置中断标志。但在我看来,这样做并不是一个好的做法...

如果您确实希望 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文