Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HD44780.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2014.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2014 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Copyright 2012 Simon Foster (simon.foster [at] inbox [dot] com)
  10. Permission to use, copy, modify, distribute, and sell this
  11. software and its documentation for any purpose is hereby granted
  12. without fee, provided that the above copyright notice appear in
  13. all copies and that both that the copyright notice and this
  14. permission notice and warranty disclaimer appear in supporting
  15. documentation, and that the name of the author not be used in
  16. advertising or publicity pertaining to distribution of the
  17. software without specific, written prior permission.
  18. The author disclaims all warranties with regard to this
  19. software, including all implied warranties of merchantability
  20. and fitness. In no event shall the author be liable for any
  21. special, indirect or consequential damages or any damages
  22. whatsoever resulting from loss of use, data or profits, whether
  23. in an action of contract, negligence or other tortious action,
  24. arising out of or in connection with the use or performance of
  25. this software.
  26. */
  27. #include "HD44780.h"
  28. static void HD44780_WriteNibble(const uint8_t nib)
  29. {
  30. /* Read PORTD and clear the ENABLE and PD0..3 bits
  31. then OR in the data */
  32. PORTD = (PORTD & ~(ENABLE | LO4_MASK)) | (nib & LO4_MASK);
  33. /* Enforce address setup time (tAS) 60ns
  34. 60 @ 16MHz = <1
  35. Let's us a few NOPs for good measure */
  36. asm volatile("nop\n\t"
  37. "nop\n\t"
  38. :: );
  39. /* Take enable high and enforce Enable High time (tEH=450ns)
  40. 450ns @ 16MHz = 7.2 => 7 NOPs */
  41. PORTD |= ENABLE;
  42. asm volatile("nop\n\t"
  43. "nop\n\t"
  44. "nop\n\t"
  45. "nop\n\t"
  46. "nop\n\t"
  47. "nop\n\t"
  48. "nop\n\t"
  49. "nop\n\t"
  50. :: );
  51. /* Take enable low and enforce Enable Low time (tEL=500ns)
  52. 500ns @ 16MHz = 8.0 => 7 NOPs */
  53. PORTD &= ~ENABLE;
  54. asm volatile("nop\n\t"
  55. "nop\n\t"
  56. "nop\n\t"
  57. "nop\n\t"
  58. "nop\n\t"
  59. "nop\n\t"
  60. "nop\n\t"
  61. "nop\n\t"
  62. :: );
  63. }
  64. static void HD44780_WriteByte(const uint8_t c)
  65. {
  66. HD44780_WriteNibble(HI4(c));
  67. HD44780_WriteNibble(LO4(c));
  68. }
  69. static void HD44780_PowerUp4Bit(void)
  70. {
  71. /* Wait for more than 40 ms after VCC rises to 2.7 V */
  72. _delay_ms(40);
  73. HD44780_WriteNibble(0x03); // FN_SET 8-bit
  74. /* Wait for more than 4.1 ms */
  75. _delay_ms(5);
  76. HD44780_WriteNibble(0x03); // FN_SET 8-bit
  77. /* Wait for more than 100 µs */
  78. _delay_us(100);
  79. HD44780_WriteNibble(0x03); // FN_SET 8-bit
  80. /* From now on we must allow 40us for each command */
  81. _delay_us(50);
  82. HD44780_WriteNibble(0x02); // FN_SET 4-bit
  83. /* The LCD is now in 4-bit mode so we can continue
  84. using the 4-bit API */
  85. _delay_us(50);
  86. }
  87. void HD44780_Initialize(void)
  88. {
  89. PORTD &= ~ALL_BITS;
  90. DDRD |= ALL_BITS;
  91. HD44780_PowerUp4Bit();
  92. }
  93. void HD44780_WriteCommand(const uint8_t c)
  94. {
  95. PORTD &= ~RS;
  96. HD44780_WriteByte(c);
  97. _delay_us(50);
  98. }
  99. void HD44780_WriteData(const uint8_t c)
  100. {
  101. PORTD |= RS;
  102. HD44780_WriteByte(c);
  103. PORTD &= ~RS;
  104. _delay_us(50);
  105. }