2012-01-06 22 views
1

能否在中断标志由代码在下面的例子中设置或者是该行只是思维的错误?这只是主要功能。在这个代码下面,snipet是它自己的中断,在代码的最后清除中断标志是否正确和不合理?将中断标志

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; 
} 
    } 
+2

这是什么平台? – Staven 2012-01-06 13:55:40

+0

看代码它是一个Microchip PIC微控制器 – greydet 2012-01-06 15:04:06

+0

PIC18微芯片 – Christian 2012-01-06 19:17:40

回答

1

是的,你可以通过软设置中断标志。但IMO并不是一个很好的做法......

如果你真的想要你的ISR的行为在正常情况下执行,为什么不把你的ISR代码外化到一个函数中可以调用你的主要功能?

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

+0

啊..谢谢!偶尔会发生这种情况(它只是循环)。此代码之前没有clr标志。所以我打算调试它,但是我的调试器不会去那里,因为代码是在#define SoftPwm()这样的头文件中。而且它被放在那里导致某人,尽管它会更快。 – Christian 2012-01-06 14:51:04

+0

为什么我不外部化那是因为我有更多的优质ISR是这样的: 的#ifdef FLOW_H \t的#ifdef SOFT_PWM_H \t \t的#pragma中断Microchip_InterruptHook \t \t无效Microchip_InterruptHook(无效) \t \t { \t \t \t Flow_Interrupt(); \t \t \t SoftPWM_Interrupt(); \t \t} \t #endif #endif – Christian 2012-01-06 14:52:16