News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

How do I synchronise HPWM to interrupt ?

Started by See_Mos, Jan 25, 2023, 12:50 PM

Previous topic - Next topic

See_Mos

I have been testing Les' HPWM.INC with 18F25K22.  I need to synchronise the HPWM with an interrupt on PORTB.0

I can use the interrupt routine to stop and start the HPWM but the output is free running and not synchronised to HPWM_Start() so starting HPWM is not locked to the interrupt and as seen on an oscilloscope drifts relative to the interrupt which occurs at regular timing.

I have tried starting and stopping Timer 2 before starting or stopping HPWM. According to the data sheet writing to T2CON clears the pre scale and post scale counters but that did not work.

It could be another ISIS simulation error so I might try real hardware.

Any pointers please?

top204

You can try something I had to do in the DDS firmware I created, in order to synchronise the PWM signal that operates as a DAC:

Repeat : Until PIR1bits_TMR2IF = 1                  ' \ Synchronise to the start of the PWM cycle
PIR1bits_TMR2IF = 0                                 ' /


Without the above code, the PWM output was all over the place, but as soon as I added the loop, it all fell into place.

It may work for you as well.

See_Mos

after looking again at the data sheet and section 13 of PICmicro MID-RANGE MCU FAMILY it became clear that what I needed was to simply clear TMR2 register and this can be done immediately before or after HPWM1_Start()

top204

Looking at my loop, that is also what it is doing, but in a round-about way. :-)

When the flag is set, TMR2 has overflowed to 0, so it is reset so it can be tested again in another loop iteration.

I'll see how a simple TMR2 = 0 performs. It was a case of not seeing the wood for the trees by me. LOL