Autor Tema: Desmenuzar un drivers de LCD - CCS  (Leído 10448 veces)

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

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Desmenuzar un drivers de LCD - CCS
« en: 21 de Agosto de 2009, 13:38:51 »
hola amigos

Cuando quieres crear o modificar un drivers de lcd lo primero que te planteas es desmenuzar un drivers ya echo ,saber lo que hace cada parte y como añadirle cosas, si ademas esta comentado pues mejor.

Me gustaria que los coladoradores y seres supremos y otros entes de este foro me echaran un capote, aunque es verano,.. alguno pillaremos aburrido en casa.

Partiremos del driver de CCS asi que empezaremos:

- Que un driver

Que es un DRIVER o CONTROLADOR.

Es un documento escrito en lenguaje de programacion en alto o bajo nivel,  con estensión propia en nuestro caso .C o .H dentro del cual se encuentra definidas las entradas y salidas de un dipositivo electronico o un moulo electronico,  puede estar codificado o no.

Este trocito de programa tambien contendra como debe responder ante ciertos estimulo el dispositico y que comportaminetos se beben de adoptar.

Para su uso la libreria suele acompañar de una funciones basicas implementadas por el fabricante en algunos casos y en otras por desarrolladores o implementadores, estas funciones de forma particular son muchas veces ampliadas por usuarios, llegando a perderse si no se dan a conocer.


- para que vale

Cual es el uso de estos controladores.

Un driver técnicamente es un software o programa que sirve de intermediario entre un dispositivo de hardware y el sistema operativo.
En nuestro caso sera sera una libreria en .C o .H en la cual estara definido el dipositivo a controlar y algunas funciones para facilitar su manejo.



- Estandarizacion

LA estandarizacion.

La estandarización de un dispositivo o controlador tiene sus ventajas, ya que el uso del mismo lo hacen muchos usuarios y es facil probar sus fallas y sus mejoras estan dispuestas a disposicion de más cabecitas pensantes.



drivers

LCD.c
LCD420.c
flex.c

LCD_01
LCD_02
LCD_03
LCD_04
LCD_05

#Funciones de uso incorporadas en el drivers.


#Mejoras


#Enlaces de interes.

#Driver g

#enlaces preferentes

libreria full LCD para CCS    http://www.todopic.com.ar/foros/index.php?topic=24085.msg195373#msg195373
                                         http://www.todopic.com.ar/foros/index.php?topic=24085.20






« Última modificación: 27 de Agosto de 2009, 17:41:47 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #1 en: 21 de Agosto de 2009, 13:39:41 »
LCD 16x2   (CCS)



Libreria Original

Código: CSS
  1. ///////////////////////////////////////////////////////////////////////////////
  2. ////                             LCD.C                                     ////
  3. ////                 Driver for common LCD modules                         ////
  4. ////                                                                       ////
  5. ////  lcd_init()   Must be called before any other function.               ////
  6. ////                                                                       ////
  7. ////  lcd_putc(c)  Will display c on the next position of the LCD.         ////
  8. ////                     The following have special meaning:               ////
  9. ////                      \f  Clear display                                ////
  10. ////                      \n  Go to start of second line                   ////
  11. ////                      \b  Move back one position                       ////
  12. ////                                                                       ////
  13. ////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)        ////
  14. ////                                                                       ////
  15. ////  lcd_getc(x,y)   Returns character at position x,y on LCD             ////
  16. ////                                                                       ////
  17. ////  CONFIGURATION                                                        ////
  18. ////  The LCD can be configured in one of two ways: a.) port access or     ////
  19. ////  b.) pin access.  Port access requires the entire 7 bit interface     ////
  20. ////  connected to one GPIO port, and the data bits (D4:D7 of the LCD)     ////
  21. ////  connected to sequential pins on the GPIO port.  Pin access           ////
  22. ////  has no requirements, all 7 bits of the control interface can         ////
  23. ////  can be connected to any GPIO using several ports.                    ////
  24. ////                                                                       ////
  25. ////  To use port access, #define LCD_DATA_PORT to the SFR location of     ////
  26. ////  of the GPIO port that holds the interface, -AND- edit LCD_PIN_MAP    ////
  27. ////  of this file to configure the pin order.  If you are using a         ////
  28. ////  baseline PIC (PCB), then LCD_OUTPUT_MAP and LCD_INPUT_MAP also must  ////
  29. ////  be defined.                                                          ////
  30. ////                                                                       ////
  31. ////  Example of port access:                                              ////
  32. ////     #define LCD_DATA_PORT getenv("SFR:PORTD")                         ////
  33. ////                                                                       ////
  34. ////  To use pin access, the following pins must be defined:               ////
  35. ////     LCD_ENABLE_PIN                                                    ////
  36. ////     LCD_RS_PIN                                                        ////
  37. ////     LCD_RW_PIN                                                        ////
  38. ////     LCD_DATA0                                                         ////
  39. ////     LCD_DATA1                                                         ////
  40. ////     LCD_DATA2                                                         ////
  41. ////     LCD_DATA3                                                         ////
  42. ////     LCD_DATA4                                                         ////
  43. ////                                                                       ////
  44. ////  Example of pin access:                                               ////
  45. ////     #define LCD_ENABLE_PIN  PIN_E0                                    ////
  46. ////     #define LCD_RS_PIN      PIN_E1                                    ////
  47. ////     #define LCD_RW_PIN      PIN_E2                                    ////
  48. ////     #define LCD_DATA0       PIN_D4                                    ////
  49. ////     #define LCD_DATA1       PIN_D5                                    ////
  50. ////     #define LCD_DATA2       PIN_D6                                    ////
  51. ////     #define LCD_DATA3       PIN_D7                                    ////
  52. ////                                                                       ////
  53. ///////////////////////////////////////////////////////////////////////////////
  54. ////        (C) Copyright 1996,2009 Custom Computer Services           ////
  55. //// This source code may only be used by licensed users of the CCS C  ////
  56. //// compiler.  This source code may only be distributed to other      ////
  57. //// licensed users of the CCS C compiler.  No other use, reproduction ////
  58. //// or distribution is permitted without written permission.          ////
  59. //// Derivative programs created using this software in object code    ////
  60. //// form are not restricted in any way.                               ////
  61. ///////////////////////////////////////////////////////////////////////////
  62.  
  63. typedef struct  
  64. {                            // This structure is overlayed
  65.    BOOLEAN enable;           // on to an I/O port to gain
  66.    BOOLEAN rs;               // access to the LCD pins.
  67.    BOOLEAN rw;               // The bits are allocated from
  68.    BOOLEAN unused;           // low order up.  ENABLE will
  69.    int     data : 4;         // be LSB pin of that port.
  70.   #if defined(__PCD__)       // The port used will be LCD_DATA_PORT.
  71.    int    reserved: 8;
  72.   #endif
  73. } LCD_PIN_MAP;
  74.  
  75. #if defined(__PCB__)
  76.    // these definitions only need to be modified for baseline PICs.
  77.    // all other PICs use LCD_PIN_MAP or individual LCD_xxx pin definitions.
  78. /*                                    EN, RS,   RW,   UNUSED,  DATA  */
  79.  const LCD_PIN_MAP LCD_OUTPUT_MAP =  {0,  0,    0,    0,       0};
  80.  const LCD_PIN_MAP LCD_INPUT_MAP =   {0,  0,    0,    0,       0xF};
  81. #endif
  82.  
  83. #ifndef LCD_ENABLE_PIN
  84.    #define lcd_output_enable(x) lcdlat.enable=x
  85.    #define lcd_enable_tris()   lcdtris.enable=0
  86. #else
  87.    #define lcd_output_enable(x) output_bit(LCD_ENABLE_PIN, x)
  88.    #define lcd_enable_tris()  output_drive(LCD_ENABLE_PIN)
  89. #endif
  90.  
  91. #ifndef LCD_RS_PIN
  92.    #define lcd_output_rs(x) lcdlat.rs=x
  93.    #define lcd_rs_tris()   lcdtris.rs=0
  94. #else
  95.    #define lcd_output_rs(x) output_bit(LCD_RS_PIN, x)
  96.    #define lcd_rs_tris()  output_drive(LCD_RS_PIN)
  97. #endif
  98.  
  99. #ifndef LCD_RW_PIN
  100.    #define lcd_output_rw(x) lcdlat.rw=x
  101.    #define lcd_rw_tris()   lcdtris.rw=0
  102. #else
  103.    #define lcd_output_rw(x) output_bit(LCD_RW_PIN, x)
  104.    #define lcd_rw_tris()  output_drive(LCD_RW_PIN)
  105. #endif
  106.  
  107. #ifndef LCD_DATA_PORT
  108.    #if defined(__PCB__)
  109.       #define LCD_DATA_PORT      0x06     //portb
  110.       #define set_tris_lcd(x)   set_tris_b(x)
  111.    #elif defined(__PCM__)
  112.       #define LCD_DATA_PORT      getenv("SFR:PORTD")     //portd
  113.    #elif defined(__PCH__)
  114.       #define LCD_DATA_PORT      getenv("SFR:PORTD")    //portd
  115.    #elif defined(__PCD__)
  116.       #define LCD_DATA_PORT      getenv("SFR:PORTD")    //portd
  117.    #endif  
  118. #endif
  119.  
  120. #if defined(__PCB__)
  121.    LCD_PIN_MAP lcd, lcdlat;
  122.    #byte lcd = LCD_DATA_PORT
  123.    #byte lcdlat = LCD_DATA_PORT
  124. #elif defined(__PCM__)
  125.    LCD_PIN_MAP lcd, lcdlat, lcdtris;
  126.    #byte lcd = LCD_DATA_PORT
  127.    #byte lcdlat = LCD_DATA_PORT
  128.    #byte lcdtris = LCD_DATA_PORT+0x80
  129. #elif defined(__PCH__)
  130.    LCD_PIN_MAP lcd, lcdlat, lcdtris;
  131.    #byte lcd = LCD_DATA_PORT
  132.    #byte lcdlat = LCD_DATA_PORT+9
  133.    #byte lcdtris = LCD_DATA_PORT+0x12
  134. #elif defined(__PCD__)
  135.    LCD_PIN_MAP lcd, lcdlat, lcdtris;
  136.    #word lcd = LCD_DATA_PORT
  137.    #word lcdlat = LCD_DATA_PORT+2
  138.    #word lcdtris = LCD_DATA_PORT-0x02
  139. #endif
  140.  
  141. #ifndef LCD_TYPE
  142.    #define LCD_TYPE 2           // 0=5x7, 1=5x10, 2=2 lines
  143. #endif
  144.  
  145. #ifndef LCD_LINE_TWO
  146.    #define LCD_LINE_TWO 0x40    // LCD RAM address for the second line
  147. #endif
  148.  
  149. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  150.                              // These bytes need to be sent to the LCD
  151.                              // to start it up.
  152.  
  153.  
  154. BYTE lcd_read_nibble(void);
  155.  
  156. BYTE lcd_read_byte(void)
  157. {
  158.    BYTE low,high;
  159.  
  160.  #if defined(__PCB__)
  161.    set_tris_lcd(LCD_INPUT_MAP);
  162.  #else
  163.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  164.    output_float(LCD_DATA0);
  165.    output_float(LCD_DATA1);
  166.    output_float(LCD_DATA2);
  167.    output_float(LCD_DATA3);
  168.   #else
  169.    lcdtris.data = 0xF;
  170.   #endif
  171.  #endif
  172.        
  173.    lcd_output_rw(1);
  174.    delay_cycles(1);
  175.    lcd_output_enable(1);
  176.    delay_cycles(1);
  177.    high = lcd_read_nibble();
  178.      
  179.    lcd_output_enable(0);
  180.    delay_cycles(1);
  181.    lcd_output_enable(1);
  182.    delay_us(1);
  183.    low = lcd_read_nibble();
  184.      
  185.    lcd_output_enable(0);
  186.  
  187.  #if defined(__PCB__)
  188.    set_tris_lcd(LCD_INPUT_MAP);
  189.  #else
  190.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  191.    output_drive(LCD_DATA0);
  192.    output_drive(LCD_DATA1);
  193.    output_drive(LCD_DATA2);
  194.    output_drive(LCD_DATA3);
  195.   #else
  196.    lcdtris.data = 0x0;
  197.   #endif
  198.  #endif
  199.  
  200.    return( (high<<4) | low);
  201. }
  202.  
  203. BYTE lcd_read_nibble(void)
  204. {
  205.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  206.    BYTE n = 0x00;
  207.  
  208.    /* Read the data port */
  209.    n |= input(LCD_DATA0);
  210.    n |= input(LCD_DATA1) << 1;
  211.    n |= input(LCD_DATA2) << 2;
  212.    n |= input(LCD_DATA3) << 3;
  213.    
  214.    return(n);
  215.   #else
  216.    return(lcd.data);
  217.   #endif
  218. }
  219.  
  220. void lcd_send_nibble(BYTE n)
  221. {
  222.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  223.    /* Write to the data port */
  224.    output_bit(LCD_DATA0, BIT_TEST(n, 0));
  225.    output_bit(LCD_DATA1, BIT_TEST(n, 1));
  226.    output_bit(LCD_DATA2, BIT_TEST(n, 2));
  227.    output_bit(LCD_DATA3, BIT_TEST(n, 3));
  228.   #else      
  229.    lcdlat.data = n;
  230.   #endif
  231.      
  232.    delay_cycles(1);
  233.    lcd_output_enable(1);
  234.    delay_us(2);
  235.    lcd_output_enable(0);
  236. }
  237.  
  238. void lcd_send_byte(BYTE address, BYTE n)
  239. {
  240.    lcd_output_rs(0);
  241.    while ( bit_test(lcd_read_byte(),7) ) ;
  242.    lcd_output_rs(address);
  243.    delay_cycles(1);
  244.    lcd_output_rw(0);
  245.    delay_cycles(1);
  246.    lcd_output_enable(0);
  247.    lcd_send_nibble(n >> 4);
  248.    lcd_send_nibble(n & 0xf);
  249. }
  250.  
  251. void lcd_init(void)
  252. {
  253.    BYTE i;
  254.  
  255.  #if defined(__PCB__)
  256.    set_tris_lcd(LCD_OUTPUT_MAP);
  257.  #else
  258.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  259.    output_drive(LCD_DATA0);
  260.    output_drive(LCD_DATA1);
  261.    output_drive(LCD_DATA2);
  262.    output_drive(LCD_DATA3);
  263.   #else
  264.    lcdtris.data = 0x0;
  265.   #endif
  266.    lcd_enable_tris();
  267.    lcd_rs_tris();
  268.    lcd_rw_tris();
  269.  #endif
  270.  
  271.    lcd_output_rs(0);
  272.    lcd_output_rw(0);
  273.    lcd_output_enable(0);
  274.    
  275.    delay_ms(15);
  276.    for(i=1;i<=3;++i)
  277.    {
  278.        lcd_send_nibble(3);
  279.        delay_ms(5);
  280.    }
  281.    
  282.    lcd_send_nibble(2);
  283.    for(i=0;i<=3;++i)
  284.       lcd_send_byte(0,LCD_INIT_STRING[i]);
  285. }
  286.  
  287. void lcd_gotoxy(BYTE x, BYTE y)
  288. {
  289.    BYTE address;
  290.  
  291.    if(y!=1)
  292.       address=LCD_LINE_TWO;
  293.    else
  294.       address=0;
  295.      
  296.    address+=x-1;
  297.    lcd_send_byte(0,0x80|address);
  298. }
  299.  
  300. void lcd_putc(char c)
  301. {
  302.    switch (c)
  303.    {
  304.       case '\f'   :  lcd_send_byte(0,1);
  305.                      delay_ms(2);
  306.                      break;
  307.                      
  308.       case '\n'   : lcd_gotoxy(1,2);        break;
  309.      
  310.       case '\b'   : lcd_send_byte(0,0x10);  break;
  311.      
  312.       default     : lcd_send_byte(1,c);     break;
  313.    }
  314. }
  315.  
  316. char lcd_getc(BYTE x, BYTE y)
  317. {
  318.    char value;
  319.  
  320.    lcd_gotoxy(x,y);
  321.    while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  322.    lcd_output_rs(1);
  323.    value = lcd_read_byte();
  324.    lcd_output_rs(0);
  325.    
  326.    return(value);
  327. }




Libreria Comentada y traducida


Código: CSS
  1. ///////////////////////////////////////////////////////////////////////////////
  2. ////                             LCD.C                                     ////
  3. ////                 CONTROLADOR  PARA  MODULO LCD COMUN                   ////
  4. ////                                                                       ////
  5. ////  lcd_init()   Debe ser llamada antes que cualquier otra función.      ////
  6. ////                                                                       ////
  7. ////  lcd_putc(c)  Mostrara c en la próxima posición del LCD.              ////
  8. ////                     Lo siguiente tiene especial significado:          ////
  9. ////                      \f  Limpiar display                              ////
  10. ////                      \n  Ir al inicio de la segunda línea             ////
  11. ////                      \b  Retroceder una posición                      ////
  12. ////                                                                       ////
  13. ////  lcd_gotoxy(x,y) situa cursor en LCD (arriba izquierda es 1,1)        ////
  14. ////                                                                       ////
  15. ////  lcd_getc(x,y)   Retorna caracter en posición x,y en el LCD           ////
  16. ////                                                                       ////
  17. ////  CONFIGURACION                                                        ////
  18. ////  El LCD puede ser configurado en uno o dos modos:                     ////
  19. ////  a.) acceso al puerto; requiere la entrada de 7 bits conectados       ////
  20. ////  al puerto y los bit de datos(D4:D7 del LCD) conectados               ////
  21. ////  secuencialmente.                                                     ////
  22. ////  b.) acceso al pin; no requiere conesion secuancial y los 7 bit       ////
  23. ////  pueden estar conectados usando cualquier puerto.                     ////
  24. ////                                                                       ////
  25. ////  To use port access, #define LCD_DATA_PORT to the SFR location of     ////
  26. ////  of the GPIO port that holds the interface, -AND- edit LCD_PIN_MAP    ////
  27. ////  of this file to configure the pin order.  If you are using a         ////
  28. ////  baseline PIC (PCB), then LCD_OUTPUT_MAP and LCD_INPUT_MAP also must  ////
  29. ////  be defined.                                                          ////
  30. ////                                                                       ////
  31. ////  Ejemplo de puerto de acceso:                                         ////
  32. ////     #define LCD_DATA_PORT getenv("SFR:PORTD")                         ////
  33. ////                                                                       ////
  34. ////  Para usar pin de acceso, los siguientes pines deben ser definidos:   ////
  35. ////     LCD_ENABLE_PIN                                                    ////
  36. ////     LCD_RS_PIN                                                        ////
  37. ////     LCD_RW_PIN                                                        ////
  38. ////     LCD_DATA0                                                         ////
  39. ////     LCD_DATA1                                                         ////
  40. ////     LCD_DATA2                                                         ////
  41. ////     LCD_DATA3                                                         ////
  42. ////     LCD_DATA4                                                         ////
  43. ////                                                                       ////
  44. ////  Ejemplo de pin de acceso:                                            ////
  45. ////     #define LCD_ENABLE_PIN  PIN_E0                                    ////
  46. ////     #define LCD_RS_PIN      PIN_E1                                    ////
  47. ////     #define LCD_RW_PIN      PIN_E2                                    ////
  48. ////     #define LCD_DATA0       PIN_D4                                    ////
  49. ////     #define LCD_DATA1       PIN_D5                                    ////
  50. ////     #define LCD_DATA2       PIN_D6                                    ////
  51. ////     #define LCD_DATA3       PIN_D7                                    ////
  52. ////                                                                       ////
  53. ///////////////////////////////////////////////////////////////////////////////
  54. ////        (C) Copyright 1996,2009 Custom Computer Services           ////
  55. //// This source code may only be used by licensed users of the CCS C  ////
  56. //// compiler.  This source code may only be distributed to other      ////
  57. //// licensed users of the CCS C compiler.  No other use, reproduction ////
  58. //// or distribution is permitted without written permission.          ////
  59. //// Derivative programs created using this software in object code    ////
  60. //// form are not restricted in any way.                               ////
  61. ///////////////////////////////////////////////////////////////////////////
  62.  
  63.  
  64. // define una esctructura cuyo contenido son los pines de control E,RS,RW
  65. //
  66. // B0
  67. // B1
  68. // B2
  69. // B3
  70. // B4
  71. // B5
  72. // B6
  73. // B7
  74. //
  75.  
  76. /*
  77. typedef struct  
  78. {                            // La estructuta esta superpuesta
  79.    BOOLEAN enable;           // sobre las entradas del puerto
  80.    BOOLEAN rs;               // para acceder a los pines del LCD.  
  81.    BOOLEAN rw;               // Los bits estan localizados en
  82.    BOOLEAN unused;           // orden de abajo a arriba.  ENABLE sera el pin
  83.    int     data : 4;         // LSB (bit menos significativo) del puerto.
  84.   #if defined(__PCD__)       // El puerto usado sera LCD_DATA_PORT.
  85.    int    reserved: 8;
  86.   #endif
  87. } LCD_PIN_MAP;
  88. */
  89.  
  90. typedef struct               // Definición de estructura
  91. {                            
  92.    BOOLEAN enable;           //
  93.    BOOLEAN rs;               //  
  94.    BOOLEAN rw;               //
  95.    BOOLEAN unused;           //
  96.    int     data : 4;         //
  97.   #if defined(__PCD__)       // si el compilador es de pic24 y DSpic
  98.    int    reserved: 8;       // reserva ram para el uso del compilador
  99.   #endif                     //
  100. } LCD_PIN_MAP;               // variable
  101.  
  102.  
  103.  
  104.  
  105. #if defined(__PCB__)         //evalua el pre-procesador si es pic 10 (12 bits)
  106.    // Estas definiciones solo necesitan ser modificadas en referencia a los PIC de referencia basica.
  107.    // Todos los demas PIC usan "LCD_PIN_MAP" o individual "LCD_xxx" definiciones de pines.
  108.  
  109. /*                                    EN, RS,   RW,   UNUSED,  DATA  */
  110.  const LCD_PIN_MAP LCD_OUTPUT_MAP =  {0,  0,    0,    0,       0};
  111.  const LCD_PIN_MAP LCD_INPUT_MAP =   {0,  0,    0,    0,       0xF};
  112. #endif
  113.  
  114. #ifndef LCD_ENABLE_PIN                                              //si no esta definido el pin enable
  115.    #define lcd_output_enable(x) lcdlat.enable=x                     //lo define en x
  116.    #define lcd_enable_tris()   lcdtris.enable=0                     //lo define como salida  
  117. #else
  118.    #define lcd_output_enable(x) output_bit(LCD_ENABLE_PIN, x)       //
  119.    #define lcd_enable_tris()  output_drive(LCD_ENABLE_PIN)          //
  120. #endif
  121.  
  122. #ifndef LCD_RS_PIN
  123.    #define lcd_output_rs(x) lcdlat.rs=x
  124.    #define lcd_rs_tris()   lcdtris.rs=0
  125. #else
  126.    #define lcd_output_rs(x) output_bit(LCD_RS_PIN, x)
  127.    #define lcd_rs_tris()  output_drive(LCD_RS_PIN)
  128. #endif
  129.  
  130. #ifndef LCD_RW_PIN
  131.    #define lcd_output_rw(x) lcdlat.rw=x
  132.    #define lcd_rw_tris()   lcdtris.rw=0
  133. #else
  134.    #define lcd_output_rw(x) output_bit(LCD_RW_PIN, x)
  135.    #define lcd_rw_tris()  output_drive(LCD_RW_PIN)
  136. #endif
  137.  
  138. #ifndef LCD_DATA_PORT
  139.    #if defined(__PCB__)
  140.       #define LCD_DATA_PORT      0x06     //portb
  141.       #define set_tris_lcd(x)   set_tris_b(x)
  142.    #elif defined(__PCM__)
  143.       #define LCD_DATA_PORT      getenv("SFR:PORTD")     //portd
  144.    #elif defined(__PCH__)
  145.       #define LCD_DATA_PORT      getenv("SFR:PORTD")    //portd
  146.    #elif defined(__PCD__)
  147.       #define LCD_DATA_PORT      getenv("SFR:PORTD")    //portd
  148.    #endif  
  149. #endif
  150.  
  151. #if defined(__PCB__)
  152.    LCD_PIN_MAP lcd, lcdlat;
  153.    #byte lcd = LCD_DATA_PORT
  154.    #byte lcdlat = LCD_DATA_PORT
  155. #elif defined(__PCM__)
  156.    LCD_PIN_MAP lcd, lcdlat, lcdtris;
  157.    #byte lcd = LCD_DATA_PORT
  158.    #byte lcdlat = LCD_DATA_PORT
  159.    #byte lcdtris = LCD_DATA_PORT+0x80
  160. #elif defined(__PCH__)
  161.    LCD_PIN_MAP lcd, lcdlat, lcdtris;
  162.    #byte lcd = LCD_DATA_PORT
  163.    #byte lcdlat = LCD_DATA_PORT+9
  164.    #byte lcdtris = LCD_DATA_PORT+0x12
  165. #elif defined(__PCD__)
  166.    LCD_PIN_MAP lcd, lcdlat, lcdtris;
  167.    #word lcd = LCD_DATA_PORT
  168.    #word lcdlat = LCD_DATA_PORT+2
  169.    #word lcdtris = LCD_DATA_PORT-0x02
  170. #endif
  171.  
  172. #ifndef LCD_TYPE
  173.    #define LCD_TYPE 2           // 0=5x7, 1=5x10, 2=2 lineas
  174. #endif
  175.  
  176. #ifndef LCD_LINE_TWO
  177.    #define LCD_LINE_TWO 0x40    // direcion RAM del LCD para la segunda linea
  178. #endif
  179. /*
  180. BYTE const LCD_INIT_STRING[4] =
  181. {
  182.  0x20 | (lcd_type << 2),        // modo 4-bits, 2lineas, 5x8 puntos
  183.  0xc,                           // Display on
  184.  1,                             // Limpiar displays
  185.  6                              // incrementa cursor
  186. };
  187.                              // estos bytes son necesarios enviarlos al LCD
  188.                              // para iniciar el arranque.
  189. */
  190.  
  191. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  192.                              // estos bytes son necesarios enviarlos al LCD
  193.                              // para iniciar el arranque.
  194.  
  195. BYTE lcd_read_nibble(void);  //lee 4 bits
  196.  
  197. BYTE lcd_read_byte(void)     //lee 8 bits
  198. {
  199.    BYTE low,high;
  200.  
  201.  #if defined(__PCB__)
  202.    set_tris_lcd(LCD_INPUT_MAP);
  203.  #else
  204.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  205.    output_float(LCD_DATA0);
  206.    output_float(LCD_DATA1);
  207.    output_float(LCD_DATA2);
  208.    output_float(LCD_DATA3);
  209.   #else
  210.    lcdtris.data = 0xF;
  211.   #endif
  212.  #endif
  213.        
  214.    lcd_output_rw(1);
  215.    delay_cycles(1);
  216.    lcd_output_enable(1);
  217.    delay_cycles(1);
  218.    high = lcd_read_nibble();
  219.      
  220.    lcd_output_enable(0);
  221.    delay_cycles(1);
  222.    lcd_output_enable(1);
  223.    delay_us(1);
  224.    low = lcd_read_nibble();
  225.      
  226.    lcd_output_enable(0);
  227.  
  228.  #if defined(__PCB__)
  229.    set_tris_lcd(LCD_INPUT_MAP);
  230.  #else
  231.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  232.    output_drive(LCD_DATA0);
  233.    output_drive(LCD_DATA1);
  234.    output_drive(LCD_DATA2);
  235.    output_drive(LCD_DATA3);
  236.   #else
  237.    lcdtris.data = 0x0;
  238.   #endif
  239.  #endif
  240.  
  241.    return( (high<<4) | low);
  242. }
  243.  
  244. BYTE lcd_read_nibble(void)
  245. {
  246.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  247.    BYTE n = 0x00;
  248.  
  249.    /* Read the data port */
  250.    n |= input(LCD_DATA0);
  251.    n |= input(LCD_DATA1) << 1;
  252.    n |= input(LCD_DATA2) << 2;
  253.    n |= input(LCD_DATA3) << 3;
  254.    
  255.    return(n);
  256.   #else
  257.    return(lcd.data);
  258.   #endif
  259. }
  260.  
  261. void lcd_send_nibble(BYTE n)
  262. {
  263.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  264.    /* Write to the data port */
  265.    output_bit(LCD_DATA0, BIT_TEST(n, 0));
  266.    output_bit(LCD_DATA1, BIT_TEST(n, 1));
  267.    output_bit(LCD_DATA2, BIT_TEST(n, 2));
  268.    output_bit(LCD_DATA3, BIT_TEST(n, 3));
  269.   #else      
  270.    lcdlat.data = n;
  271.   #endif
  272.      
  273.    delay_cycles(1);
  274.    lcd_output_enable(1);
  275.    delay_us(2);
  276.    lcd_output_enable(0);
  277. }
  278.  
  279. void lcd_send_byte(BYTE address, BYTE n)
  280. {
  281.    lcd_output_rs(0);
  282.    while ( bit_test(lcd_read_byte(),7) ) ;
  283.    lcd_output_rs(address);
  284.    delay_cycles(1);
  285.    lcd_output_rw(0);
  286.    delay_cycles(1);
  287.    lcd_output_enable(0);
  288.    lcd_send_nibble(n >> 4);
  289.    lcd_send_nibble(n & 0xf);
  290. }
  291.  
  292. void lcd_init(void)
  293. {
  294.    BYTE i;
  295.  
  296.  #if defined(__PCB__)
  297.    set_tris_lcd(LCD_OUTPUT_MAP);
  298.  #else
  299.   #if (defined(LCD_DATA0) && defined(LCD_DATA1) && defined(LCD_DATA2) && defined(LCD_DATA3))
  300.    output_drive(LCD_DATA0);
  301.    output_drive(LCD_DATA1);
  302.    output_drive(LCD_DATA2);
  303.    output_drive(LCD_DATA3);
  304.   #else
  305.    lcdtris.data = 0x0;
  306.   #endif
  307.    lcd_enable_tris();
  308.    lcd_rs_tris();
  309.    lcd_rw_tris();
  310.  #endif
  311.  
  312.    lcd_output_rs(0);
  313.    lcd_output_rw(0);
  314.    lcd_output_enable(0);
  315.    
  316.    delay_ms(15);
  317.    for(i=1;i<=3;++i)
  318.    {
  319.        lcd_send_nibble(3);
  320.        delay_ms(5);
  321.    }
  322.    
  323.    lcd_send_nibble(2);
  324.    for(i=0;i<=3;++i)
  325.       lcd_send_byte(0,LCD_INIT_STRING[i]);
  326. }
  327.  
  328. void lcd_gotoxy(BYTE x, BYTE y)
  329. {
  330.    BYTE address;
  331.  
  332.    if(y!=1)
  333.       address=LCD_LINE_TWO;
  334.    else
  335.       address=0;
  336.      
  337.    address+=x-1;
  338.    lcd_send_byte(0,0x80|address);
  339. }
  340.  
  341. void lcd_putc(char c)
  342. {
  343.    switch (c)
  344.    {
  345.       case '\f'   :  lcd_send_byte(0,1);
  346.                      delay_ms(2);
  347.                      break;
  348.                      
  349.       case '\n'   : lcd_gotoxy(1,2);        break;
  350.      
  351.       case '\b'   : lcd_send_byte(0,0x10);  break;
  352.      
  353.       default     : lcd_send_byte(1,c);     break;
  354.    }
  355. }
  356.  
  357. char lcd_getc(BYTE x, BYTE y)
  358. {
  359.    char value;
  360.  
  361.    lcd_gotoxy(x,y);
  362.    while ( bit_test(lcd_read_byte(),7) ); // wait until busy flag is low
  363.    lcd_output_rs(1);
  364.    value = lcd_read_byte();
  365.    lcd_output_rs(0);
  366.    
  367.    return(value);
  368. }



« Última modificación: 22 de Agosto de 2009, 07:02:37 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #2 en: 21 de Agosto de 2009, 13:40:19 »


Código: CSS
  1. ////////////////////////////////////////////////////////////////////////////
  2. ////                             LCD420.C                               ////
  3. ////            Driver for common 4x20 LCD modules                      ////
  4. ////                                                                    ////
  5. ////  lcd_init()   Must be called before any other function.            ////
  6. ////                                                                    ////
  7. ////  lcd_putc(c)  Will display c on the next position of the LCD.      ////
  8. ////                     The following have special meaning:            ////
  9. ////                      \f  Clear display                             ////
  10. ////                      \n  Go to start of second line                ////
  11. ////                      \b  Move back one position                    ////
  12. ////                                                                    ////
  13. ////  lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)     ////
  14. ////                                                                    ////
  15. ////  lcd_getc(x,y)   Returns character at position x,y on LCD          ////
  16. ////                                                                    ////
  17. ////////////////////////////////////////////////////////////////////////////
  18. ////        (C) Copyright 1996,1997 Custom Computer Services            ////
  19. //// This source code may only be used by licensed users of the CCS C   ////
  20. //// compiler.  This source code may only be distributed to other       ////
  21. //// licensed users of the CCS C compiler.  No other use, reproduction  ////
  22. //// or distribution is permitted without written permission.           ////
  23. //// Derivative programs created using this software in object code     ////
  24. //// form are not restricted in any way.                                ////
  25. ////////////////////////////////////////////////////////////////////////////
  26.  
  27. // As defined in the following structure the pin connection is as follows:
  28. //     B0  enable
  29. //     B1  rs
  30. //     B2  rw
  31. //     B4  D4
  32. //     B5  D5
  33. //     B6  D6
  34. //     B7  D7
  35. //
  36. //   LCD pins D0-D3 are not used and PIC B3 is not used.
  37.  
  38. struct lcd_pin_map {                 // This structure is overlayed
  39.            BOOLEAN enable;           // on to an I/O port to gain
  40.            BOOLEAN rs;               // access to the LCD pins.
  41.            BOOLEAN rw;               // The bits are allocated from
  42.            BOOLEAN unused;           // low order up.  ENABLE will
  43.            int     data : 4;         // be pin B0.
  44.         } lcd;
  45. //seleccion del puerto : A(5), B(6), C(7), D(8), E(9)
  46. #byte lcd = 6                        // This puts the entire structure
  47.                                      // on to port B (at address 6)
  48.  
  49. #define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
  50.  
  51.  
  52. BYTE const LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
  53.                              // These bytes need to be sent to the LCD
  54.                              // to start it up.
  55.  
  56.  
  57.                              // The following are used for setting
  58.                              // the I/O port direction register.
  59.  
  60. struct lcd_pin_map const LCD_WRITE = {0,0,0,0,0}; // For write mode all pins are out
  61. struct lcd_pin_map const LCD_READ = {0,0,0,0,15}; // For read mode data pins are in
  62.  
  63.  
  64. BYTE lcdline;
  65.  
  66. BYTE lcd_read_byte() {
  67.       BYTE low,high;
  68.  
  69.       set_tris_b(LCD_READ);
  70.       lcd.rw = 1;
  71.       delay_cycles(1);
  72.       lcd.enable = 1;
  73.       delay_cycles(1);
  74.       high = lcd.data;
  75.       lcd.enable = 0;
  76.       delay_cycles(1);
  77.       lcd.enable = 1;
  78.       delay_us(1);
  79.       low = lcd.data;
  80.       lcd.enable = 0;
  81.       set_tris_b(LCD_WRITE);
  82.       return( (high<<4) | low);
  83. }
  84.  
  85.  
  86. void lcd_send_nibble( BYTE n ) {
  87.       lcd.data = n;
  88.       delay_cycles(1);
  89.       lcd.enable = 1;
  90.       delay_us(2);
  91.       lcd.enable = 0;
  92. }
  93.  
  94.  
  95. void lcd_send_byte( BYTE address, BYTE n ) {
  96.  
  97.       lcd.rs = 0;
  98.       while ( bit_test(lcd_read_byte(),7) ) ;
  99.       lcd.rs = address;
  100.       delay_cycles(1);
  101.       lcd.rw = 0;
  102.       delay_cycles(1);
  103.       lcd.enable = 0;
  104.       lcd_send_nibble(n >> 4);
  105.       lcd_send_nibble(n & 0xf);
  106. }
