Displaying A/D converter value on LCD

Thread Starter

Ebuka

Joined Dec 8, 2010
10
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.

Rich (BB code):
#include <REGX52.H>
#include <Stdio.h>
#include <Stdlib.H>


#define LCD_data P2        // Data will be sent to/recieved from the LCD throught this port P2 
#define LCD_rs P1_0       // 0 = Instruction input      1 = Data input
#define LCD_rw P1_1       //0 = Write to LCD module   1 = Read from LCD module
#define LCD_en P1_2       //  Enable Signal

#define  ADC_port P2 // A/D converter port
 #define rd   P1_3
 #define wr   P1_4
 #define cs   P1_5
 #define intr P1_6
 #define OUTPUT P3 // A/D value moved here.

// Function Declerations.... 
                                             
void LCD_Power_on(void);
void LCD_Busy(void);
void LCD_Get_signal(unsigned char signal);
void LCD_Send_signal(unsigned char signal);
void LCD_Send_String(unsigned char *signal);
void LCD_Initialize(void);

void convert();
 void read();
unsigned int ADC_value;

// The main loop....

main (void)
{
int    i;
unsigned int a,b,c,d,e,f,h,g,I,J;
unsigned int convertr[9]; 
   
 while(1){
 convert();
   read();  
  OUTPUT = ADC_value;
}

    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;  

convertr[0] = ((OUTPUT + 1) * a) / 16 ;        //round to 0     
convertr[1] = ((OUTPUT + 1) * b) / 16 ;        //round to 1 
convertr[2] = ((OUTPUT + 1) * c) / 16 ;        //round to 1     
convertr[3] = ((OUTPUT + 1) * d) / 16 ;        //round to 1 
convertr[4] = ((OUTPUT + 1) * e) / 16 ;        //round to 0     
convertr[5] = ((OUTPUT + 1) * f) / 16 ;        //round to 1 
convertr[6] = ((OUTPUT + 1) * g) / 16 ;        //round to 1     
convertr[7] = ((OUTPUT + 1) * h) / 16 ;        //round to 1
convertr[8] = ((OUTPUT + 1) * I) / 16 ;        //round to 1     
convertr[9] = ((OUTPUT + 1) * J) / 16 ;        //round to 1
 
                LCD_Power_on();    // 15ms delay
                LCD_Initialize();
                
  while(1){
   for(i = 0; i< 10;i++)      {
              
LCD_Get_signal(convertr); // Set CGRAM adress,data is Avaible from uC    
                   LCD_Send_signal(convertr);
                LCD_data = convertr;
                scanf(" %d",convertr);  
        
 }
 }     
}
     
/// End of main loop

void LCD_Power_on(void)
{
int a;
for(a=0;a<25000;a++);
}

void LCD_Busy(void)
{
unsigned char i,j;
        for(i=0;i<100;i++)
                for(j=0;j<255;j++);
}
/* to get your signal you have to enable the LCD pins to read and more*/
void LCD_Get_signal(unsigned char signal)
{
     LCD_data  = signal;      //Function set: 2 Line, 8-bit, 5x8 dots
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in instruction register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_Busy();          //Wait for LCD to process the command
}

void LCD_Send_String(unsigned char *signal)
 {
   while(*signal)              //till string ends
  LCD_Send_signal(*signal++);  //send characters one by one
 }

  void LCD_Send_signal(unsigned char signal)
  {
     LCD_data  = signal;      //Function set: 2 Line, 8-bit, 5x8 dots
     LCD_rs   = 1;        //Selected command register
     LCD_rw   = 0;        //We are writing in instruction register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_Busy();          //Wait for LCD to process the command
  }
 void LCD_Initialize(void)
 {
     LCD_data = 0x38;     //Function set: 2 Line, 8-bit, 5x8 dots
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_Busy();          //Wait for LCD to process the command
     LCD_data  = 0x0F;     //Display on, Curson blinking command
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_Busy();          //Wait for LCD to process the command
     LCD_data  = 0x01;     //Clear LCD
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;
     LCD_Busy();          //Wait for LCD to process the command
     LCD_data  = 0x06;     //Entry mode, auto increment with no shift
     LCD_rs   = 0;        //Selected command register
     LCD_rw   = 0;        //We are writing in data register
     LCD_en   = 1;        //Enable H->L
     LCD_en   = 0;        //Enable H->L
     LCD_Busy();    
 }
 void convert()
 {
  cs = 0;
  wr = 0;
  cs = 1;
  wr = 1;
  while( intr == 1);
 }
 void read()
 {
  cs = 0;
  rd = 0;
  ADC_value = ADC_port;
  cs = 1;
  rd = 1;
 }

 
Last edited by a moderator:
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.
 

t06afre

Joined May 11, 2009
5,934
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.
 

spinnaker

Joined Oct 29, 2009
7,830
Check wiring between mcu and ldc (looks like you have wrong connection on the data lines, easily upside down). Post the schematic along with the code.

Cheers

Alberto

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.
 

Attachments

Thread Starter

Ebuka

Joined Dec 8, 2010
10
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.
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;"...
 

spinnaker

Joined Oct 29, 2009
7,830
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.
 

thatoneguy

Joined Feb 19, 2009
6,359
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?
 

spinnaker

Joined Oct 29, 2009
7,830
@ thatoneguy.


I can write srtings to the LCD without problems.
But that of the A/D conv. I can't.
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.
 

thatoneguy

Joined Feb 19, 2009
6,359
@ thatoneguy.


I can write srtings to the LCD without problems.
But that of the A/D conv. I can't.
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.
 

jdraughn

Joined Jan 30, 2009
33
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:
Top