i have recently been into atmega world after i heard it is much better than pic and it is my second tutorial about writing to LCD i wrote this program in atmel studio but it doesnt work i double checked the hardware and everything was okay, what is the problem??

#include <avr/io.h>
#include <util/delay.h>

void initLCD(void);
void writeChar(int);
void sendCommand(int);
void checkIfBusy(void);

int main()
    DDRC = 0xFF;
    DDRD = 0b11100000;

void checkIfBusy(){
    DDRC = 0;
    PORTD |= (1<<5);
    PORTD |= (0<<6);
    while(bit_is_set(PIND, 5)){
        PORTD |= (1 << 6);
        PORTD &= (0 << 6);

    DDRC = 0xFF;

void sendCommand(int command){
    PORTD = 0;
    PORTC = command;
    PORTD = 0b10000000;
    PORTD = 0;

void initLCD(){
    //Clear Display
    //Function Set
    //Display Control
void writeChar(int character){
    PORTD = 0b01000000;
    //Write the character
    PORTC = character;
    PORTD = 0b11000000;
    PORTD = 0b01000000;


I changed the code but it still doesn't work, what is the problem then?

  • \$\begingroup\$ I think the problem is that the delays you use in initialization are too small. Increase the first one to at least 4.1ms and the other two to at least 100us. You should also add some delay after power up, add 15ms or more \$\endgroup\$
    – alexan_e
    Commented Feb 21, 2015 at 8:15

1 Answer 1


It is suggested that you must mention the pin functions you allocated. But I realize the following. Please verify that.

You have used,

Ok, The 1st mistake that I spotted was that you have confusion in how to set & clear a particular bit.

You have used PORTD |= (0<<6) <-- What does this mean? Actually it does nothing. It simply leaves PORTD as it is....Oops !!!

Then you have also used PORT &= (0<<6) <--- What this means is, it simply clears all 8 bits in PORTD. Your intention was not this, right?

Here is the correct way ,

Setting a particular bit, suppose say, PD6 (your intended bit), use the following:

PORTD |= (1<<6);

Clearing the same bit,

PORTD & = ~(1<<6);

Ok, now that this is fine, you also have a logical issue behind the "checkIfBusy" routine.

Actually all tha you have to do is given below in a sequence:

First assume

PORTD as input, "DDRD=0x00";  
RS=0, "PORTD&=~(1<<6);
RW=1, "PORTD|=(1<<5);

Then give a trigger, check if the 7th data bit is set, if set wait there, else quit from the loop.
It is how you have to write this.




SOME MORE: You have to send ASCII characters to LCD.
So please send a char or string value. Sending Integer isn't going to help you fulfill your desire.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.