Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 8 months ago. This question has been closed. Reason: Unclear question
Problem with timer in a class
Hi !
I'm having a problem with a class that I'm creating to read a PWM on a digital input.
This class takes an InterrputIn and a timer as parameter and then get the duty_cycle and the period of the signal on the interrupt in.
My problem is : when I read the period, the value of the timer isn't always the same. I used the same code out of the class and it works, that's why I think that the class is the problem. To solve it I now use pointers for the InterruptIn and the timer but it doesn't work.
However, my code works for the duty_cycle which is good, but strange.
The code :
My class and the tests
class CPwmIn
{
protected:
Timer* t;
float duty_cycle;
float period;
public:
CPwmIn(InterruptIn* _PwmIn,Timer* _t):t(_t),duty_cycle(0),period(0)
{
_PwmIn->rise(this, &CPwmIn::start_timer);
_PwmIn->fall(this, &CPwmIn::stop_timer);
}
void start_timer()
{
period=t->read_us();
period = period/1000000;
printf("%f \n\r\t",period);
t->reset();
t->start();
}
void stop_timer(){
duty_cycle = t->read_us();
duty_cycle = duty_cycle/1000000;
duty_cycle = duty_cycle / period;
}
float GetPeriod(){return period;}
float GetDutyCycle(){return duty_cycle;}
};
Timer t;
AnalogIn pot(p20);
PwmOut test(p22);
InterruptIn _m2(p15);
CPwmIn e1(&_m2,&t);
void test1(){
test.period(0.0025);
while(1){
test.write(pot.read());
}
}
Output :
/media/uploads/adrienPPS/screen_shot_2015-03-14_at_22.13.34.png
Thank you !
ANSWER : No printf in an ISR...