i want to make frequency counter using LCD

Discussion in 'Embedded Systems and Microcontrollers' started by ect_09, Apr 28, 2013.

  1. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Code ( (Unknown Language)):
    1.  
    2. #include<P18f458.h>
    3.  
    4. #define mydata PORTB
    5. #define rs PORTCbits.RC1
    6. #define en PORTCbits.RC3
    7. void lcdcmd(unsigned value);
    8. void lcddata (unsigned value);
    9. void delay(unsigned int t);
    10. void lcdstring(unsigned char *string2lcd);
    11. unsigned char lowbyte, highbyte;
    12. unsigned int result;
    13. unsigned char str_result[10];
    14. unsigned char msg1[] = "MALIK";
    15.  
    16. void main()
    17. {
    18.     lcdcmd(0x38);
    19.     delay(15);
    20.     lcdcmd(0x00);
    21.     delay(15);
    22.     lcdcmd(0x0E);
    23.     delay(15);
    24.     lcdcmd(0x01);
    25.     delay(15);
    26.     lcdcmd(0x86);
    27.     delay(15);
    28.  
    29. CCP1CON=0x05;
    30. T3CON=0X00;
    31. T1CON=0X00;
    32. TRISB=0;
    33. TRISCbits.TRISC2=1;
    34. CCPR1L=0x00;
    35. CCPR1H=0x00;
    36. while(1)
    37. {
    38. TMR1H=0;
    39. TMR1L=0;
    40. PIR1bits.CCP1IF=0;
    41. while(PIR1bits.CCP1IF==0)
    42. T1CONbits.TMR1ON=1;
    43. PIR1bits.CCP1IF=0;
    44. T1CONbits.TMR1ON=0;
    45. lowbyte=CCPR1H;
    46. highbyte=CCPR1L;
    47. result = highbyte;
    48. result = result << 8;
    49. result = result | lowbyte;
    50. str_result[0] = result/10000 + 48;
    51. str_result[1] = (result/1000)%10 + 48;
    52. str_result[2] = (result/100)%10 + 48;
    53. str_result[3] = (result/10)%10 + 48;
    54. str_result[4] = (result/1)%10 + 48;
    55. str_result[5] = '\0';
    56.  
    57. PORTB=str_result[0];
    58.  
    59. //send str_result to lcd print function.
    60. lcdstring(&msg1);
    61. lcdstring(&str_result[0]);
    62. lcdstring(&str_result[1]);
    63. lcdstring(&str_result[2]);
    64. lcdstring(&str_result[3]);
    65. lcdstring(&str_result[4]);
    66. }
    67. }
    68. void lcdcmd(unsigned value)
    69.     {
    70.     mydata=value;
    71.     rs=0;
    72.     en=1;
    73.     delay(1);
    74.     en=0;
    75. }
    76. void lcddata(unsigned value)
    77.     {
    78.     mydata=value;
    79.     rs=1;
    80.     en=1;
    81.     delay(1);
    82.     en=0;
    83.  
    84. }
    85. void delay(unsigned int t){
    86.    
    87.     unsigned int i,j;
    88.     for(i=0;i<t;i++)
    89.     for(j=0;j<135;j++);
    90. }
    91.  
    92. void lcdstring(unsigned char *string2lcd){
    93.  
    94.     while(string2lcd){
    95.         lcddata(*string2lcd++);
    96.         delay(15);
    97.     }
    98.  
    99.  
    100.  
    101. }
    102.  
    103.  
    please check this code.i just tried to write it generally.
    its not displaying on LCD .please sort out my mistakes and give me tips what should i do.

    Thanks..........!!
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    That is your job not ours. We are here to help not do you job for you.


    Where is your schematic ? Where are you initializing your latches that are connected to the LCD? It looks like you are using port registers for your LCD. You should be using latches. You read ports and set latches.


    Why aren't you using one of the many code libraries already out there to send data to the LCD? Just search this forum, there are many examples of writing to an LCD.

    Forget about all of the other code that does not write to the LCD. In fact create a new program that does nothing but write to the LCD. Again search this forum. I believe The_RB and a few others have posted code examples a couple of times.
     
  3. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i attached image..

    and just send data on PORT.

    i'm using CCP module ..
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    First off you need a long delay (see your spec sheet) after you power on. However, first thing you do is start banging commands to the LCD. Add a delay first thing. The one I use wants 15 mS.

    Next, do the simplest thing possible. Do things in steps. Don't sample frequency, convert to ASCII, display on LCD, and when you get nothing start and wonder.

    First display something on the LCD in a simple way, like call lcddata() character by character. When that works THEN try the lcdstring() function.

    Then try converting a number (not a reading, a number you typed in) to ASCII and display that.

    Once you get the basic stuff all working, THEN go on to reading frequency, etc. Break things into steps.

    Does the code compile clean? Do you have an in circuit debugger?
     
  5. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    Apparently we have another forum member that refuses to take advice. I suggested simple, you elaborated on it which is good.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    As suggested. It is a good idea. To split your problems into several isolated problems. First try to make a good LCD routine. Then that is working. Do something else. And also the debugger is your friend. Which compiler do you use? It could be that you can use more of builtin the compiler functions than you do now
     
  7. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    Code ( (Unknown Language)):
    1.  
    2. #include<p18f452.h>
    3.  
    4. #define mydata PORTB
    5. #define rs PORTCbits.RC2
    6. #define en PORTCbits.RC3
    7.  
    8. void lcdcmd(unsigned value);
    9. void lcddata (unsigned value);
    10. void delay(unsigned int t);
    11. void lcdstring(unsigned char *string2lcd);
    12. unsigned char msg1[] = "frequency";
    13.  
    14.  
    15. void main (){
    16.  
    17.     TRISB=0x00;
    18.     TRISCbits.TRISC0=1;
    19.     TRISCbits.TRISC1=1;
    20.     TRISCbits.TRISC2=0;
    21.     TRISCbits.TRISC3=0;
    22.    
    23.     lcdcmd(0x38);
    24.     delay(15);
    25.     lcdcmd(0x00);
    26.     delay(15);
    27.     lcdcmd(0x0E);
    28.     delay(15);
    29.     lcdcmd(0x01);
    30.     delay(15);
    31.     lcdcmd(0x86);
    32.     delay(15);
    33.    
    34.     while(1){
    35.    
    36.  
    37.         lcdstring(&msg1);
    38.    
    39.  
    40.  
    41.     }
    42. }
    43.  
    44. void lcdcmd(unsigned value){
    45.     mydata=value;
    46.     rs=0;
    47.     en=1;
    48.     delay(1);
    49.     en=0;
    50. }
    51. void lcddata (unsigned value){
    52.     mydata=value;
    53.     rs=1;
    54.     en=1;
    55.     delay(1);
    56.     en=0;
    57.  
    58. }
    59. void delay(unsigned int t){
    60.    
    61.     unsigned int i,j;
    62.     for(i=0;i<t;i++)
    63.     for(j=0;j<135;j++);
    64. }
    65.  
    66. void lcdstring(unsigned char *string2lcd){
    67.  
    68.     while(string2lcd){
    69.         lcddata(*string2lcd++);
    70.         delay(15);
    71.     }
    72. }
    73.  
    this is code for LCD
    i also attached image.
    kindly check this.is it ok??

    kindly advice me for this.if its not good according to you people.
     
  8. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Did you even read what they wrote? You need to wait at least x miliseconds (depends on your lcd) before you issue the first command. You are clearly not doing that.

    Second thing, that delay(15) function delays by 15 potatoes? Why dont you use some library that actually does proper delays so that you know how long it will be?
     
  9. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i didnot use library and i dont know libraries that how to utilize..
     
  10. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i didnt use library and i dont know about library that how to use.
    if these libraries helpful then kindly guide me sir..

    Thanks.........!
     
  11. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    What compiler do you use? What LCD do you use?
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Somehow I think the LCD works. At least in the Proteus simulator. Since it displays the word frequency. And that is kind of a start. But as long as the OP holds back on vital information. It is hard to help. I asked the compiler question some postings ago.
     
  13. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    i am using MPLAB-C18-Full-v3_10.

    that is,which i install after installing MPLAB 8.60...

    and 16x2 LCD i used..its just simulation on proteous .
     
  14. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    experts kindly tell me what should i do?
     
  15. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I see some problems here. First you do not set the configuration words in your code. They may be set in MPLAB. But the best thing is to set them in the code. Have you taken care of this? Looking at your code. You have none functions that take care of the frequency counting. I can nor will not just write these code segments for you. But you can take a look at the first post here http://forum.allaboutcircuits.com/showthread.php?t=44852 and the "Compiled Tips ‘N Tricks Guide" I also highly recommend taking a look at the other links.
     
  16. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    "proteous" seems to be telling you your code is in the proper order... so something in the real world hardware isn't working. Possible candidates are:

    - No power on wait before accessing LCD (as you have been told)
    - The configuration bits are incorrect. This can't be checked as you did not put them in your code.
    - some other thing, wiring error, bad part, a short... all the usual suspects.
     
  17. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    I believe C18 has a library for LCD. Search all the C18 folders for XLCD. There may be a sample program there.

    I don't like the library as it stands. I use it, but I have heavily modified it in some areas.

    The way it handles and defines delays are the main problem.
     
    ect_09 likes this.
  18. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    can anyone tell me how to use libraries in MPLAB.??
    if possible give link for tutorial.

    Thanks...
     
  19. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    1. Find the dot H file for the library. "#include library_name_here.h" for each and every library you use in your dot C file.

    2. In some cases you may need to add a search path to the project, on the menu Project | Build Options Project. On the "Directories" tab select "Library Search Path" hit "New" and drill towards your library folder and hit OK.

    3. Some code (such as XLCD) is not a true library in the sense it is pre-compiled. For these you add the dot C files into your project (under Source Files) so they all get built together.
     
    ect_09 likes this.
  20. ect_09

    Thread Starter Member

    May 6, 2012
    180
    1
    sir i try to made a little library just as a sample..
    i attached file.
    kindly check this..

    am i going right......??

    i feel happy because it reduce my time..
     
Loading...