« Última modificación: 21 de Agosto de 2009, 14:06:26 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #3 en: 21 de Agosto de 2009, 13:40:39 »
flex.c

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #4 en: 21 de Agosto de 2009, 13:41:14 »
Controlador Flexible LCD 16x1

Código: CSS
  1. // Flex_lcd_16x1.c
  2.  
  3. // These are randomly assigned pins, used to test
  4. // that the driver can work with any arrangement
  5. // of i/o pins.
  6. #define LCD_DB4   PIN_D4
  7. #define LCD_DB5   PIN_B1
  8. #define LCD_DB6   PIN_C0
  9. #define LCD_DB7   PIN_E0
  10.  
  11. #define LCD_RS    PIN_E2
  12. #define LCD_RW    PIN_B2
  13. #define LCD_E     PIN_D6
  14.  
  15. /*
  16. #define LCD_DB4   PIN_D4
  17. #define LCD_DB5   PIN_D5
  18. #define LCD_DB6   PIN_D6
  19. #define LCD_DB7   PIN_D7
  20.  
  21. #define LCD_RS    PIN_E0
  22. #define LCD_RW    PIN_E1
  23. #define LCD_E     PIN_E2
  24. */
  25.  
  26. // If you want to use a 6-pin interface for your LCD, then
  27. // connect the R/W pin on the LCD to ground and comment
  28. // out the following line.  A 6-pin interface to the LCD
  29. // is used if only have a few free i/o pins available on
  30. // your PIC, and you want to use the smallest possible
  31. // number of pins for the LCD.
  32. #define USE_LCD_RW   1      
  33.  
  34. //========================================
  35. // Use "2 lines" as the lcd type for the 16x1 LCD.
  36. // The LCD is the same as an 8x2 LCD, but with the
  37. // bottom line appended on the right side of the first line.
  38. #define LCD_TYPE  2         // 0=5x7, 1=5x10, 2=2 lines
  39. #define LCD_2ND_HALF_ADDRESS  0x40
  40.  
  41. #define LCD_WIDTH  16
  42. #define LCD_HALF_WIDTH  (LCD_WIDTH/2)
  43.  
  44. int8 const LCD_INIT_STRING[4] =
  45. {
  46.  0x20 | (LCD_TYPE << 2), // Func set: 4-bit, 2 lines, 5x8 dots
  47.  0xc,                    // Display on
  48.  1,                      // Clear display
  49.  6                       // Increment cursor
  50.  };
  51.  
  52. int8 lcd_xcoord;
  53.                              
  54.  
  55. //-------------------------------------
  56. void lcd_send_nibble(int8 nibble)
  57. {
  58. // Note:  !! converts an integer expression
  59. // to a boolean (1 or 0).
  60.  output_bit(LCD_DB4, !!(nibble & 1));
  61.  output_bit(LCD_DB5, !!(nibble & 2));
  62.  output_bit(LCD_DB6, !!(nibble & 4));  
  63.  output_bit(LCD_DB7, !!(nibble & 8));  
  64.  
  65.  delay_cycles(1);
  66.  output_high(LCD_E);
  67.  delay_us(2);
  68.  output_low(LCD_E);
  69. }
  70.  
  71. //-----------------------------------
  72. // This sub-routine is only called by lcd_read_byte().
  73. // It's not a stand-alone routine.  For example, the
  74. // R/W signal is set high by lcd_read_byte() before
  75. // this routine is called.    
  76.  
  77. #ifdef USE_LCD_RW
  78. int8 lcd_read_nibble(void)
  79. {
  80. int8 retval;
  81. // Create bit variables so that we can easily set
  82. // individual bits in the retval variable.
  83. #bit retval_0 = retval.0
  84. #bit retval_1 = retval.1
  85. #bit retval_2 = retval.2
  86. #bit retval_3 = retval.3
  87.  
  88. retval = 0;
  89.  
  90. output_high(LCD_E);
  91. delay_us(1);
  92.  
  93. retval_0 = input(LCD_DB4);
  94. retval_1 = input(LCD_DB5);
  95. retval_2 = input(LCD_DB6);
  96. retval_3 = input(LCD_DB7);
  97.  
  98. output_low(LCD_E);
  99. delay_us(1);
  100.  
  101. return(retval);  
  102. }  
  103. #endif
  104.  
  105. //---------------------------------------
  106. // Read a byte from the LCD and return it.
  107.  
  108. #ifdef USE_LCD_RW
  109. int8 lcd_read_byte(void)
  110. {
  111. int8 low;
  112. int8 high;
  113.  
  114. output_high(LCD_RW);
  115. delay_cycles(1);
  116.  
  117. high = lcd_read_nibble();
  118.  
  119. low = lcd_read_nibble();
  120.  
  121. return((high << 4) | low);
  122. }
  123. #endif
  124.  
  125. //----------------------------------------
  126. // Send a byte to the LCD.
  127. void lcd_send_byte(int8 address, int8 n)
  128. {
  129. output_low(LCD_RS);
  130.  
  131. #ifdef USE_LCD_RW
  132. while(bit_test(lcd_read_byte(), 7)) ;
  133. #else
  134. delay_us(60);
  135. #endif
  136.  
  137. if(address)
  138.   output_high(LCD_RS);
  139. else
  140.   output_low(LCD_RS);
  141.    
  142. delay_cycles(1);
  143.  
  144. #ifdef USE_LCD_RW
  145. output_low(LCD_RW);
  146. delay_cycles(1);
  147. #endif
  148.  
  149. output_low(LCD_E);
  150.  
  151. lcd_send_nibble(n >> 4);
  152. lcd_send_nibble(n & 0xf);
  153. }
  154.  
  155. //----------------------------
  156. void lcd_init(void)
  157. {
  158. int8 i;
  159.  
  160. output_low(LCD_RS);
  161.  
  162. #ifdef USE_LCD_RW
  163. output_low(LCD_RW);
  164. #endif
  165.  
  166. output_low(LCD_E);
  167.  
  168. // Some LCDs require 15 ms minimum delay after
  169. // power-up.  Others require 30 ms.  I'm going
  170. // to set it to 35 ms, so it should work with
  171. // all of them.
  172. delay_ms(35);    
  173.  
  174. for(i=0 ;i < 3; i++)
  175.    {
  176.     lcd_send_nibble(0x03);
  177.     delay_ms(5);
  178.    }
  179.  
  180. lcd_send_nibble(0x02);
  181.  
  182. for(i=0; i < sizeof(LCD_INIT_STRING); i++)
  183.    {
  184.     lcd_send_byte(0, LCD_INIT_STRING[i]);
  185.    
  186.     // If the R/W signal is not used, then
  187.     // the busy bit can't be polled.  One of
  188.    // the init commands takes longer than
  189.    // the hard-coded delay of 60 us, so in
  190.    // that case, lets just do a 5 ms delay
  191.    // after all four of them.
  192.    #ifndef USE_LCD_RW
  193.    delay_ms(5);
  194.    #endif
  195.   }
  196.  
  197. lcd_xcoord = 1;
  198. }
  199.  
  200. //----------------------------
  201. // The x-coordinate can be 1 to 16.
  202. // The y coordinate is ignored.
  203. // This x,y interface is kept in order to be
  204. // consistent with other CCS LCD drivers.
  205. void lcd_gotoxy(int8 x, int8 y)
  206. {
  207. int8 address;
  208.  
  209. // Update the global x-coordinate variable with the
  210. // current x coordinate.
  211. lcd_xcoord = x;
  212.  
  213. // Convert the x-coordinate from CCS format (1-16) to
  214. // the 0-15 format used by the LCD hardware.
  215. address = x - 1;
  216.  
  217. // If the x-coordinate is within the 2nd half of the
  218. // LCD line, the address must be adjusted because
  219. // of the special architecture of the 8x2 LCD.
  220. if(address >= LCD_HALF_WIDTH)
  221.  {  
  222.   address += (LCD_2ND_HALF_ADDRESS - LCD_HALF_WIDTH);
  223.  }
  224.  
  225. lcd_send_byte(0, 0x80 | address);
  226. }
  227.  
  228.  
  229. //-----------------------------
  230. void lcd_putc(char c)
  231. {
  232. switch(c)
  233.   {
  234.    case '\f':
  235.      lcd_send_byte(0,1);
  236.      delay_ms(2);
  237.      lcd_xcoord = 1;
  238.      break;
  239.  
  240.    case '\n':
  241.       lcd_gotoxy(1,1);  //  Goto start of line 1
  242.       break;
  243.  
  244.    case '\b':
  245.       lcd_send_byte(0, 0x10);
  246.       lcd_xcoord--;
  247.       if(lcd_xcoord == LCD_HALF_WIDTH)
  248.          lcd_gotoxy(LCD_HALF_WIDTH, 1);
  249.       break;
  250.  
  251.    default:
  252.       lcd_send_byte(1, c);
  253.       lcd_xcoord++;
  254.       if(lcd_xcoord == (LCD_HALF_WIDTH +1))
  255.          lcd_gotoxy(LCD_HALF_WIDTH +1, 1);
  256.       break;
  257.   }
  258. }
  259.  
  260. //------------------------------
  261. #ifdef USE_LCD_RW
  262. char lcd_getc(int8 x, int8 y)
  263. {
  264. char value;
  265.  
  266. lcd_gotoxy(x,y);
  267.  
  268. // Wait until busy flag is low.
  269. while(bit_test(lcd_read_byte(),7));
  270.  
  271. output_high(LCD_RS);
  272. value = lcd_read_byte();
  273. output_low(lcd_RS);
  274.  
  275. return(value);
  276. }
  277. #endif
« Última modificación: 22 de Agosto de 2009, 09:53:49 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #5 en: 21 de Agosto de 2009, 13:41:44 »

Controlador Flexible 16x2

