Ticker problems

21 Oct 2010

Hello

My problem is that the ticker prevents my programm from working. Before the ticker ticks for the first time(first three seconds) everything works fine. But when the ticker ticks the first time, only the messages are send and nothing else works.

Any idears?

 

#include "mbed.h"

void led_dimm_streifen();

PwmOut myled1(LED1);
PwmOut myled2(LED2);
PwmOut myled3(LED3);
PwmOut myled4(LED4);

AnalogIn ain(p20);
AnalogIn ain2(p19);

Ticker ticktack;

float p;

void info_ticker() {
    printf("LED: %f  PWM: %f \r\n", ain.read(), p);
}

int main() {
    ticktack.attach(&info_ticker, 3.0);
    while(1) {
        led_dimm_streifen();
    }
}

void led_dimm_streifen() {
    p=ain2;
    myled1 = (ain > 0.2) ? p : 0;
    myled2 = (ain > 0.4) ? p : 0;
    myled3 = (ain > 0.6) ? p : 0;
    myled4 = (ain > 0.8) ? p : 0;
}
22 Oct 2010

Most probably it is not able to finish the ain reading and sending it throug the uart before 3 seconds. So after the interrupt routine for the ticker finishes it starts to serve the next interrupt for the ticker, so your main routine does not get any time to execute. try to print in the main routine and just read the ain in the int routine, I think this will solve your problem.

22 Oct 2010

One more thing is if you gonna use a global variable inside the int routine than define it as volatile, in your case "p".

22 Oct 2010 . Edited: 22 Oct 2010

Hi

EDIT: I didnt see your latest post. But even when i declare the variables used in the interrupt-routine as volatile, nothing changes.

I think the time isn't a problem, it takes under 100ms to send the message.

In this new version I only change a variable in the ticker function, and check it in the main() funtion. Now the LEDs work fine, but nothing is send, the ticker routine will never be called.

Arghh.

#include "mbed.h"
void led_dimm_streifen();

PwmOut myled1(LED1);
PwmOut myled2(LED2);
PwmOut myled3(LED3);
PwmOut myled4(LED4);

AnalogIn ain(p20);
AnalogIn ain2(p19);

Ticker ticktack;

float p; 
float leds;
volatile int flag=1;

void info_ticker() {
//leds=ain.read();
//p=ain2.read();
    flag=1;
}

int main() {
    ticktack.attach(&info_ticker, 2.0);
    leds=ain.read();
    p=ain2.read();

    if (flag==1) {
        printf("LED: %f  PWM: %f \r\n",leds, p);
        //printf("LED: %f  PWM: %f \r\n",ain.read(), ain2.read());
        flag=0;
    } else {}

    while (1) {
        led_dimm_streifen();
    }
}

void led_dimm_streifen() {
        p=ain2.read();
    myled1 = (ain > 0.2) ? p : 0;
    myled2 = (ain > 0.4) ? p : 0;
    myled3 = (ain > 0.6) ? p : 0;
    myled4 = (ain > 0.8) ? p : 0;
}
22 Oct 2010

Shouldn't your if (flag == 1) { ... } be inside the while(1) { ... } loop ?

 

22 Oct 2010

I don't think so... Why do you mean it should be there?

Could it be possible that interrupts don't work while reading the ADC?

22 Oct 2010

Think about it. Your ticket callback function sets flag = 1. You test that flag once, and only once, just before entering the while(1) loop. You never test the flag again in the while(1) loop. So why would you expect the printf() to call to be made when the timer times out? The ticket is setting flag = 1 but nothing is testing that flag or resetting it back to zero. You need maybe this:-

    while (1) {
        led_dimm_streifen();
    
        // Each time around the loop test to see if the ticker callback
        // has set flag equal to one.
        if (flag == 1) {
            printf("LED: %f  PWM: %f \r\n",leds, p); 
            flag = 0;
        } 
    }

 

24 Oct 2010

thanks.

I thought the main routine will start at the beginning, after the int-routine was called.

25 Oct 2010

in this code

   } else {}

Seems like it doesn't belong there at all.
Though it's not invalid, it is also missing a semicolon.
26 Oct 2010

@steve childress

I know it's not needed, but for me the "else" belongs to the "if", it simply looks nicer.