2021年3月21日星期日

pulse width using interruption flag

I would like to measure a pulse width using pic 18f4550 CCP in capture mode. The idea is: when the CCP model receives a rising edge, the timer1 starts, and after each interruption of the Timer 1, x which is a constant with an initial value of 0 increases by 1, when the CCP model receives a falling edge timer1 stops and the increase of x must also stop so that its value can be used in the calculation of the pulse. the problem i found is that the increasing of x doesn't stop and the code works like a counter and as i am a newbie in pic programming I couldn't find the error. if anyone can help i will be grateful. the code I am using is bellow. thanks!

#include <stdio.h>  #include <stdlib.h>    #include "osc_config.h"  #include "LCD_8bit_file.h"  #include <string.h>    int x=0;    void main()  {      unsigned long comtage;      unsigned long DEPHASAGE[20];      float Deph_tempo;            TRISCbits.TRISC2=1;            IRCF0=1;           IRCF1=1;      IRCF2=1;              LCD_Init();            LCD_String_xy(0,0,"pulse");          PIE1bits.CCP1IE=1;      PIR1bits.CCP1IF=0;             CCP1CON=0b00000101;       CCPR1=0;                    T1CONbits.RD16=0;      T1CKPS0=0;      T1CKPS1=0;      TMR1CS=0;            while(1)              {              CCP1CON         = 0b00000101;          PIR1bits.CCP1IF = 0;          TMR1ON          = 0;           TMR1            = 0;            while (!PIR1bits.CCP1IF)              ;            TMR1ON          = 1;           CCP1CON         = 0b00000100;          PIR1bits.CCP1IF = 0;                if (TMR1IF==1)          {              x++;              TMR1IF=0;          }                while (!PIR1bits.CCP1IF)              ;             comtage = x;                 Deph_tempo = ((float)comtage /7843.13 );           sprintf(DEPHASAGE,"%.5f  ",Deph_tempo);             LCD_String_xy(2,0,DEPHASAGE);      }      x = 0;  }                                  ``  
https://stackoverflow.com/questions/66694817/pulse-width-using-interruption-flag March 19, 2021 at 12:09AM

没有评论:

发表评论