Timer help,

11 Jan 2010

Hi,

Could some one please point me in the right direction! How do I make use of the other timers on the LPC1768? I am using the ticker function to drive a stepper motor (which i think uses timer3),

 

I beleive I need to make use of the other timer(s) for additional motors as the ticker does not work properly for me as below,

#include "mbed.h"
#include "TextLCD.h"
#include "init.h"
#include "stepper.h"

LocalFileSystem local("local");
Serial pc(USBTX, USBRX);
Ticker xstepper;
Ticker ystepper;
TextLCD lcd(p24, p25, p26, p27, p28, p29, p30); // rs, rw, e, d0, d1, d2, d3

PwmOut lcdled(p22);


int main() {
X_DIR=1;
    lcd.cls();
    lcd.locate(0, 0);               // ROW 1
    lcd.printf(" Hello ");

    for (float p = 0.00f; p < 1.00f; p += 0.01f) {
        lcdled = p;
        wait(0.01);

    }
    
    xstepper.attach_us(&step_x, 100);
    ystepper.attach_us(&step_y, 100);
    XTARGET=10000;
    YTARGET=10000;
    
    while(1) {
    //main loop
    
    
    
    
    }
    

The step_x & step_y are like below...

void step_x() {
if (XSTEPS<XTARGET) {
X_STEP=1;
wait_us(1);
X_STEP=0;
XSTEPS++;
}
}

 

Thanks.

Mike.

 

13 Jan 2010

Really, No one!!!

Cheers!

14 Jan 2010 . Edited: 14 Jan 2010

Hi Mike,

Just looking at that, i'm not quite clear what the intent is.

You have 2 tickers setup to tick every 100us (pretty fast), but inside the routine, you seem to be looping of the order of 10,000 times based on the variables I can see, with a delay of 1us i.e. 10,000us.

So my guess is the first ticker will tick, get in to your function, then be stalled in there for 10,000us or so, and as soon as you leave you now have a huge backlog of "ticks" to service (approx 2 x 10,000 / 100). I wonder if there is a slight logic/algorithmic error here?

Another explanation could be that the XTARGET is not set as volatile (can't see all the code), and hence your interrupt routine never sees anything other than zero?

Simon

14 Jan 2010

Okay now i'm confused.

My intention was to at a set interval call step_x and step_y (every 100us in this example), If i had not allready made all of the 10000 steps then do some more.....

Does that make sense? I want to step my motors using timers but will change the rate in which they are called dynamicly to allow speed change and accel / decel.

You say 100us is pretty fast, If I wanted to go much faster should I be using a seperate timer and not the Ticker function?

 

As for the

if (XSTEPS<XTARGET) {

Wont that simply execute the code if XSTEPS<XTARGET else skip right pass it?

Am I doing something stupid?

 

Thanks.

Mike.

14 Jan 2010

Hi Mike,

 

Michael Duffin wrote:
Okay now i'm confused.

Yep, that'd be my fault. I read your if() as a while() somehow - some breakdown between eyes and brain there. Sorry about that.

Michael Duffin wrote:
My intention was to at a set interval call step_x and step_y (every 100us in this example), If i had not allready made all of the 10000 steps then do some more.....

Ok, yes. that makes sense. I think using the Ticker should be fine with that approach. Looks like it is time for a little debugging of the code...

When you say "the ticker does not work properly for me as below", what do you actually mean? What is the behaviour you are seeing?

It might also be worth looking at things like the variables you are using. e.g.:

 

void step_x() {
  if (XSTEPS < XTARGET) {
    X_STEP=1;
    wait_us(1);
    X_STEP=0;
    XSTEPS++;
  }
}

What do the definitions of XSTEPS, XTARGET and X_STEP look like? Does the comparison always fail?

If you publish the project or share these parts of the code, someone can probably help you get it up and running! I suspect you are almost there.

Simon

14 Jan 2010

Hi,

Here is another example of what I am trying to do TickerTest

Here I want to continually step my motor at 200us, And update the number of steps so far on an LCD every 700000us.

It works but every time the second ticker (for the LCD) is fired I get a pause on the first, This is why I was wondering if using the other timers would help. To make matters worse I want to run 3 motors that could be below 100us.

Mike.