title: 4 bit 4x20 LCD with Atmega32
body: <p>I'm having trouble with my 4x20 LCD display.
  I've been looking at a numerous guides and code snippets the last few days, but nothing seems to help.
  The problem is that the displays shows nothing.</p>
  
  <p>When I compile it on my computer and print it all on my screen, everything seems to be ok.</p>
  
  <p>I would appreciate it if somebody would look through it and see if there are any obvious errors.</p>
  
  <p>Thanks in advance.</p>
  
  <pre><code>//  Connection:
  //
  //  Atmega32    LCD
  //  PB0     -&gt;  DB4
  //  PB1     -&gt;  DB5
  //  PB2     -&gt;  DB6
  //  PB3     -&gt;  DB7
  //  PB4     -&gt;  RS
  //  PB5     -&gt;  R/W
  //  PB6     -&gt;  E
  //  PB7     -&gt;  
  
  #include &lt;avr/io.h&gt;
  #include &lt;util/delay.h&gt;
  
  #define LCDPort PORTB
  #define LCDDDR  DDRB
  #define enable 6        //Enable = on
  #define readWrite 5     //Read = on, Write = off
  #define RS 4            //Send command = off, send data = on
  
  void checkBusy(void);
  void updateLCD(void);
  void sendCommand(unsigned char command);
  void sendData(unsigned char character);
  void sendInitCommand(unsigned char command);
  
  int main(void)
  {
      LCDDDR |= 15;
      LCDDDR |= 1 &lt;&lt; enable;                      //Set control lines as output (high)
      LCDDDR  |= 1 &lt;&lt; readWrite;
      LCDDDR  |= 1 &lt;&lt; RS;                         
  
      _delay_ms(100);                              //Wait for LCD to boot
      sendInitCommand(0x3);                        //Init function set 1
      _delay_ms(100);
      sendInitCommand(0x3);                        //Init function set 2
      _delay_us(100);
      sendInitCommand(0x3);                        //Init function set 3
      _delay_us(100);
      sendInitCommand(0x2);                        //Function set (set 4 bit mode)
      _delay_us(100);
      sendInitCommand(0x28);                       //Funcion set I=1, N=0, F=0
      //sendInitCommand(0x8);                      
      _delay_us(60);
      sendInitCommand(0x8);                        //On/off control D=0, C=0, B=0
      //sendInitCommand(0x8);
      _delay_us(60);
      sendCommand(0x01);                           //Clear display
      //sendInitCommand(0x1);
      _delay_ms(60);
      sendCommand(0x06);                           //Entry mode set I/D=1, S=0
      //sendInitCommand(0x6);
      _delay_us(60);
      sendCommand(0x0C);                           //On/off control D=1, C=0, B=0
      //sendInitCommand(0xC);
      _delay_us(60);
  
  
      sendData(0x41);                             //Display "A"
      sendData(0x42);                             //Display "B"
      sendData(0x43);                             //Display "C"
      sendData(0x44);                             //Display "D"
      sendData(0x45);                             //Display "E"
  
  
  
      while(1) {
  
      }
  
      return 0;
  }
  
  void checkBusy() {
  
      LCDDDR &amp;= ~15;                              //Set data DDR lines read (low)
  
  /*  LCDPort |= 1 &lt;&lt; readWrite;                  //Forget read for now - use delay instead
      LCDPort &amp;= ~1 &lt;&lt; RS;
  
      while((LCDPort &amp; 15) &gt;= 0x8) {
          updateLCD();
      }
  */
      _delay_ms(50);
      LCDDDR |= 15;                               //Set data lines DDR to write (high)
  
  
  }
  
  void updateLCD() {
  
      LCDPort |= 1 &lt;&lt; enable;                     //Enable
      asm volatile ("nop");
      asm volatile ("nop");
      LCDPort &amp;= ~1 &lt;&lt; enable;                    //Disable
  }
  
  void sendCommand(unsigned char command) {
  
      checkBusy();
      LCDPort &amp;= ~(1 &lt;&lt; readWrite | 1 &lt;&lt; RS);     //Set R/W and RS low (write command)
      LCDPort |= (command &gt;&gt; 4) &amp; 15;             //Send 4 ms bits
      updateLCD();
      LCDPort &amp;= ~15; 
      LCDPort |= command &amp; 15;                    //Send 4 ls bits
      updateLCD();
      LCDPort &amp;= ~15;                             //Clear data lines
  
  }
  
  void sendData(unsigned char character) {
  
      checkBusy();
      LCDPort &amp;= ~1 &lt;&lt; readWrite;                 //Set R/W low and RS high (write data)
      LCDPort |= 1 &lt;&lt; RS;
      LCDPort |= (character &gt;&gt; 4 &amp; 15);           //Send 4 ms bits
      updateLCD();
      LCDPort &amp;= ~1 &lt;&lt; readWrite;
      LCDPort &amp;= ~15;
      LCDPort |= 1 &lt;&lt; RS;
      LCDPort |= (character &amp; 15);                //Send 4 ls bits
      updateLCD();
      LCDPort &amp;= ~15;                             //Clear data lines
  }
  
  void sendInitCommand(unsigned char command) {
  
      LCDPort &amp;= ~(1 &lt;&lt; readWrite | 1 &lt;&lt; RS);     //Set R/W and RS low (write command)
      LCDPort |= command &amp; 15;                    
      updateLCD();
      LCDPort &amp;= ~15;                             //Clear data lines
  }
  </code></pre>