View previous topic :: View next topic |
Author |
Message |
iso9001
Joined: 02 Dec 2003 Posts: 262
|
Changing number of pulses |
Posted: Thu Sep 09, 2004 4:37 pm |
|
|
Last edited by iso9001 on Sun Sep 19, 2004 8:37 pm; edited 1 time in total |
|
|
Haplo
Joined: 06 Sep 2003 Posts: 659 Location: Sydney, Australia
|
|
Posted: Thu Sep 09, 2004 5:38 pm |
|
|
Since what you are measuring is a mechanical device, I assume your input frequency will not change suddenly, but gradually.
This is one way of doing it: Use one of the capture modules to calculate in the frequency of the pulses. Looking at the signal in four half-periods (by toggling the capture settings between rising edge and falling edge) should be enough. Once you know the input frequency, you can use the other timer module to output the desired pulses. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Sep 09, 2004 7:22 pm |
|
|
1. Measure the incoming time with a CCP
2. Multiply the time by your factor
3. Output the new pulse using another CCP
Keep measuring the input and updating the output pulse as needed. The pulse duration is probably not critical just the frequency so you could just measure the time between each edge (rising or falling). |
|
|
iso9001
Joined: 02 Dec 2003 Posts: 262
|
|
Posted: Fri Sep 10, 2004 1:15 am |
|
|
Last edited by iso9001 on Sun Sep 19, 2004 8:36 pm; edited 1 time in total |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Fri Sep 10, 2004 5:52 am |
|
|
How long does the pulse duration need to be?
Is the signal normally high and pulled low or normally low and pulsed high? |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Fri Sep 10, 2004 6:05 am |
|
|
iso9001,
I have not got a clear idea of what you are trying to do. Let me describe what I have read then give you a suggestion.
You have a system where you have a controller turning a system on. Then the controller has 10ms to check and make sure motor turned on. The motor orignally outputed frequency of ~1.3Hz. Now you have changed the system such that you have a 2.22Hz output and you need to fool the controller to think all things or correct. Or you need a 2.22Hz to 1.3Hz converter with less than 10ms delay.
First count cycles between pulses. That is set up a free running counter and use this to count the period of the signal. To do this send the input into a external interupt pin. Then you can take the period of the counter and divide by your amount.
Now set up a second timer (or use first) to count this amount and output your pulse.
Since your pulse rate and output rate is slow, 300Hz, this system will work well and you can set your accuracy by the speed of the timers.
Does this make sense?
Trampas |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Sep 10, 2004 7:08 am |
|
|
How about something simillar to a "numerically controlled oscillator" where each time you get an input you add a value to a register. The MSB of the register is your output. If it is a 8 bit register and the value is 0x8F you divide exactly by 2. If the value is a little less than 0x8F you roll over less frequently and you divide by a little more than 2. If the value is greater than 0x8F you divide by less. Overhead is just an 8 bit add and test the MSB. It takes a little math to find the value but it sounds like that will rarely change and might be stored in a look-up table. Obviously using a 16 or 32 bit register gives you greater resolution with a little more overhead. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Guest
|
|
Posted: Sat Sep 11, 2004 1:21 pm |
|
|
Signal is 5V, Pulled high. As the gear goes around it APPEARS the sensor floats the signal as opposed to grounding it however I dont think this makes any difference.
The signal coming in at 8000 pulses per X distance is going to be a slowest of 2.2 pulses per second. So that means the longest pulse I need to make is going to be 1 pulse per second.
This is where I am stuck since the slowest PWM I can make with timer_2 is somthing like 61Hz.
So I'm a little stuck on the code to make output pulses, it should be pretty easy but timers confuse me.
You lost me on "numerically controlled oscillator" so I'm looking that up right now.
I think the real problem is that I have so keep these two systems as close as possible to each other time wise. The original system is going to be reading both of them and then comparing the difference over some time
I think the problem is that I dont have much time to sample, so at a slow speed I may only get 4 pulses in over my sample period... so the faster I go the cleaner the accuracy but when I'm not moving very fast...
I'm going to look over my numbers some more cause I just dont see how I'l be able to keep close to the first signal with the accuracy I need. There is a chance the system wont care untill a higher number of pulses per ms... That would improve my timing some...
I can write the code for the input coning but when it comes to the output I just cant see the code for it, any ideas |
|
|
Trampas
Joined: 04 Sep 2004 Posts: 89 Location: NC
|
|
Posted: Sun Sep 12, 2004 5:55 pm |
|
|
Do not use the PWM. Implement the system in software...
That is setup 16 bit timerx as to count Tcy (instruction cycle time). Then have the timer interruput increment a 16 bit variable. Thus now you have a 32bit timer running a few MHz.
Now if you take the input signal and run it through a external interrupt pin with posistive edge detection. Each time the interrupt is triggered store the 32 bit timer value. Now take the last timer value and the current timer value and subtract. Now you have the time between pulses. Now take this time and divide by you scale factor. This is you new timer...
Now set up a second timer with half of this new value. Now each time the timer interrupt is triggered, toggle your output pin.
OK now in your main line code, keep track check the first timer and the last time the external interrupt was called. If the time is greater than a preset limit, turn output off. In your external interrupt turn output back on.
This has only a few uS of latency, at least well under you 10ms. Also the slower the signal the more accurate. I have my main timer running at 10Mhz for a project which allows me to count up to 20Khz using the external interrupt, with accuracy of 0.1%.
Trampas |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
Trivial NCO application |
Posted: Mon Sep 13, 2004 7:33 am |
|
|
This would be a really trivial application of a numerically controlled oscillator.
If you wanted to divide by four using an 8 bit register (phase accumulator in NCO terminology) you detect the rising edge of the input and add 64 (the phase increment) to the register. Every four inputs the register will overflow. The MSB of the register is your output. You are dividing by 256/64 = 4. The initial value of the register determines phase of the output but is irrelevant for this application.
In your case you want to divide by 8000/4200 = 1.9048 so your phase increment is 256/1.9048 = 134.4 or 134 for an 8 bit register, 34406 if you use a 16 bit register for more accuracy. You will get less input to output lag if you use both input edges and half the phase increment. So every time you get an input edge you add 134/2 = 67 to the phase accumulator register and copy the register MSB to the output. All it requires is one addition and moving a bit. No subtractions or divisions at run time. If the "4200" number varies you will need to find a new phase increment but that could be from a look-up table.
In the classic NCO operation you add the phase increment based on a constant clock. You can frequency modulate the signal by varying the phase increment, and phase modulate the signal by varying the phase accumulator. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Sep 14, 2004 4:46 am |
|
|
Quote: | The signal coming in at 8000 pulses per X distance |
Maybe you should explain a little better what you are trying to do.
At first this sounded like a frequency convertor but now it seems as though you just need to "miss" pulses. This must be some positioning device. How important is the positioning? Is there a way to resync in case the error grows too large? |
|
|
|