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

How to calculate pulse width when using external interrupt

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



Joined: 05 Apr 2023
Posts: 3

View user's profile Send private message

How to calculate pulse width when using external interrupt
PostPosted: Wed Apr 05, 2023 9:31 am     Reply with quote

I try to calculate frequency and pulse width of square waveform. I can find frequency but don't know how to do when calculate pulse width. Here is my code:
Code:

#include <TH71.h>
#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D2
#define LCD_RW_PIN PIN_D1
#define LCD_DATA4 PIN_D6
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D4
#define LCD_DATA7 PIN_D3
#include <lcd.c>

int32 quang863_pulse = 0;
int32 quang863_f = 0;
int32 quang863_time = 0;

#INT_EXT
void  EXT_isr(void)
{
   quang863_pulse++;
}

#INT_TIMER1
void  TIMER1_isr(void)
{
   quang863_time ++;
   if (quang863_time >=100)
   {
      quang863_f =quang863_pulse;
      quang863_time =0;
      quang863_pulse=0;
   }
   set_timer1(15536);
}

void main()
{
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);      //13.1 ms overflow
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   set_timer1(15536);
   set_tris_b(0xff);
   set_tris_d(0x00);

   lcd_init();
   delay_ms(100);

   while(TRUE)
   {
      lcd_gotoxy(1,1);
      printf(lcd_putc,"Tan so: %lu      ", quang863_f );
      lcd_gotoxy(1,2);
      delay_ms(100);
   }
}

Thanks for your help!
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Wed Apr 05, 2023 9:45 am     Reply with quote

You can't like that.
The interrupt will only occur on one edge. So frequency, not pulse width,
You would have to use the interrupt on change rather than INT_EXT.
This would then allow the pin to measure width as well.
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 05, 2023 12:04 pm     Reply with quote

a lot of options
IF you tell us the PIC you're using....
... and what frequencies you need...
Quang



Joined: 05 Apr 2023
Posts: 3

View user's profile Send private message

PostPosted: Wed Apr 05, 2023 10:59 pm     Reply with quote

temtronic wrote:
a lot of options
IF you tell us the PIC you're using....
... and what frequencies you need...

i use pic16f887 and pulse generator is connected to RB0 and range of freq is 0-200hz
Quang



Joined: 05 Apr 2023
Posts: 3

View user's profile Send private message

PostPosted: Wed Apr 05, 2023 11:00 pm     Reply with quote

Ttelmah wrote:
You can't like that.
The interrupt will only occur on one edge. So frequency, not pulse width,
You would have to use the interrupt on change rather than INT_EXT.
This would then allow the pin to measure width as well.

Thank you, so i need to use INT_RB and timer for measure time, don't i ?
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Apr 06, 2023 7:00 pm     Reply with quote

use the 'search' feature of this site....
one possible helpful 'hit..
https://www.ccsinfo.com/forum/viewtopic.php?t=54909

another..
https://ccsinfo.com/forum/viewtopic.php?p=185542

also google 'measuring pulse width using ccp'
PrinceNai



Joined: 31 Oct 2016
Posts: 478
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Apr 07, 2023 5:49 am     Reply with quote

You say it is a square wave. I understand that as half on, half off. It makes life easier. One CCP is enough to do what you want. It gives you the count of timer ticks between two identical transitions. You haven't said what clock speed you are using, but it is easy to configure a CCP timer for a 1us resolution with standard clock frequencies (CCS wizard does that for you, just play around with the dividers, everything is written). From that you get a duration of a signal PERIOD, period being the time between two raising or two falling edges. And that time is directly in us. From there on you only need a couple of equations to express the period in any format you want, seconds, miliseconds and to calculate frequency which is 1/period in seconds. Mind also that all writings either to a display or over serial take time, so it might be a good idea to decide how often you want to update the info and to disable interrupts while you are doing that.
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