Below was compiled in MikroC for measuring pulse width using CCP1 capture. Using a 16F690 with a 20MHz crystal. Trying to capture alternating edges. For now, just trying to UART out the value. The code compiles ok but I'm not getting data output. Any help on this would be greatly appreciated. Thanks!
Code:
#include "built_in.h"
unsigned int T1; //1st capture edge (High)
unsigned int T2; //2nd capture edge (Low)
unsigned const numerator = 170454.55; //Distance = 3" .....(May chng if prescale chngs)
unsigned int MPH;
void hardwareInit(){ // Configure Hardware
TRISC.RC5 = 1; // CCP1 pin - RC5 set to input
ANSEL = 0; // Set pins to digital
ANSELH = 0; // ""
ADCON0 = 0; // Turn off comparitors
ADCON1 = 0; // ""
INTCON = 0; // Turn off interrupts..
T1CON.CCP1IE = 1; // ...Except for CCP1
T1CON = 0b00000011; // External clock, Turn on timer
CCP1CON = 0x05; // Capture rising edge (101)
}
void main(){
hardwareInit();
while(1){}
}
void writedata(){
UART1_Init(4800);
Delay_ms(100);
MPH = (numerator/(T2-T1)); //Calculation - based on 3" distance
UART1_Write_Text("Start"); //Just a test to see if there is output
UART1_Write(10); //From MikroC library
UART1_Write(13); // ""
while(1) { // Endless loop
if (UART1_Data_Ready()) { // If data is received,
MPH = UART1_Read(); // read the received data,
UART1_Write(MPH); // and send data via UART
}
}
}
void interrupt() {
if (CCP1CON.CCP1IF){ // Flag for 1st CCP interrupt
hi(T1) = CCPR1H; //Put T1 (hi) in register
lo(T1) = CCPR1L; //Put T1 (lo) in register
CCP1CON.CCP1IE = 0; // Clear bit per datasheet
PIR1.CCP1IF = 0; // Reset flag
CCP1CON.CCP1IE = 1; // Renable CCP interrupt
}
if (CCP1CON.CCP1IF){ // Flag for 2nd capture (low)
hi(T2) = CCPR1H; //Put T2 in register
lo(T2) = CCPR1L; // "
writedata();
hardwareInit();
}
}
Last edited by a moderator: