2012-01-06 8 views
1

以下の例のようにコードによって割込みフラグを設定することはできますか?これは単なる主な機能です。このコードスニペットの下には、それ自身の割り込みがありますが、コードの最後に割り込みフラグをクリアするのは正しいですか?割込みフラグを設定する

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

+0

コードを見ると、マイクロチップPICマイクロコントローラ – greydet

+0

PIC18マイクロチップ – Christian

答えて

1

はい、あなたがソフトで、割り込みフラグを設定することができます。しかし、IMOは実際にそれを行うのは良い習慣ではありません...

あなたは本当にあなたのISRの動作を通常のコンテキストで実行したいのであれば、あなたの主な機能を呼び出すことができますか?

割り込みフラグについては、クリアしないとISRがループして実行され、メインプログラムには戻りません。

+0

ああ..ありがとう!そのようなことは時折起こります(ただループします)。このコードには以前はclrフラグがありませんでした。だから私はそれをデバッグしようとしていましたが、私のデバッガはコードが#define SoftPwm()のようなヘッダにあったために行かなかったでしょう。 – Christian

+0

そして、なぜ私はこのような、より高いISRのの持っているので、それは外部化いけない:FLOW_H #ifdefの \t SOFT_PWM_H Microchip_InterruptHook \t \t無効Microchip_InterruptHook(無効) \t \t { \t \t割り込み\t \tの#pragma #ifdefの\t Flow_Interrupt(); \t \t \t SoftPWM_Interrupt(); \t \t \t #endif #endif – Christian

関連する問題