1
\$\begingroup\$

I would like to measure a pulse using the PIC 18F4550 in capture mode. This pulse is generated by the PIC microcontroller itself.

For this, I use a function which plays the role of the XOR logic gate (you find the function that I've used below), with RC0 and RC2 being the inputs and RC6 being the signal output. The pulse leaving RC6 enters CCP2 to be measured.

schematic

The problem I found is that the CCP2 cannot detect the pulse generated by the microcontroller. I don't know if there are any conditions to connect the pins of the microcontroller or something.

If anyone has an answer or a hint to fix this, I will be grateful and if you have any questions feel free to ask.

#include <stdio.h>
#include <stdlib.h>
#include "osc_config.h"
#include "LCD_8bit_file.h"
#include <string.h>

    unsigned long comtage,capt0,x;
    char pulse[20];
    char cosinus[20];
    float period,dephTempo,deph,phi;
    
void init (){
   
   IRCF0         =1;     /* set internal clock to 8MHz */
    IRCF1         =1;
    IRCF2         =1;  
    
    PIE2bits.CCP2IE=1;
    PIR2bits.CCP2IF=0;  
    CCPR2          =0;         /*CCPR1 is capture count Register which is cleared initially*/
     
    T3CONbits.RD16=1;  
    T3CKPS0=0;
    T3CKPS1=0;
    TMR3CS=0;
    
    TMR3IF=0; 
    
    T3CCP2=0; /*Timer3 is the capture clock source for CCP2*/
}

void xor()
{
    
   if (PORTCbits.RC0==PORTCbits.RC2)
          {
           PORTCbits.RC6=0;
           }
        else if (PORTCbits.RC0!=PORTCbits.RC2)
              {
               PORTCbits.RC6=1;
               }    
  
}
void main()
{
    
    TRISCbits.TRISC0=1;
    TRISCbits.TRISC2=1;
    TRISCbits.TRISC6=0;
    
    xor();
    LCD_Init();
     
      
    while(1)        
    {
    CCP2CON         = 0b00000101;
    PIR2bits.CCP2IF = 0;
    TMR3ON          = 0; 
    TMR3            = 0;

    while (!PIR2bits.CCP2IF);

    TMR3ON          = 1; 
    CCP2CON         = 0b00000100;
    PIR2bits.CCP2IF = 0;
    
    while (!PIR2bits.CCP2IF); 
    comtage = CCPR2; 
    
    dephTempo = (((float)comtage /30.518)/65536 );
    
    sprintf(pulse,"%.3f  ",dephTempo);
    LCD_String_xy(0,0,"the pulse width is : ");
    LCD_String_xy(2,9,pulse);
    } 
}
\$\endgroup\$
1
\$\begingroup\$

The xor() must be inside the infinite loop. In particular inside the while (!PIR2bits.CCP2IF) loops.
In all cases if you are not using interrupts it is useless to use the capture compare for an internal function.

\$\endgroup\$
2
  • \$\begingroup\$ thanks for your interaction, I just want to know more about interrupts, should I use it for xor function or use it to calculate pulse width? \$\endgroup\$ – mad na Apr 19 at 21:14
  • \$\begingroup\$ The interrupt of the capture compare avoids waiting in loops and gives you the possibility to add more functionalities in parallel. Concerning the xor() function, I think in this case using an external xor is an option. In your xor code you do not need the second if condition after else. \$\endgroup\$ – Paul Ghobril Apr 19 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.