Page 4 of 4

Re: PWM + Сapture + Сomparison

Posted: Mon Jan 06, 2025 4:37 pm
by carworker
Believe me, I tried different options. Even this one. Without ";" if(PIR1 & (1 << CCP1IF)) // You wrote about this before.
And this option
if (PIR1=0b00000100) //Check if the CCP1 interrupt flag is set
{
FCM_%n(); //read int in body
PIR1bits.CCP1IF = 0; // Clear interrupt flag
}
// There are no changes

Re: PWM + Сapture + Сomparison

Posted: Mon Jan 06, 2025 9:49 pm
by mnfisher
The if(PIR1=0b000100) actually sets PIR1 to be 0b... To test for equality you need to use == (though here I would just test the relevant bit).

To strip things back a bit.

Can you measure a single pulse (and I would just try every rising pulse and not swap direction for the moment) with TMR0 disabled (not used). Then add in TMR0 - can you still measure a pulse. Then try adding a TMR0 counter. Does this break things?

What clock are you using - set as HS (in properties) - so I assume you have a 20MHz crystal attached. Does a 1s blinkie work AOK?

Have just tested a timer and pulse counter on the PIC12 and it seems to work okay - timer increases as expected and pulses still measured

Martin

Re: PWM + Сapture + Сomparison

Posted: Mon Jan 06, 2025 11:02 pm
by carworker
if(PIR1=0b000100) //it works like that too . if (CCP1IF) //this works too
I have done everything you recommend more than once. Capture works great until TMR0 is added to the program body. And then TMR0 interrupts somehow magically penetrate TMR1, thereby distorting measurements.
Microchip recommends stopping TM0 before reading CCPR1H CCPR1L or reading them 2 times.
I wonder if you use Flowcode to write to the chip?

Re: PWM + Сapture + Сomparison

Posted: Tue Jan 07, 2025 9:39 am
by LeighM
if(PIR1=0b000100) //it works like that too . if (CCP1IF) //this works too
Just to reiterate what Martin said,
and in addition, those two statements will always evaluate to true,
so you will always run the true clause code,
this will have unintended consequences.

Re: PWM + Сapture + Сomparison

Posted: Wed Jan 08, 2025 12:34 pm
by carworker
If anyone is still interested. I did it! Now TM0 does not affect TM1. A month ago I did not know S. As it turned out, this is not a problem. Thank you Martin. You helped. Indeed, Flowcode is a flexible system. You just need to learn to adapt this environment to your tasks.
Best regards, Petro

Re: PWM + Сapture + Сomparison

Posted: Wed Jan 08, 2025 2:20 pm
by mnfisher
Great that you've got it up and running.

Glad to have helped a little along the way - and I learnt something too, I'd not seen about the capture compare facility in PICs before and it looks very useful....

Martin

Re: PWM + Сapture + Сomparison

Posted: Wed Jan 08, 2025 4:51 pm
by chipfryer27
Hi

I was following with interest and it saved me from a ton of work to figure out.

Regards