frequency counter

Thread Starter

thanigaimalar

Joined Jun 21, 2012
10
I have developed the program for frequency counter using P89v51RD2 micro controller.I selected the timer0 with mode1 as a counter for counting the frequency.It counts up to 45kHz.After this range the value is displayed and then it become 0.Again I have to reset.This is the problem which I'm facing.I need the solution.Thank you all in advance.
 

Thread Starter

thanigaimalar

Joined Jun 21, 2012
10
I have connected the lcd data lines to P1.0-P1.7 and the control lines rs, rw, en to P2.2,P2.3,P2.4 respectively.The square wave from the function generator is given to P3.4 of a micro controller as external input.I used the timer0 as a event counter.It will count the input pulses.It has to count the number of pulses for one second.This counter value will be the frequency of the input pulse.
 

ErnieM

Joined Apr 24, 2011
8,377
Sometimes the "number" where the problem occurs can be a hint when it indicated things like a 16 bit register overflowing and rolling back to zero, but that would be at 65.5KHz

Why you say you need to "reset" is also an interesting clue, but there is no source code to look into.
 

Thread Starter

thanigaimalar

Joined Jun 21, 2012
10
I wrote one number display program using keil for 89v51RD2 micro controller.I assigned one variable for input.I passed this value to the LCD.The output as follows,
Input lCD output
10 000010
253 000253
3456 003456
15643 015643
.
.
.

655359 655359

655360 000000
655361 000001
.
.
.
720896 065536
The display value is correct till 655359.Why there is a mismatch in the lcd display after 655360. I have attached the program.Please check it .
 

Attachments

Thread Starter

thanigaimalar

Joined Jun 21, 2012
10
I wrote one number display program using keil for 89v51RD2 micro controller.I assigned one variable for input.I passed this value to the LCD.The output as follows,
Input lCD output
10 000010
253 000253
3456 003456
15643 015643
.
.
.
655359 655359
655360 000000
655361 000001
.
.
.
720896 065536
The display value is correct till 655359.Why there is a mismatch in the lcd display after 655360.
 

Markd77

Joined Sep 7, 2009
2,806
65536 is 256x256 and I'm guessing is the limit for an unsigned int. If you change some of your variables to unsigned long int you might find an improvement.
 

WBahn

Joined Mar 31, 2012
30,045
The display value is correct till 655359.Why there is a mismatch in the lcd display after 655360.
As others have pointed out, 65535 is the largest number that can be represented by a 16-bit unsigned integer.

The thing I'm curious about is how you are getting the units digit. Would it be possible to post your code (or at least what you think is the relevant piece of it)?
 

absf

Joined Dec 29, 2010
1,968
The OP has posted the code on post #6 and here it is again.

Rich (BB code):
#include<reg51.h>
sfr  z0=0x90;
sbit intr=P3^4;
sbit rs=P2^2;
sbit rw=P2^3;
sbit en=P2^4;
void lcd_cmd(char val); 
void lcd_data(char val);
void lcd_string(unsigned char *str);
void lcd();
void wait(int j);
unsigned  int d0,d1,d2,d3,d4,d5;
unsigned  int r,s,t,u,v,w,x,y,z,z1;

unsigned long int q2;

void wait(int j)
{
long int i;
for(i=0;i<j;i++);

}

void main(void)
{
lcd();
lcd_string(" *   ENIXS  *");
wait(10000);

while(1)
{

q2=655360;

r=q2%10;
s=q2/10;
t=s%10;
u=s/10;
v=u%10;
w=u/10;
x=w%10;
y=w/10;
z=y%10;
z1=y/10;

d0=48 + z1;
d1=48 + z;
d2=48 + x;
d3=48 + v;
d4=48 + t;
d5=48 + r;

lcd();
lcd_data(d0);
wait(5);
lcd_data(d1);
wait(5);
lcd_data(d2);
wait(5);
//lcd_data(0x2e);
//wait(5);
lcd_data(d3);
wait(5);
lcd_data(d4);
wait(5);
lcd_data(d5);
wait(10000);

}
                
}

void lcd_cmd(char val)
{
z0=val;
rs=0;
rw=0;
en=1;
wait(10);
en=0;
return;
}

void lcd_data(char val)
{
z0=val;
rs=1;
rw=0;
en=1;
wait(100);
en=0;
return;
}

void lcd_string(unsigned char *str)
{
int k=0;
while(str[k]!='\0')
{
lcd_data(str[k]);
k++;
wait(100);
}
return;
}

void lcd()
{
lcd_cmd(0x38);
wait(250);
lcd_cmd(0x0e);
wait(250);
lcd_cmd(0x01);
wait(250);
lcd_cmd(0x06);
wait(250);
lcd_cmd(0x80);
wait(250);
}
 

WBahn

Joined Mar 31, 2012
30,045
Thanks. I don't know why I didn't spot it. I guess I just skimmed down too quickly when I first scanned the thread.

Yeah, it's obvious why it behaves the way it does, since q2 is an unsigned long it has no problems with the least significant digit. The problem is when q2/10 is greater than 65535 which translates into when q2 is greater than 655359.
 

Thread Starter

thanigaimalar

Joined Jun 21, 2012
10
I referred the data sheet of P89V51RD2.The operating frequency of micro controller is up to 40MHz.I wanted to increase the rate of counter value of the micro controller.So I connected 40MHz crystal to achieve the maximum counter value.But it displays the value which is equal to the triple the value of the input.Say for example if the input frequency is 5Hz means it displays as 15Hz, 1kHz means 3kHz.....For the 20MHz crystal it displays the correct value.Where is the problem?What should I do to rectify this problem?Thanks in advance.
 

absf

Joined Dec 29, 2010
1,968
The 8952 uses 12 T cycles for one instruction (one instruction cycle). So a 12MHz crystal would execute one instruction per uS. Some instructions need more than one instruction cycle. Unlike the PIC or AVR, 8952 is a not a RISC mcu.

So if you used 12Mhz crystal in your mcu, it would run at 1x speed, but if you want to run at 4x the speed, you'd need a 48MHz crystal.

Take a look at the datasheet of the timer of 8952 here
 

Attachments

Last edited:

Thread Starter

thanigaimalar

Joined Jun 21, 2012
10
Thank you.I used 30MHz crystal instead of 40MHz crystal.Now the counting rate is increased.I need some idea about prescaler.I want to display up to 10Mhz.Which prescaler will be suitable? I referred the MC12080-1.1GHz prescaler.It has the high operating frequency.Yes..Prescaler is used to reduce high frequency .I know that one.In my frequency counter the specification is 5MHz. MC12080 has different dividing ratio.Using divide by 10 mode the 5MHz frequency will be reduced to 500kHz.Can I use this MC12080 for my circuit?Will it reduce the 5MHz frequency to 500kHz frequency.Thank you all in advance.
 
Top