Código: CSS
  1. // flex_lcd.c
  2.  
  3. // These pins are for the Microchip PicDem2-Plus board,
  4. // which is what I used to test the driver.  Change these
  5. // pins to fit your own board.
  6.  
  7. #define LCD_DB4   PIN_D0
  8. #define LCD_DB5   PIN_D1
  9. #define LCD_DB6   PIN_D2
  10. #define LCD_DB7   PIN_D3
  11.  
  12. #define LCD_E     PIN_A1
  13. #define LCD_RS    PIN_A3
  14. #define LCD_RW    PIN_A2
  15.  
  16. // If you only want a 6-pin interface to your LCD, then
  17. // connect the R/W pin on the LCD to ground, and comment
  18. // out the following line.
  19.  
  20. #define USE_LCD_RW   1    
  21.  
  22. //========================================
  23.  
  24. #define lcd_type 2        // 0=5x7, 1=5x10, 2=2 lines
  25. #define lcd_line_two 0x40 // LCD RAM address for the 2nd line
  26.  
  27.  
  28. int8 const LCD_INIT_STRING[4] =
  29. {
  30.  0x20 | (lcd_type << 2), // Func set: 4-bit, 2 lines, 5x8 dots
  31.  0xc,                    // Display on
  32.  1,                      // Clear display
  33.  6                       // Increment cursor
  34.  };
  35.                              
  36.  
  37. //-------------------------------------
  38. void lcd_send_nibble(int8 nibble)
  39. {
  40. // Note:  !! converts an integer expression
  41. // to a boolean (1 or 0).
  42.  output_bit(LCD_DB4, !!(nibble & 1));
  43.  output_bit(LCD_DB5, !!(nibble & 2));
  44.  output_bit(LCD_DB6, !!(nibble & 4));  
  45.  output_bit(LCD_DB7, !!(nibble & 8));  
  46.  
  47.  delay_cycles(1);
  48.  output_high(LCD_E);
  49.  delay_us(2);
  50.  output_low(LCD_E);
  51. }
  52.  
  53. //-----------------------------------
  54. // This sub-routine is only called by lcd_read_byte().
  55. // It's not a stand-alone routine.  For example, the
  56. // R/W signal is set high by lcd_read_byte() before
  57. // this routine is called.    
  58.  
  59. #ifdef USE_LCD_RW
  60. int8 lcd_read_nibble(void)
  61. {
  62. int8 retval;
  63. // Create bit variables so that we can easily set
  64. // individual bits in the retval variable.
  65. #bit retval_0 = retval.0
  66. #bit retval_1 = retval.1
  67. #bit retval_2 = retval.2
  68. #bit retval_3 = retval.3
  69.  
  70. retval = 0;
  71.  
  72. output_high(LCD_E);
  73. delay_cycles(1);
  74.  
  75. retval_0 = input(LCD_DB4);
  76. retval_1 = input(LCD_DB5);
  77. retval_2 = input(LCD_DB6);
  78. retval_3 = input(LCD_DB7);
  79.  
  80. output_low(LCD_E);
  81.  
  82. return(retval);  
  83. }  
  84. #endif
  85.  
  86. //---------------------------------------
  87. // Read a byte from the LCD and return it.
  88.  
  89. #ifdef USE_LCD_RW
  90. int8 lcd_read_byte(void)
  91. {
  92. int8 low;
  93. int8 high;
  94.  
  95. output_high(LCD_RW);
  96. delay_cycles(1);
  97.  
  98. high = lcd_read_nibble();
  99.  
  100. low = lcd_read_nibble();
  101.  
  102. return( (high<<4) | low);
  103. }
  104. #endif
  105.  
  106. //----------------------------------------
  107. // Send a byte to the LCD.
  108. void lcd_send_byte(int8 address, int8 n)
  109. {
  110. output_low(LCD_RS);
  111.  
  112. #ifdef USE_LCD_RW
  113. while(bit_test(lcd_read_byte(),7)) ;
  114. #else
  115. delay_us(60);
  116. #endif
  117.  
  118. if(address)
  119.   output_high(LCD_RS);
  120. else
  121.   output_low(LCD_RS);
  122.    
  123. delay_cycles(1);
  124.  
  125. #ifdef USE_LCD_RW
  126. output_low(LCD_RW);
  127. delay_cycles(1);
  128. #endif
  129.  
  130. output_low(LCD_E);
  131.  
  132. lcd_send_nibble(n >> 4);
  133. lcd_send_nibble(n & 0xf);
  134. }
  135.  
  136. //----------------------------
  137. void lcd_init(void)
  138. {
  139. int8 i;
  140.  
  141. output_low(LCD_RS);
  142.  
  143. #ifdef USE_LCD_RW
  144. output_low(LCD_RW);
  145. #endif
  146.  
  147. output_low(LCD_E);
  148.  
  149. delay_ms(15);
  150.  
  151. for(i=0 ;i < 3; i++)
  152.   {
  153.    lcd_send_nibble(0x03);
  154.    delay_ms(5);
  155.   }
  156.  
  157. lcd_send_nibble(0x02);
  158.  
  159. for(i=0; i < sizeof(LCD_INIT_STRING); i++)
  160.   {
  161.    lcd_send_byte(0, LCD_INIT_STRING[i]);
  162.  
  163.    // If the R/W signal is not used, then
  164.    // the busy bit can't be polled.  One of
  165.     // the init commands takes longer than
  166.     // the hard-coded delay of 60 us, so in
  167.     // that case, lets just do a 5 ms delay
  168.     // after all four of them.
  169.     #ifndef USE_LCD_RW
  170.     delay_ms(5);
  171.     #endif
  172.    }
  173.  
  174. }
  175.  
  176. //----------------------------
  177.  
  178. void lcd_gotoxy(int8 x, int8 y)
  179. {
  180. int8 address;
  181.  
  182. if(y != 1)
  183.    address = lcd_line_two;
  184. else
  185.    address=0;
  186.  
  187. address += x-1;
  188. lcd_send_byte(0, 0x80 | address);
  189. }
  190.  
  191. //-----------------------------
  192. void lcd_putc(char c)
  193. {
  194.  switch(c)
  195.    {
  196.     case '\f':
  197.       lcd_send_byte(0,1);
  198.       delay_ms(2);
  199.       break;
  200.    
  201.     case '\n':
  202.        lcd_gotoxy(1,2);
  203.        break;
  204.    
  205.     case '\b':
  206.        lcd_send_byte(0,0x10);
  207.        break;
  208.    
  209.     default:
  210.        lcd_send_byte(1,c);
  211.        break;
  212.    }
  213. }
  214.  
  215. //------------------------------
  216. #ifdef USE_LCD_RW
  217. char lcd_getc(int8 x, int8 y)
  218. {
  219. char value;
  220.  
  221. lcd_gotoxy(x,y);
  222.  
  223. // Wait until busy flag is low.
  224. while(bit_test(lcd_read_byte(),7));
  225.  
  226. output_high(LCD_RS);
  227. value = lcd_read_byte();
  228. output_low(lcd_RS);
  229.  
  230. return(value);
  231. }
  232. #endif





« Última modificación: 21 de Agosto de 2009, 17:15:50 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #6 en: 21 de Agosto de 2009, 13:42:00 »
lcd03
« Última modificación: 21 de Agosto de 2009, 13:51:05 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #7 en: 21 de Agosto de 2009, 13:44:28 »

Controlador Flexible LCD 20x4


Código: CSS
  1. // Flex_LCD420.c
  2.  
  3. // These pins are for my Microchip PicDem2-Plus board,
  4. // which I used to test this driver.
  5. // An external 20x4 LCD is connected to these pins.
  6. // Change these pins to match your own board's connections.
  7.  
  8. #define LCD_DB4   PIN_D4
  9. #define LCD_DB5   PIN_D5
  10. #define LCD_DB6   PIN_D6
  11. #define LCD_DB7   PIN_D7
  12.  
  13. #define LCD_RS    PIN_E0
  14. #define LCD_RW    PIN_E1
  15. #define LCD_E     PIN_E2
  16.  
  17. /*
  18. // To prove that the driver can be used with random
  19. // pins, I also tested it with these pins:
  20. #define LCD_DB4   PIN_D4
  21. #define LCD_DB5   PIN_B1
  22. #define LCD_DB6   PIN_C5
  23. #define LCD_DB7   PIN_B5
  24.  
  25. #define LCD_RS    PIN_E2
  26. #define LCD_RW    PIN_B2
  27. #define LCD_E     PIN_D6
  28. */
  29.  
  30. // If you want only a 6-pin interface to your LCD, then
  31. // connect the R/W pin on the LCD to ground, and comment
  32. // out the following line.  Doing so will save one PIC
  33. // pin, but at the cost of losing the ability to read from
  34. // the LCD.  It also makes the write time a little longer
  35. // because a static delay must be used, instead of polling
  36. // the LCD's busy bit.  Normally a 6-pin interface is only
  37. // used if you are running out of PIC pins, and you need
  38. // to use as few as possible for the LCD.
  39. #define USE_RW_PIN   1    
  40.  
  41.  
  42. // These are the line addresses for most 4x20 LCDs.
  43. #define LCD_LINE_1_ADDRESS 0x00
  44. #define LCD_LINE_2_ADDRESS 0x40
  45. #define LCD_LINE_3_ADDRESS 0x14
  46. #define LCD_LINE_4_ADDRESS 0x54
  47.  
  48. // These are the line addresses for LCD's which use
  49. // the Hitachi HD66712U controller chip.
  50. /*
  51. #define LCD_LINE_1_ADDRESS 0x00
  52. #define LCD_LINE_2_ADDRESS 0x20
  53. #define LCD_LINE_3_ADDRESS 0x40
  54. #define LCD_LINE_4_ADDRESS 0x60
  55. */
  56.  
  57.  
  58. //========================================
  59.  
  60. #define lcd_type 2   // 0=5x7, 1=5x10, 2=2 lines(or more)
  61.  
  62. int8 lcd_line;
  63.  
  64. int8 const LCD_INIT_STRING[4] =
  65. {
  66. 0x20 | (lcd_type << 2),  // Set mode: 4-bit, 2+ lines, 5x8 dots
  67. 0xc,                     // Display on
  68. 1,                       // Clear display
  69. 6                        // Increment cursor
  70. };
  71.                            
  72.  
  73. //-------------------------------------
  74. void lcd_send_nibble(int8 nibble)
  75. {
  76. // Note:  !! converts an integer expression
  77. // to a boolean (1 or 0).
  78. output_bit(LCD_DB4, !!(nibble & 1));
  79. output_bit(LCD_DB5, !!(nibble & 2));
  80. output_bit(LCD_DB6, !!(nibble & 4));  
  81. output_bit(LCD_DB7, !!(nibble & 8));  
  82.  
  83. delay_cycles(1);
  84. output_high(LCD_E);
  85. delay_us(2);
  86. output_low(LCD_E);
  87. }
  88.  
  89. //-----------------------------------
  90. // This sub-routine is only called by lcd_read_byte().
  91. // It's not a stand-alone routine.  For example, the
  92. // R/W signal is set high by lcd_read_byte() before
  93. // this routine is called.    
  94.  
  95. #ifdef USE_RW_PIN
  96. int8 lcd_read_nibble(void)
  97. {
  98. int8 retval;
  99. // Create bit variables so that we can easily set
  100. // individual bits in the retval variable.
  101. #bit retval_0 = retval.0
  102. #bit retval_1 = retval.1
  103. #bit retval_2 = retval.2
  104. #bit retval_3 = retval.3
  105.  
  106. retval = 0;
  107.    
  108. output_high(LCD_E);
  109. delay_us(1);
  110.  
  111. retval_0 = input(LCD_DB4);
  112. retval_1 = input(LCD_DB5);
  113. retval_2 = input(LCD_DB6);
  114. retval_3 = input(LCD_DB7);
  115.  
  116. output_low(LCD_E);
  117. delay_us(1);
  118.    
  119. return(retval);  
  120. }  
  121. #endif
  122.  
  123. //---------------------------------------
  124. // Read a byte from the LCD and return it.
  125.  
  126. #ifdef USE_RW_PIN
  127. int8 lcd_read_byte(void)
  128. {
  129. int8 low;
  130. int8 high;
  131.  
  132. output_high(LCD_RW);
  133. delay_cycles(1);
  134.  
  135. high = lcd_read_nibble();
  136.  
  137. low = lcd_read_nibble();
  138.  
  139. return( (high<<4) | low);
  140. }
  141. #endif
  142.  
  143. //----------------------------------------
  144. // Send a byte to the LCD.
  145. void lcd_send_byte(int8 address, int8 n)
  146. {
  147. output_low(LCD_RS);
  148.  
  149. #ifdef USE_RW_PIN
  150. while(bit_test(lcd_read_byte(),7)) ;
  151. #else
  152. delay_us(60);
  153. #endif
  154.  
  155. if(address)
  156.    output_high(LCD_RS);
  157. else
  158.    output_low(LCD_RS);
  159.      
  160.  delay_cycles(1);
  161.  
  162. #ifdef USE_RW_PIN
  163. output_low(LCD_RW);
  164. delay_cycles(1);
  165. #endif
  166.  
  167. output_low(LCD_E);
  168.  
  169. lcd_send_nibble(n >> 4);
  170. lcd_send_nibble(n & 0xf);
  171. }
  172. //----------------------------
  173.  
  174. void lcd_init(void)
  175. {
  176. int8 i;
  177.  
  178. lcd_line = 1;
  179.  
  180. output_low(LCD_RS);
  181.  
  182. #ifdef USE_RW_PIN
  183. output_low(LCD_RW);
  184. #endif
  185.  
  186. output_low(LCD_E);
  187.  
  188. // Some LCDs require 15 ms minimum delay after
  189. // power-up.  Others require 30 ms.  I'm going
  190. // to set it to 35 ms, so it should work with
  191. // all of them.
  192. delay_ms(35);        
  193.  
  194. for(i=0 ;i < 3; i++)
  195.   {
  196.    lcd_send_nibble(0x03);
  197.    delay_ms(5);
  198.   }
  199.  
  200. lcd_send_nibble(0x02);
  201.  
  202. for(i=0; i < sizeof(LCD_INIT_STRING); i++)
  203.   {
  204.    lcd_send_byte(0, LCD_INIT_STRING[i]);
  205.  
  206.    // If the R/W signal is not used, then
  207.    // the busy bit can't be polled.  One of
  208.     // the init commands takes longer than
  209.     // the hard-coded delay of 50 us, so in
  210.     // that case, lets just do a 5 ms delay
  211.     // after all four of them.
  212.     #ifndef USE_RW_PIN
  213.     delay_ms(5);
  214.     #endif
  215.    }
  216.  
  217. }
  218.  
  219. //----------------------------
  220.  
  221. void lcd_gotoxy(int8 x, int8 y)
  222. {
  223. int8 address;
  224.  
  225.  
  226. switch(y)
  227.   {
  228.    case 1:
  229.      address = LCD_LINE_1_ADDRESS;
  230.      break;
  231.  
  232.    case 2:
  233.      address = LCD_LINE_2_ADDRESS;
  234.      break;
  235.  
  236.    case 3:
  237.      address = LCD_LINE_3_ADDRESS;
  238.      break;
  239.  
  240.    case 4:
  241.      address = LCD_LINE_4_ADDRESS;
  242.      break;
  243.  
  244.    default:
  245.      address = LCD_LINE_1_ADDRESS;
  246.      break;
  247.      
  248.   }
  249.  
  250. address += x-1;
  251. lcd_send_byte(0, 0x80 | address);
  252. }
  253.  
  254. //-----------------------------
  255. void lcd_putc(char c)
  256. {
  257.  switch(c)
  258.    {
  259.     case '\f':
  260.       lcd_send_byte(0,1);
  261.       lcd_line = 1;
  262.       delay_ms(2);
  263.       break;
  264.    
  265.     case '\n':
  266.        lcd_gotoxy(1, ++lcd_line);
  267.        break;
  268.    
  269.     case '\b':
  270.        lcd_send_byte(0,0x10);
  271.        break;
  272.    
  273.     default:
  274.        lcd_send_byte(1,c);
  275.        break;
  276.    }
  277. }
  278.  
  279. //------------------------------
  280. #ifdef USE_RW_PIN
  281. char lcd_getc(int8 x, int8 y)
  282. {
  283. char value;
  284.  
  285. lcd_gotoxy(x,y);
  286.  
  287. // Wait until busy flag is low.
  288. while(bit_test(lcd_read_byte(),7));
  289.  
  290. output_high(LCD_RS);
  291. value = lcd_read_byte();
  292. output_low(LCD_RS);
  293.  
  294. return(value);
  295. }
  296. #endif
