Autor Tema: Como trabajar con LCD en xc8  (Leído 7539 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado electroipod

  • PIC16
  • ***
  • Mensajes: 166
Como trabajar con LCD en xc8
« en: 06 de Febrero de 2013, 16:22:34 »
Hola a todos, he estado practicando con el compilador xc8, se como trabajar con el tmr0, interrupciones, adc, y otras cositas pero no encuentro tutoriales de como trabajar con un lcd, alguien de ustedes sabra como trabajar con un lcd?.

Desconectado Palomino86

  • PIC16
  • ***
  • Mensajes: 180
Re: Como trabajar con LCD en xc8
« Respuesta #1 en: 06 de Febrero de 2013, 21:15:40 »
Que tipo de comunicación maneja el LCD? Serie o paralelo? y para que pic es?
Lo que no me destruye, me fortalece

Desconectado todopic

  • Administrador
  • DsPIC30
  • *******
  • Mensajes: 3495
    • http://www.todopicelectronica.com.ar
Re: Como trabajar con LCD en xc8
« Respuesta #2 en: 06 de Febrero de 2013, 22:39:27 »
Hola, estoy realizando un curso de C... así que te paso un ejemplo, y el archivo de control de LCD...

Código: C
  1. #include <xc.h>
  2. #define _XTAL_FREQ 4000000
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6.  
  7. #include "lcd_pic16.c"
  8.  
  9. // PIC16F887 Configuration Bit Settings
  10.  
  11.  
  12. // CONFIG1
  13. #pragma config FOSC =XT        // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
  14. #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
  15. #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
  16. #pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
  17. #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
  18. #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
  19. #pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR enabled)
  20. #pragma config IESO = OFF        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
  21. #pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
  22. #pragma config LVP = OFF         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
  23.  
  24. // CONFIG2
  25. #pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
  26. #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
  27.  
  28.  
  29. //variables globales
  30. unsigned char buffer[20],i;
  31. unsigned int contador=0;
  32. //funciones prototipo
  33. void init(void);
  34.  
  35.  
  36. void main(void)
  37. {
  38.  
  39.         init();
  40. while(1)
  41.         {
  42.             for(i=0;i<18;i++)
  43.             {
  44.    
  45.                 SetDDRamAddr(0x00);
  46.                 putrsXLCD("Conta= ");
  47.                 sprintf(buffer,"%05u",contador);
  48.                 putsXLCD(buffer);
  49.                 SetDDRamAddr(0x40+i);
  50.                 putrsXLCD(" prueba 887 ");
  51.                 __delay_ms(200);
  52.           //      CLS_Line2();
  53.                
  54.                 contador=contador +1;
  55.                  if (i==2)
  56.                 {  RB0=1;
  57.                     RB7=0;
  58.                 }
  59.                 if (i==4)
  60.                 {   RB1=1;
  61.                     RB0=0;
  62.                 }
  63.  
  64.                 if (i==6)
  65.                 {    RB2=1;
  66.                     RB1=0;
  67.                 }
  68.               if (i==8)
  69.               {  RB3=1;
  70.                     RB2=0;
  71.               }
  72.  
  73.                if (i==10)
  74.                {     RB4=1;
  75.                     RB3=0;
  76.                }
  77.                if (i==12)
  78.                {    RB5=1;
  79.                     RB4=0;
  80.                }
  81.                if (i==14)
  82.                {    RB6=1;
  83.                     RB5=0;
  84.                }
  85.  
  86.                 if (i==16)
  87.                 {   RB7=1;
  88.                     RB6=0;
  89.                 }
  90.             }
  91.  
  92.         }
  93. }
  94.  
  95. void init(void)
  96. {
  97.     //configuramos el oscilador
  98.  
  99.  
  100.     ANSEL=0;   //todo el PORT como digital
  101.    ANSELH=0;   //todo el PORT como digital
  102.  
  103.     TRISB=0;    //todo el PORTB como salidas digitales
  104.     PORTB=0;
  105.     OpenXLCD(FOUR_BIT & LINES_5X7 );
  106.  
  107.    
  108. }
  109. /*void mover_led (void)
  110. {            if (i==2)
  111.                 {  RB0=1;
  112.                     RB7=0;
  113.                 }
  114.                 if (i==4)
  115.                 {   RB1=1;
  116.                     RB0=0;
  117.                 }
  118.  
  119.                 if (i==6)
  120.                 {    RB2=1;
  121.                     RB1=0;
  122.                 }
  123.               if (i==8)
  124.               {  RB3=1;
  125.                     RB2=0;
  126.               }
  127.  
  128.                if (i==10)
  129.                {     RB4=1;
  130.                     RB3=0;
  131.                }
  132.                if (i==12)
  133.                {    RB5=1;
  134.                     RB4=0;
  135.                }
  136.                if (i==14)
  137.                {    RB6=1;
  138.                     RB5=0;
  139.                }
  140.  
  141.                 if (i==16)
  142.                 {   RB7=1;
  143.                     RB6=0;
  144.                 }
  145. }*/

este es una aplicacion, que con 2 pulsadores, te incrementa o decrementa el estado, según un estado...

y este es el archivo que debes incluir

lcd_pic16.c
Código: C
  1. /*
  2.  *
  3.  *   Notes:
  4.  *      - These libraries routines are written to support the
  5.  *        Hitachi HD44780 LCD controller.
  6.  *      - The user must define the following items:
  7.  *          - The LCD interface type (4- or 8-bits)
  8.  *          - If 4-bit mode
  9.  *              - whether using the upper or lower nibble
  10.  *          - The data port
  11.  *              - The tris register for data port
  12.  *              - The control signal ports and pins
  13.  *              - The control signal port tris and pins
  14.  *          - The user must provide three delay routines:
  15.  *              - DelayFor18TCY() provides a 18 Tcy delay
  16.  *              - DelayPORXLCD() provides at least 15ms delay
  17.  *              - DelayXLCD() provides at least 5ms delay
  18.  */
  19.  
  20. /* Interface type 8-bit or 4-bit
  21.  * For 8-bit operation uncomment the #define BIT8
  22.  */
  23. // #define BIT8
  24.  
  25. /* When in 4-bit interface define if the data is in the upper
  26.  * or lower nibble.  For lower nibble, comment the #define UPPER
  27.  */
  28.  #define UPPER
  29.  
  30. /* When in 6-10-bit interface comment the #define BUSY_LCD and conected PIN
  31.  * R/W to GND in LCD
  32.  */
  33.  //#define BUSY_LCD
  34.  
  35. /* DATA_PORT defines the port to which the LCD data lines are connected */
  36.  #define DATA_PORT                      PORTD
  37.  #define TRIS_DATA_PORT                 TRISD
  38.  
  39. /* CTRL_PORT defines the port where the control lines are connected.
  40.  * These are just samples, change to match your application.
  41.  */
  42.  #define RW_PIN   PORTEbits.RE0                 /* PORT for RW */
  43.  #define TRIS_RW  TRISEbits.TRISE0      /* TRIS for RW */
  44.  
  45.  #define RS_PIN   PORTEbits.RE1                 /* PORT for RS */
  46.  #define TRIS_RS  TRISEbits.TRISE1      /* TRIS for RS */
  47.  
  48.  #define E_PIN    PORTEbits.RE2                 /* PORT for D  */
  49.  #define TRIS_E   TRISEbits.TRISE2      /* TRIS for E  */
  50.  
  51. /* Display ON/OFF Control defines */
  52. #define DON         0b00001111  /* Display on      */
  53. #define DOFF        0b00001011  /* Display off     */
  54. #define CURSOR_ON   0b00001111  /* Cursor on       */
  55. #define CURSOR_OFF  0b00001101  /* Cursor off      */
  56. #define BLINK_ON    0b00001111  /* Cursor Blink    */
  57. #define BLINK_OFF   0b00001110  /* Cursor No Blink */
  58.  
  59. /* Cursor or Display Shift defines */
  60. #define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
  61. #define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
  62. #define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
  63. #define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */
  64.  
  65. /* Function Set defines */
  66. #define FOUR_BIT   0b00101100  /* 4-bit Interface               */
  67. #define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
  68. #define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
  69. #define LINE_5X10  0b00110100  /* 5x10 characters               */
  70. #define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */
  71.  
  72. #ifdef _OMNI_CODE_
  73. #define PARAM_SCLASS
  74. #else
  75. #define PARAM_SCLASS auto
  76. #endif
  77.  
  78. /* CLS_Line2
  79.  * Clear Screen Line 2
  80.  */
  81. void CLS_Line2(void);
  82.  
  83. /* CLS_Line1
  84.  * Clear Screen Line 1
  85.  */
  86. void CLS_Line1(void);
  87. /* OpenXLCD
  88.  * Configures I/O pins for external LCD
  89.  */
  90. void OpenXLCD(PARAM_SCLASS unsigned char);
  91.  
  92. /* SetCGRamAddr
  93.  * Sets the character generator address
  94.  */
  95. void SetCGRamAddr(PARAM_SCLASS unsigned char);
  96.  
  97. /* SetDDRamAddr
  98.  * Sets the display data address
  99.  */
  100. void SetDDRamAddr(PARAM_SCLASS unsigned char);
  101.  
  102. /* BusyXLCD
  103.  * Returns the busy status of the LCD
  104.  */
  105. unsigned char BusyXLCD(void);
  106.  
  107. /* ReadAddrXLCD
  108.  * Reads the current address
  109.  */
  110. unsigned char ReadAddrXLCD(void);
  111.  
  112. /* ReadDataXLCD
  113.  * Reads a byte of data
  114.  */
  115. char ReadDataXLCD(void);
  116.  
  117. /* WriteCmdXLCD
  118.  * Writes a command to the LCD
  119.  */
  120. void WriteCmdXLCD(PARAM_SCLASS unsigned char);
  121.  
  122. /* WriteDataXLCD
  123.  * Writes a data byte to the LCD
  124.  */
  125. void WriteDataXLCD(PARAM_SCLASS char);
  126.  
  127. /* putcXLCD
  128.  * A putc is a write
  129.  */
  130. #define putcXLCD WriteDataXLCD
  131.  
  132. /* putsXLCD
  133.  * Writes a string of characters to the LCD
  134.  */
  135. void putsXLCD(PARAM_SCLASS char *);
  136.  
  137. /* putrsXLCD
  138.  * Writes a string of characters in  to the LCD
  139.  */
  140. void putrsXLCD(const char *);
  141.  
  142. // Rutinas de tiempo auxiliares para la libreria XLCD
  143. void DelayFor18TCY(void)
  144. {
  145.         __delay_us(18);
  146. }
  147.  
  148. void DelayPORXLCD(void)
  149. {
  150.         __delay_ms(20); //Delay de 15 ms
  151. }
  152.  
  153. void DelayXLCD(void)
  154. {
  155.         __delay_ms(20); //Delay de 20 ms
  156. }
  157.  
  158. void CLS_Line2(void)
  159. {
  160.     SetDDRamAddr(0x40);
  161.     putrsXLCD("                ");
  162. }
  163.  
  164. void CLS_Line1(void)
  165. {
  166.     SetDDRamAddr(0x00);
  167.     putrsXLCD("                ");
  168. }
  169. /********************************************************************
  170. *       Function Name:  OpenXLCD                                    *
  171. *       Return Value:   void                                        *
  172. *       Parameters:     lcdtype: sets the type of LCD (lines)       *
  173. *       Description:    This routine configures the LCD. Based on   *
  174. *                       the Hitachi HD44780 LCD controller. The     *
  175. *                       routine will configure the I/O pins of the  *
  176. *                       microcontroller, setup the LCD for 4- or    *
  177. *                       8-bit mode and clear the display. The user  *
  178. *                       must provide three delay routines:          *
  179. *                       DelayFor18TCY() provides a 18 Tcy delay     *
  180. *                       DelayPORXLCD() provides at least 15ms delay *
  181. *                       DelayXLCD() provides at least 5ms delay     *
  182. ********************************************************************/
  183. void OpenXLCD(unsigned char lcdtype)
  184. {
  185.         // The data bits must be either a 8-bit port or the upper or
  186.         // lower 4-bits of a port. These pins are made into inputs
  187. #ifdef BIT8                             // 8-bit mode, use whole port
  188.         DATA_PORT = 0;
  189.         TRIS_DATA_PORT = 0x00;
  190. #else                                   // 4-bit mode
  191. #ifdef UPPER                            // Upper 4-bits of the port
  192.         DATA_PORT &= 0x0f;
  193.         TRIS_DATA_PORT &= 0x0F;
  194. #else                                   // Lower 4-bits of the port
  195.         DATA_PORT &= 0xf0;
  196.         TRIS_DATA_PORT &= 0xF0;
  197. #endif
  198. #endif
  199.         TRIS_RW = 0;                    // All control signals made outputs
  200.         TRIS_RS = 0;
  201.         TRIS_E = 0;
  202.         RW_PIN = 0;                     // R/W pin made low
  203.         RS_PIN = 0;                     // Register select pin made low
  204.         E_PIN = 0;                      // Clock pin made low
  205.  
  206.         // Delay for 15ms to allow for LCD Power on reset
  207.         DelayPORXLCD();
  208.  //-------------------reset procedure through software----------------------      
  209.                  WriteCmdXLCD(0x30);
  210.                         __delay_ms(5);
  211.  
  212.                  WriteCmdXLCD(0x30);
  213.                         __delay_ms(1);
  214.  
  215.  
  216.                  WriteCmdXLCD(0x32);
  217.                 while( BusyXLCD() );
  218. //------------------------------------------------------------------------------------------
  219.  
  220.  
  221.         // Set data interface width, # lines, font
  222.         while(BusyXLCD());              // Wait if LCD busy
  223.         WriteCmdXLCD(lcdtype);          // Function set cmd
  224.  
  225.         // Turn the display on then off
  226.         while(BusyXLCD());              // Wait if LCD busy
  227.         WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF);        // Display OFF/Blink OFF
  228.         while(BusyXLCD());              // Wait if LCD busy
  229.         WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON);           // Display ON/Blink ON
  230.  
  231.         // Clear display
  232.         while(BusyXLCD());              // Wait if LCD busy
  233.         WriteCmdXLCD(0x01);             // Clear display
  234.  
  235.         // Set entry mode inc, no shift
  236.         while(BusyXLCD());              // Wait if LCD busy
  237.         WriteCmdXLCD(SHIFT_CUR_RIGHT);   // Entry Mode
  238.                
  239.         while(BusyXLCD());              // Wait if LCD busy
  240.         WriteCmdXLCD(0x06);             // Incremente
  241.  
  242.         while(BusyXLCD());              // Wait if LCD busy
  243.         SetDDRamAddr(0x80);             // Set Display data ram address to 0
  244.        
  245.         while(BusyXLCD());              // Wait if LCD busy
  246.         WriteCmdXLCD(CURSOR_OFF);       // Cursor OFF
  247.         return;
  248. }
  249.  
  250.  
  251. /********************************************************************
  252. *       Function Name:  WriteDataXLCD                               *
  253. *       Return Value:   void                                        *
  254. *       Parameters:     data: data byte to be written to LCD        *
  255. *       Description:    This routine writes a data byte to the      *
  256. *                       Hitachi HD44780 LCD controller. The user    *
  257. *                       must check to see if the LCD controller is  *
  258. *                       busy before calling this routine. The data  *
  259. *                       is written to the character generator RAM or*
  260. *                       the display data RAM depending on what the  *
  261. *                       previous SetxxRamAddr routine was called.   *
  262. ********************************************************************/
  263. void WriteDataXLCD(char data)
  264. {
  265. #ifdef BIT8                             // 8-bit interface
  266.         TRIS_DATA_PORT = 0;             // Make port output
  267.         DATA_PORT = data;               // Write data to port
  268.         RS_PIN = 1;                     // Set control bits
  269.         RW_PIN = 0;
  270.         DelayFor18TCY();
  271.         E_PIN = 1;                      // Clock data into LCD
  272.         DelayFor18TCY();
  273.         E_PIN = 0;
  274.         RS_PIN = 0;                     // Reset control bits
  275.         TRIS_DATA_PORT = 0xff;          // Make port input
  276. #else                                   // 4-bit interface
  277. #ifdef UPPER                            // Upper nibble interface
  278.         TRIS_DATA_PORT &= 0x0f;
  279.         DATA_PORT &= 0x0f;
  280.         DATA_PORT |= data&0xf0;
  281. #else                                   // Lower nibble interface
  282.         TRIS_DATA_PORT &= 0xf0;
  283.         DATA_PORT &= 0xf0;
  284.         DATA_PORT |= ((data>>4)&0x0f);
  285. #endif
  286.         RS_PIN = 1;                     // Set control bits
  287.         RW_PIN = 0;
  288.         DelayFor18TCY();
  289.         E_PIN = 1;                      // Clock nibble into LCD
  290.         DelayFor18TCY();
  291.         E_PIN = 0;
  292. #ifdef UPPER                            // Upper nibble interface
  293.         DATA_PORT &= 0x0f;
  294.         DATA_PORT |= ((data<<4)&0xf0);
  295. #else                                   // Lower nibble interface
  296.         DATA_PORT &= 0xf0;
  297.         DATA_PORT |= (data&0x0f);
  298. #endif
  299.         DelayFor18TCY();
  300.         E_PIN = 1;                      // Clock nibble into LCD
  301.         DelayFor18TCY();
  302.         E_PIN = 0;
  303. #ifdef UPPER                            // Upper nibble interface
  304.         TRIS_DATA_PORT |= 0xf0;
  305. #else                                   // Lower nibble interface
  306.         TRIS_DATA_PORT |= 0x0f;
  307. #endif
  308. #endif
  309.         return;
  310. }
  311.  
  312. /********************************************************************
  313. *       Function Name:  WriteCmdXLCD                                *
  314. *       Return Value:   void                                        *
  315. *       Parameters:     cmd: command to send to LCD                 *
  316. *       Description:    This routine writes a command to the Hitachi*
  317. *                       HD44780 LCD controller. The user must check *
  318. *                       to see if the LCD controller is busy before *
  319. *                       calling this routine.                       *
  320. ********************************************************************/
  321. void WriteCmdXLCD(unsigned char cmd)
  322. {
  323. #ifdef BIT8                             // 8-bit interface
  324.         TRIS_DATA_PORT = 0;             // Data port output
  325.         DATA_PORT = cmd;                // Write command to data port
  326.         RW_PIN = 0;                     // Set the control signals
  327.         RS_PIN = 0;                     // for sending a command
  328.         DelayFor18TCY();
  329.         E_PIN = 1;                      // Clock the command in
  330.         DelayFor18TCY();
  331.         E_PIN = 0;
  332.         DelayFor18TCY();
  333.         TRIS_DATA_PORT = 0xff;          // Data port input
  334. #else                                   // 4-bit interface
  335. #ifdef UPPER                            // Upper nibble interface
  336.         TRIS_DATA_PORT &= 0x0f;
  337.         DATA_PORT &= 0x0f;
  338.         DATA_PORT |= cmd&0xf0;
  339. #else                                   // Lower nibble interface
  340.         TRIS_DATA_PORT &= 0xf0;
  341.         DATA_PORT &= 0xf0;
  342.         DATA_PORT |= (cmd>>4)&0x0f;
  343. #endif
  344.         RW_PIN = 0;                     // Set control signals for command
  345.         RS_PIN = 0;
  346.         DelayFor18TCY();
  347.         E_PIN = 1;                      // Clock command in
  348.         DelayFor18TCY();
  349.         E_PIN = 0;
  350. #ifdef UPPER                            // Upper nibble interface
  351.         DATA_PORT &= 0x0f;
  352.         DATA_PORT |= (cmd<<4)&0xf0;
  353. #else                                   // Lower nibble interface
  354.         DATA_PORT &= 0xf0;
  355.         DATA_PORT |= cmd&0x0f;
  356. #endif
  357.         DelayFor18TCY();
  358.         E_PIN = 1;                      // Clock command in
  359.         DelayFor18TCY();
  360.         E_PIN = 0;
  361. #ifdef UPPER                            // Make data nibble input
  362.         TRIS_DATA_PORT |= 0xf0;
  363. #else
  364.         TRIS_DATA_PORT |= 0x0f;
  365. #endif
  366. #endif
  367.         return;
  368. }
  369.  
  370. /********************************************************************
  371. *       Function Name:  SetDDRamAddr                                *
  372. *       Return Value:   void                                        *
  373. *       Parameters:     CGaddr: display data address                *
  374. *       Description:    This routine sets the display data address  *
  375. *                       of the Hitachi HD44780 LCD controller. The  *
  376. *                       user must check to see if the LCD controller*
  377. *                       is busy before calling this routine.        *
  378. ********************************************************************/
  379. void SetDDRamAddr(unsigned char DDaddr)
  380. {
  381. #ifdef BIT8                                     // 8-bit interface
  382.         TRIS_DATA_PORT = 0;                     // Make port output
  383.         DATA_PORT = DDaddr | 0b10000000;        // Write cmd and address to port
  384.         RW_PIN = 0;                             // Set the control bits
  385.         RS_PIN = 0;
  386.         DelayFor18TCY();
  387.         E_PIN = 1;                              // Clock the cmd and address in
  388.         DelayFor18TCY();
  389.         E_PIN = 0;
  390.         DelayFor18TCY();
  391.         TRIS_DATA_PORT = 0xff;                  // Make port input
  392. #else                                           // 4-bit interface
  393. #ifdef UPPER                                    // Upper nibble  interface
  394.         TRIS_DATA_PORT &= 0x0f;                 // Make port output
  395.         DATA_PORT &= 0x0f;                      // and write upper nibble
  396.         DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
  397. #else                                           // Lower nibble interface
  398.         TRIS_DATA_PORT &= 0xf0;                 // Make port output
  399.         DATA_PORT &= 0xf0;                      // and write upper nibble
  400.         DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
  401. #endif
  402.         RW_PIN = 0;                             // Set control bits
  403.         RS_PIN = 0;
  404.         DelayFor18TCY();
  405.         E_PIN = 1;                              // Clock the cmd and address in
  406.         DelayFor18TCY();
  407.         E_PIN = 0;
  408. #ifdef UPPER                                    // Upper nibble interface
  409.         DATA_PORT &= 0x0f;                      // Write lower nibble
  410.         DATA_PORT |= ((DDaddr<<4)&0xf0);
  411. #else                                           // Lower nibble interface
  412.         DATA_PORT &= 0xf0;                      // Write lower nibble
  413.         DATA_PORT |= (DDaddr&0x0f);
  414. #endif
  415.         DelayFor18TCY();
  416.         E_PIN = 1;                              // Clock the cmd and address in
  417.         DelayFor18TCY();
  418.         E_PIN = 0;
  419. #ifdef UPPER                                    // Upper nibble interface
  420.         TRIS_DATA_PORT |= 0xf0;                 // Make port input
  421. #else                                           // Lower nibble interface
  422.         TRIS_DATA_PORT |= 0x0f;                 // Make port input
  423. #endif
  424. #endif
  425.         return;
  426. }
  427.  
  428. /********************************************************************
  429. *       Function Name:  SetCGRamAddr                                *
  430. *       Return Value:   void                                        *
  431. *       Parameters:     CGaddr: character generator ram address     *
  432. *       Description:    This routine sets the character generator   *
  433. *                       address of the Hitachi HD44780 LCD          *
  434. *                       controller. The user must check to see if   *
  435. *                       the LCD controller is busy before calling   *
  436. *                       this routine.                               *
  437. ********************************************************************/
  438. void SetCGRamAddr(unsigned char CGaddr)
  439. {
  440. #ifdef BIT8                                     // 8-bit interface
  441.         TRIS_DATA_PORT = 0;                     // Make data port ouput
  442.         DATA_PORT = CGaddr | 0b01000000;        // Write cmd and address to port
  443.         RW_PIN = 0;                             // Set control signals
  444.         RS_PIN = 0;
  445.         DelayFor18TCY();
  446.         E_PIN = 1;                              // Clock cmd and address in
  447.         DelayFor18TCY();
  448.         E_PIN = 0;
  449.         DelayFor18TCY();
  450.         TRIS_DATA_PORT = 0xff;                  // Make data port inputs
  451. #else                                           // 4-bit interface
  452. #ifdef UPPER                                    // Upper nibble interface
  453.         TRIS_DATA_PORT &= 0x0f;                 // Make nibble input
  454.         DATA_PORT &= 0x0f;                      // and write upper nibble
  455.         DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
  456. #else                                           // Lower nibble interface
  457.         TRIS_DATA_PORT &= 0xf0;                 // Make nibble input
  458.         DATA_PORT &= 0xf0;                      // and write upper nibble
  459.         DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
  460. #endif
  461.         RW_PIN = 0;                             // Set control signals
  462.         RS_PIN = 0;
  463.         DelayFor18TCY();
  464.         E_PIN = 1;                              // Clock cmd and address in
  465.         DelayFor18TCY();
  466.         E_PIN = 0;
  467. #ifdef UPPER                                    // Upper nibble interface
  468.         DATA_PORT &= 0x0f;                      // Write lower nibble
  469.         DATA_PORT |= ((CGaddr<<4)&0xf0);
  470. #else                                           // Lower nibble interface
  471.         DATA_PORT &= 0xf0;                      // Write lower nibble
  472.         DATA_PORT |= (CGaddr&0x0f);
  473. #endif
  474.         DelayFor18TCY();
  475.         E_PIN = 1;                              // Clock cmd and address in
  476.         DelayFor18TCY();
  477.         E_PIN = 0;
  478. #ifdef UPPER                                    // Upper nibble interface
  479.         TRIS_DATA_PORT |= 0xf0;                 // Make inputs
  480. #else                                           // Lower nibble interface
  481.         TRIS_DATA_PORT |= 0x0f;                 // Make inputs
  482. #endif
  483. #endif
  484.         return;
  485. }
  486.  
  487. /********************************************************************
  488. *       Function Name:  ReadDataXLCD                                *
  489. *       Return Value:   char: data byte from LCD controller         *
  490. *       Parameters:     void                                        *
  491. *       Description:    This routine reads a data byte from the     *
  492. *                       Hitachi HD44780 LCD controller. The user    *
  493. *                       must check to see if the LCD controller is  *
  494. *                       busy before calling this routine. The data  *
  495. *                       is read from the character generator RAM or *
  496. *                       the display data RAM depending on what the  *
  497. *                       previous SetxxRamAddr routine was called.   *
  498. ********************************************************************/
  499. char ReadDataXLCD(void)
  500. {
  501.         char data;
  502.  
  503. #ifdef BIT8                             // 8-bit interface
  504.         RS_PIN = 1;                     // Set the control bits
  505.         RW_PIN = 1;
  506.         DelayFor18TCY();
  507.         E_PIN = 1;                      // Clock the data out of the LCD
  508.         DelayFor18TCY();
  509.         data = DATA_PORT;               // Read the data
  510.         E_PIN = 0;
  511.         RS_PIN = 0;                     // Reset the control bits
  512.         RW_PIN = 0;
  513. #else                                   // 4-bit interface
  514.         RW_PIN = 1;
  515.         RS_PIN = 1;
  516.         DelayFor18TCY();
  517.         E_PIN = 1;                      // Clock the data out of the LCD
  518.         DelayFor18TCY();
  519. #ifdef UPPER                            // Upper nibble interface
  520.         data = DATA_PORT&0xf0;          // Read the upper nibble of data
  521. #else                                   // Lower nibble interface
  522.         data = (DATA_PORT<<4)&0xf0;     // read the upper nibble of data
  523. #endif
  524.         E_PIN = 0;                      // Reset the clock line
  525.         DelayFor18TCY();
  526.         E_PIN = 1;                      // Clock the next nibble out of the LCD
  527.         DelayFor18TCY();
  528. #ifdef UPPER                            // Upper nibble interface
  529.         data |= (DATA_PORT>>4)&0x0f;    // Read the lower nibble of data
  530. #else                                   // Lower nibble interface
  531.         data |= DATA_PORT&0x0f;         // Read the lower nibble of data
  532. #endif
  533.         E_PIN = 0;                                      
  534.         RS_PIN = 0;                     // Reset the control bits
  535.         RW_PIN = 0;
  536. #endif
  537.         return(data);                   // Return the data byte
  538. }
  539.  
  540. /*********************************************************************
  541. *       Function Name:  ReadAddrXLCD                                 *
  542. *       Return Value:   char: address from LCD controller            *
  543. *       Parameters:     void                                         *
  544. *       Description:    This routine reads an address byte from the  *
  545. *                       Hitachi HD44780 LCD controller. The user     *
  546. *                       must check to see if the LCD controller is   *
  547. *                       busy before calling this routine. The address*
  548. *                       is read from the character generator RAM or  *
  549. *                       the display data RAM depending on what the   *
  550. *                       previous SetxxRamAddr routine was called.    *
  551. *********************************************************************/
  552. unsigned char ReadAddrXLCD(void)
  553. {
  554.         char data;                      // Holds the data retrieved from the LCD
  555.  
  556. #ifdef BIT8                             // 8-bit interface
  557.         RW_PIN = 1;                     // Set control bits for the read
  558.         RS_PIN = 0;
  559.         DelayFor18TCY();
  560.         E_PIN = 1;                      // Clock data out of the LCD controller
  561.         DelayFor18TCY();
  562.         data = DATA_PORT;               // Save the data in the register
  563.         E_PIN = 0;
  564.         RW_PIN = 0;                     // Reset the control bits
  565. #else                                   // 4-bit interface
  566.         RW_PIN = 1;                     // Set control bits for the read
  567.         RS_PIN = 0;
  568.         DelayFor18TCY();
  569.         E_PIN = 1;                      // Clock data out of the LCD controller
  570.         DelayFor18TCY();
  571. #ifdef UPPER                            // Upper nibble interface
  572.         data = DATA_PORT&0xf0;          // Read the nibble into the upper nibble of data
  573. #else                                   // Lower nibble interface
  574.         data = (DATA_PORT<<4)&0xf0;     // Read the nibble into the upper nibble of data
  575. #endif
  576.         E_PIN = 0;                      // Reset the clock
  577.         DelayFor18TCY();
  578.         E_PIN = 1;                      // Clock out the lower nibble
  579.         DelayFor18TCY();
  580. #ifdef UPPER                            // Upper nibble interface
  581.         data |= (DATA_PORT>>4)&0x0f;    // Read the nibble into the lower nibble of data
  582. #else                                   // Lower nibble interface
  583.         data |= DATA_PORT&0x0f;         // Read the nibble into the lower nibble of data
  584. #endif
  585.         E_PIN = 0;
  586.         RW_PIN = 0;                     // Reset the control lines
  587. #endif
  588.         return (data&0x7f);             // Return the address, Mask off the busy bit
  589. }
  590.  
  591. /********************************************************************
  592. *       Function Name:  putsXLCD
  593. *       Return Value:   void
  594. *       Parameters:     buffer: pointer to string
  595. *       Description:    This routine writes a string of bytes to the
  596. *                       Hitachi HD44780 LCD controller. The user
  597. *                       must check to see if the LCD controller is
  598. *                       busy before calling this routine. The data
  599. *                       is written to the character generator RAM or
  600. *                       the display data RAM depending on what the
  601. *                       previous SetxxRamAddr routine was called.
  602. ********************************************************************/
  603. void putsXLCD(char *buffer)
  604. {
  605.         while(*buffer)                  // Write data to LCD up to null
  606.         {
  607.                 while(BusyXLCD());      // Wait while LCD is busy
  608.                 WriteDataXLCD(*buffer); // Write character to LCD
  609.                 buffer++;               // Increment buffer
  610.         }
  611.         return;
  612. }
  613.  
  614. /********************************************************************
  615. *       Function Name:  putrsXLCD
  616. *       Return Value:   void
  617. *       Parameters:     buffer: pointer to string
  618. *       Description:    This routine writes a string of bytes to the
  619. *                       Hitachi HD44780 LCD controller. The user
  620. *                       must check to see if the LCD controller is
  621. *                       busy before calling this routine. The data
  622. *                       is written to the character generator RAM or
  623. *                       the display data RAM depending on what the
  624. *                       previous SetxxRamAddr routine was called.
  625. ********************************************************************/
  626. void putrsXLCD(const  char *buffer)
  627. {
  628.         while(*buffer)                  // Write data to LCD up to null
  629.         {
  630.                 while(BusyXLCD());      // Wait while LCD is busy
  631.                 WriteDataXLCD(*buffer); // Write character to LCD
  632.                 buffer++;               // Increment buffer
  633.         }
  634.         return;
  635. }
  636.  
  637. /********************************************************************
  638. *       Function Name:  BusyXLCD                                    *
  639. *       Return Value:   char: busy status of LCD controller         *
  640. *       Parameters:     void                                        *
  641. *       Description:    This routine reads the busy status of the   *
  642. *                       Hitachi HD44780 LCD controller.             *
  643. ********************************************************************/
  644. unsigned char BusyXLCD(void)
  645. {
  646. #ifdef BUSY_LCD
  647.         RW_PIN = 1;                     // Set the control bits for read
  648.         RS_PIN = 0;
  649.         DelayFor18TCY();
  650.         E_PIN = 1;                      // Clock in the command
  651.         DelayFor18TCY();
  652. #ifdef BIT8                             // 8-bit interface
  653.         if(DATA_PORT&0x80)                      // Read bit 7 (busy bit)
  654.         {                               // If high
  655.                 E_PIN = 0;              // Reset clock line
  656.                 RW_PIN = 0;             // Reset control line
  657.                 return 1;               // Return TRUE
  658.         }
  659.         else                            // Bit 7 low
  660.         {
  661.                 E_PIN = 0;              // Reset clock line
  662.                 RW_PIN = 0;             // Reset control line
  663.                 return 0;               // Return FALSE
  664.         }
  665. #else                                   // 4-bit interface
  666. #ifdef UPPER                            // Upper nibble interface
  667.         if(DATA_PORT&0x80)
  668. #else                                   // Lower nibble interface
  669.         if(DATA_PORT&0x08)
  670. #endif
  671.         {
  672.                 E_PIN = 0;              // Reset clock line
  673.                 DelayFor18TCY();
  674.                 E_PIN = 1;              // Clock out other nibble
  675.                 DelayFor18TCY();
  676.                 E_PIN = 0;
  677.                 RW_PIN = 0;             // Reset control line
  678.                 return 1;               // Return TRUE
  679.         }
  680.         else                            // Busy bit is low
  681.         {
  682.                 E_PIN = 0;              // Reset clock line
  683.                 DelayFor18TCY();
  684.                 E_PIN = 1;              // Clock out other nibble
  685.                 DelayFor18TCY();
  686.                 E_PIN = 0;
  687.                 RW_PIN = 0;             // Reset control line
  688.                 return 0;               // Return FALSE
  689.         }
  690. #endif
  691. #else
  692.         __delay_ms(5);
  693.         return 0;
  694. #endif
  695.  
  696. }
