Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. //// ////
  2. //// lcd_getc(x,y) Returns character at position x,y on LCD ////
  3. //// ////
  4. ///////////////////////////////////////////////////////////////////////////
  5. //// (C) Copyright 1996,2007 Custom Computer Services ////
  6. //// This source code may only be used by licensed users of the CCS C ////
  7. //// compiler. This source code may only be distributed to other ////
  8. //// licensed users of the CCS C compiler. No other use, reproduction ////
  9. //// or distribution is permitted without written permission. ////
  10. //// Derivative programs created using this software in object code ////
  11. //// form are not restricted in any way. ////
  12. ///////////////////////////////////////////////////////////////////////////
  13.  
  14. // As defined in the following structure the pin connection is as follows:
  15. // D0 enable
  16. // D1 rs
  17. // D2 rw
  18. // D4 D4
  19. // D5 D5
  20. // D6 D6
  21. // D7 D7
  22. //
  23. // LCD pins D0-D3 are not used and PIC D3 is not used.
  24.  
  25. // Un-comment the following define to use port B
  26. //#define use_portb_lcd TRUE
  27.  
  28.  
  29. struct lcd_pin_map { // This structure is overlayed
  30. BOOLEAN enable; // on to an I/O port to gain
  31. BOOLEAN rs; // access to the LCD pins.
  32. BOOLEAN rw; // The bits are allocated from
  33. BOOLEAN unused; // low order up. ENABLE will
  34. int data : 4; // be pin B0.
  35. } lcd;
  36.  
  37.  
  38. #if defined use_portb_lcd
  39. #locate lcd = getenv("sfr:PORTB") // This puts the entire structure over the port
  40. #ifdef __pch__
  41. #locate lcd = 0xf81
  42. #else
  43. #locate lcd = 6
  44. #endif
  45. #define set_tris_lcd(x) set_tris_b(x)
  46. #else
  47. #locate lcd = getenv("sfr:PORTD") // This puts the entire structure over the port
  48. #ifdef __pch__
  49. #locate lcd = 0xf83
  50. #else
  51. #locate lcd = 8
  52. #endif
  53. #define set_tris_lcd(x) set_tris_d(x)
  54. #endif
  55.  
  56. #ifndef lcd_type
  57. #define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
  58. #endif
  59.  
  60. #define lcd_line_two 0x40 // LCD RAM address for the second line
  61.  
  62.  
  63. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  64. // These bytes need to be sent to the LCD
  65. // to start it up.
  66.  
  67.  
  68. // The following are used for setting
  69. // the I/O port direction register.
  70.  
  71. struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
  72. struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
  73.  
  74.  
  75.  
  76. BYTE lcd_read_byte() {
  77. BYTE low,high;
  78. set_tris_lcd(LCD_READ); //ERROR
  79. lcd.rw = 1;
  80. delay_cycles(1);
  81. lcd.enable = 1;
  82. delay_cycles(1);
  83. high = lcd.data;
  84. lcd.enable = 0;
  85. delay_cycles(1);
  86. lcd.enable = 1;
  87. delay_us(1);
  88. low = lcd.data;
  89. lcd.enable = 0;
  90. set_tris_lcd(LCD_WRITE); //ERROR
  91. return( (high<<4) | low);
  92. }
  93.  
  94.  
  95. void lcd_send_nibble( BYTE n ) {
  96. lcd.data = n;
  97. delay_cycles(1);
  98. lcd.enable = 1;
  99. delay_us(2);
  100. lcd.enable = 0;
  101. }
  102.  
  103.  
  104. void lcd_send_byte( BYTE address, BYTE n ) {
  105.  
  106. lcd.rs = 0;
  107. while ( bit_test(lcd_read_byte(),7) ) ;
  108. lcd.rs = address;
  109. delay_cycles(1);
  110. lcd.rw = 0;
  111. delay_cycles(1);
  112. lcd.enable = 0;
  113. lcd_send_nibble(n >> 4);
  114. lcd_send_nibble(n & 0xf);
  115. }
  116.  
  117.  
  118. void lcd_init() {
  119. BYTE i;
  120. set_tris_lcd(LCD_WRITE);
  121. lcd.rs = 0;
  122. lcd.rw = 0;
  123. lcd.enable = 0;
  124. delay_ms(15);
  125. for(i=1;i<=3;++i) {
  126. lcd_send_nibble(3);
  127. delay_ms(5);
  128. }
  129. lcd_send_nibble(2);
  130. for(i=0;i<=3;++i)
  131. lcd_send_byte(0,LCD_INIT_STRING[i]);
  132. }
  133.  
  134.  
  135. void lcd_gotoxy( BYTE x, BYTE y) {
  136. BYTE address;
  137.  
  138. if(y!=1)
  139. address=lcd_line_two;
  140. else
  141. address=0;
  142. address+=x-1;
  143. lcd_send_byte(0,0x80|address);
  144. }
  145.  
  146. void lcd_putc( char c) {
  147. switch (c) {
  148. case '\f' : lcd_send_byte(0,1);
  149. delay_ms(2);
  150. break;
  151. case '\n' : lcd_gotoxy(1,2); break;
  152. case '\b' : lcd_send_byte(0,0x10); break;
  153. default : lcd_send_byte(1,c); break;
  154. }
  155. }
  156.  
  157. char lcd_getc( BYTE x, BYTE y) {
  158. char value;
  159.  
  160. lcd_gotoxy(x,y);
  161. while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  162. lcd.rs=1;
  163. value = lcd_read_byte();
  164. lcd.rs=0;
  165. return(value);
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement