Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:09:34+00:00 2026-05-27T22:09:34+00:00

Can a interrupt flag be set by the code as in the example below

  • 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;
}
     }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:09:35+00:00Added an answer on May 27, 2026 at 10:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can anyone please explain this code? #include <avr/io.h> #include <avr/interrupt.h> #include <avr/signal.h> char n
Can Thread.Abort interrupt a thread that is sleeping (using, say, Thread.Sleep(TimeSpan.FromDays(40)) ? Or will
1) How can the processor recognize the device requesting the interrupt? 2) Given that
In /proc/interrupts, I can see that some interrupt numbers belong to IO-APIC-fasteoi and some
I have a background worker which can be cancelled. The normal flows interrupt itself
Can somebody point me to a resource that explains how to go about having
How can I SILENTLY, without any messages can interrupt an installation from its CustomActions
I am trying to interrupt a thread that is running AES encryption on a
how can I interrupt python execution with a key and continue to run when
I dont know why but i can no longer interrupt my own thread. thread

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.