« Última modificación: 22 de Agosto de 2009, 09:52:56 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #8 en: 21 de Agosto de 2009, 13:58:42 »
LCD_05

drivers personalizados

Código: CSS
  1. http://www.todopic.com.ar/foros/index.php?topic=14552.20
  2.  
  3.  
  4.  
  5. /******************************************************/
  6. /*
  7. /* Manejo de un Display de Cristal Liquido LCD 16X4   */
  8. /* de 16 caracteres por 4 lineas, el LCD se encuentra */
  9. /* conectado en el Puerto D del microcontrolador, con */
  10. /* una interface de 4 lineas:                         */
  11. /*
  12. /* LCD PIC
  13. /* ------------------------------------
  14. /* D4  (11) --> RD0
  15. /* D5  (12) --> RD1
  16. /* D6  (13) --> RD2
  17. /* D7  (14) --> RD3
  18. /*
  19. /* RS  (4) --> RD4
  20. /* RW  (5) --> RD5
  21. /* E   (6) --> RD6
  22. /*
  23. /******************************************************/
  24. /* Conexiones del LCD al Microcontrolador */
  25. //#include <delays.h>
  26.  
  27. #define RS RD4
  28. #define RW RD5
  29. #define E  RD6
  30.  
  31. /* Configuracion del Display y cursor */
  32.  
  33. #define DON              0b00001111  /* Display encendido   */
  34. #define DOFF             0b00001011  /* Display apagado     */
  35. #define CURSOR_HOME      0b00000010  /* Cursor encendido    */
  36. #define CURSOR_ON        0b00001111  /* Cursor encendido    */
  37. #define CURSOR_OFF       0b00001101  /* Cursor apagado      */
  38. #define BLINK_ON         0b00001111  /* Cursor con parpadeo */
  39. #define BLINK_OFF        0b00001110  /* Cursor sin parpadeo */
  40. #define CLEAR            0b00000001  /* Display encendido   */
  41.  
  42.  
  43. /* Modo de entrada */
  44.  
  45. #define INCREMENT 0b00000110  /* Incrementa la posicion del cursor */
  46. #define DECREMENT 0b00000100  /* Decrementa la posicion del cursor */
  47.  
  48. /* Configuracion de los desplazamientos del cursor y del Display*/
  49.  
  50. #define SHIFT_CUR_LEFT   0b00010011  /* Corrimiento del cursor a la izquierda  */
  51. #define SHIFT_CUR_RIGHT  0b00010111  /* Corrimiento del cursor a la derecha    */
  52. #define SHIFT_DISP_LEFT  0b00011011  /* Corrimiento del display a la izquierda */
  53. #define SHIFT_DISP_RIGHT 0b00011111  /* Corrimiento del display a la derecha   */
  54.  
  55. /* Funciones de inicializacion */
  56.  
  57. #define NIBLE            0b00000010  /* interface a 4 bits */
  58. #define FOUR_BIT         0b00101111  /* Interface a 4-bit  */
  59. #define EIGHT_BIT        0b00111111  /* Interface a 8-bit  */
  60. #define LINE_5X7         0b00110011  /* Una linea, caracter 5x7 */
  61.  
  62. #define LINE_5X10 0b00110111  /* Una linea, caracter 5x10 */
  63. #define LINES_5X7 0b00111011  /* Dos lineas. character 5x7 */
  64.  
  65.  
  66.  
  67. /* Lineas de trabajo */
  68.  
  69.  
  70.  
  71. #define DDRAM_LINEA_1 0b10000000 /* Linea 1 */
  72. #define DDRAM_LINEA_2 0b11000000 /* Linea 2 */
  73. #define DDRAM_LINEA_3 0b10010000 /* Linea 3 */
  74. #define DDRAM_LINEA_4 0b11010000 /* Linea 4 */
  75.  
  76. /* Configuracion del puerto */
  77.  
  78. #define Config_Puerto     0b00000000          // RC<0:6> como salidas
  79. #define Mascara           0b00001111          // Parte baja del Byte  
  80.  
  81. /* Prototipos de funciones */
  82.  
  83. void Inicializa_LCD(void);                // Inicializa LCD
  84. void Comando_LCD(char);                   // Indica al LCD un comando
  85. void Dato_LCD(char);                      // Indica al LCD un caracter
  86.  
  87. /* Inicializacion del LCD segun fabricante */
  88.  
  89. void Inicializa_LCD(void)
  90. {
  91.  
  92.   int i;
  93.   PORTD = 0;                     // Puerto D a utilizar E = RS = RW = 0
  94.   TRISD = Config_Puerto;
  95.  
  96.   Delay10TCYx(110);           /* Retardo mayor a 100 useg. */
  97.   PORTD = NIBLE;              /* Interface de 4 bits RC<0:3> */
  98.   PORTDbits.RS = 0;           /* RS = 0 */
  99.   PORTDbits.RW = 0;           /* RW = 0 */
  100.   PORTDbits.E = 1;            /* E = 1 */
  101.   Delay1KTCYx(5);             /* Retardo de 5 mseg. */
  102.   PORTDbits.E = 0;            /* E = 0 */
  103.   Delay1KTCYx(15);            /* Retardo de 15 mseg. */
  104.  
  105. /* Funcion set,  DL = 0, N = 1, F = 0 */
  106. /* Dos lineas, caracter de 5X7 e interface a 4 bits */
  107.   Comando_LCD(LINES_5X7 & FOUR_BIT);
  108.  
  109. /* Display ON, D = 1, C = 1, B = 1 */
  110. /* Display encendido, cursor encendido con parpadeo */
  111.  
  112.   Comando_LCD(DON & CURSOR_ON & BLINK_ON);
  113.  
  114. /* limpia el display */
  115.   Comando_LCD(CLEAR);
  116.  
  117.  
  118. /* Entry Mode Set */
  119. /* Incremento del cursor */
  120.  
  121.   Comando_LCD(INCREMENT);
  122.  
  123. }
  124.  
  125.  
  126. /* Indica al LCD comandos */
  127. void Comando_LCD(char dato)
  128. {
  129.   char temp;                   /* variable auxiliar */
  130.   Delay1KTCYx(10);             /* Retardo de 10 mseg. */
  131.   temp = dato;                 /* Respaldo del dato original */
  132.   dato = dato >> 4;            /* Corrimiento del nible alto */
  133.   PORTD = dato & Mascara;      /* Manda parte alta del nible */
  134.   PORTDbits.RS = 0;            /* Indica que es un comando */
  135.   PORTDbits.RW = 0;            /* RW = 0 */
  136.   PORTDbits.E = 1;             /* Pone a E = 1 */
  137.   Delay1KTCYx(5);              /* Retardo de 5 mseg. */
  138.  
  139.   PORTDbits.E = 0;             /* Pone a E = 0 */
  140.   PORTD = temp & Mascara;      /* Parte baja del Byte */
  141.   PORTDbits.RS = 0;            /* Indica que es un comando */
  142.   PORTDbits.RW = 0;            /* RW = 0 */
  143.   PORTDbits.E = 1;             /* Pone a E = 1 */
  144.   Delay1KTCYx(5);              /* Retardo de 5 mseg. */
  145.   PORTDbits.E = 0;             /* Pone a E = 0 */
  146.  
  147. }
  148.  
  149. /* Manda datos al LCD */
  150.  
  151. void Dato_LCD(char dato)
  152. {
  153.   char temp;                   /* variable auxiliar */
  154.   Delay1KTCYx(10);             /* Retardo de 10 mseg. */
  155.   temp = dato;                 /* Respaldo del dato original */
  156.   dato = dato >> 4;            /* Corrimiento del nible alto */
  157.   PORTD = dato & Mascara;      /* Manda parte alta del nible */
  158.   PORTDbits.RS = 1;            /* Indica que es un caracter */
  159.   PORTDbits.RW = 0;            /* RW = 0 */
  160.   PORTDbits.E = 1;             /* Pone a E = 1 */
  161.   Delay1KTCYx(5);              /* Retardo de 5 mseg. */
  162.   PORTDbits.E = 0;             /* Pone a E = 0 */
  163.   PORTD = temp & Mascara;      /* Parte baja del Byte */
  164.   PORTDbits.RS = 1;            /* RS = 0 */
  165.   PORTDbits.RW = 0;            /* RW = 0 */
  166.   PORTDbits.E = 1;             /* Pone a E = 1 */
  167.   Delay1KTCYx(5);              /* Retardo de 5 mseg. */
  168.   PORTDbits.E = 0;             /* Pone a E = 0 */
  169. }





