Advertisement
Guest User

Untitled

a guest
Jan 29th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. /*
  2. * lcdput.c - emulate putchar for LCD display on DE2 board
  3. * Written by F Lundevall 2007-2014
  4. * Modified 2012-10-01 by F Lundevall: corrected TIMER_100U address
  5. * Modified 2014-06-25 by F Lundevall: added newline at end of file
  6. * Copyright abandoned. This file is in the public domain.
  7. */
  8. #define TIMER_100U ((volatile int *) 0x900 )
  9. #define LCD_CTRL ( (volatile int *) 0x808 )
  10. #define LCD_ROWSIZE (16)
  11. #define LCD_FUNCCODE (0x38)
  12.  
  13. static void lcd_write_instr( int icode )
  14. {
  15. while( (*LCD_CTRL & 0x80) == 0x80 ); /* busy, wait */
  16. *LCD_CTRL = (icode & 0xff); /* write */
  17. }
  18.  
  19. static void lcd_write_data( int c )
  20. {
  21. volatile int * p = LCD_CTRL;
  22. while( (*LCD_CTRL & 0x80) == 0x80 ); /* busy, wait */
  23. p[1] = c & 0xff;
  24. }
  25. /*
  26. * lcdinit - initialize LCD module
  27. */
  28. void lcdinit()
  29. {
  30. volatile int * p = TIMER_100U;
  31. int r;
  32.  
  33. p[1] = 6; /* continuous mode, no interrupt,
  34. plus redundant START bit */
  35. while( (p[0] & 1) == 0 ); /* wait for timeout */
  36. p[0] = 17; /* reset timeout flag */
  37.  
  38. /* initialization sequence, step 1:
  39. * wait for more than 15 ms after power-up */
  40. for( r = 0; r <= 15000 /* us */; r += 100 /* us per tick */)
  41. { while( (p[0] & 1) == 0 ); p[0] = 17; };
  42. *LCD_CTRL = LCD_FUNCCODE; /* Function Set */
  43.  
  44. /* initialization sequence, step 2:
  45. * wait for more than 4.1 ms, then Function Set again */
  46. for( r = 0; r <= 4100 /* us */; r += 100 /* us per tick */)
  47. { while( (p[0] & 1) == 0 ); p[0] = 17; };
  48. *LCD_CTRL = LCD_FUNCCODE; /* Function Set (again)*/
  49.  
  50. /* initialization sequence, step 3:
  51. * wait for more than 100 us, then Function Set again */
  52. for( r = 0; r <= 100 /* us */; r += 100 /* us per tick */)
  53. { while( (p[0] & 1) == 0 ); p[0] = 17; };
  54. *LCD_CTRL = LCD_FUNCCODE; /* Function Set (again)*/
  55.  
  56. /* initialization sequence, step 4, 5, 6, 7:
  57. * Function Set, display off, clear, entry mode */
  58. while( (p[0] & 1) == 0 ); p[0] = 17; /* wait just in case */
  59. lcd_write_instr( LCD_FUNCCODE ); /* Function Set (again!!!)*/
  60. lcd_write_instr( 0x80 ); /* display off */
  61. lcd_write_instr( 0x01 ); /* display off */
  62. lcd_write_instr( 0x06 ); /* cursor direction & shift */
  63. }
  64.  
  65. /*
  66. * lcdput - new version 2009.
  67. *
  68. * New features and bugfixes in this version:
  69. * 1) Checks for cursor moving beyond right edge of screen.
  70. * 2) Scrolls display when doing newline from lower row.
  71. *
  72. * Control codes:
  73. * 0x0a == '\n', line feed, move to the next line.
  74. * For this LCD driver, line feed has the side effect
  75. * that the cursor is reset to the beginning of the line.
  76. * 0x0d == '\r', carriage return, move to beginning of same line.
  77. * 0x0c == 12, form feed, move to upper left corner of display.
  78. *
  79. * Static (persistent) local variables:
  80. * onlowrow = 0 for upper (initial value), 1 for lower.
  81. * currentpos = 1 for left edge (initial value), 16 at right edge.
  82. * lowrow[] = contents of row 1, used for scrolling, origin 1.
  83. *
  84. * Temporary local variables:
  85. * i = loop counter.
  86. */
  87. void lcdput( int c )
  88. {
  89. static int onlowrow = 0;
  90. static int currentpos = 1;
  91. static char lowrow[17]; /* Need 17 elements since we use origin 1. */
  92.  
  93. int i;
  94.  
  95. c = (c & 0xff);
  96.  
  97. if( 13 == c ) /* Carriage Return, move to left edge on same row. */
  98. {
  99. if( onlowrow ) lcd_write_instr( 0xc0 ); /* Cursor to lower left corner. */
  100. else lcd_write_instr( 0x80 ); /* Cursor to upper left corner. */
  101. }
  102. else if( 12 == c ) /* Form-feed, clear display. */
  103. {
  104. onlowrow = 0;
  105. currentpos = 1;
  106. lcd_write_instr( 1 );
  107. lcd_write_instr( 0x80 ); /* Cursor to upper left corner. */
  108. }
  109. else if( 10 == c ) /* Newline. */
  110. {
  111. if( onlowrow ) /* We are on lower row already, scroll. */
  112. {
  113. lcd_write_instr( 1 ); /* Clear display. */
  114. lcd_write_instr( 0x80 ); /* Cursor to upper left corner. */
  115. for( i = 1; i <= 16; i += 1 )
  116. if( i < currentpos ) lcd_write_data( lowrow[ i ] );
  117. else lcd_write_data( ' ' );
  118. }
  119. lcd_write_instr( 0xc0 ); /* Cursor to lower left corner. */
  120. onlowrow = 1;
  121. currentpos = 1;
  122. }
  123. else if( onlowrow ) /* Send normal character to lower row. */
  124. {
  125. lowrow[ currentpos ] = c;
  126. lcd_write_data( c );
  127. currentpos += 1;
  128. if( currentpos > 16 ) /* Uh-oh, past the edge. Now scroll. */
  129. {
  130. lcd_write_instr( 1 ); /* Clear display. */
  131. lcd_write_instr( 0x80 ); /* Cursor to upper left corner. */
  132. for( i = 1; i <= 16; i += 1 )
  133. if( i < currentpos ) lcd_write_data( lowrow[ i ] );
  134. else lcd_write_data( ' ' );
  135. lcd_write_instr( 0xc0 ); /* Cursor to lower left corner. */
  136. currentpos = 1;
  137. }
  138. }
  139. else /* Send normal character to upper row. */
  140. {
  141. lcd_write_data( c );
  142. currentpos += 1;
  143. if( currentpos > 16 ) /* Uh-oh, past the edge. */
  144. {
  145. lcd_write_instr( 0xc0 ); /* Cursor to lower left corner. */
  146. onlowrow = 1;
  147. currentpos = 1;
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement