You are reading a single comment by @CamSwords and its replies. Click here to read the full conversation.
  • Ok, I think I understand my PWM input woes now.

    An external interrupt is registered on the rising and falling edge of a pin. The interrupt code records the pin, time, and pin state and pushes an io-event to an event queue. Later, in the main program loop (in jsidle) the io-events are consumed. Here, depending on whether the user specified rising, falling or both, and the state of the pin then one of two things will happen. If the callback shouldn't be triggered (eg. falling edge when the user watches for rising edges) then the lastTime will be set to the event time and the loop will continue. If it's callback time, then the previously defined lastTime will be used along with the event time and the callback will be executed.

    The issue I'm facing is that the (event.time - event.lastTime) I'm getting is far larger than the pulse coming on the pin I'm watching. I believe this is happening because when io-events are added to the event queue they are discarded if the queue is full. That makes sense from a memory conservation point of view, however it means that "lastTime" represents the time between the current io-event and last io-event that wasn't discarded due to a full queue. That makes me distrust "lastTime" completely. I still trust "time", because it's defined when the interrupt is called, not in the main program loop.

    I think I have a work around, which is to watch the pin on both edge rising and fall and calculate the time difference using only "time". When io-events are discarded from the event queue I will still get an incorrect value, but I can recognise this because I know the minimum and maximum value that duty cycle should be on for (1ms - 2ms). Anything outside this range means that io-events were discarded and therefore I didn't get a correct reading. This means that I will get less PWM input readings, but it does mean that they will be accurate (so I believe).

    Anyone have any thoughts?

About

Avatar for CamSwords @CamSwords started