otro

Código: CSS
  1. http://www.todopic.com.ar/foros/index.php?topic=24085.20
  2.  
  3.  
  4.  
  5. /*
  6.  Yo estoy usando el compilador CCS 4.08 y el pic16f874a no sé si es un bugs o es que me enviaron para callarme,
  7.  un código cualquiera de internet por favor ayúdenme le agradesco por su comprensión.
  8. Pd aquí le envió el código completo que me entrego la empresa para el LCD:*/
  9.  
  10. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. //                                                    LCD.H                                                     //
  12. //                                                VERSION   2.1                                                 //
  13. //                                                 JULIO 2009                                                   //
  14. //                                                                                                              //
  15. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. #nolist
  17.  
  18.                                    // DEFINICION DE CONSTANTES PARA USO EN FUNCIONES
  19. #define HOME             0x02      // PARA PONER CURSOR EN LA POSICION 0 DEL DISPLAY. USAR CON WRITE_LCD_COM
  20. #define INCREASE         0x02      // INCREMENTA CURSOR. USAR CON LCD.ID
  21. #define DECREASE         0x00      // DECREMENTA CURSOS. USAR CON LCD.ID
  22. #define SHIFT_ON         0x01      // SHIFT DISPLAY. USAR CON LCD.SHIFT
  23. #define SHIFT_OFF        0x00      // NO SHIFT DISPLAY. USAR CON LCD.SHIFT
  24. #define LCD_ON           0x04      // ACTIVA LCD. USAR CON LCD.MODE
  25. #define LCD_OFF          0x00      // DESACTIVA LCD. USAR CON LCD.MODE
  26. #define CURSOR_ON        0x02      // ACTIVA CURSOR. USAR CON LCD.CURSOR
  27. #define CURSOR_OFF       0x00      // DESACTIVA CURSOR. USAR CON LCD.CURSOR
  28. #define BLINK_ON         0x01      // ACTIVA PARPADEO DEL CURSOR. USAR CON LCD.BLINK  
  29. #define BLINK_OFF        0x00      // DESACTIVA PARPADEO DEL CURSOR. USAR CON LCD.BLINK  
  30. #define BUS_8            0x10      // DEFINE BUS DE 8 BITS. USAR CON LCD.INTERFASE
  31. #define BUS_4            0x00      // DEFINE BUS DE 4 BITS. USAR CON LCD.INTERFASE
  32. #define CLEAR            0x01      // LIMPIA DISPLAY. USAR CON FUNCION WRITE_LCD_DATA()
  33. #define NOCLEAR          0x00      // NO LIMPIA DISPLAY. USAR CON FUNCION WRITE_LCD_DATA()
  34. #define LINE1            0x00      // PRIMERA DIRECCION DE LINEA 1.
  35. #define LINE2            0x40      // PRIMERA DIRECCION DE LINEA 2.
  36. #define LINE3            0x14      // PRIMERA DIRECCION DE LINEA 3.
  37. #define LINE4            0x54      // PRIMERA DIRECCION DE LINEA 4.
  38. #define LARGO            16        // Editar esta línea para indicar la cantidad de caracteres por línea del display
  39. #define LINEAS           2         // Editar este línea para indicar la cantidad de líneas que dispone el display
  40. #define NULL             0x00
  41.  
  42. #list
  43.  
  44. struct
  45. {
  46.    char TEXT[LARGO+1];             // define texto para escribir en lcd  
  47.    int ID;                         // define si el cursor de incrementará o decrementará
  48.    int SHIFT;                      // define el shift del cursor
  49.    int MODE;                       // define si el display está ON ú OFF
  50.    int CURSOR;                     // define si el cursor estará visible
  51.    int BLINK;                      // define si el cursor parpadeará
  52.    int INTERFASE;                  // define interfase de 4/8 bits
  53. } lcd;
  54.  
  55. // CONFIGURAR AQUI LOS PUERTO QUE SE USARAN PARA BUS DE DATOS Y DE CONTROL
  56.  
  57. #byte DataBus = PORTD              // DEFINE BUS DE DATOS
  58. #byte Tris_DataBus = TRISD         // DEFINE TRIS DE BUS DE DATOS
  59. #byte ControlBus = PORTE           // DEFINE BUS DE CONTROL
  60. #byte Tris_ControlBus = TRISE      // DEFINE TRIS DEL BUS DE CONTROL
  61.  
  62.  
  63. // GENERA PULSOS DE ESCRITURA PARA COMANDOS DEL DISPLAY Y SACA DATO
  64.  
  65. void WriteLcdComData(int x)
  66. {
  67.    Tris_ControlBus = 0xF8 & Tris_ControlBus;
  68.    Tris_DatalBus = 0x00;
  69.    DataBus = ControlBus = 0x00;
  70.    ControlBus = 3;
  71.    ControlBus = 0;
  72.    ControlBus = 4;
  73.    DataBus = x;
  74.    ControlBus = 0;
  75.    ControlBus = 3;
  76.    delay_us(50);
  77. }
  78.  
  79. // LIMPIA DISPLAY
  80. void ClearLcd(void)
  81. {
  82.    WriteLcdComData(CLEAR);               // display clear
  83.    delay_ms(2);
  84. }
  85.  
  86. // INICIALIZA EL EL DISPLAY
  87.  void IniLcd(void)
  88. {
  89.    delay_ms(20);
  90.    WriteLcdComData(0x30);
  91.    delay_ms(5);  
  92.    WriteLcdComData(0x30);
  93.    delay_us(150);
  94.    WriteLcdComData(0x30);  
  95. }
  96.  
  97. // CONFIGURA EL DISPLAY. LOS DATOS PARA LA CONFIGURACION DEBEN SER PUERTOS ANTES DE LLAMAR
  98. // A ESTA FUNCION. LOS DATOS SON PARTE DE LA ESTRUCTURA LCD.
  99. void SetupLcd(void)
  100. {
  101.    WriteLcdComData(0x04 | lcd.ID | lcd.SHIFT);                     //ENTRY MODE SET
  102.    WriteLcdComData(0x08 | lcd.MODE | lcd.CURSOR | lcd.BLINK);      // DISPLAY ON OFF
  103.    WriteLcdComData(0x28 | lcd.INTERFASE);                          // SET FUNCTION
  104. }
  105.  
  106. // ESCRIBE DATOS EN EL DISPLAY. X REPRSENTA DESDE QUE DIRECCIÓN DEL DISPLAY SE DESEAN ESCRIBIR LOS DATOS
  107. // E Y INDICA SE SE LIMPIARÁ EL DISPLAY ANTES DE ESCRIBIR
  108. // SI C = CLEAN, EL DISPLAY ES LIMPIADO
  109. // SI C = NOCLEAN, SOLO SE ESCRIBEN LOS DATOS DESDE LA POCISION ESPECIFICADA POR POS
  110. // LOS DATOS A ESCRIBIR DEBE SER COPIADOS PREVIAMENTE EN EL ELEMENTO LCD.TEXT DE LA ESCTRUCTIRA LCD
  111. void WriteLcd(int pos, int c)
  112. {
  113.    int *p;
  114.    Tris_ControlBus = 0xF8 & Tris_ControlBus;
  115.    Tris_DatalBus = 0x00;
  116.    DataBus = ControlBus =0x00;
  117.    if (c)
  118.       {
  119.          clear_lcd();
  120.          delay_ms(2);
  121.       }
  122.    pos|= 0x80;
  123.    WriteLcdComData(pos);
  124.    p = lcd.TEXT;
  125.    for (; *p!=NULL; p++)
  126.    {  
  127.       DataBus = 0xff;
  128.       ControlBus = 2;
  129.       ControlBus = 1;
  130.       ControlBus = 5;
  131.       DataBus = *p;
  132.       ControlBus = 1;
  133.       ControlBus = 2;
  134.       delay_us(50);
  135.    }
  136. }
  137.  
  138. void PutcLcd(int ini, char data)
  139. {
  140.    if (ini != CURRENT)
  141.       {
  142.          ini|= 0x80;
  143.          WriteLcdComData(ini);
  144.       }
  145.    Tris_ControlBus = 0xF8 & Tris_ControlBus;
  146.    Tris_DatalBus = ControlBus = 0x00;
  147.    DataBus = 0xff;
  148.    ControlBus = 2;
  149.    ControlBus = 1;
  150.    ControlBus = 5;
  151.    DataBus = data;
  152.    ControlBus = 1;
  153.    ControlBus = 2;
  154.    delay_us(50);
  155. }
  156.  
  157. void ClearLcdLine(int x)
  158. {
  159.    int y;
  160.    for (y=0; y<=LARGO; y++)
  161.       {
  162.          lcd.TEXT[y] = ' ';
  163.       }
  164.    lcd.TEXT[++y] = NULL;
  165.    switch (x)
  166.       {
  167.          case 1   :   WriteLcd(LINE1,NOCLEAR);
  168.                         break;
  169.          case 2   :   WriteLcd(LINE2,NOCLEAR);
  170.                         break;
  171.          case 3  :   WriteLcd(LINE3,NOCLEAR);
  172.                         break;
  173.          case 4  :   WriteLcd(LINE4,NOCLEAR);
  174.                         break;
  175.          default :   break;
  176.       }
  177. }








« Última modificación: 27 de Agosto de 2009, 08:00:05 por pajaro »

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #9 en: 21 de Agosto de 2009, 13:59:56 »


