Estoy desarrollando un buscador de rango ultrasónico. Estoy usando PIC16f877a y el compilador MikroC. Estoy usando el pin CCP1 en modo PWM para generar un pulso ultrasónico. Este pulso se reflejará desde un obstáculo y se recibirá en el pin CCP2. Por lo tanto, estoy usando CCP2 en modo de captura. El problema es TMR1H y los valores de TMR1L permanecen en cero en todo momento. es decir, mi temporizador no se incrementa y no entiendo por qué es eso. También estoy confundido sobre el temporizador y los registros. Por lo que yo sé, TMR1 se va a utilizar. Aquí está el código:
unsigned int startTimerValueH; //Global variable to store values of timer for calculation
unsigned int startTimerValueL;
unsigned int endTimerValueH;
unsigned int endTimerValueL;
unsigned int time_interval;
unsigned int display_value;
unsigned int distance;
unsigned int arr[4];
void Gen_UltraSound ()
{
TRISB = 0x00;
PORTB = 0;
CMCON = 0x07;
ADCON1 = 0x06;
PWM1_init(40000); //initialize PWM module
PWM1_set_duty(128); //set the duty cycle to fifty percent
T1CON.TMR1CS = 1; //external clock is used as clock source for timer1
T1CON.T1SYNC = 0; //synchronizes the timer1 with external clock
T1CON.T1CKPS1 = 0;
T1CON.T1CKPS0 = 0;
PIR1.TMR1IF = 0; //disables overflow
T1CON.T1OSCEN = 0; //disables oscillator
T1CON.TMR1ON = 1; //turns on the timer1
startTimerValueH = TMR1H; //stores the initial value of the timer as a binary number
startTimerValueL = TMR1L;
PWM1_start(); //start generating the signal
delay_ms(20); //delay during which the pulse is sent out to the speaker
PWM1_stop(); //stops the signal generator
}
void displayDistance()
{
TRISD=0x00;
TRISB=0x00;
PORTB=0x00;
PORTD = 0x01;
}
void distanceMeasurer ()
{
unsigned int remainder= 0;
unsigned int quotient=0;
unsigned int divisor=100;
int i;
distance = time_interval * 340; //measures distance from the obstacle
distance = distance / 2000;
remainder = distance;
for (i=0; i<3; ++i)
{
quotient = remainder/divisor;
remainder = remainder%divisor;
arr[i] = quotient;
divisor = divisor/10;
}
displayDistance(); //after measuring distance, display it
}
void main() {
INTCON = 0b11000000; //enables global interrrupts
PIE2.CCP2IE = 1; //enables peripheral interrupts so capture interrupt can occur
CCP2CON.CCP2M3 = 0; // sets CCP2 module to capture mode
CCP2CON.CCP2M2 = 1;
CCP2CON.CCP2M1 = 0;
CCP2CON.CCP2M0 = 1;
Gen_UltraSound(); //Ultra sound generator through PWM module
PIR1.CCP2IF = 1 ; //for debugging purposes only
}
interrupt()
{
TRISD = 0x00;
if (PIR1.CCP2IF == 1)
{
PIE2.CCP2IE = 0; //disables undesirable peripheral interrupts
PIR1.CCP2IF = 0; //disables capture interrupts
endTimerValueH = TMR1H; //get timer value when echo received, in binary
endTimerValueL = TMR1L;
time_interval = endTimerValueL - startTimerValueL; //time interval between transmission and reciept, in binary
distanceMeasurer(); //measures the distance only when echo received
}
}
EDITAR: He usado un reloj externo de 20MHz de cristal. Deseo sincronizar TMR1 con eso. El cristal está en los pines OSC1 y OSC2