Electronics > Beginners

Using PIC Capture Compare Module For Frequency Measurement

(1/1)

khatus:
Hello guys i am a beginner in pic micro controller programming. i have a code for frequency measurement using pic 18f4550 microcontroller.but the problem is although the code sometimes works in real hardware but it did not simulate i Proteus simulation software. This code is very important for me since I want to understand how capture module works in pic.Can any guys fix this code as well as post the simulation file in this forum so that i can download them and simulate it.Or provide some alternative code for Testing capture module functionality of pic 18f4550.





--- Code: ---// some random garbage uncommented code found on forum
// 20MHz external crystal with no PLL to give 20MHz system clock
// a 5 kHz test square wave is provided on RC1 - link RC1 to RC2 to test operation

#define f_timer 5000000                                    // T1 clock is Fosc/4
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

void main ( void ){
unsigned long signal_period,data1,data2;
char frequency_Hz[20];
float Frequency;

    Lcd_Init();
    TRISC.TRISC1 = 1;
    OSCCON = 0b1110000;
    PIE1.CCP1IE = 1;
    PIR1.CCP1IF = 0;
    CCP1CON = 0x05;
    CCPR1 = 0x00;
    PIR1.TMR1IF = 0;
    T1CON = 0x80;
    T1CON.TMR1ON = 1;
    TRISB = 0;
    TRISA = 0;                                                  // all outputs
    LATA = 0;                                                   // all low to start with
    TMR1H = 0;
    TMR1L = 0;

    PWM2_Init(5000);                                            // provide test frequency  output on RC1
    PWM2_Set_Duty(127);                                         // square wave
    PWM2_Start();
   
    while( 1 ){
        while(!(PIR1.CCP1IF));
        PIR1.CCP1IF=0;
        data1 = CCPR1;
        while(!(PIR1.CCP1IF));
        PIR1.CCP1IF = 0;
        data2 = CCPR1;

        if(data1 < data2){
            Lcd_Cmd(_LCD_CLEAR);                                        // clear out old information
            signal_period = data2 - data1;
            Frequency = ((float)f_timer / (float)signal_period);
            FloatToStr( Frequency,frequency_Hz );
            ///LongWordToStrWithZeros(Frequency, frequency_Hz);
            Lcd_Out(1, 1, frequency_Hz);                                // display new frequency value
            TMR1H = 0;
            TMR1L = 0;
        }
        Delay_ms(500);                                                  // Limit display rate to reduce flicker
    }
}
--- End code ---

Ian.M:
Duplicate: https://www.eevblog.com/forum/beginners/testing-capture-module-functionality-in-pic-18f4550/

Navigation

[0] Message Index

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod