3      Copyright (C) Dean Camera, 2013. 
   5   dean [at] fourwalledcubicle [dot] com 
  10   Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com) 
  11   Copyright 2012  Simon Foster (simon.foster [at] inbox [dot] com) 
  13   Permission to use, copy, modify, distribute, and sell this 
  14   software and its documentation for any purpose is hereby granted 
  15   without fee, provided that the above copyright notice appear in 
  16   all copies and that both that the copyright notice and this 
  17   permission notice and warranty disclaimer appear in supporting 
  18   documentation, and that the name of the author not be used in 
  19   advertising or publicity pertaining to distribution of the 
  20   software without specific, written prior permission. 
  22   The author disclaims all warranties with regard to this 
  23   software, including all implied warranties of merchantability 
  24   and fitness.  In no event shall the author be liable for any 
  25   special, indirect or consequential damages or any damages 
  26   whatsoever resulting from loss of use, data or profits, whether 
  27   in an action of contract, negligence or other tortious action, 
  28   arising out of or in connection with the use or performance of 
  34 static void HD44780_WriteNibble(const uint8_t nib
) 
  36         /* Read PORTD and clear the ENABLE and PD0..3 bits  
  37            then OR in the data */ 
  39         PORTD 
= (PORTD 
& ~(ENABLE 
| LO4_MASK
)) | (nib 
& LO4_MASK
); 
  41         /* Enforce address setup time (tAS) 60ns  
  43            Let's us a few NOPs for good measure */ 
  44         asm volatile("nop\n\t" 
  48         /* Take enable high and enforce Enable High time (tEH=450ns) 
  49            450ns @ 16MHz = 7.2 => 7 NOPs */ 
  53         asm volatile("nop\n\t" 
  63         /* Take enable low and enforce Enable Low time (tEL=500ns) 
  64            500ns @ 16MHz = 8.0 => 7 NOPs */ 
  67         asm volatile("nop\n\t" 
  78 static void HD44780_WriteByte(const uint8_t c
) 
  80         HD44780_WriteNibble(HI4(c
)); 
  81         HD44780_WriteNibble(LO4(c
)); 
  84 static void HD44780_PowerUp4Bit(void) 
  86         /* Wait for more than 40 ms after VCC rises to 2.7 V */ 
  88         HD44780_WriteNibble(0x03);        // FN_SET 8-bit 
  90         /* Wait for more than 4.1 ms */ 
  92         HD44780_WriteNibble(0x03);        // FN_SET 8-bit 
  94         /* Wait for more than 100 µs */ 
  96         HD44780_WriteNibble(0x03);        // FN_SET 8-bit 
  98         /* From now on we must allow 40us for each command */ 
 100         HD44780_WriteNibble(0x02);        // FN_SET 4-bit 
 102         /* The LCD is now in 4-bit mode so we can continue 
 103            using the 4-bit API */ 
 107 void HD44780_Initialize(void) 
 111         HD44780_PowerUp4Bit(); 
 114 void HD44780_WriteCommand(const uint8_t c
) 
 117         HD44780_WriteByte(c
); 
 121 void HD44780_WriteData(const uint8_t c
) 
 124         HD44780_WriteByte(c
);