Elettronica -> LCD-PIC - www.robot-home.it -

 

Sunday 01st August, 2010 - 11.50:41 -

 

Site Index

-- Forum --

:: Home page ::
:: Progetto ::
:: Controllo ::
:: Codice ::
:: Elettronica ::
:: Struttura ::
:: Media ::
:: Links ::
:: Chi sono? ::

Forum news
relatà virtuale
Inviato il 20/6/10 - ore: 23:06
da: Myzhar
sul Forum: Visione Artificiale

Triangolazione
Inviato il 15/6/10 - ore: 15:06
da: pasquale.marinaro
sul Forum: Visione Artificiale

[Tutorial] Corso online sul Machine Learning
Inviato il 18/5/10 - ore: 11:05
da: Myzhar
sul Forum: Intelligenza artificiale

che sensore è questo?
Inviato il 17/5/10 - ore: 16:05
da: rmfalco89
sul Forum: Strumentazione Hardware

robocup 2010
Inviato il 27/4/10 - ore: 16:04
da: Myzhar
sul Forum: Link e news dal mondo della robotica

Sponsor TradeDoubler
Support the project
Ti sono stato utile?
Offrimi un caffè
Marchio di accettazione
Sponsor SoloDomini
LCD-PIC

Il display LDC della PicDem2 Plus: gestione

Per chi volesse discutere l'argomento oppure riscontrasse delle imperfezioni o degli errori
è disponibile un apposito thread sul forum del sito:
vai al forum

Questo tutorial discute la gestione del display LCD 16x2 assemblato sulla Demoboard Microchip PICDEM2 Plus e propone una modifica al codice fornito dalla Microchip con la Demoboard per gestire l'attivazione del display.
Il codice può essere utilizzato con qualsiasi display dotato di controller standard compatibile con l'HD44780.

Il codice di seguito fornito si basa sul seguente schema di collegamento del display:

Il display è pilotato con bus parallelo e le linee dati sono collegate alla porta RD del PIC (RD0-RD3). Il codice è realizzato in modo che il diplay possa essere collegato alla porta B per microcontrollori non dotati di porta D.
Fondamentale è l'"interruttore" controllato tramite il pin RD7. Tale "interruttore" permette di accendere e spegnere il display ed è realizzato con un semplice transistor NPN MMBT2222A o equivalente. Il pin RD6 attiva o disattiva il display, RD5 permette di indicare se il bus è utilizzato in lettura o scrittura, RD4 permette di resettare il display

Di seguito il codice per la gestione dell'LCD. Il codice è stato testato su un PIC 16F877A con clock a 20Mhz e compilato con compilatore Hitec PICC, ma può essere adattatofacilmente ad altri compilatori o altri microcontrollori con oscillatori differenti:
Codice
  1.  
  2. ///////////////////////////////////////////////////////////////////////////
  3. //// LCD_PICDEM_Plus.C ////
  4. //// Driver for common LCD mounted on PICDEM 2 Plus ////
  5. //// ////
  6. //// lcd_init() Must be called before any other function. ////
  7. //// ////
  8. //// lcd_putc(c) Will display c on the next position of the LCD. ////
  9. //// The following have special meaning: ////
  10. //// f Clear display ////
  11. //// n Go to start of second line ////
  12. //// b Move back one position ////
  13. //// ////
  14. //// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
  15. //// ////
  16. //// lcd_getc(x,y) Returns character at position x,y on LCD ////
  17. //// ////
  18. ///////////////////////////////////////////////////////////////////////////
  19. // ////
  20. // 29 Settembre 2007: Modificato da Walter Lucetti per l'utilizzo ////
  21. // su PicDem 2 Plus ////
  22. // ////
  23. ///////////////////////////////////////////////////////////////////////////
  24.  
  25.  
  26. // As defined in the following structure the pin connection is as follows:
  27. // D0 data0
  28. // D1 data1
  29. // D2 data2
  30. // D3 data3
  31. // D4 reset
  32. // D5 rw
  33. // D6 enable
  34. // D7 power
  35. //
  36.  
  37.  
  38. // Un-comment the following define to use port B
  39. // #define use_portb_lcd TRUE
  40.  
  41.  
  42. struct lcd_pin_map {
  43. int data : 4;
  44. BOOLEAN rs;
  45. BOOLEAN rw;
  46. BOOLEAN enable;
  47. BOOLEAN power; // Added by Walter Lucetti
  48. } lcd;
  49.  
  50.  
  51. #if defined use_portb_lcd
  52. #locate lcd = getenv("sfr:PORTB") // This puts the entire structure over the port
  53. #define set_tris_lcd(x) set_tris_b(x)
  54. #else
  55. #locate lcd = getenv("sfr:PORTD") // This puts the entire structure over the port
  56. #define set_tris_lcd(x) set_tris_d(x)
  57. #endif
  58.  
  59.  
  60. #define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
  61. #define lcd_line_two 0x40 // LCD RAM address for the second line
  62.  
  63.  
  64. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  65. // These bytes need to be sent to the LCD
  66. // to start it up.
  67.  
  68.  
  69. // The following are used for setting
  70. // the I/O port direction register.
  71.  
  72. struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
  73. struct lcd_pin_map const LCD_READ = {15,0,0,0,0}; // For read mode data pins are in
  74.  
  75.  
  76.  
  77. BYTE lcd_read_byte() {
  78. BYTE low,high;
  79. set_tris_lcd(LCD_READ);
  80. lcd.rw = 1;
  81. delay_cycles(1);
  82. lcd.enable = 1;
  83. delay_cycles(1);
  84. high = lcd.data;
  85. lcd.enable = 0;
  86. delay_cycles(1);
  87. lcd.enable = 1;
  88. delay_us(1);
  89. low = lcd.data;
  90. lcd.enable = 0;
  91. set_tris_lcd(LCD_WRITE);
  92. return( (high<<4) | low);
  93. }
  94.  
  95.  
  96. void lcd_send_nibble( BYTE n ) {
  97. lcd.data = n;
  98. delay_cycles(1);
  99. lcd.enable = 1;
  100. delay_us(2);
  101. lcd.enable = 0;
  102. }
  103.  
  104.  
  105. void lcd_send_byte( BYTE address, BYTE n ) {
  106.  
  107. lcd.rs = 0;
  108. while ( bit_test(lcd_read_byte(),7) ) ;
  109. lcd.rs = address;
  110. delay_cycles(1);
  111. lcd.rw = 0;
  112. delay_cycles(1);
  113. lcd.enable = 0;
  114. lcd_send_nibble(n >> 4);
  115. lcd_send_nibble(n & 0xf);
  116. }
  117.  
  118.  
  119. void lcd_init() {
  120. BYTE i;
  121. lcd.power = TRUE; // Added by Walter Lucetti
  122. delay_ms( 5 );
  123. set_tris_lcd(LCD_WRITE);
  124. lcd.rs = 0;
  125. lcd.rw = 0;
  126. lcd.enable = 0;
  127. delay_ms(15);
  128. for(i=1;i<=3;++i) {
  129. lcd_send_nibble(3);
  130. delay_ms(5);
  131. }
  132. lcd_send_nibble(2);
  133. for(i=0;i<=3;++i)
  134. lcd_send_byte(0,LCD_INIT_STRING[i]);
  135. }
  136.  
  137.  
  138. void lcd_gotoxy( BYTE x, BYTE y) {
  139. BYTE address;
  140.  
  141. if(y!=1)
  142. address=lcd_line_two;
  143. else
  144. address=0;
  145. address+=x-1;
  146. lcd_send_byte(0,0x80|address);
  147. }
  148.  
  149. void lcd_putc( char c) {
  150. switch (c) {
  151. case 'f' : lcd_send_byte(0,1);
  152. delay_ms(2);
  153. break;
  154. case 'n' : lcd_gotoxy(1,2); break;
  155. case 'b' : lcd_send_byte(0,0x10); break;
  156. default : lcd_send_byte(1,c); break;
  157. }
  158. }
  159.  
  160. char lcd_getc( BYTE x, BYTE y) {
  161. char value;
  162.  
  163. lcd_gotoxy(x,y);
  164. while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  165. lcd.rs=1;
  166. value = lcd_read_byte();
  167. lcd.rs=0;
  168. return(value);
  169. }
  170.  
Le funzioni fornite dal driver sono poche e semplici da utilizzare:
- lcd_init(): deve essere chiamata prima di ogni comando per inizializzare il display.
- lcd_putc(c): mostra il carattere 'c' alla posizione successiva a quella corrente del cursore.
             Esistono dei caratteri speciali:
                      - f: pulisce lo schermo
                      - n: va a capo sulla seconda riga
                      - b: torna indietro di una posizione
        
- lcd_gotoxy(x,y): muove il cursore in posizione (x,y).
- lcd_getc(x,y): ritorna il carattere in posizione (x,y).

Come sempre commenti e suggerimenti saranno ben apprezzati... il forum è a vostra disposizione.


Il tutorial ti è stato utile?
Offrimi un caffè, sarò felice di realizzarne di migliori.
Marchio di accettazione
Sponsor Criteo
Sponsor Heyos
Copyright 2006© - Everyone can use the contents of these pages in the way he thinks better. Webmaster Myzhar. Site Admin