Subversion Repositories HomeAutomation

Rev

Rev 140 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  LCD routines (HD44780)
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/branches/unstable/EmbeddedSoftware/PIC/canLCD/Source/lcd.c $
  7.  * Last changed:    $LastChangedDate: 2006-11-21 10:28:39 +0100 (Tue, 21 Nov 2006) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 140 $
  10.  ********************************************************************/
  11.  
  12. #include <lcd.h>
  13.  
  14. #ifdef USE_LCD
  15.  
  16. static void lcdWrite(BYTE c);
  17.  
  18. #define LCD_STROBE  ((LCD_EN_IO = 1),(LCD_EN_IO = 0))
  19.  
  20. /*
  21.     OBS OBS OBS OBS!!
  22.  
  23.     DELAYS ARE FOR 40Mhz (10Mhz PLL4)
  24.  
  25.     Definitions needed:
  26.  
  27.     LCD_RS_TRIS and LCD_RS_IO
  28.     LCD_EN_TRIS and LCD_EN_IO
  29.     LCD_DATA_4_TRIS and LCD_DATA_4_IO
  30.     LCD_DATA_5_TRIS and LCD_DATA_5_IO
  31.     LCD_DATA_6_TRIS and LCD_DATA_6_IO
  32.     LCD_DATA_7_TRIS and LCD_DATA_7_IO
  33.    
  34.    
  35.  
  36. */
  37.  
  38. /*
  39. *   Function: lcdWrite
  40. *
  41. *   Input:  Byte to write.
  42. *   Output: none
  43. *   Pre-conditions: none
  44. *   Affects: Write to the dislay
  45. *   Depends: none.
  46. */
  47. void lcdWrite(BYTE c)
  48. {    
  49.     LCD_DATA_4_IO=((c)&0b10000)>>4;
  50.     LCD_DATA_5_IO=((c)&0b100000)>>5;
  51.     LCD_DATA_6_IO=((c)&0b1000000)>>6;
  52.     LCD_DATA_7_IO=((c)&0b10000000)>>7;
  53.     LCD_STROBE;
  54.     LCD_DATA_4_IO=((c)&0b1);
  55.     LCD_DATA_5_IO=((c)&0b10)>>1;
  56.     LCD_DATA_6_IO=((c)&0b100)>>2;
  57.     LCD_DATA_7_IO=((c)&0b1000)>>3;
  58.     LCD_STROBE;
  59.     Delay100TCYx(8);    // 80uS
  60. }
  61.  
  62.  
  63. /*
  64. *   Function: lcdClear
  65. *
  66. *   Input:  none.
  67. *   Output: none
  68. *   Pre-conditions: none
  69. *   Affects: Clear the display
  70. *   Depends: none.
  71. */
  72. void lcdClear(void)
  73. {
  74.     LCD_RS_IO = 0;
  75.     lcdWrite(0x1);   // Clear the Display
  76.     Delay10KTCYx(2);        // 2ms
  77.     lcdWrite(0x02);  // Home the display
  78.     Delay10KTCYx(2);        // 2ms
  79. }
  80.  
  81. /*
  82. *   Function: lcdPuts
  83. *
  84. *   Input:  string to write.
  85. *   Output: none
  86. *   Pre-conditions: none
  87. *   Affects: none.
  88. *   Depends: none.
  89. */
  90. void lcdPuts(char * s)
  91. {
  92.     LCD_RS_IO = 1;  // write characters
  93.     while(*s) lcdWrite(*s++);
  94. }
  95.  
  96. /*
  97. *   Function: lcdPutrs
  98. *
  99. *   Input:  string to write.
  100. *   Output: none
  101. *   Pre-conditions: none
  102. *   Affects: none.
  103. *   Depends: none.
  104. */
  105. void lcdPutrs(const rom char *s)
  106. {
  107.     LCD_RS_IO = 1;  // write characters
  108.     while(*s) lcdWrite(*s++);
  109. }
  110.  
  111. /*
  112. *   Function: lcdPutc
  113. *
  114. *   Input:  char to write.
  115. *   Output: none
  116. *   Pre-conditions: none
  117. *   Affects: none.
  118. *   Depends: none.
  119. */
  120. void lcdPutc(char c)
  121. {
  122.     LCD_RS_IO=1;    // write character
  123.     lcdWrite(c);
  124. }
  125.  
  126.  
  127.  
  128. /*
  129. *   Function: lcdGoto
  130. *
  131. *   Input:  Position to goto
  132. *   Output: none
  133. *   Pre-conditions: none
  134. *   Affects: none.
  135. *   Depends: none.
  136. */
  137. void lcdGoto(BYTE pos)
  138. {
  139.     LCD_RS_IO = 0;
  140.     lcdWrite(0x80+pos);           // sets the DDRAM Address
  141. }
  142.  
  143.    
  144. /*
  145. *   Function: lcdInit
  146. *
  147. *   Input:  none.
  148. *   Output: none
  149. *   Pre-conditions: none
  150. *   Affects: none.
  151. *   Depends: Setup to use 4 bit mode
  152. */
  153. void lcdInit(void)
  154. {
  155.     // Setup IO (all output)
  156.     LCD_RS_TRIS = 0;
  157.     LCD_EN_TRIS = 0;
  158.     LCD_DATA_4_TRIS = 0;
  159.     LCD_DATA_5_TRIS = 0;
  160.     LCD_DATA_6_TRIS = 0;
  161.     LCD_DATA_7_TRIS = 0;
  162.  
  163.     LCD_RS_IO = 0;  // write control bytes
  164.     Delay10KTCYx(15);   // power on delay, 15ms
  165.     lcdWrite(0x3);  // attention!
  166.     Delay10KTCYx(5);        // 5ms
  167.     LCD_STROBE;
  168.     Delay100TCYx(10);   // 100us
  169.     LCD_STROBE;
  170.     Delay10KTCYx(5);        // 5ms
  171.     lcdWrite(0x2);  // set 4 bit mode
  172.     Delay100TCYx(4);    // 40 us
  173.     lcdWrite(0x28); // 4 bit mode, 1/16 duty, 5x8 font
  174.     lcdWrite(0x08); // display off
  175.     lcdWrite(0x0C); // display on, unblink curson off
  176.     lcdWrite(0x06); // entry mode
  177. }
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184. #endif
  185.  
  186.