Find time difference between two pulses using PIC16F628 - microcontroller

I want to find time difference between two pulses using PIC16F628.
I am using a 4MHz external oscillator, MikroC compiler.
As a simple example let's assume there is a push button. When we press it, it sends a high signal to a pin. We press this button twice with some delay in between, I want to find the time difference between these two button presses.
Thank you.

As mentioned in the comments, the simplest way to do this is to use a timer/counter combo. I found this quick tutorial on how to do this specifically for PIC: http://www.mikroe.com/chapters/view/17/chapter-4-examples/#c4v5.
Have a look at 4.5 and 4.6, they give you exactly the information you'll need to get the count of timer intervals between pulses. The basic technique is to start a timer, associate an interrupt handler (Read: function) with the timer, and then increment a counter everytime the interrupt handler is called. Next time you see the pulse, read what the counter value is.
After that, all you need to know is the timebase you've set the counter to (which will be some integer subdivision of your oscillator rate, and is selectable in code usually) and you can convert # of timer intervals to time in seconds/millis/nanos.

Related

Arduino Lightbulb Control

I am completely new to arduinos, and I am trying to run a lightbulb-style apparatus off of an arduino uno, and I need it to only operate on a certain time interval that I can adjust according to the situation. I believe that there is an internal time function in the arduino that counts in milliseconds. All i need it to do is turn on after a set amount of time, and then turn off after about a minute. How would I go about setting up the code for this?
You can use the millis() function to get the current arduino operational time in milliseconds. Then compare it in the next loop(). You probably don't care what the real time is, just the relative time since the last time you checked or did something. You can create a variable to store the last event time and compare the current time to that.
Be aware that millis() may be quite large if your program runs for a long time so you should use an unsigned long type, otherwise the value may roll over the top bit and become interpreted as a negative number (this is a common problem).

Can I delay/bundle reactions to QPlainTextEditor.textChanged events?

I have a small IDE for a modeling language I wrote, implemented in PyQt/PySide, and am trying to implement a code navigator that let's you jump to different sections in the file being edited.
The current implementation is: (1) connect to QPlainTextEditor.textChanged, (2) any time a change is made, (sloppily) parse the file and update the navigator pane
It seems to work OK, but I'm worried this could cause major performance issues for large files on slower systems, in particular if more stuff is connected to textChanged in the future.
My question: Has anybody here implemented a delayed reaction to events, so that multiple events (i.e. keystrokes) within a short period only trigger a single update (say once per second)? And is there a proper QT way of doing this?
Thanks,
Michael
You can try using timers if you want some "delay".
There would be 2 ways to use them (with different results).
One is only parse after no input has been done for a certain amount of time
NOTE: I only know C++ Qt but I assume the same things are valid for pyqt so this is kind of "pseudocode" I hope you get the concept though.
QTimer timer; //somewhere
timer.setSingleShot(true); //only fire once
connect(timer,QTimer::timeout(),OnTimerDone(...);
OnTextChanged(...)
{
timer.start(500); //wait 500ms
}
OnTimerDone(...)
{
DoStuff(...);
}
This will restart the timer every input, so when you call that and the timer is not done the timeout signal is not emitted. When no input is done for an amount of time the timer timeouts and you parse the file.
The second option would be to have a periodic timer running (singleShot(false)).
Just start the timer for like each second. and timeout will be called once a second. You can combine that with a variable which you set to true when the input changes and to false when the file is parsed. So you avoid parsing when nothing has changed.
In C++Qt you won't have to worry about multi-threading because the slot gets called in the GUI thread. I assume it is the same for python but you should probably check this.

Display time with push button interrupt Arduino

I need to write a code that contains an interval loop but contains a push button interrupt that displays RTC values when activated. I have found a way to individually do each task i.e change pushbutton state, loop in intervals, display RTC value but I cannot seem to combine them and create a working. If someone can provide links or an explanation on how to accomplish this I would be so grateful.
If you do not use delay(interval); in your main loop, you can run as many tasks in parallel as you want. Understand the BlinkWithoutDelay sample, and try to extend it to two leds blinking independently. Or read a button while blinking.
And push button and interrupt does not go together well, BTW.
You even might add a small delay(2); to slow down polling the button pin.
This is usually fine for the other parallel running tasks and implements a very simple debounce mechanism.

Need an Arduino Sketch that uses digital write for a certian number of seconds

I need a simple way to run a program using digital write for a certain number of seconds.
I am driving two DC Motors. I already have my setup complete, and have driven the motors using pause() and digitalWrite(). I will be making time measurements in milliseconds
Adjustable runtime, and would preferable have non-blocking code.
You could use a timer-driven interrupt triggering code execution which will handle the output (decrementing required time value and eventually switching off the output) or use threads.
I would suggest using threads.
Your requirement is similar to a "blinking diodes" case I described in a different thread
If you replace defines setting time intervals and use variables instead you could use this code to drive outputs or simplify the whole code by using only one thread working the same way aforementioned timer interrupt would work.
If you would like to try timer interrupt-driven approach this post gives a good overview and examples (but you have to change OCR1A to about 16 to get overflow each 1ms) .

capture the incoming signal

i'm using msp430f2013 micro controller in my project.. in that i need to calculate the incoming train of pulse signal frequency.... i don't know how to do it.... can anyone help me in this.. example code is more usefull to me.... advance thanks for
You need to read the manual for the micro-controller, then work out how to set up a timer which can measure the interval between two pulse edges (e.g. from one leading edge to the next). The frequency, f, will be the reciprocal of this time interval, t, i.e.
f = 1 / t
There are various ways to do this, perhaps the simplest to understand is to setup a timer as a simple counter. Poll the input pin, when it changes state save the count on the timer, when it changes state again save the count on the timer, subtract one time from the other and that is how many clock ticks of some frequency X ticks per second. your difference is y ticks per input pulse. y / x the ticks cancel out and you get seconds per pulse. If you are measuring a full period rising edge to rising edge or falling edge to falling edge then it is the same solution the difference is which timer samples to subtract (last rising edge and current rising edge for example).
Some microcontrollers have the ability to interrupt when there is a state change on the input pin (or at least the same edge, rising or falling), and you may prefer to use that method to sample the timer, subtract and get ticks per period, etc to get cycles per second (frequency).
Using a timer can be tricky, I always start by using the timer to blink an led, first once per second to get in the ball park, then once every 5 or 10 or 30 seconds, and compare that to a second hand on a watch or some other reference to verify that you are accurate and not a dozen percent off this way or that. That establishes understanding of the timer and its divisor, from there you can start to work on using it to measure the input. to make sure I have the gpio programmed right (the led exercise covers some of that already) I sample the input pin and change the led state with the input pin state and can often then look at the led to see blinks or a dull glow to see that I am able to sample the gpio pin. then put it all together and sample the timer when the input changes state, first polling then if need be interrupts or other.

Resources