View previous topic :: View next topic |
Author |
Message |
Quang
Joined: 05 Apr 2023 Posts: 3
|
How to calculate pulse width when using external interrupt |
Posted: Wed Apr 05, 2023 9:31 am |
|
|
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
|
|
Posted: Wed Apr 05, 2023 9:45 am |
|
|
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
|
|
Posted: Wed Apr 05, 2023 12:04 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2023 10:59 pm |
|
|
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
|
|
Posted: Wed Apr 05, 2023 11:00 pm |
|
|
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
|
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Fri Apr 07, 2023 5:49 am |
|
|
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. |
|
|
|