Autor Tema: PICDEM 2 PLUS 18f452 en C18  (Leído 1906 veces)

0 Usuarios y 2 Visitantes están viendo este tema.

Desconectado jherran

  • PIC10
  • *
  • Mensajes: 1
PICDEM 2 PLUS 18f452 en C18
« en: 22 de Marzo de 2012, 11:31:20 »
Hola a todos,

Estoy intentando visualizar datos en la pantalla del LCD del PICDEM 2 con el 18f452. Estoy programando en C18 y la conexión serie con el computador funciona bien pero no con el LCD. Si alguien me puede orientar por favor? Sé que hay que modificar el fichero xlcd.h pero no consigo nada. Gracias! Un saludo

Desconectado jukinch

  • Colaborador
  • PIC24F
  • *****
  • Mensajes: 608
Re: PICDEM 2 PLUS 18f452 en C18
« Respuesta #1 en: 22 de Marzo de 2012, 20:13:53 »
jherran:
         Probá con esta librería xlcd_portd.h que te adjunto más abajo. Es la misma de c18 pero agregué en el mismo archivo xlcd.h todas las funciones para que sean recompiladas y se vean reflejados los cambios de pines al compilar el código.
         Sino podés seguir los pasos que indica suky en la página 58 de su tutorial de c18. Porque sinó al compilar tu código el compilador no te está tomando los cambios de pines que has hecho.

http://www.micros-designs.com.ar/tutorial-mplab-c18/

Saludos.
       Jukinch

Pd: chincheta al tutorial de Suky!!  :-/


Código: C
  1. /********************************************************************************************************************
  2.  
  3. LIBRERIA MODIFICADA PARA USO EN PUERTO D CON PIN RW ACTIVO, Y 4 BITS
  4.  
  5. CONTROLES EN LOS PINES portd0-3 Y DATOS EN portd4-7
  6.  
  7. ------------------------------------------------------------
  8. CONEXIONES:
  9.  
  10. lcd conectado en PUERTO D
  11.  
  12. bus de datos en el nible alto conector idc rb 4 a 7
  13.  
  14. DATA7    PUERTO D.7
  15. DATA6    PUERTO D.6
  16. DATA5    PUERTO D.5
  17. DATA4    PUERTO D.4
  18.  
  19.  
  20. bus de control en el nible bajo conector idc rb 0 a 3
  21.  
  22. E_PIN    PUERTO D.3
  23. RS_PIN   PUERTO D.2
  24. RW_PIN   PUERTO D.1
  25.  
  26.  
  27. SE DEBE SELECCIONAR EL MODO DE MEMORIA LARGO LARGE MEMORY MODEL. SINO HAY QUE CAMBIAR LOS FAR POR NEAR.
  28.  
  29.  
  30. *******************************************************************************************************************
  31. */
  32.  
  33.  
  34. #ifndef __XLCD_H
  35. #define __XLCD_H
  36. #include "p18cxxx.h"
  37. /* PIC18 XLCD peripheral routines.
  38.  *
  39.  *   Notes:
  40.  *      - These libraries routines are written to support the
  41.  *        Hitachi HD44780 LCD controller.
  42.  
  43.  *      - The user must define the following items:
  44.  *          - The LCD interface type (4- or 8-bits)
  45.  *          - If 4-bit mode
  46.  *              - whether using the upper or lower nibble
  47.  
  48.  *          - The data port
  49.  *              - The tris register for data port
  50.  *              - The control signal ports and pins
  51.  *              - The control signal port tris and pins
  52.  
  53.  *          - The user must provide three delay routines:
  54.  *              - DelayFor18TCY() provides a 18 Tcy delay
  55.  *              - DelayPORXLCD() provides at least 15ms delay
  56.  *              - DelayXLCD() provides at least 5ms delay
  57.  */
  58.  
  59. /* Interface type 8-bit or 4-bit
  60.  * For 8-bit operation uncomment the #define BIT8
  61.  */
  62. /* #define BIT8 */
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. /* When in 4-bit interface define if the data is in the upper
  70.  * or lower nibble.  For lower nibble, comment the #define UPPER
  71.  */
  72. #define UPPER          
  73.  
  74. /* DATA_PORT defines the port to which the LCD data lines are connected */
  75. #define DATA_PORT      PORTD
  76. #define TRIS_DATA_PORT TRISD
  77.  
  78. /* CTRL_PORT defines the port where the control lines are connected.
  79.  * These are just samples, change to match your application.
  80.  */
  81. #define RW_PIN   LATDbits.LATD1   /* PORT for RW */
  82. #define TRIS_RW  TRISDbits.TRISD1    /* TRIS for RW */
  83.  
  84. #define RS_PIN   LATDbits.LATD2   /* PORT for RS */
  85. #define TRIS_RS  TRISDbits.TRISD2    /* TRIS for RS */
  86.  
  87. #define E_PIN    LATDbits.LATD3   /* PORT for D  */
  88. #define TRIS_E   TRISDbits.TRISD3    /* TRIS for E  */
  89.  
  90.  
  91. /* Display ON/OFF Control defines */
  92. #define DON         0b00001111  /* Display on      */
  93. #define DOFF        0b00001011  /* Display off     */
  94. #define CURSOR_ON   0b00001111  /* Cursor on       */
  95. #define CURSOR_OFF  0b00001101  /* Cursor off      */
  96. #define BLINK_ON    0b00001111  /* Cursor Blink    */
  97. #define BLINK_OFF   0b00001110  /* Cursor No Blink */
  98.  
  99. /* Cursor or Display Shift defines */
  100. #define SHIFT_CUR_LEFT    0b00010011  /* Cursor shifts to the left   */
  101. #define SHIFT_CUR_RIGHT   0b00010111  /* Cursor shifts to the right  */
  102. #define SHIFT_DISP_LEFT   0b00011011  /* Display shifts to the left  */
  103. #define SHIFT_DISP_RIGHT  0b00011111  /* Display shifts to the right */
  104.  
  105. /* Function Set defines */
  106. #define FOUR_BIT   0b00101111  /* 4-bit Interface               */
  107. #define EIGHT_BIT  0b00111111  /* 8-bit Interface               */
  108. #define LINE_5X7   0b00110011  /* 5x7 characters, single line   */
  109. #define LINE_5X10  0b00110111  /* 5x10 characters               */
  110. #define LINES_5X7  0b00111011  /* 5x7 characters, multiple line */
  111.  
  112. #define PARAM_SCLASS auto
  113. #define MEM_MODEL far  // MODIFICAMOS FAR POR NEAR
  114.  
  115. #define LINE_0   0b10000000     //linea inicial
  116. #define NEXT_LINE 0b11000000
  117.  
  118.  
  119.  
  120.  
  121. // definiciones de funciones --------------------------------------------------------
  122.  
  123.  
  124. /* OpenXLCD
  125.  * Configures I/O pins for external LCD
  126.  */
  127. void OpenXLCD(PARAM_SCLASS unsigned char);
  128.  
  129.  
  130.  
  131.  
  132. /* SetCGRamAddr
  133.  * Sets the character generator address
  134.  */
  135. void SetCGRamAddr(PARAM_SCLASS unsigned char);
  136.  
  137.  
  138.  
  139.  
  140. /* SetDDRamAddr
  141.  * Sets the display data address
  142.  */
  143. void SetDDRamAddr(PARAM_SCLASS unsigned char);
  144.  
  145.  
  146.  
  147.  
  148. /* BusyXLCD
  149.  * Returns the busy status of the LCD
  150.  */
  151. unsigned char BusyXLCD(void);
  152.  
  153.  
  154.  
  155.  
  156. /* ReadAddrXLCD
  157.  * Reads the current address
  158.  */
  159. unsigned char ReadAddrXLCD(void);
  160.  
  161.  
  162.  
  163. /* ReadDataXLCD
  164.  * Reads a byte of data
  165.  */
  166. char ReadDataXLCD(void);
  167.  
  168.  
  169.  
  170.  
  171. /* WriteCmdXLCD
  172.  * Writes a command to the LCD
  173.  */
  174. void WriteCmdXLCD(PARAM_SCLASS unsigned char);
  175.  
  176.  
  177.  
  178.  
  179. /* WriteDataXLCD
  180.  * Writes a data byte to the LCD
  181.  */
  182. void WriteDataXLCD(PARAM_SCLASS char);
  183.  
  184.  
  185.  
  186.  
  187. /* putcXLCD
  188.  * A putc is a write
  189.  */
  190. #define putcXLCD WriteDataXLCD
  191.  
  192.  
  193.  
  194.  
  195. /* putsXLCD
  196.  * Writes a string of characters to the LCD
  197.  */
  198. void putsXLCD(PARAM_SCLASS char *);
  199.  
  200.  
  201.  
  202.  
  203. /* putrsXLCD
  204.  * Writes a string of characters in ROM to the LCD
  205.  */
  206. void putrsXLCD(PARAM_SCLASS const MEM_MODEL rom char *);
  207.  
  208.  
  209.  
  210. /* User defines these routines according to the oscillator frequency */
  211. extern void DelayFor18TCY(void);
  212. extern void DelayPORXLCD(void);
  213. extern void DelayXLCD(void);
  214.  
  215. #endif
  216.  
  217.  
  218.  
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231. /********************************************************************
  232. *       Function Name:  OpenXLCD                                    *
  233. *       Return Value:   void                                        *
  234. *       Parameters:     lcdtype: sets the type of LCD (lines)       *
  235. *       Description:    This routine configures the LCD. Based on   *
  236. *                       the Hitachi HD44780 LCD controller. The     *
  237. *                       routine will configure the I/O pins of the  *
  238. *                       microcontroller, setup the LCD for 4- or    *
  239. *                       8-bit mode and clear the display. The user  *
  240. *                       must provide three delay routines:          *
  241. *                       DelayFor18TCY() provides a 18 Tcy delay     *
  242. *                       DelayPORXLCD() provides at least 15ms delay *
  243. *                       DelayXLCD() provides at least 5ms delay     *
  244. ********************************************************************/
  245. void OpenXLCD(unsigned char lcdtype)
  246. {
  247.         // The data bits must be either a 8-bit port or the upper or
  248.         // lower 4-bits of a port. These pins are made into inputs
  249. #ifdef BIT8                             // 8-bit mode, use whole port
  250.         DATA_PORT = 0;
  251.         TRIS_DATA_PORT = 0xff;
  252. #else                                   // 4-bit mode
  253. #ifdef UPPER                            // Upper 4-bits of the port
  254.         DATA_PORT &= 0x0f;
  255.         TRIS_DATA_PORT |= 0xf0;
  256. #else                                   // Lower 4-bits of the port
  257.         DATA_PORT &= 0xf0;
  258.         TRIS_DATA_PORT |= 0x0f;
  259. #endif
  260. #endif
  261.         TRIS_RW = 0;                    // All control signals made outputs
  262.         TRIS_RS = 0;
  263.         TRIS_E = 0;
  264.         RW_PIN = 0;                     // R/W pin made low
  265.         RS_PIN = 0;                     // Register select pin made low
  266.         E_PIN = 0;                      // Clock pin made low
  267.  
  268.         // Delay for 15ms to allow for LCD Power on reset
  269.         DelayPORXLCD();
  270.        
  271.         // Setup interface to LCD
  272. #ifdef BIT8                             // 8-bit mode interface
  273.         TRIS_DATA_PORT = 0;             // Data port output
  274.         DATA_PORT = 0b00110000;         // Function set cmd(8-bit interface)
  275. #else                                   // 4-bit mode interface
  276. #ifdef UPPER                            // Upper nibble interface
  277.         TRIS_DATA_PORT &= 0x0f;
  278.         DATA_PORT &= 0x0f;
  279.         DATA_PORT |= 0b00100000;        // Function set cmd(4-bit interface)
  280. #else                                   // Lower nibble interface
  281.         TRIS_DATA_PORT &= 0xf0;
  282.         DATA_PORT &= 0xf0;
  283.         DATA_PORT |= 0b00000010;        // Function set cmd(4-bit interface)
  284. #endif
  285. #endif
  286.         E_PIN = 1;                      // Clock the cmd in
  287.         DelayFor18TCY();
  288.         E_PIN = 0;
  289.        
  290.         // Delay for at least 4.1ms
  291.         DelayXLCD();
  292.  
  293.         // Setup interface to LCD
  294. #ifdef BIT8                             // 8-bit interface
  295.         DATA_PORT = 0b00110000;         // Function set cmd(8-bit interface)
  296. #else                                   // 4-bit interface
  297. #ifdef UPPER                            // Upper nibble interface
  298.         DATA_PORT &= 0x0f;              // Function set cmd(4-bit interface)
  299.         DATA_PORT |= 0b00100000;
  300. #else                                   // Lower nibble interface
  301.         DATA_PORT &= 0xf0;              // Function set cmd(4-bit interface)
  302.         DATA_PORT |= 0b00000010;
  303. #endif
  304. #endif
  305.         E_PIN = 1;                      // Clock the cmd in
  306.         DelayFor18TCY();
  307.         E_PIN = 0;
  308.  
  309.         // Delay for at least 100us
  310.         DelayXLCD();
  311.  
  312.         // Setup interface to LCD
  313. #ifdef BIT8                             // 8-bit interface
  314.         DATA_PORT = 0b00110000;         // Function set cmd(8-bit interface)
  315. #else                                   // 4-bit interface
  316. #ifdef UPPER                            // Upper nibble interface
  317.         DATA_PORT &= 0x0f;              // Function set cmd(4-bit interface)
  318.         DATA_PORT |= 0b00100000;
  319. #else                                   // Lower nibble interface
  320.         DATA_PORT &= 0xf0;              // Function set cmd(4-bit interface)
  321.         DATA_PORT |= 0b00000010;
  322. #endif
  323. #endif
  324.         E_PIN = 1;                      // Clock cmd in
  325.         DelayFor18TCY();
  326.         E_PIN = 0;
  327.  
  328. #ifdef BIT8                             // 8-bit interface
  329.         TRIS_DATA_PORT = 0xff;          // Make data port input
  330. #else                                   // 4-bit interface
  331. #ifdef UPPER                            // Upper nibble interface
  332.         TRIS_DATA_PORT |= 0xf0;         // Make data nibble input
  333. #else                                   // Lower nibble interface
  334.         TRIS_DATA_PORT |= 0x0f;         // Make data nibble input
  335. #endif
  336. #endif
  337.  
  338.         // Set data interface width, # lines, font
  339.         while(BusyXLCD());              // Wait if LCD busy
  340.         WriteCmdXLCD(lcdtype);          // Function set cmd
  341.  
  342.         // Turn the display on then off
  343.         while(BusyXLCD());              // Wait if LCD busy
  344.         WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF);        // Display OFF/Blink OFF
  345.         while(BusyXLCD());              // Wait if LCD busy
  346.         WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON);           // Display ON/Blink ON
  347.  
  348.         // Clear display
  349.         while(BusyXLCD());              // Wait if LCD busy
  350.         WriteCmdXLCD(0x01);             // Clear display
  351.  
  352.         // Set entry mode inc, no shift
  353.         while(BusyXLCD());              // Wait if LCD busy
  354.         WriteCmdXLCD(SHIFT_CUR_LEFT);   // Entry Mode
  355.  
  356.         // Set DD Ram address to 0
  357.         while(BusyXLCD());              // Wait if LCD busy
  358.         SetDDRamAddr(0);                // Set Display data ram address to 0
  359.  
  360.         return;
  361. }
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387. /********************************************************************
  388. *       Function Name:  putrsXLCD
  389. *       Return Value:   void
  390. *       Parameters:     : pointer to string
  391. *       Description:    This routine writes a string of bytes to the
  392. *                       Hitachi HD44780 LCD controller. The user
  393. *                       must check to see if the LCD controller is
  394. *                       busy before calling this routine. The data
  395. *                       is written to the character generator RAM or
  396. *                       the display data RAM depending on what the
  397. *                       previous SetxxRamAddr routine was called.
  398. ********************************************************************/
  399. void putrsXLCD(const rom char *buffer_)
  400. {
  401.         while(*buffer_)                  // Write data to LCD up to null
  402.         {
  403.                 while(BusyXLCD());      // Wait while LCD is busy
  404.                 WriteDataXLCD(*buffer_); // Write character to LCD
  405.                 buffer_++;               // Increment buffer
  406.         }
  407.         return;
  408. }
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425. /********************************************************************
  426. *       Function Name:  putsXLCD
  427. *       Return Value:   void
  428. *       Parameters:     buffer: pointer to string
  429. *       Description:    This routine writes a string of bytes to the
  430. *                       Hitachi HD44780 LCD controller. The user
  431. *                       must check to see if the LCD controller is
  432. *                       busy before calling this routine. The data
  433. *                       is written to the character generator RAM or
  434. *                       the display data RAM depending on what the
  435. *                       previous SetxxRamAddr routine was called.
  436. ********************************************************************/
  437. void putsXLCD(char *buffer)
  438. {
  439.         while(*buffer)                  // Write data to LCD up to null
  440.         {
  441.                 while(BusyXLCD());      // Wait while LCD is busy
  442.                 WriteDataXLCD(*buffer); // Write character to LCD
  443.                 buffer++;               // Increment buffer
  444.         }
  445.         return;
  446. }
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456. /*********************************************************************
  457. *       Function Name:  ReadAddrXLCD                                 *
  458. *       Return Value:   char: address from LCD controller            *
  459. *       Parameters:     void                                         *
  460. *       Description:    This routine reads an address byte from the  *
  461. *                       Hitachi HD44780 LCD controller. The user     *
  462. *                       must check to see if the LCD controller is   *
  463. *                       busy before calling this routine. The address*
  464. *                       is read from the character generator RAM or  *
  465. *                       the display data RAM depending on what the   *
  466. *                       previous SetxxRamAddr routine was called.    *
  467. *********************************************************************/
  468. unsigned char ReadAddrXLCD(void)
  469. {
  470.         char data;                      // Holds the data retrieved from the LCD
  471.  
  472. #ifdef BIT8                             // 8-bit interface
  473.         RW_PIN = 1;                     // Set control bits for the read
  474.         RS_PIN = 0;
  475.         DelayFor18TCY();
  476.         E_PIN = 1;                      // Clock data out of the LCD controller
  477.         DelayFor18TCY();
  478.         data = DATA_PORT;               // Save the data in the register
  479.         E_PIN = 0;
  480.         RW_PIN = 0;                     // Reset the control bits
  481. #else                                   // 4-bit interface
  482.         RW_PIN = 1;                     // Set control bits for the read
  483.         RS_PIN = 0;
  484.         DelayFor18TCY();
  485.         E_PIN = 1;                      // Clock data out of the LCD controller
  486.         DelayFor18TCY();
  487. #ifdef UPPER                            // Upper nibble interface
  488.         data = DATA_PORT&0xf0;          // Read the nibble into the upper nibble of data
  489. #else                                   // Lower nibble interface
  490.         data = (DATA_PORT<<4)&0xf0;     // Read the nibble into the upper nibble of data
  491. #endif
  492.         E_PIN = 0;                      // Reset the clock
  493.         DelayFor18TCY();
  494.         E_PIN = 1;                      // Clock out the lower nibble
  495.         DelayFor18TCY();
  496. #ifdef UPPER                            // Upper nibble interface
  497.         data |= (DATA_PORT>>4)&0x0f;    // Read the nibble into the lower nibble of data
  498. #else                                   // Lower nibble interface
  499.         data |= DATA_PORT&0x0f;         // Read the nibble into the lower nibble of data
  500. #endif
  501.         E_PIN = 0;
  502.         RW_PIN = 0;                     // Reset the control lines
  503. #endif
  504.         return (data&0x7f);             // Return the address, Mask off the busy bit
  505. }
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521. /********************************************************************
  522. *       Function Name:  ReadDataXLCD                                *
  523. *       Return Value:   char: data byte from LCD controller         *
  524. *       Parameters:     void                                        *
  525. *       Description:    This routine reads a data byte from the     *
  526. *                       Hitachi HD44780 LCD controller. The user    *
  527. *                       must check to see if the LCD controller is  *
  528. *                       busy before calling this routine. The data  *
  529. *                       is read from the character generator RAM or *
  530. *                       the display data RAM depending on what the  *
  531. *                       previous SetxxRamAddr routine was called.   *
  532. ********************************************************************/
  533. char ReadDataXLCD(void)
  534. {
  535.         char data;
  536.  
  537. #ifdef BIT8                             // 8-bit interface
  538.         RS_PIN = 1;                     // Set the control bits
  539.         RW_PIN = 1;
  540.         DelayFor18TCY();
  541.         E_PIN = 1;                      // Clock the data out of the LCD
  542.         DelayFor18TCY();
  543.         data = DATA_PORT;               // Read the data
  544.         E_PIN = 0;
  545.         RS_PIN = 0;                     // Reset the control bits
  546.         RW_PIN = 0;
  547. #else                                   // 4-bit interface
  548.         RW_PIN = 1;
  549.         RS_PIN = 1;
  550.         DelayFor18TCY();
  551.         E_PIN = 1;                      // Clock the data out of the LCD
  552.         DelayFor18TCY();
  553. #ifdef UPPER                            // Upper nibble interface
  554.         data = DATA_PORT&0xf0;          // Read the upper nibble of data
  555. #else                                   // Lower nibble interface
  556.         data = (DATA_PORT<<4)&0xf0;     // read the upper nibble of data
  557. #endif
  558.         E_PIN = 0;                      // Reset the clock line
  559.         DelayFor18TCY();
  560.         E_PIN = 1;                      // Clock the next nibble out of the LCD
  561.         DelayFor18TCY();
  562. #ifdef UPPER                            // Upper nibble interface
  563.         data |= (DATA_PORT>>4)&0x0f;    // Read the lower nibble of data
  564. #else                                   // Lower nibble interface
  565.         data |= DATA_PORT&0x0f;         // Read the lower nibble of data
  566. #endif
  567.         E_PIN = 0;                                      
  568.         RS_PIN = 0;                     // Reset the control bits
  569.         RW_PIN = 0;
  570. #endif
  571.         return(data);                   // Return the data byte
  572. }
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579. /********************************************************************
  580. *       Function Name:  SetCGRamAddr                                *
  581. *       Return Value:   void                                        *
  582. *       Parameters:     CGaddr: character generator ram address     *
  583. *       Description:    This routine sets the character generator   *
  584. *                       address of the Hitachi HD44780 LCD          *
  585. *                       controller. The user must check to see if   *
  586. *                       the LCD controller is busy before calling   *
  587. *                       this routine.                               *
  588. ********************************************************************/
  589. void SetCGRamAddr(unsigned char CGaddr)
  590. {
  591. #ifdef BIT8                                     // 8-bit interface
  592.         TRIS_DATA_PORT = 0;                     // Make data port ouput
  593.         DATA_PORT = CGaddr | 0b01000000;        // Write cmd and address to port
  594.         RW_PIN = 0;                             // Set control signals
  595.         RS_PIN = 0;
  596.         DelayFor18TCY();
  597.         E_PIN = 1;                              // Clock cmd and address in
  598.         DelayFor18TCY();
  599.         E_PIN = 0;
  600.         DelayFor18TCY();
  601.         TRIS_DATA_PORT = 0xff;                  // Make data port inputs
  602. #else                                           // 4-bit interface
  603. #ifdef UPPER                                    // Upper nibble interface
  604.         TRIS_DATA_PORT &= 0x0f;                 // Make nibble input
  605.         DATA_PORT &= 0x0f;                      // and write upper nibble
  606.         DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0);
  607. #else                                           // Lower nibble interface
  608.         TRIS_DATA_PORT &= 0xf0;                 // Make nibble input
  609.         DATA_PORT &= 0xf0;                      // and write upper nibble
  610.         DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f);
  611. #endif
  612.         RW_PIN = 0;                             // Set control signals
  613.         RS_PIN = 0;
  614.         DelayFor18TCY();
  615.         E_PIN = 1;                              // Clock cmd and address in
  616.         DelayFor18TCY();
  617.         E_PIN = 0;
  618. #ifdef UPPER                                    // Upper nibble interface
  619.         DATA_PORT &= 0x0f;                      // Write lower nibble
  620.         DATA_PORT |= ((CGaddr<<4)&0xf0);
  621. #else                                           // Lower nibble interface
  622.         DATA_PORT &= 0xf0;                      // Write lower nibble
  623.         DATA_PORT |= (CGaddr&0x0f);
  624. #endif
  625.         DelayFor18TCY();
  626.         E_PIN = 1;                              // Clock cmd and address in
  627.         DelayFor18TCY();
  628.         E_PIN = 0;
  629. #ifdef UPPER                                    // Upper nibble interface
  630.         TRIS_DATA_PORT |= 0xf0;                 // Make inputs
  631. #else                                           // Lower nibble interface
  632.         TRIS_DATA_PORT |= 0x0f;                 // Make inputs
  633. #endif
  634. #endif
  635.         return;
  636. }
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645. /********************************************************************
  646. *       Function Name:  SetDDRamAddr                                *
  647. *       Return Value:   void                                        *
  648. *       Parameters:     CGaddr: display data address                *
  649. *       Description:    This routine sets the display data address  *
  650. *                       of the Hitachi HD44780 LCD controller. The  *
  651. *                       user must check to see if the LCD controller*
  652. *                       is busy before calling this routine.        *
  653. ********************************************************************/
  654. void SetDDRamAddr(unsigned char DDaddr)
  655. {
  656. #ifdef BIT8                                     // 8-bit interface
  657.         TRIS_DATA_PORT = 0;                     // Make port output
  658.         DATA_PORT = DDaddr | 0b10000000;        // Write cmd and address to port
  659.         RW_PIN = 0;                             // Set the control bits
  660.         RS_PIN = 0;
  661.         DelayFor18TCY();
  662.         E_PIN = 1;                              // Clock the cmd and address in
  663.         DelayFor18TCY();
  664.         E_PIN = 0;
  665.         DelayFor18TCY();
  666.         TRIS_DATA_PORT = 0xff;                  // Make port input
  667. #else                                           // 4-bit interface
  668. #ifdef UPPER                                    // Upper nibble  interface
  669.         TRIS_DATA_PORT &= 0x0f;                 // Make port output
  670.         DATA_PORT &= 0x0f;                      // and write upper nibble
  671.         DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0);
  672. #else                                           // Lower nibble interface
  673.         TRIS_DATA_PORT &= 0xf0;                 // Make port output
  674.         DATA_PORT &= 0xf0;                      // and write upper nibble
  675.         DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f);
  676. #endif
  677.         RW_PIN = 0;                             // Set control bits
  678.         RS_PIN = 0;
  679.         DelayFor18TCY();
  680.         E_PIN = 1;                              // Clock the cmd and address in
  681.         DelayFor18TCY();
  682.         E_PIN = 0;
  683. #ifdef UPPER                                    // Upper nibble interface
  684.         DATA_PORT &= 0x0f;                      // Write lower nibble
  685.         DATA_PORT |= ((DDaddr<<4)&0xf0);
  686. #else                                           // Lower nibble interface
  687.         DATA_PORT &= 0xf0;                      // Write lower nibble
  688.         DATA_PORT |= (DDaddr&0x0f);
  689. #endif
  690.         DelayFor18TCY();
  691.         E_PIN = 1;                              // Clock the cmd and address in
  692.         DelayFor18TCY();
  693.         E_PIN = 0;
  694. #ifdef UPPER                                    // Upper nibble interface
  695.         TRIS_DATA_PORT |= 0xf0;                 // Make port input
  696. #else                                           // Lower nibble interface
  697.         TRIS_DATA_PORT |= 0x0f;                 // Make port input
  698. #endif
  699. #endif
  700.         return;
  701. }
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711. /********************************************************************
  712. *       Function Name:  BusyXLCD                                    *
  713. *       Return Value:   char: busy status of LCD controller         *
  714. *       Parameters:     void                                        *
  715. *       Description:    This routine reads the busy status of the   *
  716. *                       Hitachi HD44780 LCD controller.             *
  717. ********************************************************************/
  718. unsigned char BusyXLCD(void)
  719. {
  720.         RW_PIN = 1;                     // Set the control bits for read
  721.         RS_PIN = 0;
  722.         DelayFor18TCY();
  723.         E_PIN = 1;                      // Clock in the command
  724.         DelayFor18TCY();
  725. #ifdef BIT8                             // 8-bit interface
  726.         if(DATA_PORT&0x80)                      // Read bit 7 (busy bit)
  727.         {                               // If high
  728.                 E_PIN = 0;              // Reset clock line
  729.                 RW_PIN = 0;             // Reset control line
  730.                 return 1;               // Return TRUE
  731.         }
  732.         else                            // Bit 7 low
  733.         {
  734.                 E_PIN = 0;              // Reset clock line
  735.                 RW_PIN = 0;             // Reset control line
  736.                 return 0;               // Return FALSE
  737.         }
  738. #else                                   // 4-bit interface
  739. #ifdef UPPER                            // Upper nibble interface
  740.         if(DATA_PORT&0x80)
  741. #else                                   // Lower nibble interface
  742.         if(DATA_PORT&0x08)
  743. #endif
  744.         {
  745.                 E_PIN = 0;              // Reset clock line
  746.                 DelayFor18TCY();
  747.                 E_PIN = 1;              // Clock out other nibble
  748.                 DelayFor18TCY();
  749.                 E_PIN = 0;
  750.                 RW_PIN = 0;             // Reset control line
  751.                 return 1;               // Return TRUE
  752.         }
  753.         else                            // Busy bit is low
  754.         {
  755.                 E_PIN = 0;              // Reset clock line
  756.                 DelayFor18TCY();
  757.                 E_PIN = 1;              // Clock out other nibble
  758.                 DelayFor18TCY();
  759.                 E_PIN = 0;
  760.                 RW_PIN = 0;             // Reset control line
  761.                 return 0;               // Return FALSE
  762.         }
  763. #endif
  764. }
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779. /********************************************************************
  780. *       Function Name:  WriteCmdXLCD                                *
  781. *       Return Value:   void                                        *
  782. *       Parameters:     cmd: command to send to LCD                 *
  783. *       Description:    This routine writes a command to the Hitachi*
  784. *                       HD44780 LCD controller. The user must check *
  785. *                       to see if the LCD controller is busy before *
  786. *                       calling this routine.                       *
  787. ********************************************************************/
  788. void WriteCmdXLCD(unsigned char cmd)
  789. {
  790. #ifdef BIT8                             // 8-bit interface
  791.         TRIS_DATA_PORT = 0;             // Data port output
  792.         DATA_PORT = cmd;                // Write command to data port
  793.         RW_PIN = 0;                     // Set the control signals
  794.         RS_PIN = 0;                     // for sending a command
  795.         DelayFor18TCY();
  796.         E_PIN = 1;                      // Clock the command in
  797.         DelayFor18TCY();
  798.         E_PIN = 0;
  799.         DelayFor18TCY();
  800.         TRIS_DATA_PORT = 0xff;          // Data port input
  801. #else                                   // 4-bit interface
  802. #ifdef UPPER                            // Upper nibble interface
  803.         TRIS_DATA_PORT &= 0x0f;
  804.         DATA_PORT &= 0x0f;
  805.         DATA_PORT |= cmd&0xf0;
  806. #else                                   // Lower nibble interface
  807.         TRIS_DATA_PORT &= 0xf0;
  808.         DATA_PORT &= 0xf0;
  809.         DATA_PORT |= (cmd>>4)&0x0f;
  810. #endif
  811.         RW_PIN = 0;                     // Set control signals for command
  812.         RS_PIN = 0;
  813.         DelayFor18TCY();
  814.         E_PIN = 1;                      // Clock command in
  815.         DelayFor18TCY();
  816.         E_PIN = 0;
  817. #ifdef UPPER                            // Upper nibble interface
  818.         DATA_PORT &= 0x0f;
  819.         DATA_PORT |= (cmd<<4)&0xf0;
  820. #else                                   // Lower nibble interface
  821.         DATA_PORT &= 0xf0;
  822.         DATA_PORT |= cmd&0x0f;
  823. #endif
  824.         DelayFor18TCY();
  825.         E_PIN = 1;                      // Clock command in
  826.         DelayFor18TCY();
  827.         E_PIN = 0;
  828. #ifdef UPPER                            // Make data nibble input
  829.         TRIS_DATA_PORT |= 0xf0;
  830. #else
  831.         TRIS_DATA_PORT |= 0x0f;
  832. #endif
  833. #endif
  834.         return;
  835. }
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843. /********************************************************************
  844. *       Function Name:  WriteDataXLCD                               *
  845. *       Return Value:   void                                        *
  846. *       Parameters:     data: data byte to be written to LCD        *
  847. *       Description:    This routine writes a data byte to the      *
  848. *                       Hitachi HD44780 LCD controller. The user    *
  849. *                       must check to see if the LCD controller is  *
  850. *                       busy before calling this routine. The data  *
  851. *                       is written to the character generator RAM or*
  852. *                       the display data RAM depending on what the  *
  853. *                       previous SetxxRamAddr routine was called.   *
  854. ********************************************************************/
  855. void WriteDataXLCD(char data)
  856. {
  857. #ifdef BIT8                             // 8-bit interface
  858.         TRIS_DATA_PORT = 0;             // Make port output
  859.         DATA_PORT = data;               // Write data to port
  860.         RS_PIN = 1;                     // Set control bits
  861.         RW_PIN = 0;
  862.         DelayFor18TCY();
  863.         E_PIN = 1;                      // Clock data into LCD
  864.         DelayFor18TCY();
  865.         E_PIN = 0;
  866.         RS_PIN = 0;                     // Reset control bits
  867.         TRIS_DATA_PORT = 0xff;          // Make port input
  868. #else                                   // 4-bit interface
  869. #ifdef UPPER                            // Upper nibble interface
  870.         TRIS_DATA_PORT &= 0x0f;
  871.         DATA_PORT &= 0x0f;
  872.         DATA_PORT |= data&0xf0;
  873. #else                                   // Lower nibble interface
  874.         TRIS_DATA_PORT &= 0xf0;
  875.         DATA_PORT &= 0xf0;
  876.         DATA_PORT |= ((data>>4)&0x0f);
  877. #endif
  878.         RS_PIN = 1;                     // Set control bits
  879.         RW_PIN = 0;
  880.         DelayFor18TCY();
  881.         E_PIN = 1;                      // Clock nibble into LCD
  882.         DelayFor18TCY();
  883.         E_PIN = 0;
  884. #ifdef UPPER                            // Upper nibble interface
  885.         DATA_PORT &= 0x0f;
  886.         DATA_PORT |= ((data<<4)&0xf0);
  887. #else                                   // Lower nibble interface
  888.         DATA_PORT &= 0xf0;
  889.         DATA_PORT |= (data&0x0f);
  890. #endif
  891.         DelayFor18TCY();
  892.         E_PIN = 1;                      // Clock nibble into LCD
  893.         DelayFor18TCY();
  894.         E_PIN = 0;
  895. #ifdef UPPER                            // Upper nibble interface
  896.         TRIS_DATA_PORT |= 0xf0;
  897. #else                                   // Lower nibble interface
  898.         TRIS_DATA_PORT |= 0x0f;
  899. #endif
  900. #endif
  901.         return;
  902. }
"Divide las dificultades que examinas en tantas partes como sea posible para su mejor solución." -René Descartes