Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] ATMega32 and 16*2 LCD

Status
Not open for further replies.

yviswanathbe

Full Member level 4
Joined
Jun 14, 2007
Messages
221
Helped
10
Reputation
20
Reaction score
6
Trophy points
1,298
Activity points
3,066
Hi,

I am doing LCD interfacing with AtMega32 Micro controller.
Just for testing i am sending one character in the main function while loop.But the character is priniting at first row second column. Even if i give the command first row, first column also same is happening.

I have tried this in PROTEUS software,but the result is same.

I have attached PROTEUS file and the source code.

can some body suggest me where i am doing wrong.

Thanks and Regards,
Viswanath
 

Attachments

  • atmega32V1.rar
    15 KB · Views: 93

Here is the code:
Code:
#include <mega32.h>
#include <delay.h>
#include<stdio.h>


typedef unsigned char bit_8;                      


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                              LCD Port Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#define LCD_PORT    PORTD
#define rs          PORTB.0
#define rw          PORTB.1
#define en          PORTB.2
#define flag        PIND.7
#define led         PORTB.1
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          LCD Command MACROS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#define LCD_CLEAR             0x01
#define RETURN_HOME           0X02 
#define DEC_CURSOR            0x04
#define INC_CURSOR            0x06
#define DISP_OFF_CUR_OFF      0x08
#define DISP_OFF_CUR_ON       0x0A
#define DISP_ON_CUR_OFF       0x0C
#define DISP_ON_CUR_BLINK     0x0E
#define SHIFT_CUR_LEFT        0x10
#define SHIFT_CUR_RIGHT       0x14
#define SHIFT_DISP_LEFT       0x18
#define SHIFT_DISP_RIGHT      0x1C

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    Function Prototypes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void lcd_init(void);                    // Initialize LCD
void busy(void);                              // Check Busy
void wrt_cmd(bit_8);                    // Sending Command
void wrt_data(bit_8);                    // Sending single character
void wrt_string(bit_8 *);          // Sending String
void wrt_value(bit_8);                    // Sending 8-bit Decimal value
void cursorxy(bit_8,bit_8);          // Bringing Cursor to (X,Y) location X -> 1,2 and Y -> 1-16
unsigned char *flash buf="Hello";

