Sensor cannot display on LCD

Talk about Arduino board, sheilds. Sharing Arduino projects, program, problems, solutions, suggestions..... many more, all are welcome.

Sensor cannot display on LCD

Postby Lam » Sun Mar 11, 2012 12:43 am

Hi all,

I faced a problem here. I'm using Humidity sensor
http://www.cytron.com.my/viewProduct.php?pcode=SN-HMD&name=Humidity%20Sensor and LCD(2x16) interface with Atmega32 microcontroller.

Unfortunately, my LCD doesn't display anything on screen. Besides, at same time I've been tested using LEDs, but it works!
I attached my coding, hope anyone can advice and help me.

CODE: SELECT_ALL_CODE
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

#define LCD_CPRT   PORTC
#define LCD_CDDR   DDRC
#define LCD_APRT   PORTA
#define LCD_ADDR   DDRA
#define LCD_RS      5
#define LCD_RW   6
#define LCD_EN      7

void delay_us(unsigned int d)
{
   _delay_us(d);
}

void delay_ms(unsigned int d)
{
   _delay_ms(d);
}

void E_Pulse(void)
{
   LCD_APRT |= (1<<LCD_EN);   //Enable=1
   delay_us(100);
   LCD_APRT &= ~(1<<LCD_EN);   //Enable=0
   delay_us(100);
}

void lcdCommand(unsigned char cmnd)
{
   LCD_CPRT = cmnd;
   LCD_APRT &= ~(1<<LCD_RS);   //RS=0
   LCD_APRT &= ~(1<<LCD_RW);   //RW=0
   E_Pulse();
}

void lcdData(unsigned char data)
{
   LCD_CPRT = data;
   LCD_APRT |= (1<LCD_RS);      //RS=1
   LCD_APRT &= ~(1<<LCD_RW);   //RW=0
   E_Pulse();
}

void lcd_init()
{
   LCD_CDDR = 0xFF;
   LCD_ADDR = LCD_ADDR | 0b11100000;   //PA5-PA7 as output
   
   LCD_APRT &= ~(1<<LCD_EN);
   delay_us(2000);
   lcdCommand(0x38);
   lcdCommand(0x0E);
   lcdCommand(0x01);
   delay_us(2000);
   lcdCommand(0x06);
}

void lcd_gotoxy(unsigned char x, unsigned char y)
{
   unsigned char firstCharAdr[] = {0x80,0xC0,0x94,0xD4};
   lcdCommand(firstCharAdr[y-1] + x - 1);
   delay_us(100);
}

void lcd_print(char * str)
{
   unsigned char i=0;
   while(str[i]!=0)
   {
      lcdData(str[i]);
      i++;
   }
}



int main(void)
{   
lcd_init();
   delay_us(100);
   DDRA &= 0b11110000;
   DDRD = 0xFF;      //set PORTD as output for LEDs blinking
   delay_us(100);
   ADCSRA = 0x87;
   ADMUX = 0x61;
   
   while(1)
   {      
      ADCSRA |= (1<<ADSC);         //Start conversion
      while ((ADCSRA & (1<<ADIF)) == 0);
      PORTD = ADCH;            //Output of sensor display on LEDs
      char string[16];
      itoa(ADCH, string, 10);         //Convert 16 characters to decimal value
      delay_us(10);
      lcd_gotoxy(1,1);         //Location of LCD
      lcd_print(string + 0x30);      //Add ASCII code of '0'
      delay_ms(1000);      
   }
   return 0;
}
Lam
Novice
 
Posts: 22
Joined: Thu Jan 26, 2012 7:54 pm

Re: Sensor cannot display on LCD

Postby robosang » Mon Mar 12, 2012 4:12 pm

actually I am not good in Atmel AVR, familiar with PIC, I touches Arduino a bit :0

I think the problem is lcd_init() and how you start your microcontroller. I thought the lcd_init() should be called after you initialize the Atmel I/O port, else how can those output pin become output?

Furthermore, don do anything complicated once, do it step by step.
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: Sensor cannot display on LCD

Postby Lam » Mon Mar 12, 2012 4:34 pm

Is it you mean I should initialize for my I/O ports such as PORTA(ADC and LCD's control pin),PORTC(LCD DB0-DB7) first then only call for lcd_init???

In my previous work, I'had been succeed display something such as my name on LCD. But I connected sensor to LCD, its not display anything and address LCD(0x80) there blinking. Even my in coding, I tried to convert it to ascii.
Lam
Novice
 
Posts: 22
Joined: Thu Jan 26, 2012 7:54 pm

Re: Sensor cannot display on LCD

Postby ABSF » Mon Mar 12, 2012 8:14 pm

while(1)
   {      
      ADCSRA |= (1<<ADSC);         //Start conversion
      while ((ADCSRA & (1<<ADIF)) == 0);
      PORTD = ADCH;            //Port D contains 8 bit binary number
      char string[16];
      itoa(ADCH, string, 10);         //Convert 16 characters to decimal value
string contains the ascii of the decimal equilvalent of ADCH
      delay_us(10);
      lcd_gotoxy(1,1);         //Location of LCD
      lcd_print(string + 0x30);      //Add ASCII code of '0'
Why you still need to convert string to ASC here????
      delay_ms(1000);      
   }


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Sensor cannot display on LCD

Postby Lam » Tue Mar 13, 2012 12:32 am

while(1)
{
ADCSRA |= (1<<ADSC); //Start conversion
while ((ADCSRA & (1<<ADIF)) == 0);
PORTD = ADCH; //Port D contains 8 bit binary number
char string[16];
itoa(ADCH, string, 10); //Convert 16 characters to decimal value
string contains the ascii of the decimal equilvalent of ADCH
delay_us(10);
lcd_gotoxy(1,1); //Location of LCD
lcd_print(string + 0x30); //Add ASCII code of '0'
Why you still need to convert string to ASC here????
delay_ms(1000);
}


8 bits found from ADCH and another 2 bits found from ADCL. Because I set my coding as Right-Justified so I ignored ADCL that part (I wish at least can display something first then only proceed details).

In lcd_print(string + 0x30), the part 0x30 can be ignored because I tried to see any difference or not. In this case, no difference and I made a mistake here.

string contains the ascii of the decimal equilvalent of ADCH This part I'm not understand, can you tell me any worng with this line coding?
Lam
Novice
 
Posts: 22
Joined: Thu Jan 26, 2012 7:54 pm

Re: Sensor cannot display on LCD

Postby ABSF » Tue Mar 13, 2012 9:23 am

Lam WROTE:
while(1)
{
ADCSRA |= (1<<ADSC); //Start conversion
while ((ADCSRA & (1<<ADIF)) == 0);
PORTD = ADCH; //Port D contains 8 bit binary number
char string[16];
itoa(ADCH, string, 10); //Convert 16 characters to decimal value
string contains the ascii of the decimal equilvalent of ADCH
delay_us(10);
lcd_gotoxy(1,1); //Location of LCD
lcd_print(string + 0x30); //Add ASCII code of '0'
Why you still need to convert string to ASC here????
delay_ms(1000);
}


string contains the ascii of the decimal equilvalent of ADCH This part I'm not understand, can you tell me any worng with this line coding?


No there's nothing wrong with the line. Just to make sure how itoa() function works:
Assumming :
PORTD = ADCH = 0b10100011 (0xA3)
char string[16];
string[0..15] = 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00;
after itoa(ADCH,string,10);
ADCH=0xa3 (base 16)=163 (base 10);
string becomes:
string[0..15] = 0x31,0x36,0x33,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00; 4th byte becomes null
lcd_print(string);
should print the first 4 bytes ascii "163"/0

That's what I understand how your program works. Correct me if I am wrong. :)

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Sensor cannot display on LCD

Postby Lam » Tue Mar 13, 2012 9:45 am

lcd_print(string);
should print the first 4 bytes ascii "163"/0


Thanks to u! U make me more understand what I'm doing now, because previously I confuse with it.
Besides, how to print out 4 bytes ascii as u mentioned? I knew have to break out every character of ascii and display on the address that I need to set.
I'm not sure correct or not, you may correct me. =)

CODE: SELECT_ALL_CODE
lcd_gotoxy(1,1);         //Location of LCD
      lcd_print(string[0]);
      lcd_gotoxy(1,2);
      lcd_print(string[1]);
      lcd_gotoxy(1,3);
      lcd_print(string[2]);
      lcd_gotoxy(1,4);
      lcd_print(string[3]);
Lam
Novice
 
Posts: 22
Joined: Thu Jan 26, 2012 7:54 pm

Re: Sensor cannot display on LCD

Postby ABSF » Tue Mar 13, 2012 10:03 am

CODE: SELECT_ALL_CODE
void lcd_print(char * str)
{
   unsigned char i=0;
   while(str[i]!=0)
   {
      lcdData(str[i]);
      i++;
   }
}


Your lcd_print() is actually a LCD print string function. It is not a print_char() function as you can see from the above LCD_print in the code you attached. It is looping and printing a character while str[i] != "null".

In your program below, you should replace lcd_print() with lcdData().

CODE: SELECT_ALL_CODE
lcd_gotoxy(1,1);         //Location of LCD
      lcd_print(string[0]);      //Add ASCII code of  '0'  to last character of string.
      lcd_gotoxy(1,2);
      lcd_print(string[1]);
      lcd_gotoxy(1,3);
      lcd_print(string[2]);
      lcd_gotoxy(1,4);
      lcd_print(string[3]);


Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: Sensor cannot display on LCD

Postby Lam » Tue Mar 13, 2012 10:47 am

I'm tried lcdData but my LCD remain same, that's row 1 all seen the boxes(dots).

I attach my coding again, see is it any wrong here.

First try:

CODE: SELECT_ALL_CODE
      lcd_gotoxy(1,1);         //Location of LCD
      lcdData(string[0]);
      lcd_print(string);
      lcd_gotoxy(1,2);
      lcdData(string[1]);
      lcd_print(string);
      lcd_gotoxy(1,3);
      lcdData(string[2]);
      lcd_print(string);
      lcd_gotoxy(1,4);
      lcdData(string[3]);
      lcd_print(string);
      delay_ms(1000);      


Second try: without put lcd_print. But I think is impossible to display anything without this function. =(
Lam
Novice
 
Posts: 22
Joined: Thu Jan 26, 2012 7:54 pm

Re: Sensor cannot display on LCD

Postby ABSF » Tue Mar 13, 2012 11:39 am

Lam WROTE:In my previous work, I'had been succeed display something such as my name on LCD. But I connected sensor to LCD, its not display anything and address LCD(0x80) there blinking. Even my in coding, I tried to convert it to ascii.


Can you show us your previous code that works displaying something on the LCD?
Can you try that program on your board again?
Are you doing the whole thing on a breadboard?

I suspect it was a HW problem.

Allen
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Next

Return to Arduino Based

Who is online

Users browsing this forum: No registered users and 29 guests

cron