CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Measuring PWM signal width

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ahmed Adel Hosni



Joined: 16 Aug 2010
Posts: 12

View user's profile Send private message

Measuring PWM signal width
PostPosted: Wed Sep 26, 2012 2:37 pm     Reply with quote

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 Smile

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;
      }      
   }   
}   
Rolling Eyes
Ahmed Adel Hosni



Joined: 16 Aug 2010
Posts: 12

View user's profile Send private message

PostPosted: Wed Sep 26, 2012 2:44 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 26, 2012 3:25 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Sep 26, 2012 5:31 pm     Reply with quote

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 Smile

But I understand that you prefer not waste others' time in something has already been done before.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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