Pulse generation and measuring.

roho

Member
I'm trying -- and at the moment, failing -- to generate and measure a pulse of accurate width. The set up is very simple: using a 40X2, pull up pin B.7 using a 10k resistor. I'm also using port A to display a couple of register values using a bank of LEDs. Please see the attached code and note that the PICAXE can actively drive pin B.7 low, but a high level logic is achieved by defining pin B.7 as an input and the external resistor pulling the undriven line high. The intention is to be able to connect multiple PICAXEs together, though for the moment just a single chip is in use.

Simulations are promising: the actual length of the pulse at the pin is the same as defined in the register at 41 ms, and the value measured using the timer is 40 ms. I think that I've set the timer up correctly to measure milliseconds.

Not for the first time, real silicon differs from the simulations. Here the initially set value is the same 41 ms, but the actual pulse width at the pin is 55.45 ms and the value measured by the timer (as displayed on the port A LEDs) is 46 ms. These differences are well outside what I'm prepared to tolerate. It seems that only by stripping away commands can I approach the intended value of pulse width.

I don't have any problem with simulations and real silicon differing, but I don't understand at the moment why they are different. Is there some mistake in my coding that can be put right, or am I facing unpredictable or predictable delays due to the commands in sequence with the PAUSE statements? (Have I been missing something in setting up the simulator if the simulated time for executing an instruction differs so much from reality?) The former can hopefully be put right, but the latter may mean I'm trying to do something that's effectively impossible and so must find an alternative method.

Roger.

View attachment PulseTest40X2.bas
 

hippy

Technical Support
Staff member
Simulation and real silicon can differ because it simulates rather than emulates. It would probably be best to use SERTXD to output what your actual results are rather than rely on LED displays.

Not sure what the issue is as it would take some time to analyse exactly what you are doing in your code.

This thread may give some ideas as to how to create a very accurate high resolution timer, though it is probably not very 'novice freindly' -

http://www.picaxeforum.co.uk/showthread.php?14517-High-Accuracy-Timer
 

roho

Member
After a bit more experimenting, it seems that the major part of the discrepancy (between what is set in the register and what appears at the pins) in the generation of the pulse is due to the timer being active. It appears not to matter whether I actually use the timer in the programme or not. Does that make any sense? Further improvements have been made by running the clock first at 16MHz and then at 32MHz. I'm now at the point of the register being set to 41 ms, the pulse width at the pins being 44 ms and the measured value using the timer being 42 ms. Still a bit to go in terms of accuracy, but a big improvement on 48 hours ago.

Thanks for the code, hippy, and also for the tips -- SERTXD taken into use, old HW habits die hard, I guess. The code should be something I can take into use more or less directly (I think, I still need to work out what's being calculated in a couple of the sections). I'll try it anyway, and see how it goes.
 
Top