void lcd_init(void)
{
      wrt_cmd(0x38);
      wrt_cmd(0x06);
      wrt_cmd(0x08);
      wrt_cmd(0x01);
      wrt_cmd(0x0F);
      wrt_cmd(0x02);  
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**** Cheacking the busy flag of LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void busy()
{
         // delay_ms(1000);
          //flag=1;
          PORTD=0x00;
          DDRD=0x00;
          rs=0;
          rw=1;
          led=1;
          while(flag!=0)
          {
                    en=0;
                    delay_ms(2);
                    en=1;
          }
          led=0;
          PORTD=0xFF;
          DDRD=0xFF;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing command to LCD 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_cmd(bit_8 val)
{        
          busy();
          LCD_PORT=val;
          rs=0;
          rw=0;
          en=1;
          delay_ms(2);
          en=0;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing string to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_string(char *string)
{
          busy();
          while(*string)
                    wrt_data(*string++);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing data to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_data(bit_8 ch)
{
          busy();
          LCD_PORT = ch;
          rs=1;
          rw=0;
          en=1;
          delay_ms(2);
          en=0;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing 8-bit Value to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_value(bit_8 x)
{
          bit_8 temp;
          temp = x/0x10;
          if(temp>9)
                    return;
          else
                    wrt_data(temp+0x30);
          temp = x%0x10;
          if(temp>9)
                    return;
          else
                    wrt_data(temp+0x30);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Bringing Cursor to (X,Y) location of LCD
                    X -> 1,2
                    Y -> 1,16
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void cursorxy(bit_8 x, bit_8 y)
{
          if((x<1||x>2)&&(y<1||y>16))
          {
                    x=1;
                    y=1;
          }
          if(x == 1)
                    wrt_cmd(0x7F+y);
          else
                    wrt_cmd(0xBF+y);
}

void main()
{         
          
          PORTB=0x00;
          DDRB=0x07;
          //PORTD=0x00;
          //DDRD=0x00;
          delay_ms(20); 
          lcd_init();
            
               
          while(1)
          {
          delay_ms(1000);
          cursorxy(1,1);
          //wrt_cmd(0x80);              
          //delay_ms(100);
          //buf="Hello";
          //wrt_string(buf);
          //delay_ms(100);
          //wrt_cmd(0xC0);
          //delay_ms(100);
          wrt_data('A');
          //delay_ms(100);
          delay_ms(1000);
          }
}

Problem is, when you were defining RS, RW, etc, you defined RS as PORTB.0 but you also define led as PORTB.0 . So, when you are checking if the LCD is busy or not, you sent signals to RS. I changed LED to PORTB.1 and it's okay now.

Hope this helps.
Tahmid.
 

Hi Tahmid,

Thanks you very much.

Its working now.

Thanks and Regards,
Viswanath

---------- Post added at 10:55 ---------- Previous post was at 10:45 ----------


HI Tahmid,

One more question.

I am facing problem with write string function.

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Writing string to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_string(char *string)
{
busy();
while(*string)
wrt_data(*string++);
}

if i give wrt_string("HELLO"); in the main loop i am getting the following error
"function argument #1 of type 'flash unsigned char * *' is incompatible with required parameter of type 'bit_8 *'"

and is working only when i declare a variable as unsigned char *flash buf="Hello"; and passing this parameter in to the wrt_string function.

what could be the reason for compilation error? (I am using codevision avr.)

Regards,
Viswanath
 

Hi,
I think you wrote
Code:
wrt_string('HELLO');
This shows error.

But if I write
Code:
wrt_string("HELLO");
This is fine and does not show any error.

I used this and it simulates fine.
Code:
#include <mega32.h>
#include <delay.h>
#include<stdio.h>


typedef unsigned char bit_8;                      


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                              LCD Port Configuration
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#define LCD_PORT    PORTD
#define rs          PORTB.0
#define rw          PORTB.1
#define en          PORTB.2
#define flag        PIND.7
#define led         PORTB.1
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          LCD Command MACROS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

#define LCD_CLEAR             0x01
#define RETURN_HOME           0X02 
#define DEC_CURSOR            0x04
#define INC_CURSOR            0x06
#define DISP_OFF_CUR_OFF      0x08
#define DISP_OFF_CUR_ON       0x0A
#define DISP_ON_CUR_OFF       0x0C
#define DISP_ON_CUR_BLINK     0x0E
#define SHIFT_CUR_LEFT        0x10
#define SHIFT_CUR_RIGHT       0x14
#define SHIFT_DISP_LEFT       0x18
#define SHIFT_DISP_RIGHT      0x1C

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    Function Prototypes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void lcd_init(void);                    // Initialize LCD
void busy(void);                              // Check Busy
void wrt_cmd(bit_8);                    // Sending Command
void wrt_data(bit_8);                    // Sending single character
void wrt_string(bit_8 *);          // Sending String
void wrt_value(bit_8);                    // Sending 8-bit Decimal value
void cursorxy(bit_8,bit_8);          // Bringing Cursor to (X,Y) location X -> 1,2 and Y -> 1-16
unsigned char *flash buf="Hello";

void lcd_init(void)
{
      wrt_cmd(0x38);
      wrt_cmd(0x06);
      wrt_cmd(0x08);
      wrt_cmd(0x01);
      wrt_cmd(0x0F);
      wrt_cmd(0x02);  
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**** Cheacking the busy flag of LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void busy()
{
         // delay_ms(1000);
          //flag=1;
          PORTD=0x00;
          DDRD=0x00;
          rs=0;
          rw=1;
          led=1;
          while(flag!=0)
          {
                    en=0;
                    delay_ms(2);
                    en=1;
          }
          led=0;
          PORTD=0xFF;
          DDRD=0xFF;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing command to LCD 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_cmd(bit_8 val)
{        
          busy();
          LCD_PORT=val;
          rs=0;
          rw=0;
          en=1;
          delay_ms(2);
          en=0;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing string to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_string(char *string)
{
          busy();
          while(*string)
                    wrt_data(*string++);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing data to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_data(bit_8 ch)
{
          busy();
          LCD_PORT = ch;
          rs=1;
          rw=0;
          en=1;
          delay_ms(2);
          en=0;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Writing 8-bit Value to LCD
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void wrt_value(bit_8 x)
{
          bit_8 temp;
          temp = x/0x10;
          if(temp>9)
                    return;
          else
                    wrt_data(temp+0x30);
          temp = x%0x10;
          if(temp>9)
                    return;
          else
                    wrt_data(temp+0x30);
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          Bringing Cursor to (X,Y) location of LCD
                    X -> 1,2
                    Y -> 1,16
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void cursorxy(bit_8 x, bit_8 y)
{
          if((x<1||x>2)&&(y<1||y>16))
          {
                    x=1;
                    y=1;
          }
          if(x == 1)
                    wrt_cmd(0x7F+y);
          else
                    wrt_cmd(0xBF+y);
}

void main()
{         
          
          PORTB=0x00;
          DDRB=0x07;
          //PORTD=0x00;
          //DDRD=0x00;
          delay_ms(20); 
          lcd_init();
            
               
          while(1)
          {
          delay_ms(1000);
          cursorxy(1,1);
          //wrt_cmd(0x80);              
          //delay_ms(100);
          //buf="Hello";
          //wrt_string(buf);
          //delay_ms(100);
          //wrt_cmd(0xC0);
          //delay_ms(100);
          wrt_data('A');   
          cursorxy(1,3);
          //delay_ms(100);
          wrt_string("HELLO");
          delay_ms(1000);
          }
}

Hope this helps.
Tahmid.
 
Hi Tahmid,

I did same as you did.
I am still getting the error.

Which version are you using?
I am using Code Vision AVR 2.03.

is the problem with my code vision avr?

Thanks and Regards,
Viswanath

---------- Post added at 11:41 ---------- Previous post was at 11:35 ----------

Hi Tahmid,

Now it is working fine.

I have uninstalled and re installed the software.

Thanks for your time.

Regards,
Viswanath
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top