PIC16F946 code to measure time between two edges

Status
Not open for further replies.
I am nor familiar with the architecture of your specific controller, but generally you could do it like this
Prescale your timer 0 to a desired value
Setup interrupt on RB0 to positive edge
In interrupt routine do this
read timer 0 into a variable, say "t"
clear timer 0
now compute this
Code:
    char t = get_timer0();
    float time = ((t * [your prescaler]) / (crystal_value/4); //this time is in seconds
 

I am now trying with CCP,how to save old time and subtract with new value.
If possible can you give me code to configure CCP and to measure time between two edges...8-O

is below code is correct????
int CCP1
void isr(void)
{
value = value-CCPR1; //CCPR1 holds the new time
}
 

Please refer to "PIC Microcontrollers and Embedded Systems Using Assembly and C" by Muhammed Ali Mazidi. It has an example of that.

http://www.microdigitaled.com/PIC/Code/PIC_codes.htm

file 15-3C


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
CCP1CON=0x05;   
    T3CON=0x0;          
    T1CON=0x0;      
    TRISB=0;            
    TRISD=0;            
    TRISCbits.TRISC2=1;     
    CCPR1L=0;           
    CCPR1H=0;           
  while(1)
    {
    TMR1H=0;            
    TMR1L=0;
    PIR1bits.CCP1IF=0;  
    while(PIR1bits.CCP1IF==0);  
    T1CONbits.TMR1ON=1; 
    PIR1bits.CCP1IF=0;  
    while(PIR1bits.CCP1IF==0);  
    T1CONbits.TMR1ON=0; 
    PORTB=CCPR1L;
    PORTD=CCPR1H;       
    }

 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…