7 years, 5 months ago.

My program stops when i use function wait

I have an example code in which i send time through printf to a terminal(tera term). If i use the function wait when the program executes for the first time function wait it stops for ever, while if i use a Timeout object to wait time the program sends time to video every second correctly. Why? Thanks. Here i report the 2 codes:

CODE with problems:

#include "mbed.h"

DigitalOut myled(LED1);

int main() {
    
    printf("RTC example\n"); 
    set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
    printf("Date and time are set.\n");

    while(1) {

        time_t seconds = time(NULL);

        //printf("Time as seconds since January 1, 1970 = %d\n", seconds);
        
        printf("Time as a basic string = %s", ctime(&seconds));

        //char buffer[32];
        //strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
        //printf("Time as a custom formatted string = %s", buffer);

        myled = !myled;      
        wait(1);
    }
}

CODE without problem

#include "mbed.h"

DigitalOut myled(LED1);
Timeout timeout;

void timer_interrupt()
{   
    timeout.attach(&timer_interrupt,1);
    time_t seconds = time(NULL);

    //printf("Time as seconds since January 1, 1970 = %d\n", seconds);
    
    printf("Time as a basic string = %s", ctime(&seconds));

    //char buffer[32];
    //strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
    //printf("Time as a custom formatted string = %s", buffer);

    
    myled = !myled;   
}

int main() {
        printf("RTC example\n"); 
        wait(5.0);
        set_time(1387188323); // Set RTC time to 16 December 2013 10:05:23 UTC
        
        printf("Date and time are set.\n");
        
        timeout.attach(&timer_interrupt,1);
        
        while(1);
    
    }

1. Please edit your post to use <<code>> tags e.g.

<<code>>
#include "mbed.h"
void main () {
}
<</code>>

2. Please define "doesn't work". We know your program doesn't work, if it did work you wouldn't be posting a question. So please tell us what it is doing wrong. It doesn't wait? It doesn't print anything? It prints something and then stops? Does the LED blink but nothing comes out? On the version that does work do you get all of the expected output or is some of it missing?

posted by Andy A 07 Nov 2016

Thanks. I edited my question

posted by Gabriele Piscopo 07 Nov 2016

It's funny, because I would have guessed the second bit was the one with problems. It is my understanding you should not call printf from within an interrupt. Also you call .attach twice, once from main as expected and once inside the interrupt itself. Line 8 needs to be removed entirely. I am not seeing any obvious problems with the first one. It is probably stopping because of an exception somewhere, be sure you can view any error messages.

posted by Graham S. 08 Nov 2016
Be the first to answer this question.