7 years, 10 months ago.

Read Analoge in Ticker

/media/uploads/Eyal/analoge_and_ticker.txt

hii

I am using NUCLEO-F103RB

I am tring to read analog data with a ticker but when i am running it on the controller the program gets stuck

Does any one knows why? And how can i fix it ?

Will this problem will also happen in different type of communications (I2c, digital and so on)?

this is an example code that I wrote : (the code is in the txt file)

1 Answer

7 years, 10 months ago.

First - Please use

<<code>>
your program
<</code>>

to post code rather than linking to a text file. It makes your code visible while answering the question.

//An exam code only

#include "mbed.h"

//Global Variables
float *an;
int ani;
 
//Analog
AnalogIn wheel_ang(PC_1);
AnalogIn front_brake(PC_0);

//Ticker
Ticker analog_tic;
//in my code there is more tickers 

//functions
void Analog_read_all( )
{
    *(an+0+ani)=wheel_ang.read();
    *(an+1+ani)=front_brake.read();
    ani=ani+2;
}
// main code:
int main()
{
    ani=0;
    printf("start\r\n");//just for exam
    //call Tickers
    analog_tic.attach(&Analog_read_all,0.1);
    wait(0.2);
    printf("After Ticker\r\n");//just for exam
    while(1)
    {
        wait(0.5);
        analog_tic.detach();
        //more in in my code in this loop

        printf("in while");//just for exam
        ani=0;
   
        //call Tickers
        analog_tic.attach(&Analog_read_all,0.1);
        
    }
}

The reason the program is crashing is nothing to do with the ticker or the analog in, it's because the code you are calling inside the ticker is writing random values to random memory addresses. That will almost always cause a program to crash.

Your code is

    *(an+0+ani)=wheel_ang.read();

where ani will be between 0 and 14 and an is undefined.

an will probably be 0 but there is no guarantee of that, it could be anything since you never set its value.

You need to define an as pointing to a block of memory that is allocated for storing the results. The easiest way would be to make the following change

//float *an;
float an[16]; 

The rest of the code should then work correctly.

thanks , i tried it , still did not help , if you see another problem i will be happy know

posted by Eyal Doppelt 24 Jun 2016