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.

Need simple code for LCD use ATmega16 and IAR copiletor C la

Status
Not open for further replies.

brolija2dekan

Newbie level 3
Newbie level 3
Joined
Mar 16, 2006
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,328
lcd+atmega16+c

Hi, can anyone please post a simple LCD code for me?
I just need to know how to show a word in a 2x16 LCD. I'm using ATmega16 and
IAR Workbench for AVR compiletor need C code
 

atmega16 lcd

please any one has the IAR programming manual for AVR controller please upload it
i am facing problem in IAR (AVR) tool box:cry:
 

    V

    Points: 2
    Helpful Answer Positive Rating
what about using avr studio? how would the code be?
 

    V

    Points: 2
    Helpful Answer Positive Rating
I had known the basics of the lcd...
and the question is how can i determine where i can write on the lcd (like to write on the middle of the second line) what is the code of it???should i use lcd instruction : 00000001xx?
 

foxbrain said:
how can i determine where i can write on the lcd

should i use lcd instruction : 00000001xx?

Assuming that the LCD_Instruction() routine works well, you should first set the cursor to the desired address:

Code:
LCD_Instruction(0xCA);  //Somewhere in the middle of the second line of a 4x20 lcd

Then if you want to write character:

Code:
LCD_Data('H');  //Display character on address 0xCA
LCD_Data('I');  //Display character on address 0xCB


Regards,
Alexandros
 

LCD_Instruction(0xCA);
what is this function? and what is this value CA?
Note this is my code and it's working good:



/*----------------------------------------------------------------
-----------------HEADER FILES-------------------------------------
-----------------------------------------------------------------*/
#include<avr/io.h>
#include <util/delay.h>


#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))

/*----------------------------------------------------------------
-------------CONNECTION BETWEEN LCD AND ATMEGA32-----------------
-----------------------------------------------------------------*/
#define DATA_DDR DDRB
#define DATA_PORT PORTB


#define CONTROL_DDR DDRA
#define CONTROL_PORT PORTA
#define Enable_Pin 2
#define RegSelect_Pin 0
#define ReadWrite_Pin 1
#define CONTROL_MASK 0X07


/*----------------------------------------------------------------
-------------CONTROL BITS OF LCD --------------------------------
-----------------------------------------------------------------*/

#define Set_Enable CONTROL_PORT|=_BV(Enable_Pin)
#define Clear_Enable CONTROL_PORT&=~_BV(Enable_Pin)
#define Write_Lcd CONTROL_PORT&=~_BV(ReadWrite_Pin)
#define Read_Lcd CONTROL_PORT|=_BV(ReadWrite_Pin)
#define Select_InstructionRegister CONTROL_PORT&=~_BV(RegSelect_Pin)
#define Select_DataRegister CONTROL_PORT|=_BV(RegSelect_Pin)
#define Data_Lcd(a) DATA_PORT=a
#define delay(a) _delay_ms(a)




/*----------------------------------------------------------------
-----------------SEND A CHARACTER TO LCD-------------------------
-----------------------------------------------------------------*/
void Lcd_Send(unsigned char a)
{
CLEARBIT(PORTD,2);
Select_DataRegister;
Write_Lcd;
Data_Lcd(a);
Set_Enable;
delay(30);
Clear_Enable;
delay(30);
}






/*----------------------------------------------------------------
-----------------FUNCTIONS TO INITIALIZE PORTS--------------------
-----------------------------------------------------------------*/
void Init_Ports(void)
{
CLEARBIT(PORTC,0);
DATA_DDR=0XFF; //Setting data port for output
CONTROL_DDR=CONTROL_MASK;//setting selected pins of control port for output
CONTROL_PORT&=~(_BV(Enable_Pin)|_BV(RegSelect_Pin )|_BV(ReadWrite_Pin)); //setting values to 0 at starting
}




/*----------------------------------------------------------------
------------FUNCTION TO INITIATE LCD -----------------------------
-----------------------------------------------------------------*/
void Init_Lcd(void)
{
CLEARBIT(PORTC,1);
char init[10];
int i;
init[0] = 0x01;
init[1] = 0x38;
init[2] = 0x0c;
init[3] = 0x06;
init[4] = 0x80;
Select_InstructionRegister;
Write_Lcd;
for(i=0;i<5;i++)
{
Data_Lcd(init);
Set_Enable;
delay(30);
Clear_Enable;
delay(30);
}
}


/*----------------------------------------------------------------
----------------MAIN FUNCTION--------------------------------------
-----------------------------------------------------------------*/
int main(void)
{
DDRC=0xFF;
PORTC=0x00;
DDRD=0xFF;
PORTD=0x00;
SETBIT(PORTC,0);
SETBIT(PORTC,1);
SETBIT(PORTD,2);
int len,i;
char string[] = {'H','E','L','L','O',' ','J','O','J','O'};
Init_Ports();
//CLEARBIT(PORTC,0);
Init_Lcd();
//CLEARBIT(PORTC,1);
len=10;
for (i=0; i< len;i++)
Lcd_Send(string);


//CLEARBIT(PORTD,2);
}


so my main question is what are the addresses that determine the place we write on the LCD?
 

foxbrain said:
LCD_Instruction(0xCA);

what is this function? and what is this value CA?

0xCA is the cursor position. I use 4x20 LCD, so the printable characters are 80. Each one has an address. Those 80 addresses are:

Line1 addresses: 0x80 to 0x93
Line2 addresses: 0xC0 to 0xD3
Line3 addresses: 0x94 to 0xA7
Line4 addresses: 0xD4 to 0xE7

According to the above table, the middle of line 2 will be address 0xC9. So following your function names, if you want to display a "HI" on the middle of line 2 that would be:

Code:
Data_Lcd(0xC9);  //Set cursor at the desired address
Lcd_Send('H');  //print an 'H' at address 0xC9
Lcd_Send('I');  //print an 'I' at address 0xCA


Hope that helped.
 

are these addresses general in all lcds (i don't think so).
how can i know that for my lcds (that is 16X02)and i'll attach the datasheet of it?
 

Attachments

  • 16X02A.pdf
    151.2 KB · Views: 182

foxbrain said:
are these addresses general in all lcds

Yes they are the same. I think than in a 2x16 LCD the two lines start from the same address (0x80 - 0xC0). To be accurate the address is 0x00 and 0x40, and the command is 0x80+DDRAM address. Anyway, try 0x80 and 0xC0 and tell us what happened.
 
ya it worked i had to change init[4] = 0x80; (0x80 to what address i want to start with)...
thanks.
......................
 
Last edited:

i've got another question how can i write a function that can check if the lcd is busy or not?
as i know i have to set R/W to high and RS to low to read the busy flag and then what shall i do?
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top