Código: CSS
  1. /////////////////////////////////////////////////////////////////////////
  2. ////                             KS0108.c                            ////
  3. ////                                                                 ////
  4. //// This file contains drivers for using a KS0108 LCD controller    ////
  5. //// for a 64 by 64 pixel display. The driver treats the upper left  ////
  6. //// pixel as (0,0).                                                 ////
  7. ////                                                                 ////
  8. //// Use #define FAST_GLCD if the target chip has at least 512 bytes ////
  9. //// of RAM to decrease the time it takes to update the display.     ////
  10. //// KS0108_update() must then be called to update the display after ////
  11. //// changing the display data.                                      ////
  12. //// See HDM64GS12.c for controlling a display with two controllers  ////
  13. /////////////////////////////////////////////////////////////////////////
  14. ////                                                                 ////
  15. ////  KS0108_init(int1 mode)                                         ////
  16. ////     * Must be called before any other function                  ////
  17. ////     * TRUE turns the display on, FALSE turns display off        ////
  18. ////                                                                 ////
  19. ////  KS0108_fill()                                                  ////
  20. ////     * Fills the entire LCD with the given data                  ////
  21. ////       - color can be ON or OFF                                  ////
  22. ////                                                                 ////
  23. ////  KS0108_pixel(int8 x, int8 y, int1 color)                       ////
  24. ////     * Set the color of the pixel at (x,y)                       ////
  25. ////     * TRUE turns the pixel on, FALSE turns the pixel off        ////
  26. ////                                                                 ////
  27. ////  KS0108_update()                                                ////
  28. ////     * Write the display data stored in RAM to the LCD           ////
  29. ////     * Only available if FAST_GLCD is defined                    ////
  30. ////                                                                 ////
  31. ////  int8 KS0108_status()                                           ////
  32. ////     * Read the status register                                  ////
  33. ////       7:    0 - Ready   1 - Busy                                ////
  34. ////       6:    0                                                   ////
  35. ////       5:    0 - On      1 - Off                                 ////
  36. ////       4:    0 - Normal  1 - Reset                               ////
  37. ////       3-0:  0                                                   ////
  38. ////                                                                 ////
  39. ////  KS0108_topRow(row)                                             ////
  40. ////     * Specify the row appearing at the top of the screen (0-63) ////
  41. ////                                                                 ////
  42. ////  KS0108_on()                                                    ////
  43. ////     * Turn the display on                                       ////
  44. ////                                                                 ////
  45. ////  KS0108_off()                                                   ////
  46. ////     * Turn the display off                                      ////
  47. ////                                                                 ////
  48. ////  KS0108_page(page)                                              ////
  49. ////     * Set the page number (0-7)                                 ////
  50. ////                                                                 ////
  51. ////  KS0108_column(column)                                          ////  
  52. ////     * Set the column address (0-63)                             ////
  53. ////                                                                 ////
  54. ////  KS0108_inst()                                                  ////
  55. ////     * Specify reads and writes are instructions                 ////
  56. ////                                                                 ////
  57. ////  KS0108_data()                                                  ////
  58. ////     * Specify reads and writes are data                         ////
  59. ////                                                                 ////
  60. /////////////////////////////////////////////////////////////////////////
  61. ////        (C) Copyright 1996, 2004 Custom Computer Services        ////
  62. //// This source code may only be used by licensed users of the CCS  ////
  63. //// C compiler.  This source code may only be distributed to other  ////
  64. //// licensed users of the CCS C compiler.  No other use,            ////
  65. //// reproduction or distribution is permitted without written       ////
  66. //// permission.  Derivative programs created using this software    ////
  67. //// in object code form are not restricted in any way.              ////
  68. /////////////////////////////////////////////////////////////////////////
  69.  
  70. #ifndef KS0108
  71. #define KS0108
  72.  
  73. #ifndef KS0108_CS
  74. #define KS0108_CS             PIN_B0   // Chip select
  75. #endif
  76. #ifndef KS0108_DI                      //      ___________
  77. #define KS0108_DI             PIN_B2   // Data/Instruction
  78. #endif
  79.  
  80. #ifndef KS0108_RW                      //      _____
  81. #define KS0108_RW             PIN_B4   // Read/Write
  82. #endif
  83.  
  84. #ifndef KS0108_E
  85. #define KS0108_E              PIN_B5   // Enable
  86. #endif
  87.  
  88. #ifndef KS0108_RST
  89. #define KS0108_RST            PIN_C0   // Reset
  90. #endif
  91.  
  92.  
  93. #define KS0108_CMD_ON         0x3F
  94. #define KS0108_CMD_OFF        0x3E
  95. #define KS0108_CMD_PAGE       0xB8
  96. #define KS0108_CMD_COLUMN     0x40
  97. #define KS0108_CMD_TOP_RAM    0xC0
  98.  
  99.  
  100. #ifdef FAST_GLCD
  101. int8 displayData[512];
  102. #endif
  103.  
  104.  
  105. void KS0108_init(int1 mode);
  106. void KS0108_fill(data);
  107. void KS0108_pixel(int8 x, int8 y, int1 color);
  108. int8 KS0108_status();
  109. void KS0108_write(int8 data);
  110. int8 KS0108_read();
  111.  
  112.  
  113. // Purpose:    Specify the row appearing at the top of the screen
  114. // Inputs:     The row number (0 - 63)
  115. #define KS0108_topRow(row)     KS0108_write(KS0108_CMD_TOP_RAM | row)
  116.  
  117.  
  118. // Purpose:    Turn the display on
  119. #define KS0108_on()            KS0108_write(KS0108_CMD_ON)
  120.  
  121.  
  122. // Purpose:    Turn the display off
  123. #define KS0108_off()           KS0108_write(KS0108_CMD_OFF)
  124.  
  125.  
  126. // Purpose:    Set the page number
  127. // Inputs:     A page number (0 - 7)
  128. #define KS0108_page(page)      KS0108_write(KS0108_CMD_PAGE | page)
  129.  
  130.  
  131. // Purpose:    Set the column address
  132. // Inputs:     The column address (0 - 63)
  133. #define KS0108_column(column)  KS0108_write(KS0108_CMD_COLUMN | column)
  134.  
  135. // Purpose:    Specify reads and writes are instructions
  136. #define KS0108_inst()          output_low(KS0108_DI);
  137.  
  138.  
  139. // Purpose:    Specify reads and writes are data
  140. #define KS0108_data()          output_high(KS0108_DI);
  141.  
  142.  
  143. // Purpose:    Initialize the display controller
  144. // Inputs:     TRUE  - Initialize with the display on
  145. //             FALSE - Initialize with the display off
  146. void KS0108_init(int1 mode)
  147. {
  148.    // Initialze some pins
  149.    output_high(KS0108_RST);
  150.    output_low(KS0108_E);
  151.    output_low(KS0108_CS);
  152.  
  153.    KS0108_inst();             // Set for instruction
  154.    KS0108_topRow(0);          // First RAM line at the top of the screen
  155.    KS0108_page(0);            // Set the page address to 0
  156.    KS0108_column(0);          // Set the column address to 0
  157.  
  158.    if(mode)
  159.       KS0108_on();            // Turn the display on
  160.    else
  161.       KS0108_off();           // Turn the display off
  162.  
  163.    KS0108_fill(0);            // Clear the display
  164. }
  165.  
  166.  
  167. // Purpose:    Fill a display with a vertical byte pattern
  168. // Inputs:     The byte of data to fill with
  169. void KS0108_fill(data)
  170. #ifdef FAST_GLCD
  171. {
  172.    int16 i;
  173.  
  174.    for(i=0; i<512; ++i)
  175.    {
  176.       displayData[i] = data;
  177.    }
  178. }
  179. #else
  180. {
  181.    int8 i, j;
  182.  
  183.    // Loop through the vertical pages
  184.    for(i = 0; i < 8; ++i)
  185.    {
  186.       KS0108_inst();          // Set for instruction
  187.       KS0108_page(i);         // Set page address
  188.       KS0108_column(0);       // Set horizontal address to 0
  189.       KS0108_data();          // Set for data
  190.  
  191.       // Loop through the horizontal sections
  192.       for(j = 0; j < 64; ++j)
  193.       {
  194.          KS0108_write(data);  // Write the byte of data
  195.       }
  196.    }
  197. }
  198. #endif
  199.  
  200.  
  201. // Purpose:    Turn the pixel at (x,y) on or off
  202. // Inputs:     1) The x coordinate (0 - 63)
  203. //             2) The y coordinate (0 - 63)
  204. //             3) TRUE turns the pixel on, FALSE turns the pixel off
  205. void KS0108_pixel(int8 x, int8 y, int1 color)
  206. #ifdef FAST_GLCD
  207. {
  208.    int16 index = 64;
  209.    index *=  y/8;
  210.    index += x;
  211.  
  212.    if(color)
  213.    {
  214.       bit_set(displayData[index], y%8);
  215.    }
  216.    else
  217.    {
  218.       bit_clear(displayData[index], y%8);
  219.    }
  220. }
  221. #else
  222. {
  223.    int8 data;
  224.  
  225.    KS0108_inst();             // Set for instruction
  226.    KS0108_column(x);          // Set the horizontal address
  227.    KS0108_page((y/8));        // Set the page address
  228.    KS0108_data();             // Set for data
  229.    KS0108_read();             // Need two reads to get data at new address
  230.    data = KS0108_read();
  231.  
  232.    if(color)
  233.       bit_set(data, y%8);     // Turn the pixel on
  234.    else                       // or
  235.       bit_clear(data, y%8);   // turn the pixel off
  236.  
  237.    KS0108_inst();             // Set for instruction
  238.    KS0108_column(x);          // Set the horizontal address
  239.    KS0108_data();             // Set for data
  240.    KS0108_write(data);        // Write the pixel data
  241. }
  242. #endif
  243.  
  244.  
  245. // Purpose:    Update the LCD with data from the display array
  246. #ifdef FAST_GLCD
  247. void KS0108_update()
  248. {
  249.    int8 i, j;
  250.    int8 *p;
  251.    
  252.    p = displayData;
  253.  
  254.    // Loop through the vertical pages
  255.    for(i = 0; i < 8; ++i)
  256.    {
  257.       KS0108_inst();          // Set for instruction
  258.       KS0108_page(i);         // Set page address
  259.       KS0108_column(0);       // Set horizontal address to 0
  260.       KS0108_data();          // Set for data
  261.  
  262.       // Loop through the horizontal sections
  263.       for(j = 0; j < 64; ++j)
  264.       {
  265.          KS0108_write(*p++);  // Write the byte of data
  266.       }
  267.    }
  268. }
  269. #endif
  270.  
  271.  
  272. // Purpose:    Read the status register
  273. // 7:    0 - Ready   1 - Busy
  274. // 6:    0
  275. // 5:    0 - On      1 - Off
  276. // 4:    0 - Normal  1 - Reset
  277. // 3-0:  0
  278. int8 KS0108_status()
  279. {
  280.    KS0108_inst();             // Set for instruction
  281.    return KS0108_read();      // Read and return the status
  282. }
  283.  
  284.  
  285. // Purpose:    Write a byte to the specified chip
  286. // Inputs:     The byte to write
  287. // Note:       Specify instruction or data before calling
  288. void KS0108_write(int8 data)
  289. {
  290.    output_high(KS0108_CS);    // Raise the chip select
  291.    output_low(KS0108_RW);     // Set for writing
  292.    output_d(data);            // Write to the data bus
  293.    delay_cycles(2);
  294.    output_high(KS0108_E);     // Pulse the enable pin
  295.    delay_cycles(10);
  296.    output_low(KS0108_E);
  297.    output_low(KS0108_CS);     // Lower the chip select
  298.    delay_cycles(10);
  299. }
  300.  
  301.  
  302. // Purpose:    Reads a byte of data from the specified chip
  303. // Ouputs:     A byte of data read from the chip
  304. // Note:       Specify instruction or data before calling
  305. int8 KS0108_read()
  306. {
  307.    int8 data;                 // Stores the data read from the LCD
  308.  
  309.    set_tris_d(0xFF);          // Set port d to input
  310.    output_high(KS0108_CS);    // Raise the chip select
  311.    output_high(KS0108_RW);    // Set for reading
  312.    delay_cycles(2);
  313.    output_high(KS0108_E);     // Raise the enable pin
  314.    delay_cycles(10);
  315.    data = input_d();          // Read from the data bus
  316.    output_low(KS0108_E);      // Lower the enable pin
  317.    output_low(KS0108_CS);     // Lower the chip select lines
  318.    delay_cycles(10);
  319.  
  320.    return data;               // Return the read data
  321. }
  322.  
  323. #endif

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #10 en: 21 de Agosto de 2009, 14:00:41 »
LCDg01

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #11 en: 21 de Agosto de 2009, 14:01:02 »
LCDg02

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #12 en: 21 de Agosto de 2009, 14:02:24 »
LCDg03

Desconectado MLO__

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 4581
Re: Desmenuzar un drivers de LCD - CCS
« Respuesta #13 en: 21 de Agosto de 2009, 15:41:26 »
Ojo con las librerias originales del CCS, tienen copyright.
El papel lo aguanta todo