This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Create a 1-shot timer from Timer unit on '240 or '24xx

I currently have a periodic waveform generated using the Timer compare hardware.

When the timers' count register matches it's compare register, an output signal is generated.

It relies on watching the timer counter to stop the timer after one cycle.

I don't want a continuous periodic waveform, just a single pulse.

I would prefer not to have to watch the timer counter to see when the period is over.

I'd like to simply setup the timer and compare unit and let it generate a single pulse, a 1 shot,  without

further monitoring and control of the timer.

 

Anyone know how to do this?

TIA,

Jim

 

 

  • Hi Jim! The only way I know of is to write an interrupt function (ISR) which is triggered on timer period match (T1PINT) which stops the timer. You have to be careful to leave enough margin in CMP registers to give time to the CPU to stop the timer once it reaches the period (when you will enter the interrupt with all the context saving, the timer will roll over, begin to count from 0 up). So CMP registers always have to have value bigger than the value the timer reaches before the ISR stops it.

    Regards, Mitja

  • Thanks for yor suggestion Mitja. Having an ISR turn off the Timer would work.

    But I had other higher priority interrupts running.

    I wanted to avoid all the possible ISR interactions.

     

    What I ended up doing was setting the period register, TxPR to 0xFFFF.

    This would give the longest time to turn off the Timer beffore another pulse is generated.

    I  turn it off by polling the Timer TxCNT register to see when it counts up to the desired value.

    Other ISR's pay interrupt and I may miss it, but the Compare hardware will generate an accurate pulse edge.

    To further increase pulse accuracy, I disable all interrupts briefly while I set the GPTCONA and TxCON registers to ensure the counter starts as soon as the pulse edge begins. This prevents an ISR from interrupting right after the pulse starts , but before the timer is started, and artificially lenggthening the pulse width.

    Jim

     

  • Jim,

    Does the 24xx PWM have a phase register like the 28xx parts?

    John