View previous topic :: View next topic |
Author |
Message |
Ahmed Adel Hosni
Joined: 16 Aug 2010 Posts: 12
|
Measuring PWM signal width |
Posted: Wed Sep 26, 2012 2:37 pm |
|
|
First of all I have different problems writing a code to measure the width of a PWM signal so I will go step by step in each problem.
The code below has a problem that it will miss one pulse but lets leave that problem aside until we finish some problems I am facing now.
The problem I am facing now is that when I measure a 38KHz pulse i succeed in reading the measurement correctly (I guess) but when I change the pulse to more than 30 uc for example the program lags and sometimes it resets. I also toke care of adding the overflow value but it also don't work correctly.
Another small problem is that I guess I am facing a problem with the printf specifier so the number are sometimes printed wrong.
I hope that I am clear enough.
So, what am I missing to measure the pulse correctly ?
Thank you in advance
Code: |
#include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int y = 0;
int16 pulses[35] = {};
long overflowarray[35] = {};
int count = 0;
int16 overflow;
#INT_RTCC
void inttimer0()
{
overflow = overflow +1;
}
#INT_EXT
void int_ext_isr()
{
pulses[count] = get_timer0();
overflowarray[count] = overflow;
set_timer0(0);
count++;
overflow = 0;
}
void main(void)
{
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
setup_timer_0(RTCC_INTERNAL | RTCC_8_BIT | RTCC_DIV_4);
enable_interrupts(INT_RTCC);
while(1)
{
if(input(PIN_C4) == 1)
{
while(input(PIN_C4));
printf("%d:%3.2f|", y, 0.8*pulses[y]);
printf("%ld|", overflowarray[y]);
y = y+1;
}
}
}
| |
|
|
Ahmed Adel Hosni
Joined: 16 Aug 2010 Posts: 12
|
|
Posted: Wed Sep 26, 2012 2:44 pm |
|
|
I will illustrate the code
I am trying to just measure randomly 35 pulses for example a save them to an array pulses[35] which saves the number of counts by the timer, and overflowarray[35] carries the number of flows for the same pulse.
A button connected to C4 begins in printing each signal beginning by signal of index 0 and increments when i press the button every time.
As far as I understand, I am using a 20M crystal and timer0 with prescaler equal to 4 so each count will be equal to 1/(20/4) us * 4 = 0.8 per count and 0.8 * 256 = 204.8 uc to overflow. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 26, 2012 3:25 pm |
|
|
Quote: | when I measure a 38KHz pulse
|
Is this a TV remote control IR decoding project ?
The reason I ask is, many times people will come on the forum and ask
detailed questions about how to make some programming technique work.
Then many people on the forum get sucked into answering these questions
in a long thread.
But all the time, the actual project was about something that has already
been done, and the best way to do it is well known, and all the posts on
the thread are a big waste of time. If the person had simply said what
the project was about, they could be answered very quickly with the best
way to do it, and usually given sample working code.
So that's why I ask what your project is about. |
|
|
Ahmed Adel Hosni
Joined: 16 Aug 2010 Posts: 12
|
|
Posted: Wed Sep 26, 2012 5:31 pm |
|
|
Yes actually it is and I am sorry that I didn't mention that but I guess I wanted a step by step help not just to take a code and run it
But I understand that you prefer not waste others' time in something has already been done before. |
|
|
|