Displaying A/D converter value on LCD

Discussion in 'Embedded Systems and Microcontrollers' started by Ebuka, Dec 8, 2010.

  1. Ebuka

    Thread Starter New Member

    Dec 8, 2010
    10
    0
    Hi, can anyone guide me through, Am working on a small hom work which involves A/D interfacing with LCD, my code diplays unradable symbols, I can't find out my problem. can anyone help.
    my codes are below.

    Thanx in advance.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <REGX52.H>
    4. #include <Stdio.h>
    5. #include <Stdlib.H>
    6.  
    7.  
    8. #define LCD_data P2        // Data will be sent to/recieved from the LCD throught this port P2
    9. #define LCD_rs P1_0       // 0 = Instruction input      1 = Data input
    10. #define LCD_rw P1_1       //0 = Write to LCD module   1 = Read from LCD module
    11. #define LCD_en P1_2       //  Enable Signal
    12.  
    13. #define  ADC_port P2 // A/D converter port
    14.  #define rd   P1_3
    15.  #define wr   P1_4
    16.  #define cs   P1_5
    17.  #define intr P1_6
    18.  #define OUTPUT P3 // A/D value moved here.
    19.  
    20. // Function Declerations....
    21.                                              
    22. void LCD_Power_on(void);
    23. void LCD_Busy(void);
    24. void LCD_Get_signal(unsigned char signal);
    25. void LCD_Send_signal(unsigned char signal);
    26. void LCD_Send_String(unsigned char *signal);
    27. void LCD_Initialize(void);
    28.  
    29. void convert();
    30.  void read();
    31. unsigned int ADC_value;
    32.  
    33. // The main loop....
    34.  
    35. main (void)
    36. {
    37. int    i;
    38. unsigned int a,b,c,d,e,f,h,g,I,J;
    39. unsigned int convertr[9];
    40.    
    41.  while(1){
    42.  convert();
    43.    read();  
    44.   OUTPUT = ADC_value;
    45. }
    46.  
    47.     a = 1.5; b = 4.9; c = 9.2; d = 6.5; e = 4; f = 6.4; g = 8; h = 12; I = 14.2; J = 13;  
    48.  
    49. convertr[0] = ((OUTPUT + 1) * a) / 16 ;        //round to 0    
    50. convertr[1] = ((OUTPUT + 1) * b) / 16 ;        //round to 1
    51. convertr[2] = ((OUTPUT + 1) * c) / 16 ;        //round to 1    
    52. convertr[3] = ((OUTPUT + 1) * d) / 16 ;        //round to 1
    53. convertr[4] = ((OUTPUT + 1) * e) / 16 ;        //round to 0    
    54. convertr[5] = ((OUTPUT + 1) * f) / 16 ;        //round to 1
    55. convertr[6] = ((OUTPUT + 1) * g) / 16 ;        //round to 1    
    56. convertr[7] = ((OUTPUT + 1) * h) / 16 ;        //round to 1
    57. convertr[8] = ((OUTPUT + 1) * I) / 16 ;        //round to 1    
    58. convertr[9] = ((OUTPUT + 1) * J) / 16 ;        //round to 1
    59.  
    60.                 LCD_Power_on();    // 15ms delay
    61.                 LCD_Initialize();
    62.                
    63.   while(1){
    64.    for(i = 0; i< 10;i++)      {
    65.              
    66. LCD_Get_signal(convertr[i]); // Set CGRAM adress,data is Avaible from uC    
    67.                    LCD_Send_signal(convertr[i]);
    68.                 LCD_data = convertr[i];
    69.                 scanf(" %d",convertr[i]);  
    70.        
    71.  }
    72.  }    
    73. }
    74.      
    75. /// End of main loop
    76.  
    77. void LCD_Power_on(void)
    78. {
    79. int a;
    80. for(a=0;a<25000;a++);
    81. }
    82.  
    83. void LCD_Busy(void)
    84. {
    85. unsigned char i,j;
    86.         for(i=0;i<100;i++)
    87.                 for(j=0;j<255;j++);
    88. }
    89. /* to get your signal you have to enable the LCD pins to read and more*/
    90. void LCD_Get_signal(unsigned char signal)
    91. {
    92.      LCD_data  = signal;      //Function set: 2 Line, 8-bit, 5x8 dots
    93.      LCD_rs   = 0;        //Selected command register
    94.      LCD_rw   = 0;        //We are writing in instruction register
    95.      LCD_en   = 1;        //Enable H->L
    96.      LCD_en   = 0;
    97.      LCD_Busy();          //Wait for LCD to process the command
    98. }
    99.  
    100. void LCD_Send_String(unsigned char *signal)
    101.  {
    102.    while(*signal)              //till string ends
    103.   LCD_Send_signal(*signal++);  //send characters one by one
    104.  }
    105.  
    106.   void LCD_Send_signal(unsigned char signal)
    107.   {
    108.      LCD_data  = signal;      //Function set: 2 Line, 8-bit, 5x8 dots
    109.      LCD_rs   = 1;        //Selected command register
    110.      LCD_rw   = 0;        //We are writing in instruction register
    111.      LCD_en   = 1;        //Enable H->L
    112.      LCD_en   = 0;
    113.      LCD_Busy();          //Wait for LCD to process the command
    114.   }
    115.  void LCD_Initialize(void)
    116.  {
    117.      LCD_data = 0x38;     //Function set: 2 Line, 8-bit, 5x8 dots
    118.      LCD_rs   = 0;        //Selected command register
    119.      LCD_rw   = 0;        //We are writing in data register
    120.      LCD_en   = 1;        //Enable H->L
    121.      LCD_en   = 0;
    122.      LCD_Busy();          //Wait for LCD to process the command
    123.      LCD_data  = 0x0F;     //Display on, Curson blinking command
    124.      LCD_rs   = 0;        //Selected command register
    125.      LCD_rw   = 0;        //We are writing in data register
    126.      LCD_en   = 1;        //Enable H->L
    127.      LCD_en   = 0;
    128.      LCD_Busy();          //Wait for LCD to process the command
    129.      LCD_data  = 0x01;     //Clear LCD
    130.      LCD_rs   = 0;        //Selected command register
    131.      LCD_rw   = 0;        //We are writing in data register
    132.      LCD_en   = 1;        //Enable H->L
    133.      LCD_en   = 0;
    134.      LCD_Busy();          //Wait for LCD to process the command
    135.      LCD_data  = 0x06;     //Entry mode, auto increment with no shift
    136.      LCD_rs   = 0;        //Selected command register
    137.      LCD_rw   = 0;        //We are writing in data register
    138.      LCD_en   = 1;        //Enable H->L
    139.      LCD_en   = 0;        //Enable H->L
    140.      LCD_Busy();    
    141.  }
    142.  void convert()
    143.  {
    144.   cs = 0;
    145.   wr = 0;
    146.   cs = 1;
    147.   wr = 1;
    148.   while( intr == 1);
    149.  }
    150.  void read()
    151.  {
    152.   cs = 0;
    153.   rd = 0;
    154.   ADC_value = ADC_port;
    155.   cs = 1;
    156.   rd = 1;
    157.  }
    158.  
    159. [/i][/i][/i][/i]
     
    Last edited by a moderator: Dec 8, 2010
  2. wannaBinventor

    Member

    Apr 8, 2010
    179
    4
    I didn't look at your code as I don't know C, but I'm wondering if you are able to write anything to the LCD at all? I'm guessing not.

    From a debugging perspective, I'd see if I could write anything at all and go from there.

    What clock speed are you running? At a glance (not that I'd know what to look for in C), I didn't see any delays. You may be doing things faster than the LCD can handle.
     
    Ebuka likes this.
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    A LCD is quite sensitive regarding the timing. Are you sure all delays are 100% correct timed? Also most MCU development system offer software simulation. Many errors may be tracked down using such tool. I do not know your MCU brand so I can only offer this much help.
     
    Ebuka likes this.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016

    This is a very good suggestion and usually the cause of most problems. Her is what I do. I temporarily add some test code to set each output to the LCD low then high. I use debug mode and step through the code to make sure each input to the LCD is being set correctly.

    Aslo your code is relying on a for loop for it's delay. It is going to take a lot of trail and error to get those delays right.

    I am attaching LCD code I have been using that uses set delay time. You will need to modify the LCD code so it uses the outputs for your specific LCD. You will also need to customize the delay code for your clock speed.
     
    • lcd.zip
      File size:
      2.4 KB
      Views:
      11
    Ebuka likes this.
  5. Ebuka

    Thread Starter New Member

    Dec 8, 2010
    10
    0
    I have the schematics of my connections. I will appreciate any help fro u guyz.
     
  6. Ebuka

    Thread Starter New Member

    Dec 8, 2010
    10
    0
    void LCD_char(unsigned char letter)
    {
    //This is the same as the lcd_cmd function except
    //that is sets RS to 1 telling the lcd this is characters
    unsigned char temp;

    temp=letter;
    temp=temp>>4;
    LCD_nybble(temp,1);

    temp=letter;
    temp=temp&0x0f;
    LCD_nybble(temp,1);
    }

    void LCD_char(unsigned char letter)
    {
    //This is the same as the lcd_cmd function except
    //that is sets RS to 1 telling the lcd this is characters
    unsigned char temp;

    temp=letter;
    temp=temp>>4;
    LCD_nybble(temp,1);

    temp=letter;
    temp=temp&0x0f;
    LCD_nybble(temp,1);
    }
    what does the right-shift do in your code? "temp=temp>>4;"...
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    I did not write the code. But what I think it does to to send the high order 4 bits of the byte to the LCD.


    After looking at you schematic again, your code uses all 8 bits. There is really no need. You can communicate to the LCD in 4 bit mode. You connect the 4 high order bits to the PIC and tie the 4 lower bits to ground. You can also tie R/W to ground.

    It will make your design much more simple.
     
    Ebuka likes this.
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Have you been able to write to an LCD standalone and have it work in simulation?

    Does the circuit work in simulation but not when physically built?
     
    Ebuka likes this.
  9. Ebuka

    Thread Starter New Member

    Dec 8, 2010
    10
    0
    @ thatoneguy.


    I can write srtings to the LCD without problems.
    But that of the A/D conv. I can't.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Wait. You are saying that if you write something like "Hello World" to the LCD that writes just fine. But if you write your results from the A/D conversion it displays garbage?

    Show me the line in your code where you are outputting the results of the A/D to the LCD. I am not seeing it.


    Looks to me like you have lots of problems.


    First there is this

    while(1){
    convert();
    read();
    OUTPUT = ADC_value;

    }

    It never exits from that loop.
     
  11. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Can you step through code in the IDE and see what you are trying to send to the LCD? That, and make sure it at least steps into the send to LCD procedure.
     
  12. jdraughn

    Active Member

    Jan 30, 2009
    33
    0
    It sounds like he is trying to write "2.34" straight to the lcd without first converting each character to the correct ascii code.

    You need to split the whole number from the fraction number, then split each of those into their individual digits by dividing by 10 and using the % mod operator by 10, then send each digit to the LCD while also adding 48 to it (48 is the ascii symbol for 0, so 48 + 2 means the LCD will output the number 2).

    Generally you build up a buffer[] with the contents, which will be backwards, then write it out to the lcd from back to front. But, if you change the shift from left to right too right to left, then you don't need to buffer it, but you need to change the cursor location ahead however many places it would use.

    You can also use the sprintf libraries, which I never had much luck with personally, they suck the memory out of my microcontrollers.

    Check out this article http://support2.microchip.com/KBSearch/KB_StdProb.aspx?ID=SQ6UJ9A003PQX
     
    Last edited by a moderator: Dec 14, 2010
Loading...