frequency counter

Discussion in 'Embedded Systems and Microcontrollers' started by thanigaimalar, Jun 21, 2012.

  1. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    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.
     
  2. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    Without knowing your hardware connections and your source codes, it is very hard to give you any useful advices.:rolleyes:

    Allen
     
    thanigaimalar likes this.
  3. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    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.
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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.
     
    thanigaimalar likes this.
  5. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    I need the example code for checking the timer overflow for 'n' times.Thanks in advance....
     
  6. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    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 .
     
  7. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    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.
     
  8. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    Are you aware that 65536 is 2^16?

    You have to move to 32-bit arithmetic.
     
  9. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    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.
     
    thanigaimalar likes this.
  10. MrChips

    Moderator

    Oct 2, 2009
    12,446
    3,362
    In your code the variable s must be declared as unsigned long int.
     
  11. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    Yes.The value 2^16 is 65536. I have to try with 32bit arithmetic.
     
  12. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    Exactly! you are right.I assigned the variable 's' with the data type unsigned long int.Now it displays the value correctly.Thanks a lot.
     
  13. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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)?
     
  14. absf

    Senior Member

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

    Code ( (Unknown Language)):
    1. #include<reg51.h>
    2. sfr  z0=0x90;
    3. sbit intr=P3^4;
    4. sbit rs=P2^2;
    5. sbit rw=P2^3;
    6. sbit en=P2^4;
    7. void lcd_cmd(char val);
    8. void lcd_data(char val);
    9. void lcd_string(unsigned char *str);
    10. void lcd();
    11. void wait(int j);
    12. unsigned  int d0,d1,d2,d3,d4,d5;
    13. unsigned  int r,s,t,u,v,w,x,y,z,z1;
    14.  
    15. unsigned long int q2;
    16.  
    17. void wait(int j)
    18. {
    19. long int i;
    20. for(i=0;i<j;i++);
    21.  
    22. }
    23.  
    24. void main(void)
    25. {
    26. lcd();
    27. lcd_string(" *   ENIXS  *");
    28. wait(10000);
    29.  
    30. while(1)
    31. {
    32.  
    33. q2=655360;
    34.  
    35. r=q2%10;
    36. s=q2/10;
    37. t=s%10;
    38. u=s/10;
    39. v=u%10;
    40. w=u/10;
    41. x=w%10;
    42. y=w/10;
    43. z=y%10;
    44. z1=y/10;
    45.  
    46. d0=48 + z1;
    47. d1=48 + z;
    48. d2=48 + x;
    49. d3=48 + v;
    50. d4=48 + t;
    51. d5=48 + r;
    52.  
    53. lcd();
    54. lcd_data(d0);
    55. wait(5);
    56. lcd_data(d1);
    57. wait(5);
    58. lcd_data(d2);
    59. wait(5);
    60. //lcd_data(0x2e);
    61. //wait(5);
    62. lcd_data(d3);
    63. wait(5);
    64. lcd_data(d4);
    65. wait(5);
    66. lcd_data(d5);
    67. wait(10000);
    68.  
    69. }
    70.                
    71. }
    72.  
    73. void lcd_cmd(char val)
    74. {
    75. z0=val;
    76. rs=0;
    77. rw=0;
    78. en=1;
    79. wait(10);
    80. en=0;
    81. return;
    82. }
    83.  
    84. void lcd_data(char val)
    85. {
    86. z0=val;
    87. rs=1;
    88. rw=0;
    89. en=1;
    90. wait(100);
    91. en=0;
    92. return;
    93. }
    94.  
    95. void lcd_string(unsigned char *str)
    96. {
    97. int k=0;
    98. while(str[k]!='\0')
    99. {
    100. lcd_data(str[k]);
    101. k++;
    102. wait(100);
    103. }
    104. return;
    105. }
    106.  
    107. void lcd()
    108. {
    109. lcd_cmd(0x38);
    110. wait(250);
    111. lcd_cmd(0x0e);
    112. wait(250);
    113. lcd_cmd(0x01);
    114. wait(250);
    115. lcd_cmd(0x06);
    116. wait(250);
    117. lcd_cmd(0x80);
    118. wait(250);
    119. }
     
  15. WBahn

    Moderator

    Mar 31, 2012
    17,757
    4,800
    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.
     
  16. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    Yes.You are right.I declared the variable 's' as unsigned long int.Now it displays properly.Thanks a lot.
     
  17. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    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.
     
  18. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    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
     
    Last edited: Jul 16, 2012
  19. thanigaimalar

    Thread Starter New Member

    Jun 21, 2012
    10
    0
    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.
     
Loading...