View previous topic :: View next topic |
Author |
Message |
Alexandr
Joined: 07 Mar 2014 Posts: 3 Location: Moscow, Russian Federation
|
18F46K22 number pulse generator |
Posted: Fri Mar 07, 2014 5:42 am |
|
|
Hi embedded systems experts! There is one problem: Need a code CCS to get the number of pulses. The required amount must be set programmatically, from 1 to 32767 pulses. Period and frequency should also be regulated. How do I start CCP1 module in this mode? Prompt please.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Mar 07, 2014 6:03 am |
|
|
You don't....
You would have to use _two_ CCP modules.
One programmed as a PWM (to generate the pulses), and the other as a counter. The first you program as a normal PWM, and the settings for this control the frequency and pulse width. The second one you set to interrupt at a count (for the number of pulses), and when it interrupts you stop the PWM. The PWM output from the first has to be connected externally to the timer input for the second.. |
|
|
Alexandr
Joined: 07 Mar 2014 Posts: 3 Location: Moscow, Russian Federation
|
|
Posted: Fri Mar 07, 2014 6:27 am |
|
|
Thank you Ttelmah! I guessed this. Need internal configuration module CCP1 & (synchronize)SPECIAL EVENT TRIGGER? Is that possible?
There is no free pins! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Mar 07, 2014 8:20 am |
|
|
No.
If you run as a CCP (counter), you can't also run as a PWM.
However you could do it another way, if you have a timer available:
Setup a PWM, for the pulse frequency/width needed.
Calculate how long this will take to generate the number of pulses required.
Program a timer, so it'll overflow at this time (just before actually).
Start the PWM
Start the timer
When the timer interrupts, stop the PWM.
Best Wishes |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Sat Mar 08, 2014 3:30 am |
|
|
You don't tell us the range of period and frequency you want.
One other possibility:-
1) Set up a PWM.
2) Use the PWM clock to generate an interrupt.
3) Simply count the interrupts.
Or, if frequency is too fast to allow time for interrupt on every pulse.
1) Set up a PWM.
2) Use the PWM clock to generate an interrupt after say 16 cycles.
3) Adjust pulse count by 16 on each interrupt.
4) Poll the PWM output pin for the last few pulses.
.........................
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Sat Mar 08, 2014 3:47 am |
|
|
Very much agreed.
Caveat (as with the timer approach, which is why I say 'just before actually', is that the PWM period updates on the next count. So you want to set the period to zero, a small fraction before you want it to stop.
Best Wishes |
|
|
Alexandr
Joined: 07 Mar 2014 Posts: 3 Location: Moscow, Russian Federation
|
|
Posted: Mon Mar 17, 2014 8:21 am |
|
|
Mike Walne wrote: | You don't tell us the range of period and frequency you want.
1) Set up a PWM.
2) Use the PWM clock to generate an interrupt after say 16 cycles.
3) Adjust pulse count by 16 on each interrupt.
4) Poll the PWM output pin for the last few pulses.
.........................
Mike |
Thank you all!
1. Free pin no have.
2. The output frequency from 10 Hz to 10 kHz.
3. Duty Cycle 10-90%.
Need to digest what was said. The last option I like more.
Repairs in the apartment, a little delayed topic! |
|
|
|