Page 2 of 2
Re: How do I get out of the WaituntilHigh macro?
Posted: Tue Oct 29, 2024 6:15 am
by mnfisher
If it's a digital signal (with a short length) then shouldn't use the switch component (debounce isn't needed and it is probably too slow) - timing PWM pulses might be pushing it !
Use an input of a digital port (set the port as a property (say inp) then read = inp in a calculation block) Will need to shift >> 2.
Using the timer is also slightly more problematic as it uses floating point maths for GetTime - perhaps try with GetRaw instead of Getmilliseconds
Would have to experiment with PWM (using the timer component might cause a clash too)
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Tue Oct 29, 2024 1:38 pm
by ChrisT66
Ok Martin, thank you!
Then I'm back to the original solution with the four Pulsin macros that you helped me with. Or do you think it's better to do this directly with an input instead of the button query?
Re: How do I get out of the WaituntilHigh macro?
Posted: Tue Oct 29, 2024 2:00 pm
by mnfisher
I'd try using an input rather than a button - the button component is going to be too slow to time 1-2ms pulses.
It's probably going to be tight on timing anyway - I would try using something similar to the GetTime macro in the last example - but read port c directly (inp = port) and rather than using a timer component count the number of loops required for the pin(s) to change.
I'd (wrongly) got the impression that the pins would be high at the start of the program and needed to timed until off - so you'll also need a way to wait for a pin (or pins) to go high or a timeout - which complicates things as well..
Something like
Loop
Inp = portc
Until inp or timeout
But unless all 4 pins go high at the same time then yes, would have to measure each pin separately - although you can do this with one macro and a bit mask.
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Tue Oct 29, 2024 9:45 pm
by mnfisher
I did a very small test program - to time a 'pulse' on a pin.
Here I just count 'loops' - but the results seem very consistent - for example a 4.1ms pulse gives a result of 8288 to 8290 (loops) reliably. I didn't test with a 2ms pulse but would expect ~4000 counts (I did test various lengths though - and results seem sensible - setting the length of the pulse on the smartscope seems a little hit or miss - and I checked the timing using the scope!)
I've used a Arduino Uno and output results to UART - but this should work on a Mega2560 (though you might need some work to get the output)
It waits for a 'low' on a pin then for a high (or times out) if it received a 'high' then it counts loops until pin goes low again. The port to use is in properties ('port') - I used port D (and pin 3)
I used a square wave generator on my oscilloscope to generate pulses (5v with a common ground)
Sorry about the ' in the file name
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Wed Oct 30, 2024 9:17 am
by ChrisT66
Thank you for your efforts to help! Great as always.
I'll test it with my signals, it'll take a while...
Re: How do I get out of the WaituntilHigh macro?
Posted: Wed Oct 30, 2024 7:20 pm
by mnfisher
I tested with a proper function generator (GW Instek) - so about as accurate as I can generate at home...
500us pulse gives 998
1ms ------------ 2010
2ms ------------- 3988
3ms ------------- 6003
4ms ------------- 8018
5ms ------------- 10093
I also tested 50us -> 96, 25us -> 46
So - it's not quite linear, but not far off. Count will of course depend on the clock speed (so in my case 16MHz). One issue is that very short pulses - all give a count of 1...
Martin
Re: How do I get out of the WaituntilHigh macro?
Posted: Thu Oct 31, 2024 8:30 am
by ChrisT66
Works very well. I have tested it with the 328P and 8MHz. A 1.5ms signal gives 1498, great.)
I will now try to read in all four signals one after the other.
Thanks again!