lpcware

Invert Timer output

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by rickta59 on Fri May 10 16:33:44 MST 2013

I'm trying to figure out how to use the timers properly. I using something like the following code to end up with a 20msec period with a 1msec duty. Typical of what you might find with an analog servo. I can't figure out how to invert the signal without using hardware or writing an my own interrupt handler to toggle gpio pins. Is it possible using PWM to have the timer start high and then go low when the match is reached?


<pre><code>    // enable clock to 16bit timers (3.5.14)
    LPC_SYSCON->SYSAHBCLKCTRL |= (1 << 7);

    LPC_IOCON->PIO0_8 |= (1<<1);        // output MR0

    LPC_TMR16B0->PR = (F_CPU/1000000)-1; // set prescaler to 1 us

    LPC_TMR16B0->MR3 = 20000;           // 20msec
    LPC_TMR16B0->MR0 = 1000;            // 1msdc

    // use MR3 for period, use MR0, MR1 as the duty cycle
    LPC_TMR16B0->MCR |= (0x01 << 10);

    // enable PWM for MAT0, MAT1
    LPC_TMR16B0->PWMC |= 0b11;             // enable pwm P0.8/P0.9

    NVIC_EnableIRQ(TIMER_16_0_IRQn);
 
    LPC_TMR16B0->TCR = 0x1;
</code>
</pre>

The code above does what I want, except it starts low and goes high on the match. I realize I could use 19000 for the match and then have it go high for the last 1msec, however that doesn't start at the beginning of the period.


I've searched for sample servo code but didn't find anything.


-rick

Outcomes