How to PWM using a RTOS

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to PWM using a RTOS

3,350 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by SnEAky on Thu Mar 18 04:25:58 MST 2010
Hello,

How is it possible to control a servo motor using a 50Hz PWM. It's for a  LPCXpresso 1343. I've managed to port the FreeRTOS to 1343, but I have  no idea how to use the timers. I got the PWM code working -without- FreeRTOS but I can't get it working -with- FreeRTOS.

I  just have no idea how to make this work in FreeRTOS. I've put the init  in setuphardware(); and i call that function in the main.
Next I start a  task that will set the pulse width, but for some reason  nothing happens, the output is just a logical '1' and nothing happens.

I hope that someone can help me,

Thanks in advance
8 Replies

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by SnEAky on Fri Mar 19 12:37:52 MST 2010
I have not changed anything in that matter. I've actually used a nearly naked FreeRTOS that used to be for the LPC17xx and made it work for the LPC13xx. In that nearly naked version, it only made 1 task and let it run. To test it out, I first created two simple tasks with a taskdelay to make two different LED's blink, this worked perfectly. Anyways, I didn't change anything about timer matters.  I will now try to use tim 16B0 instead of 16B1, maybe that will work. Secondly I will try to call the init within a task.
Meanwhile I'm hoping for someone who knows how to solve this problem. If needed I'll post the whole main.c.

Thanks so far.
0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanMorris on Fri Mar 19 12:00:56 MST 2010
I was just wondering if sneaky had changed this and there was a conflict....

I guess another thing to try would be to call the PWM init routine from within the task rather than before the scheduler is started.
0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by wrighflyer on Fri Mar 19 11:39:18 MST 2010

Quote: IanMorris
Which timer is freeRTOS using?


SYSTICK according to port.c in GCC/ARM_CM3 that I'm looking at.

(I guess it makes sense - that 's surely the whole purpose for SYSTICK?)

From the LPC13xx user manual:

Quote:
The System Tick Timer is an integral part of the Cortex-M3. The System Tick Timer is intended to generate a fixed 10 millisecond interrupt for use by an operating system or other system management software.

Since the System Tick Timer is a part of the Cortex-M3, it facilitates porting of software by providing a standard timer that is available on Cortex-M3 based devices.

0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by IanMorris on Fri Mar 19 08:51:15 MST 2010
Which timer is freeRTOS using?
0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by SnEAky on Fri Mar 19 02:31:27 MST 2010
I previously enabled a LED in that task, so I assume it is executing that task. But even if I don't use the task to set MR0 & set it in the init it won't PWM. Correct me if I'm wrong, FreeRTOS doesn't use the 16bit timer, so with this init it should work ?

But for some reason I don't get a PWM output.
0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by brucesegal on Thu Mar 18 19:27:05 MST 2010
I can't see much wrong. Are you sure that your vUserTask1 is executing to setup the PWM pulse width?

Also I don't think you need the GPIOSetDir on PIO1.9 and PIO1.10 as you have set these pins up to connect to the timer match outputs.
0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by SnEAky on Thu Mar 18 09:21:30 MST 2010
My PWM init:

void PWM_init(void){
    /** Timer 16 / PWM Settings **/
    // PWM output: 16bit_timer1

    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);      // Enable clock to timer 1
    LPC_IOCON->PIO1_9           &= ~0x1F;     // - remove bits 0,1 & 2 and pull resistors
    LPC_IOCON->PIO1_9           |= 0x01;      /* Timer1_16 MAT0 */ //- set bit 1
    LPC_IOCON->PIO1_10          &= ~0x1F;        // - remove bits 0,1 & 2 and pull resistors
    LPC_IOCON->PIO1_10          |= 0x02;        /* Timer1_16 MAT1 */

    LPC_TMR16B1->MCR = 0x0400;        /* Reset at MR3*/
    LPC_TMR16B1->PR = 50;                /* set prescaler 50*28800 = 72MHz */
    LPC_TMR16B1->MR1 = 7000;          /* Match register 1 - no PWM - toggle test */
    LPC_TMR16B1->MR3 = 28800;//14399; /* Match register 3 - cycle length */
    LPC_TMR16B1->EMR = 0xC2;          /* External Match register Settings for PWM channel have no effect.*/
    LPC_TMR16B1->PWMC = 0x01;         /* PWMC register -MAT0 is PWM. */
    GPIOSetDir( 1, 9, 0 );              // Set as output
    GPIOSetDir( 1, 10, 1 );             // set as output
    LPC_TMR16B1->TCR = 1;   // Enable timer 1
}

My main uses:

PWM_init();
xTaskCreate( vUserTask1, ( signed portCHAR * ) "Task1", USERTASK_STACK_SIZE, NULL, 1, NULL );
vTaskStartScheduler();

the vUserTask1 looks like this:

LPC_TMR16B1->MR0 = 28800-2880;    /* Match register 0 - pulse width */

Thats how my code looks like in big lines. So nothing special, you might say. But for some reason I'm not getting a PWM output on P1.09 not 1.10

Is it just me who's overseeing something, or am I getting a conflict somewhere?

I hope someone can help me
0 Kudos

1,768 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by brucesegal on Thu Mar 18 08:43:33 MST 2010
You might want to post bits of your code here. Maybe then someone would see something obvious. AS an aside there should be no difference with or without the RTOS as long as you don't have any hardware conflicts.

Just saying 'It doesn't work' won't get very far.









http://catb.org/~esr/faqs/smart-questions.html
0 Kudos