Making a frequency measurement meter with LCD display

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
khatus
Posts: 56
Joined: 07 Apr 2018 21:08

Making a frequency measurement meter with LCD display

#1 Post by khatus » 22 Jul 2019 16:16

Hello guys i am currently making a frequency meter using pic 18f4550 micro controller. Here is my code written in mikroc pro for pic

Code: Select all

#define f_timer 2000000
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;
unsigned long frequency_Hz[20];
float Frequency;
TRISC.TRISC2=1;
OSCCON=0b1110000;
PIE1.CCP1IE=1;
PIR1.CCP1IF=0;
CCP1CON=0x05;
CCPR1=0x00;
PIR1.TMR1IF=0;
T1CON=0x80;
TMR1H=0;
TMR1L=0;
T1CON.TMR1ON=1;
Lcd_Init();
TRISB = 0;
TRISA = 0;
PORTA = 0;


while( 1 )
{

while(!(PIR1.CCP1IF));
PIR1.CCP1IF=0;
data1 = CCPR1;
while(!(PIR1.CCP1IF));
PIR1.CCP1IF=0;
data2 = CCPR1;

if(data1 < data2)
           {
            signal_period = data2 - data1;
            Frequency = ((float)f_timer / (float)signal_period);
            FloatToStr( Frequency,frequency_Hz );
            Lcd_Out_Cp(frequency_Hz);
           while(1)
           {

            }
        TMR1H=0;
        TMR1L=0;

    }
}
}
The code compiled successfully.The problem is the LCD display did not show any value.Can anybody correct my code??

Image

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Making a frequency measurement meter with LCD display

#2 Post by hexreader » 22 Jul 2019 17:45

MikroC is not certified for use with Proteus - Much better to develop on real hardware.

Fixed code is attached, but this design makes for a poor frequency counter.

Had to guess at many things, as you have provided insufficient information and zero commenting in code.

Tested on real hardware, not Proteus.
Attachments
forum.zip
(60.98 KiB) Downloaded 87 times
Start every day with a smile...... (get it over with) :)

khatus
Posts: 56
Joined: 07 Apr 2018 21:08

Re: Making a frequency measurement meter with LCD display

#3 Post by khatus » 22 Jul 2019 19:09

Thank you very much :D :)

khatus
Posts: 56
Joined: 07 Apr 2018 21:08

Re: Making a frequency measurement meter with LCD display

#4 Post by khatus » 22 Jul 2019 19:58

Just one question why did you define

Code: Select all

TRISA = 0;
LATA = 0; 

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Making a frequency measurement meter with LCD display

#5 Post by hexreader » 22 Jul 2019 20:05

Because I was not thinking properly.

It should have been:

Code: Select all

    TRISA = 0;                                                  // all outputs
    LATA = 0;                                                   // all low to start with
The code was correct but the comment was wrong
Start every day with a smile...... (get it over with) :)

khatus
Posts: 56
Joined: 07 Apr 2018 21:08

Re: Making a frequency measurement meter with LCD display

#6 Post by khatus » 06 Aug 2019 19:13

I have checked it in real hardware but the code did not work
here is my simulation in real hardware
Image

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Making a frequency measurement meter with LCD display

#7 Post by hexreader » 06 Aug 2019 19:48

Did you use 8MHz crystal (and 2 capacitors), same as me?

Did you use the same configuration bit settings same as me?

How did you input a frequency - what frequency, amplitude and shape?
I fed 1Khz TTL square wave to RC2 and the display mostly showed the correct frequency (with minor glitches)
100Hz to 100kHz seems sort of OK with my code, my configuration settings and my hardware (as in my attachment)
Last edited by hexreader on 06 Aug 2019 20:09, edited 2 times in total.
Start every day with a smile...... (get it over with) :)

Sparky1039
Posts: 1179
Joined: 24 Nov 2005 20:07
Location: Colorado, USA

Re: Making a frequency measurement meter with LCD display

#8 Post by Sparky1039 » 06 Aug 2019 19:59

Other points:

CCP1 is a 16-bit register requiring you to read the high and low bytes in order to get the actual time interval for calculating the period. Edit: I guess mikroC performs this two step read for you by allowing the CCPR1 command.

Remember there should be a delay after the LCD write in order to allow it time to actually post the characters before looping back to get another frequency measurement. As written the code only takes one measurement and then halts (while (1)). This may not sufficient to get a usable reading.

Clearing the CCP interrupt flag should always be done after you have obtained the CCP1 data values. This prevents false triggering while reading the registers.

khatus
Posts: 56
Joined: 07 Apr 2018 21:08

Re: Making a frequency measurement meter with LCD display

#9 Post by khatus » 07 Aug 2019 09:20

i am using 20 mhz oscillator

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Making a frequency measurement meter with LCD display

#10 Post by hexreader » 07 Aug 2019 11:34

khatus wrote:i am using 20 mhz oscillator
I assume that you mean 20MHz (megaHerz) not 20mHz (milliHerz).

New project attached, adjusted for 20MHz crystal or oscillator.

I have provided a test signal at RC1 output. Connect RC1 to RC2 and you should see 5000 displayed on LCD.
Attachments
forum.zip
(64.29 KiB) Downloaded 69 times
Start every day with a smile...... (get it over with) :)

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: Making a frequency measurement meter with LCD display

#11 Post by hexreader » 12 Aug 2019 21:54

Nothing to do with your project, but attached is another way to make a frequency counter.

Principle is to measure 1 second as accurately as possible and have a timer count external asynchronous input pulses.

A little inaccurate due to 20MHz is a poor choice of MCU clock. 8MHz x 4 PLL for 32 MHz gives a far more accurate 1 second measurement.

Ignore this project if you choose - it may just cause confusion.

In my humble opinion, it is far better than your way of making a frequency counter. There are probably far better ways still... but maybe none as simple.
Attachments
freq.zip
(43.64 KiB) Downloaded 82 times
Start every day with a smile...... (get it over with) :)

Post Reply

Return to “mikroC PRO for PIC General”