code C of a frequency checker (pic18f45k22)

Discussion in 'Embedded Systems and Microcontrollers' started by hussein_nas, Oct 25, 2015.

  1. hussein_nas

    Thread Starter New Member

    Oct 25, 2015
    1
    0
    Hello, this code displays properly the period if the clock was around 5KHz but gives wrong answer if the frequency was for example 50Hz , so i think the problem is with the prescalor.
    And after doing f1 = 1/period and display it the answer becomes totally wrong.
    The frequency sources (clocks) are connected to ccp1 and ccp2 pins 16 ,17.
    Any help will be appreciated.



    #include <p18cxxx.h>
    #include "LCD4xlib.h"
    #include <math.h>

    unsigned char Period;
    unsigned char Period1;
    unsigned int f1;
    unsigned int f2;

    char Digits [16];
    char Digits1 [5];

    #define Wait4Edge() {PIR1bits.CCP1IF = 0; while (!PIR1bits.CCP1IF);}


    #define ROM const far rom char
    #define E PORTEbits.RE2
    #define RW PORTEbits.RE1
    #define RS PORTEbits.RE0


    void setup(void);
    void Measure(void);
    void Measure2(void);

    void main(void)
    {
    setup();
    while(1)
    {
    Measure();
    Measure2();
    }
    }
    void setup(void)
    {
    InitLCD();
    DispRomStr(Ln1Ch0, (ROM *) "freq1 = _____");
    DispRomStr(Ln2Ch0, (ROM *) "freq2 = _____");
    CCP1CON=0x05;
    CCP2CON=0x05;
    T1CONbits.TMR1ON=1;
    T0CONbits.TMR0ON=1;




    }

    void Measure (void)
    {
    TMR1H=0x00;
    TMR1L=0x00;
    Wait4Edge()
    Period=CCPR1;
    Wait4Edge()
    Period=CCPR1L - Period;
    f1=(1/Period);
    Bin2AscE(f1, Digits);
    DispVarStr(Digits, Ln1Ch8, 16);

    }
    void Measure2 (void)
    {
    TMR1H=0x00;
    TMR1L=0x00;
    Wait4Edge()
    Period1=CCPR2;
    Wait4Edge()
    Period1=CCPR2L - Period1;
    f2=1/Period1;
    Bin2AscE(f2, Digits1);
    DispVarStr(Digits1, Ln2Ch8, 5);
    }
     
Loading...