New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add micros command #570
Comments
The To add a microsecond counter would require using up a precious hardware timer. I didn't want to include such a feature by default, because it takes away the use of a timer for other things. But you can implement such a microsecond counter yourself:
Get the current micros using:
Set it with:
Technical notes: we use timer 2 because it's a 32-bit timer and so takes longer to wrap around. The prescaler is 83 which means the timer counts at a frequency 168MHz/2/(83+1)=1MHz. 168MHz is the CPU frequency. The period is large so that the timer counts up to a large number before wrapping back to 0. It will take about 17 minutes before it wraps around to 0. |
Of course, you can change the prescaler to whatever you want to make a counter that counts faster or slower. |
Thanks for your answer. It's work but it's not precise. I suppose it's because of the time needed by the command to execute ? I have got as answer: Thanks, |
Can you provide more details about the sensor that you're connecting? When you start getting into timings this precise you may need to consider using C or figuring out if there is a way to configure one of the timers to capture your timing information for you. |
Yes for sure. In fact I was trying as a first project to convert the dht22 (humidity sensor http://playground.arduino.cc/Main/DHTLib) library to python. Original c++: micropython: Thanks, |
This is a very nice example of something that we should be able to get working in micropython without resorting to any special hardware features. Yes, the timers can most likey be used to capture a time period, but it would be great to have a way of doing this in pure Python. There are many solutions. First, let's try to write the code more efficiently:
Give that a try. If it is still inaccurate, then we can try direct memory access for the pin and timer values. Or we could write this function in assembler. Or we could use an SPI bus (or UART maybe) to sample the pin and then count the number of samples that were high. There is also ADC.read_timed which may be useful here. Lots of solutions, which I can elaborate on if need be. BTW, the case above where pyb.delay(1) gives 1300us is not so surprising: pyb.delay only has an accuracy of +/- 1 ms, so a 1ms delay will be between 1 and 2ms. |
You can try also the above but using the native emitter (the code will run about 2x as fast, so should be more accurate):
This function will return the number of microseconds that the pin was high. |
Oh yeah, and you might find it is more robust if you disable interrupts while timing the pin:
|
Would it also help if you used interrupts (assuming you haven't disabled them of course!) to detect the rising/falling pin, rather than using functions and while loops? Or is the code here simple enough that using interrupts wouldn't give any benefit? |
Probably easier to use simple polling. Interrupts would be possible, but would probably have more overhead than the polling example above. Still, might be nice to also write an interrupt version, and check the difference with some tests. |
Thanks for all these information. It's work better but I'm still missing some data in the begining of transmission so I will look at it to improve. I suppose I have to make a factory reset. I will follow the nice tuto ! |
Flashing with a new DFU image does not reset the filesystem. Filesystem reset is needed for that. Interesting that disable_irq crashes the board... I'll look into it. |
disable_irq and enable_irq are working for me. Try this:
That should give a fairly accurate measurement of the pin being held high. Note that it first waits while the pin is low, until it goes high, then starts timing. If you run this at the REPL, eg with |
Ok, was original question re: micros() answered? Can this be closed now and further discussion perhaps taken to forum? |
Yes , thanks for the support I'm still working on it. The original question has been answered. I will post on the forum my result. |
Hi,
Is it possible to add pyb.micros() to show the µs ellapsed since the board was last reset ?
There is pyb.millis() but when you work with signal of a few µs it doesn't help.
As example : http://arduino.cc/en/reference/micros
Thanks,
The text was updated successfully, but these errors were encountered: