Capture mode in PIC16F8777a

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
HI,

I am working on capture mode for measuring pulse at pic input pin...
here is code pls have a look is that fine after that i will watch on lcd..
Rich (BB code):
#include<pic.h>
#define _XTAL_FREQ 20000000L
__CONFIG(0x3F18);
 unsigned char low,high;
void main (void){

TRISC2=1;
CCP1CON=0b00000101;//Capture mode, every rising edge
T1CON=0b00001001;//Internal clock,Enables Timer1
TMR1IF =0;
 TMR1H  = 0x00; 
 TMR1L = 0x00;
GIE =1;  // Interrupt Enable 
  PEIE =1;
CCP1IE =1;
while(1){}

}



static void interrupt isr(void) {
 
  if ( CCP1IF ) {
    low=CCPR1L;
high=CCPR1H;
    CCP1IF = 0;
  }

  // if timer interrupt: reset timer and toggle Port B.2
  if ( TMR1IF ) {
    
    TMR1H = 0x00;  TMR1L = 0x00;
    TMR1IF = 0;
  }
}
 

t06afre

Joined May 11, 2009
5,936
Blessed is the one who finds wisdom, and the one who gets understanding, for the gain from her is better than gain from silver and her profit better than gold. She is more precious than jewels, and nothing you desire can compare with her. Long life is in her right hand; in her left hand are riches and honor. Her ways are ways of pleasantness, and all her paths are peace. She is a tree of life to those who lay hold of her those who hold her fast are called blessed
 

THE_RB

Joined Feb 11, 2008
5,430
Here is a simple and reliable way;

Rich (BB code):
// capturing a timed pulse period using TMR1
main()
{
  // reset timer
  T1CON = 0;        // timer1 OFF
  TMR1H = 0;        
  TMR1L = 0;
  
  while(PORTB.F0 == 1) continue;     // wait for \ edge
  while(PORTB.F0 == 0) continue;     // wait for / edge  
  T1CON = 0b00001001;                // start timing
  
  while(PORTB.F0 == 1) continue;     // wait for \ edge
  while(PORTB.F0 == 0) continue;     // wait for / edge  
  T1CON = 0;                         // stop timing
  
  // the period from / to / is now in TMRH and TMR1L.
}
 
Top