Bonjour,
Ci joint un code écrit en C à l'aide de MikroC qui permet de mesurer un lapse de temps entre 2 fronts montants.
Je sèche un peu sur ce code surtout iciPourquoi inverser les bits de tOld et pourquoi ajouter 1 à la fin ?// Calculate length of pulse from rising edge to rising edge
tword = ~tOld + tNew+1;
@+Code:/* * Project name: Capture Sample * Copyright: Nicholas Sirirak * Description: This is a simple capture a lenght of pulse. Capture from rising edge to rising edge and send to terimianl via RS232 * Test configuration: MCU: PIC16F877A Dev.Board: EasyPIC3 Oscillator: HS, 16.0000 MHz Ext. Modules: - SW: mikroC v8.1.0.0 * NOTES: */ unsigned tOld, tNew; char th,tl; char edge = 0; char capture = 0; unsigned tword; char txt[6]; void interrupt() { if(PIR1.CCP1IF){ if(!edge){ tOld = (256*CCPR1H)+CCPR1L; // keep starting value edge = 1; }else{ tNew = (256*CCPR1H)+CCPR1L; // keep ending value capture = 1; // complete capture, set a flag edge = 0; } PIR1.CCP1IF = 0; //clear CCP flag } } void main() { char i; TRISB = 0; // PORTB is output PORTB = 0; // Initialize PORTB TRISA = 0; TRISC = 0; CCP1CON = 0x05; // Capture mode every rising edge TRISC.F2 = 1; // RC2 must be input T1CON = 0x21; // timer1 ON, internal clock Fosc/4, prescaler 1:4 INTCON.GIE = 1; INTCON.PEIE =1; PIE1.CCP1IE = 1; // enable interrupt capture PIR1.CCP1IF = 0; //clear CCP flag uart1_init(9600); while(1){ if(capture){ PIE1.CCP1IE = 0; // disable interrupt while processing capture = 0; // clear flag // Calculate length of pulse from rising edge to rising edge tword = ~tOld + tNew+1; // tword contain length of pulse in micro second WordToStr(tword, txt); // convert to string and send back to terminal for(i=0; i<6; i++) uart1_write(txt[i]); delay_ms(1000); PIR1.CCP1IF = 0; //clear CCP flag PIE1.CCP1IE = 1; // enable interrupt } } }
-----