/* * Uses routines from delay.c * Potentiometer Readout RA0 PIN 2 * * Frequency in RC2 PIN 13 * */ #include #include //#include "injectie.h" #include "delay.h" #include "adc.h" #include "sci.h" /*************************************** VARIABLES ***************************************/ volatile bit first; volatile bit ready; volatile bit timeout; volatile unsigned char time_ctr; volatile unsigned int pulse_ctr; volatile long int stamp; volatile long int period; long int pulse_copy; unsigned char freq[6]; volatile bit busy; unsigned int copy; unsigned long int stamp_copy; int value; /* Sample code to set up the A2D module */ void init_a2d(void){ ADCON0=0; // select Fosc/2 ADCON1=0x80; // select left justify result. A/D port configuration 0 ADON=1; // turn on the A2D conversion module } /* Return an 10 bit result */ long int read_a2d(unsigned char channel){ long int value_ADRESH; channel&=0x07; // truncate channel to 3 bits ADCON0&=0xC5; // clear current channel select ADCON0|=(channel<<3); // apply the new channel select ADGO=1; // initiate conversion on the selected channel while(ADGO)continue; value_ADRESH=((ADRESH<<8)+(ADRESL)); return(value_ADRESH); // return 10 Bits of the result } int calculate_thousands(int value) { int thousands = 0 ; /************************************************************* /* Calculate tens *************************************************************/ do { if (value > 1000) { value = value - 1000; thousands = thousands+ 1; } else { } }while(value > 1000); return thousands; } int calculate_hundreds(int value) { int hundreds = 0 ; /************************************************************* /* Calculate tens *************************************************************/ do { if (value > 100) { value = value - 100; hundreds = hundreds + 1; } else { } }while(value > 100); return hundreds; } int calculate_tens(int value) { int tens = 0; /************************************************************* /* Calculate tens *************************************************************/ do { if (value > 10) { value = value - 10; tens = tens + 1; } else { } }while(value > 10); return tens; } int calculate_ones(int value) { int ones = 0; /************************************************************* /* Calculate ones from temp_c *************************************************************/ do { if (value > 1) { value = value - 1; ones = ones + 1; } else { } }while(value > 1); return ones; } /*************************************** INTERRUPT ***************************************/ void interrupt ISR(void) { if(CCP1IF)// check for rinsing edge on input pin { CCP1IF = 0;// clear flag if(!busy)// if we are not treating some other things you can proceed { if(first)// is this the first capture ?? { TMR1ON = 0;// then we reset the timers and counters TMR1H = 0; TMR1L =21;// place an offset in timer 0 from the time the interrupt has been called TMR1ON = 1; first = 0; stamp = 0xFFFF;// offset for timing purposes timeout = 0; time_ctr = 0; pulse_ctr = 1; } else if(timeout)// if timeout occured then we have enough samples {// for a good precision copy = (CCPR1H << 8) | CCPR1L; pulse_copy = pulse_ctr; ready = 1; first = 1; stamp_copy = stamp; } else pulse_ctr++;// continue to count the input pulses } } else if(TMR1IF) { TMR1IF = 0; if(time_ctr < 76)// the constant number is the sampling time 76 => +-500ms sampling { time_ctr++; stamp += 0xFFFF; } else timeout = 1; } } /*************************************** INIT REGISTER ***************************************/ void init_reg(void) { OPTION = 0x40; PEIE = 1; CCP1IF = 0; CCP1IE = 1; TMR1IF = 0; TMR1IE = 1; CCP1CON = 0x05; T1CON = 0x01; } main() { unsigned int value_high, value_low, value, last; unsigned char byte, i; long int x; init_reg(); sci_Init(57600, SCI_EIGHT); PIR1 = 0; /* clear any pending interrupts */ PEIE = 1; /* enable perhipheral interrupts */ GIE = 1; /* global interrupts enabled */ busy = 0; init_a2d(); // initialise the A2D module TRISA = 0xF3; // control lines are output TRISB = 0x01; // Bit 0 input rest output Port B TRISC = 0x00; // Make all Port C data lines output PORTA = 0; PORTB = 0; PORTC = 0; for(;;) { x=read_a2d(0); // sample the analog value on RA0 // Bereken en schrijf de duizendtallen value = calculate_thousands(x); sci_PutByte((value+0x30)); x = x - (1000 * value); // Bereken en schrijf de honderdtallen value = calculate_hundreds(x); sci_PutByte((value+0x30)); x = x - (100 * value); // Bereken en schrijf de tientallen value = calculate_tens(x); sci_PutByte((value+0x30)); x = x - (10 * value); // Bereken en schrijf de eenheden value = calculate_ones(x); sci_PutByte((value+0x30)); x = x - (1 * value); sci_PutByte(0x09); if(ready) { ready = 0; busy = 1; stamp += copy; period = stamp / pulse_copy; period = 4000000 / period; x=(pulse_copy*33); // Bereken en schrijf de duizendtallen value = calculate_thousands(x); sci_PutByte((value+0x30)); x = x - (1000 * value); // Bereken en schrijf de honderdtallen value = calculate_hundreds(x); sci_PutByte((value+0x30)); x = x - (100 * value); // Bereken en schrijf de tientallen value = calculate_tens(x); sci_PutByte((value+0x30)); x = x - (10 * value); // Bereken en schrijf de eenheden value = calculate_ones(x); sci_PutByte((value+0x30)); x = x - (1 * value); sci_PutByte(0x0A); // right here period contains the frequency in hertz\ // you only need to cast it in a string or whatever else you want busy = 0; } } }