PIC24F: Make TMR reset with OC module self-sync

Thread Starter

Robin66

Joined Jan 5, 2016
281
Hi. Does anyone know a clean way to have an OC module self-sync but also reset the associated TMR at the same time? I want to do this so that I can use the TMR3 rollover event to trigger an ADC capture at the start of the OC's PWM period. It doesn't appear to be possible to trigger an ADC capture directly from the OC module.

Here's my config:

Code:
    //****************************************
    // TIMER3: L pump ON/OFF: 16MHz increment
    //****************************************
    T3CONbits.TCS = 0; // internal clk
    T3CONbits.TCKPS = 0; // no prescaler
    T3CONbits.TON = 0; // ensure OFF to start with
  
    // set up output compare on OC1
    OC1CON1 = 0;
    OC1CON1bits.OCM = 0b110; // edge-aligned PWM
    OC1CON1bits.OCTSEL = 1; // TMR3 drives clock
    OC1CON2bits.SYNCSEL = 0x1F; // self-sync
    IFS0bits.OC1IF = 0;
    IEC0bits.OC1IE = 1; // interrupt on every OC1 event
    OC1R  = 8;
    OC1RS = 100;
I determined that TMR3 wasn't being reset by checking its value upon the OC1 interrupt. It's random
Code:
void __attribute__((interrupt,auto_psv)) _OC1Interrupt(void)
{ // start of L PWM pulse
  
    da = TMR3;
    IFS0bits.OC1IF = 0;
}
 

Thread Starter

Robin66

Joined Jan 5, 2016
281
I've got a workaround for anyone trying to do the same thing. Sync the OC module with TMR3 and use PR3 to control the period. It's a bit clumsy but it seems to be working.

Ps. actually this is a minefield. The OC1R and OC1RS vars are buffered whereas PR3 is not! Found this difficulty the hard way
 
Last edited:
Top