« Última modificación: 06 de Febrero de 2013, 22:45:08 por todopic »
Firmat - Santa Fe - Argentina

www.TodoPic.net

Solo se tiran piedras, al arbol que tiene frutos...

Desconectado Jeremas

  • PIC10
  • *
  • Mensajes: 1
Re: Como trabajar con LCD en xc8
« Respuesta #3 en: 11 de Febrero de 2013, 14:36:49 »
Hola! Soy Jeremías.
Estuve investigando con la librería esta de pic16 para el XC8 y no tuve forma de hacer andar el display de una linea, de dos lineas no tengo asi que no se si funcionará.
La cosa es que logra conectarse, porque desaparece la linea oscura de configuración de contraste, y nunca muestra nada.
No se si será una mala configuración, mala conexión no creo. En la simulación logro mover el cursor a lo largo del lcd, pero tampoco muestra texto. ¿A alguien le pasó algo similar?

Saludos desde San Francisco!

Desconectado electroipod

  • PIC16
  • ***
  • Mensajes: 166
Re: Como trabajar con LCD en xc8
« Respuesta #4 en: 19 de Febrero de 2013, 00:30:15 »
Gracias por la lbreria pero donde la pongo o como la añado dentro del MPLABX.

Desconectado jukinch

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 608
Re: Como trabajar con LCD en xc8
« Respuesta #5 en: 19 de Febrero de 2013, 07:37:09 »
Agrega la librería en el directorio de tu proyecto y añade esta línea al código en main.c
#include "lcd_pic16.c"
"Divide las dificultades que examinas en tantas partes como sea posible para su mejor solución." -René Descartes

Desconectado electroipod

  • PIC16
  • ***
  • Mensajes: 166
funciono
« Respuesta #6 en: 19 de Febrero de 2013, 11:38:42 »
gracias por la libreria ya logre que funcione, estas librerias vienen con el xc8 pero para pic18fxxx y tu lo has modificado que buena, gracias.
« Última modificación: 19 de Febrero de 2013, 12:28:18 por electroipod »

Desconectado electroipod

  • PIC16
  • ***
  • Mensajes: 166
Re: Como trabajar con LCD en xc8
« Respuesta #7 en: 19 de Febrero de 2013, 21:52:22 »
---
« Última modificación: 21 de Febrero de 2013, 21:26:26 por electroipod »

Desconectado doppel

  • Moderadores
  • PIC24H
  • *****
  • Mensajes: 1534
Re: Como trabajar con LCD en xc8
« Respuesta #8 en: 19 de Febrero de 2013, 23:55:06 »
Hola, estoy realizando un curso de C...
Hola Norberto, lo estás haciendo online o presencial? por que hace tiempo busco algo online y no encuentro... gracias
" Para ser exitoso no tienes que hacer cosas extraordinarias. Haz cosas ordinarias, extraordinariamente bien "