|
|
View previous topic :: View next topic |
Author |
Message |
mmc01
Joined: 16 Jun 2010 Posts: 31
|
How to read duty cycle with PIC ? |
Posted: Sun Jun 03, 2012 2:51 am |
|
|
I want to read duty cycle with PIC. I try to use frequency counter code to read duty cycle but I don't know how to use it. This is my code
Is this true or false to read and calculate duty cycle.
Code: | #include <16F648A.h>
#use delay(clock=4000000)
#fuses XT,PUT,BROWNOUT,MCLR,NOWDT,NOPROTECT,NOLVP
#define TxD PIN_B2
#define RxD PIN_B1
#use rs232(baud=9600, xmit=TxD,rcv=RxD)
float time1,time2;
BOOLEAN hook_cpp1, HookRise;
#int_ccp1
void capture_isr()
{
if(HookRise)
{
time1 = get_timer1();
HookRise = FALSE;
printf("X");
}
else
{
time2 = get_timer1();
HookRise = TRUE;
hook_cpp1 = FALSE; //done
printf("Y\r\n");
}
}
void InitialChip(void)
{
setup_comparator(NC_NC_NC_NC); //Input Digital
set_tris_a(0B11111110);
set_tris_b(0B11111111);
}
float T;
void main(void)
{
InitialChip();
printf("Frequency Counter\r\n");
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); // Setup timer 1
while(1)
{
HookRise=TRUE;
hook_cpp1=TRUE;
printf("Frequency Counter\r\n");
setup_ccp1(CCP_CAPTURE_RE);
enable_interrupts(INT_CCP1); // Enable interrupt CCP1
enable_interrupts(GLOBAL); // All interrupts ON
set_timer1(0);
while(hook_cpp1==TRUE);
setup_ccp1(CCP_OFF);
disable_interrupts (GLOBAL);
T = (time2-time1)*8*1000*0.000000001;; // Td=cycle*(4/fosc)*PR
printf("Time1 = %f\r\n",time1);
printf("Time2 = %f\r\n",time2);
printf("Time = %f\r\n",time2-time1);
float t_all;
t_all = time1/(time1+time2);
printf("t_all = %f\r\n",t_all);
//printf("Duty cycle = %f\r\n",time1/(time1+time2));
printf("Frequency:%f Hz %9.3e ms\r\n",1/T,T);
delay_ms(500);
}
} |
The results of this code is.
Quote: |
time1 = 73.00
time2 = 198.00
time2-time1 = 125.00
time1/(time1+time2) = 0.26
Frequency:1000.00 Hz 9.999E-04 ms
time1 = 78.00
time2 = 203.00
time2-time1 = 125.00
time1/(time1+time2) = 0.27
Frequency:1000.00 Hz 9.999E-04 ms |
How to read duty cycle? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Sun Jun 03, 2012 5:28 am |
|
|
hmm... you could just grab the example that CCS supplies in the examples folder.....
or...
search the 'code library' or this forum for 'measuring pulse width' for complete code |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun Jun 03, 2012 7:11 am |
|
|
And, as a comment, you have 'no hope', with the printf's there. Even if the UART buffer is empty, over 1mSec 'jammed' in the interrupt handler in the second case, waiting for the data to send. Invalidates everything.
As it stands though, the interrupt is always triggering on the rising edges, so all you are measuring is subsequent rising edge times, not the pulse width/duty cycle...
Best Wishes |
|
|
mmc01
Joined: 16 Jun 2010 Posts: 31
|
|
Posted: Fri Jun 08, 2012 1:11 am |
|
|
temtronic wrote: | hmm... you could just grab the example that CCS supplies in the examples folder.....
or...
search the 'code library' or this forum for 'measuring pulse width' for complete code |
Thanks... |
|
|
|
|
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
|