|
|
View previous topic :: View next topic |
Author |
Message |
etalha
Joined: 10 May 2020 Posts: 2
|
16F1824 capture and pwm |
Posted: Sun May 10, 2020 9:30 pm |
|
|
Hello
I need help with my program related to capture. So basically I need to do a program on pic 16f1824 and it has to measure the frequency that is implemented to it and then it has to produce two pwm signals 90 degrees out of phase.
Can you check what is wrong with it ? What I have tried is I made a timer interrupt that is occurring every half second, then checks the cap and multiplies by two and finds the frequency of that signal.
Code: |
#include <capture_5.h>
int16 cap=0;
int16 freq=0;
#INT_TIMER1
void TIMER1_isr(void)
{
freq=cap*2;
}
#INT_CCP2
void CCP2_isr(void)
{
cap=cap+1;
}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //524 ms overflow
setup_timer_4(T4_DIV_BY_16,freq,1); //4.0 ms overflow, 4.0 ms interrupt
setup_timer_6(T6_DIV_BY_16,freq,1); //4.0 ms overflow, 4.0 ms interrupt
setup_ccp2(CCP_CAPTURE_RE);
setup_ccp3(CCP_PWM|CCP_TIMER4);
setup_ccp4(CCP_PWM|CCP_TIMER6);
set_pwm3_duty((int16)cap);
set_pwm4_duty((int16)cap);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP2);
enable_interrupts(GLOBAL);
while(TRUE)
{
//TODO: User Code
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon May 11, 2020 8:42 am |
|
|
You don't need to use the CCP if you are just counting pulses.
Multiple choices:
1) Use the CCP, and measure the waveform period. Frequency is then
1/period. Look at:
<http://www.ccsinfo.com/forum/viewtopic.php?t=58627>
2) Just use any of the edge interrupts to count.
3) Use a timer. Several of the timers support running from an external
clock. Feed your clock into this, then set timer to zero, wait for your time,
and the timer contains the count. No interrupt needed.
Then remember that the period fed to the PWM timer oscillator is 255
max. Your frequency could be vastly over this. Then remember that
the PR2 value sets the period, not the frequency. Currently you are
setting a period to a frequency....
Then to get 90degree phase shift, you are going to have to phase shift the
timers. Calculate how many counts == 90 degrees.
Start both timers.
Set first one to zero.
When it gets to the count for 90 degrees, set the second timer to zero.
Your PWM's are then 90 degree phase shifted. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|