capture the incoming signal - msp430

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.

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).

Find time duration between two pulse of same signal

i want to find time duration between two pulse of same signal. That's mean time duration between falling edge of first pulse and rising edge of second signal. Here, i'm using Arduino Uno board.
Here, Image show my two pulse of same signal. i want to find time t.
I want some logic or arduino code.
Check function pulseIn()
It will solve your problem.
Note: Just put LOW as parameter.

Arduino Chain counter

I'm planning to use the basic model of an arduino bike speedometer to count the amount of chain that I let out when I anchor my boat. However if I only have one sensor it would only work for the chain counting one way.
My theory is to have two sensors on the wheel so that if sensor A counts first then the chain is going out but if sensor B is counts first then the chain is coming in.
Only problem is I am much better at coding in python than I am in the arduino IDE. I would be super grateful if someone could give me an idea of how to do this?
From you application description, you really wan't to track the position of the wheel holding the chain, not the speed (or velocity). If you read how rotary encoders work, you can apply that technique to the wheel. Rotary encoders have two digital outputs which are out of phase by 90 degrees so you can not only detect motion, but direction.
See http://playground.arduino.cc/Main/RotaryEncoders for a detailed explanation
Would it not be more useful to count chain out and count back in?
Only 1 magnet on the chain gypsy that counts out your chain from the stowed position, picking up a 12v signal from the windlass direction solenoid lowering feed, stores this value at rest. When raising pick another 12v from the windlass solenoid raising feed to count the value back down to zero at the rest position?
This negates the need to determine direction by interpreting motor direction as count positive and count negative IO signals?

Find time difference between two pulses using PIC16F628

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.

Arduino encoder interrupts corrupting serial data

I have an Arduino Mega connected to a 6 axis robotic arm. All 6 interrupts are attached to encoders (one encoder pin on an interrupt, the other on a vanilla digital input). The interrupts are handled with this code:
void readEncoder1(){
//encoders is a 2d array, where the first d is the axis, and the two pin numbers
//first pin is on an interrupt (CHANGE), and second is a standard digital in
if (digitalRead(encoders[0][0]) == digitalRead(encoders[0][1])) {
positions[0]++;
} else {
positions[0]--;
}
if(servoEnable){
updatePositions(); //// compares positions[] to targets[] and adjusts motor speed accordingly
}
}
This is designed to keep the arm locked at a certain position- if the arduino detects that the position of the motor is off by a certain threshold, it updates the power going to the motor to keep the arm in position.
The problem is this, then -- if two or three (or more) axis are under load (requiring constant updating to stay in position) or they are moving, the Arduino will stop receiving intact commands on Serial input, several characters will be dropped. The interrupts are obviously running quite quickly, and for some reason this is causing commands to become corrupted. Is there any way around this? Architecturally, am I doing this right? My main instinct is to call updatePositions() in the main run loop at, say, 100 ms intervals, will this significantly reduce interrupt overhead? I guess what my question boils down to is how do I get reliable serial commands into the Arduino even if all 6 encoders are pulsing away?
Quadrature encoders were designed to be read by hardware counters. Pulse rates are generally high with the motor running at full speed. One megahertz is not unusual. The higher the number of pulses, the better the servo loop works and the more accurate you can position the motor.
Doing this is in software with a low-power cpu is, well, challenging. It will fall apart when the ISR takes longer than the interval between pulses. You'll lose pulses and thus position. Especially bad because there is no way you can detect this error condition. And that this loss happens when the robot is moving fast, the worst case condition to lose control.
You absolutely cannot afford to update the servo loop in the interrupt handler so get rid of that first. Keep the ISR to the bare minimum, only count the position and nothing else. The servo loop should be separate, driven by a timer interrupt or tick. You cannot properly control a robot with a 100 msec servo update unless it is big an sluggish, this needs to be a handful of milliseconds at most to get smooth acceleration and stable feedback.
There's a limited amount of wisdom in spending forty bucks to control thousands of dollars worth of robot hardware. Not being able to keep up in the servo loop is something you can detect, shut it down when the position error builds up too much. There's nothing you can do about losing pulses, that's a wreck. Get the hardware counters.
First rule of embedded systems:
Do as little as possible in interrupts.
In your case, just update the positions in the interrupt and run your position/speed control loop in the background or at a lower priority.
Aside: I assume you are aware that you are "losing" encoder pulses as you don't have an interrupt on one of the channels?
Also, interrupt-driven encoder-analysis is very noise-prone. If you get a noise pulse, you'll likely only see an interrupt for one of the edges as they'll be too close together to process both.
A more robust way is to use a state machine which watches all 4 transitions, but that requires either interrupts on both edges of both channels, or polling fast enough to not miss anything up the to rate you are expecting to see.

Resources