Autor Tema: LCD driver universal CCS  (Leído 4414 veces)

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

Desconectado israelmx

  • PIC10
  • *
  • Mensajes: 7
LCD driver universal CCS
« en: 17 de Enero de 2009, 04:14:09 »

Hola a todos, el motivo de este post es para iniciar un driver para LCD universal, esto quiere decir que no importe en que puerto quieras tu control, a tu bus de datos si es de 8 bits o 4 bits, si esta en el nibble bajo o en el alto, y si es de 2x16 o 4x16, estuve trabajando con el driver flex_lcd420 pero encontré bugs en dicho driver, encontre un código hecho en otro compilador que es lo bastante robusto y versátil para hacer lo que queramos con el, estuve trabajando con el pero hay sentencias de C que el compilador CCS me marca como errores, por eso espero que con la ayuda de todos podamos hacer funcionar este driver con CCS.

De antemano les agradezco infinitamente la ayuda que me a aportado este foro.

aquí esta el código, yo estoy trabajándolo con un pic18f2520.


Código: C
  1. ////////////////////////////////////////////////////////////////////////////
  2. // LCD with HD44780 drive chip
  3. ////////////////////////////////////////////////////////////////////////////
  4. //
  5. // How to use
  6. // ============================
  7. // Remember when using LCD in 4 bit mode you must connect to the LCDs DB4-DB7 pins.
  8. //
  9. // *** For a list of functions, see the END of this file.
  10. //
  11. // The time delays used in the code should mean that it will work on PIC18 with
  12. // 40MHz clock without any changes.
  13. //
  14. // The example below (which would work on a PIC16F84) operates the display
  15. // in 4bit mode and requires the following connections:
  16. // RS  to RA3
  17. // R/W to RA2
  18. // E   to RA1
  19. // DB0 to None
  20. // DB1 to None
  21. // DB2 to None
  22. // DB3 to None
  23. // DB4 to RB4
  24. // DB5 to RB5
  25. // DB6 to RB6
  26. // DB7 to RB7
  27. //
  28. // Add the following in you source file:
  29. //
  30. //#define LCD_ARGS   2, /* Interface type: mode 0 = 8bit, 1 = 4bit(low nibble), 2 = 4bit(upper nibble) */ \
  31. //    1,             /* Use busy signal: 1 = use busy, 0 = use time delays */\
  32. //    PORTB, TRISB,  /* Data port and data port tris register */ \
  33. //    PORTA, TRISA,  /* Control port and control port tris register */ \
  34. //    3,          /* Bit number of control port is connected to RS */ \
  35. //    2,          /* Bit number of control port is connected to RW */ \
  36. //    1           /* Bit number of control port is connected to Enable */
  37. //
  38. //#include <lcd_driver.h> // include the LCD template code
  39. ////////////////////////////////////////////////////////////////////////////
  40. #case
  41.  
  42. char writeDelayType;
  43.  
  44. ////////////////////////////////////////////////////////////////////////////
  45. // LCD Commands ( Refer to LCD Data Sheet )
  46. ////////////////////////////////////////////////////////////////////////////
  47. #define clear_lcd                       0x01 // Clear Display
  48. #define return_home                     0x02 // Cursor to Home position
  49. #define entry_mode                      0x06 // Normal entry mode
  50. #define entry_mode_rev                  0x04 // Normal entry mode  -reverse direction
  51. #define entry_mode_scroll               0x07 // - with shift
  52. #define entry_mode_scroll_rev   0x05 // reverse direction
  53.  
  54. #define system_set_8_bit                0x38 // 8 bit data mode 2 line ( 5x7 font )
  55. #define system_set_4_bit                0x28 // 4 bit data mode 2 line ( 5x7 font )
  56. #define system_set_reset                0x30 // Reset code
  57. #define display_on                      0x0C // Display ON - 2 line mode
  58. #define display_off                     0x08 // Display off
  59. #define set_dd_line1                    0x80 // Line 1 position 1
  60. #define set_dd_line2                    0xC0 // Line 2 position 1
  61. #define set_dd_ram                      0x80 // Line 1 position 1
  62. #define write_data                      0x00 // With RS = 1
  63. #define cursor_on                       0x0E // Switch Cursor ON
  64. #define cursor_off                      0x0C // Switch Cursor OFF
  65. #define cursor_blink_on                 0x0F // Cursor plus blink
  66. #define cursor_shift_right              0x14 // Move cursor right
  67. #define cursor_shift_left               0x10 // Move cursor left
  68. #define display_shift_right             0x1C // Scroll display right
  69. #define display_shift_left              0x18 // Scroll display left
  70.  
  71. #define WriteNoDelay    1
  72. #define WriteControlled 0
  73.  
  74. // Interface type
  75. #define LCD_8_BIT_MODE 0
  76. #define LCD_4_BIT_LO_NIB_MODE 1
  77. #define LCD_4_BIT_HI_NIB_MODE 2
  78.  
  79. // These macros make susequent code more readable, but can seem a little confusing
  80. #define _LCD_RawWriteNibble LCD_RawWriteNibble  <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  81. #define _LCD_RawWriteNibbleInline  LCD_RawWriteNibbleInline     <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  82. #define _LCD_Read                       LCD_Read                        <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  83. #define _LCD_WaitForNotBusy     LCD_WaitForNotBusy      <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  84. #define _LCD_Write                      LCD_Write                       <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  85. #define _LCD_FunctionMode       LCD_FunctionMode        <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  86. #define _LCD_DataMode           LCD_DataMode            <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  87. #define _LCD_RawWrite           LCD_RawWrite            <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  88. #define _LCD_ClockOut           LCD_ClockOut            <InterfaceType, UseBusy, DataPort, Data_PortTris, CtrlPort, Ctrl_PortTris, RS, RW, E>
  89.  
  90. #define _LCD_TEMPL              template <      unsigned char InterfaceType,\
  91.                                                                         unsigned char UseBusy,\
  92.                                                                         unsigned int DataPort, unsigned int Data_PortTris,\
  93.                                                                         unsigned int CtrlPort, unsigned int Ctrl_PortTris,\
  94.                                                                         unsigned char RS, unsigned char RW, unsigned char E>
  95.  
  96. _LCD_TEMPL
  97. inline void LCD_FunctionMode( void )
  98. {
  99.         volatile bit rs@CtrlPort.RS = 0;
  100. }
  101.  
  102. _LCD_TEMPL
  103. inline void LCD_DataMode( void )
  104. {
  105.         volatile bit rs@CtrlPort.RS = 1;
  106. }
  107.  
  108.  
  109. inline void LCD_CycleMakeupDelay()
  110. {
  111.         // Enable cycle time must be > 1000ns total for both reading and writing
  112.         // LCD_SetupDelay + LCD_EnablePulseDelay + LCD_HoldupDelay + LCD_CycleMakeupDelay >= 1000ns
  113.         //       200      +          500         +       100       +          200         >= 1000ns
  114.  
  115.         // This delay is required to meet the Sharp data sheet total cycle time of > 1000ns
  116.         // @40MHz this is 2 instructions
  117.         asm nop
  118.         asm nop
  119. }
  120.  
  121.  
  122. inline void LCD_EnablePulseDelay()
  123. {              
  124.         // PWEH > 460ns on Sharp data sheet
  125.         // @40MHz this is 5 instructions
  126.         asm nop
  127.         asm nop
  128.         asm nop
  129.         asm nop
  130.         asm nop
  131. }
  132.  
  133. inline void LCD_SetupDelay()
  134. {
  135.         // tAS > 140ns min on Sharp data sheet
  136.         // @40MHz this is 2 instructions
  137.         asm nop
  138.         asm nop
  139. }
  140.  
  141. inline void LCD_HoldupDelay()
  142. {
  143.         // tAS > 10ns min on Sharp data sheet
  144.         // @40MHz this is 1 instructions
  145.         asm nop
  146. }
  147.  
  148.  
  149.  
  150. _LCD_TEMPL
  151. char LCD_Read()
  152. {              
  153.         char d;
  154.         volatile unsigned char data@DataPort, tris@Data_PortTris;
  155.         volatile bit rw@CtrlPort.RW, e@CtrlPort.E;
  156.        
  157.         if( InterfaceType == LCD_4_BIT_HI_NIB_MODE )
  158.         {
  159.                 // upper nibble input
  160.                 tris |= 0xF0;
  161.                 rw = 1; // set reading mode
  162.                 // first high nibble   
  163.                 LCD_SetupDelay();
  164.                 e = 1;
  165.                 LCD_EnablePulseDelay();
  166.                 d = data & 0xF0; // read data
  167.                 e = 0;
  168.                 LCD_HoldupDelay();
  169.                 LCD_CycleMakeupDelay();
  170.                        
  171.                 // then low nibble
  172.                 LCD_SetupDelay();
  173.                 e = 1;
  174.                 LCD_EnablePulseDelay();
  175.                 d |= data >> 4;
  176.                 e = 0;
  177.                 LCD_HoldupDelay();
  178.                 LCD_CycleMakeupDelay();
  179.         }
  180.  
  181.  
  182.         if( InterfaceType == LCD_4_BIT_LO_NIB_MODE )
  183.         {
  184.                 // lower nibble input  
  185.                 tris |= 0x0F;
  186.                 rw = 1; // set reading mode
  187.                 // first high nibble
  188.                 LCD_SetupDelay();
  189.                 e = 1;
  190.                 LCD_EnablePulseDelay();
  191.                 d = data << 4;
  192.                 e = 0;
  193.                 LCD_HoldupDelay();
  194.                 LCD_CycleMakeupDelay();
  195.                        
  196.                 // then low nibble
  197.                 LCD_SetupDelay();
  198.                 e = 1;
  199.                 LCD_EnablePulseDelay();
  200.                 d |= data & 0x0F;
  201.                 e = 0;
  202.                 LCD_HoldupDelay();
  203.                 LCD_CycleMakeupDelay();
  204.         }
  205.  
  206.  
  207.         if( InterfaceType == LCD_8_BIT_MODE )
  208.         {
  209.                 // port input  
  210.                 tris = 0xFF;
  211.                 rw = 1; // set reading mode
  212.                 LCD_SetupDelay();
  213.                 e = 1;
  214.                 LCD_EnablePulseDelay();
  215.                 d = data;
  216.                 e = 0;
  217.                 LCD_HoldupDelay();
  218.                 LCD_CycleMakeupDelay();
  219.         }
  220.        
  221.         return d;
  222. }
  223.  
  224. _LCD_TEMPL
  225. void LCD_RawWriteNibble( char d )
  226. {
  227.         // Note: this function is duplicate below, but declared inline.
  228.         // this is to reduce stack depth usage
  229.         // Note: this function is above, but declared inline.
  230.         // this is to reduce stack depth usage
  231.         volatile unsigned char data@DataPort, tris@Data_PortTris;
  232.         volatile bit rw@CtrlPort.RW, e@CtrlPort.E;
  233.        
  234.         if( InterfaceType == LCD_4_BIT_HI_NIB_MODE )
  235.         {
  236.                 // port upper nibble output
  237.                 rw = 0; // set writing mode
  238.                 LCD_SetupDelay();
  239.                 tris &= 0x0F;
  240.                 data &= 0x0F;
  241.                 data |= d & 0xF0;
  242.                 e = 1;
  243.                 LCD_EnablePulseDelay();
  244.                 e = 0;
  245.                 LCD_HoldupDelay();
  246.                 LCD_CycleMakeupDelay();
  247.         }
  248.  
  249.         if( InterfaceType == LCD_4_BIT_LO_NIB_MODE )
  250.         {
  251.                 // port upper nibble output
  252.                 rw = 0; // set writing mode
  253.                 LCD_SetupDelay();
  254.                 tris &= 0xF0;
  255.                 data &= 0xF0;
  256.                 data |= d >> 4;
  257.                 e = 1;
  258.                 LCD_EnablePulseDelay();
  259.                 e = 0;
  260.                 LCD_HoldupDelay();
  261.                 LCD_CycleMakeupDelay();
  262.         }      
  263. }
  264.  
  265. _LCD_TEMPL
  266. inline void LCD_RawWriteNibbleInline( char d )
  267. {
  268.         // Note: this function is above, but declared inline.
  269.         // this is to reduce stack depth usage
  270.         volatile unsigned char data@DataPort, tris@Data_PortTris;
  271.         volatile bit rw@CtrlPort.RW, e@CtrlPort.E;
  272.        
  273.         if( InterfaceType == LCD_4_BIT_HI_NIB_MODE )
  274.         {
  275.                 // port upper nibble output
  276.                 rw = 0; // set writing mode
  277.                 LCD_SetupDelay();
  278.                 tris &= 0x0F;
  279.                 data &= 0x0F;
  280.                 data |= d & 0xF0;
  281.                 e = 1;
  282.                 LCD_EnablePulseDelay();
  283.                 e = 0;
  284.                 LCD_HoldupDelay();
  285.                 LCD_CycleMakeupDelay();
  286.         }
  287.  
  288.         if( InterfaceType == LCD_4_BIT_LO_NIB_MODE )
  289.         {
  290.                 // port upper nibble output
  291.                 rw = 0; // set writing mode
  292.                 LCD_SetupDelay();
  293.                 tris &= 0xF0;
  294.                 data &= 0xF0;
  295.                 data |= d >> 4;
  296.                 e = 1;
  297.                 LCD_EnablePulseDelay();
  298.                 e = 0;
  299.                 LCD_HoldupDelay();
  300.                 LCD_CycleMakeupDelay();
  301.         }      
  302. }
  303.  
  304.  
  305. _LCD_TEMPL
  306. void LCD_RawWrite( char d )
  307. {              
  308.         volatile unsigned char tris@Data_PortTris, data@DataPort;
  309.         volatile bit rw@CtrlPort.RW, e@CtrlPort.E;
  310.                
  311.         if( InterfaceType == LCD_4_BIT_HI_NIB_MODE  )
  312.         {
  313.                 // output upper nibble, then lower nibble
  314.                 bit flag = 0;
  315.                 do
  316.                 {                      
  317.                         _LCD_RawWriteNibbleInline( d );
  318.                         flag = !flag;
  319.                         d <<= 4;
  320.                 }
  321.                 while( flag );
  322.         }      
  323.  
  324.         if( InterfaceType == LCD_4_BIT_LO_NIB_MODE )
  325.         {
  326.                 // output upper nibble, then lower nibble
  327.                 bit flag = 0;
  328.                 do
  329.                 {                      
  330.                         _LCD_RawWriteNibbleInline( d );
  331.                         flag = !flag;
  332.                         d <<= 4;
  333.                 }
  334.                 while( flag );
  335.         }      
  336.  
  337.  
  338.         if( InterfaceType == LCD_8_BIT_MODE )
  339.         {
  340.                 // port b output
  341.                 rw = 0; // set writing mode
  342.                 tris = 0x00;           
  343.                 data = d;
  344.                 LCD_SetupDelay();
  345.                 e = 1;
  346.                 LCD_EnablePulseDelay();
  347.                 e = 0;
  348.                 LCD_HoldupDelay();
  349.                 LCD_CycleMakeupDelay();
  350.         }
  351. }
  352.  
  353. _LCD_TEMPL
  354. inline void LCD_WaitForNotBusy()
  355. {
  356.         volatile bit rs@CtrlPort.RS; bit old_RS = rs;  
  357.         rs = 0;
  358.         while( _LCD_Read() & 0x80 ); // wait while busy set
  359.         rs = old_RS;
  360. }
  361.  
  362. _LCD_TEMPL
  363. void LCD_Write(char d )
  364. {
  365.         volatile bit rs@CtrlPort.RS;
  366.        
  367.         if( UseBusy == 1 )
  368.         {      
  369.                 // wait until display Not busy before sending next data
  370.                 if ( writeDelayType == WriteControlled )
  371.                         _LCD_WaitForNotBusy();
  372.                
  373.                 _LCD_RawWrite( d );
  374.         }
  375.         else
  376.         {              
  377.                 _LCD_RawWrite( d );
  378.                
  379.                 // give time to complete
  380.                 if ( writeDelayType == WriteControlled )
  381.                 {
  382.                         if( !rs && (d == return_home || d == clear_lcd) )
  383.                                 delay_ms( 2 ); // return_home takes more time than other instructions to execute
  384.                         else
  385.                                 delay_10us( 5 ); // 50us - enough time for normal command execution - clear and home need longer!!                     
  386.                 }
  387.         }
  388. }
  389.  
  390. _LCD_TEMPL
  391. void LCD_Clear()
  392. {
  393.         _LCD_FunctionMode();
  394.         _LCD_Write( clear_lcd ); // clear display
  395.         _LCD_Write( return_home );
  396. }
  397.  
  398. _LCD_TEMPL
  399. void LCD_Setup( void )
  400. {
  401.         // set control port bits used to output
  402.         volatile bit trisRS@Ctrl_PortTris.RS, trisRW@Ctrl_PortTris.RW, trisE@Ctrl_PortTris.E;
  403.         trisRS = 0;
  404.         trisRW = 0;
  405.         trisE = 0;
  406.        
  407.         writeDelayType = WriteNoDelay; // no delays in data writes
  408.        
  409.         delay_ms(16); // Power up delay
  410.         _LCD_FunctionMode();
  411.  
  412.         if( InterfaceType == LCD_4_BIT_HI_NIB_MODE )
  413.         {
  414.                 // Reset sequence as described in data sheets
  415.                 _LCD_RawWriteNibble( system_set_reset );
  416.                 delay_ms(5); // min delay here of 4.1 ms
  417.                 _LCD_RawWriteNibble( system_set_reset );
  418.                 delay_10us(100); // min delay here of 100us
  419.                 _LCD_RawWriteNibble( system_set_reset );
  420.            
  421.                 // LCD busy flag is valid from this point onwards
  422.                 if( UseBusy == 1 )
  423.                         _LCD_WaitForNotBusy();
  424.                 else
  425.                         delay_10us( 5 ); // standard command delay time
  426.                
  427.                 _LCD_RawWriteNibble( system_set_4_bit );
  428.                
  429.                 if( UseBusy == 1 )
  430.                         _LCD_WaitForNotBusy();
  431.                 else
  432.                         delay_10us( 5 ); // standard command delay time
  433.  
  434.                 writeDelayType = WriteControlled;
  435.                 _LCD_Write( system_set_4_bit );
  436.         }
  437.  
  438.         if( InterfaceType == LCD_4_BIT_LO_NIB_MODE )
  439.         {
  440.                 // Reset sequence as described in data sheets
  441.                 _LCD_RawWriteNibble( system_set_reset );
  442.                 delay_ms(5); // min delay here of 4.1 ms
  443.                 _LCD_RawWriteNibble( system_set_reset );
  444.                 delay_10us(100); // min delay here of 100us
  445.                 _LCD_RawWriteNibble( system_set_reset );
  446.            
  447.                 // LCD busy flag is valid from this point onwards
  448.                 if( UseBusy == 1 )
  449.                         _LCD_WaitForNotBusy();
  450.                 else
  451.                         delay_10us( 5 ); // standard command delay time
  452.                
  453.                 _LCD_RawWriteNibble( system_set_4_bit );
  454.                
  455.                 if( UseBusy == 1 )
  456.                         _LCD_WaitForNotBusy();
  457.                 else
  458.                         delay_10us( 5 ); // standard command delay time
  459.  
  460.                 writeDelayType = WriteControlled;
  461.                 _LCD_Write( system_set_4_bit );
  462.         }
  463.        
  464.         if( InterfaceType == LCD_8_BIT_MODE )
  465.         {
  466.                 // Reset sequence as described in data sheets
  467.                 _LCD_RawWrite( system_set_reset );
  468.                 delay_ms(5); // min delay here of 4.1 ms
  469.                 _LCD_RawWrite( system_set_reset );
  470.                 delay_10us(10); // min delay here of 100us
  471.                 _LCD_RawWrite( system_set_reset );
  472.            
  473.                 // busy flag is valid from this point onwards
  474.                 if( UseBusy == 1 )
  475.                         _LCD_WaitForNotBusy();
  476.                 else
  477.                         delay_10us( 5 ); // standard command delay time
  478.  
  479.                 _LCD_RawWrite( system_set_8_bit );     
  480.  
  481.                 if( UseBusy == 1 )
  482.                         _LCD_WaitForNotBusy();
  483.                 else
  484.                         delay_10us( 5 ); // standard command delay time
  485.                
  486.                 writeDelayType = WriteControlled; // use busy
  487.         }
  488.                
  489.         _LCD_Write( entry_mode );
  490.         _LCD_Write( display_on );
  491.         _LCD_Write( set_dd_ram );
  492. }
  493.  
  494. _LCD_TEMPL
  495. void LCD_Printf( const char *lcdptr )
  496. {
  497.         char pi = 0, c;
  498.         _LCD_DataMode();
  499.     while( 1 )
  500.     {
  501.                 c = lcdptr[pi++];
  502.                 if ( !c )
  503.                         return;
  504.                 if ( c == '\n' )
  505.                 {
  506.                         _LCD_FunctionMode();
  507.                         // move to start second line
  508.                         _LCD_Write( set_dd_ram + 0x40 );
  509.                         _LCD_DataMode();
  510.                 }
  511.                 else
  512.                         _LCD_Write( c );// Display on LCD
  513.         }
  514. }
  515.  
  516. _LCD_TEMPL
  517. void LCD_Printf( rom char *lcdptr )
  518. {
  519.         char pi = 0, c;
  520.         _LCD_DataMode();
  521.     while( 1 )
  522.     {
  523.                 c = lcdptr[pi++];
  524.                 if ( !c )
  525.                         return;
  526.                 if ( c == '\n' )
  527.                 {
  528.                         _LCD_FunctionMode();
  529.                         // move to start second line
  530.                         _LCD_Write( set_dd_ram + 0x40 );
  531.                         _LCD_DataMode();
  532.                 }
  533.                 else
  534.                         _LCD_Write( c );// Display on LCD
  535.         }
  536. }
  537.  
  538. _LCD_TEMPL
  539. void LCD_Printf( const char *lcdptr, unsigned int val ) // JS - Accept unsigned by default
  540. {
  541.         unsigned char pi = 0, bi, c, fill, baseOrBits, sign, mask;
  542.         unsigned char buff[ 10 ]; // max length allow is 9
  543.         bit pad;
  544.        
  545.         _LCD_DataMode();
  546.     while( 1 )
  547.     {
  548.                 c = lcdptr[pi++]; if ( !c ) return;
  549.                
  550.                 switch( c )
  551.                 {
  552.                 case '\n':
  553.                         _LCD_FunctionMode();
  554.                         // move to start second line
  555.                         _LCD_Write( set_dd_ram + 0x40 );
  556.                         _LCD_DataMode();
  557.                         break;
  558.                 case '%':
  559.                         c = lcdptr[pi++]; if ( !c ) return;
  560.                        
  561.                         //Handle escape sequence that prints '%'
  562.                         if ( c == '%' )
  563.                         {
  564.                                 _LCD_Write( c );// Display on LCD
  565.                                 break;
  566.                         }
  567.                        
  568.                         // Next character if zero indicates that we should zero fill output
  569.                         if ( c == '0' )
  570.                         {
  571.                                 fill = '0';
  572.                                 c = lcdptr[pi++]; if ( !c ) return;
  573.                         }
  574.                         else
  575.                                 fill = ' ';
  576.  
  577.                         // Next character if valid digit indicates field width
  578.                         if( c > '0' && c <= '9' )
  579.                         {
  580.                                 pad = 1;
  581.                                 bi = c - 48;;                          
  582.                                 c = lcdptr[pi++]; if ( !c ) return;
  583.                         }
  584.                         else
  585.                         {
  586.                                 pad = 0;
  587.                                 bi = sizeof( buff ) - 1;
  588.                         }
  589.                                
  590.                        
  591.                         // Next character indicates the radix (number base)
  592.                         sign = 0;
  593.                         switch( c )
  594.                         {
  595.                         case 'd':
  596.                                 if( val & 0x8000 )      // Negative values must be adjusted to be positive // JS
  597.                                 {
  598.                                         sign = '-';
  599.                                         val ^= 0xFFFF; // 2s complement negate  // JS
  600.                                         val++;
  601.                                 }
  602.                         case 'u':
  603.                                 baseOrBits = 10; // base ten, divide by ten per digit
  604.                                 break;                 
  605.                         case 'X':
  606.                                 baseOrBits = 4; // base 16, requires a 4 bit shift per digit
  607.                                 mask = 0x0F;
  608.                                 break;
  609.                         case 'b':
  610.                                 baseOrBits = 1; // base 16, requires a 1 bit shift per digit
  611.                                 mask = 0x01;
  612.                                 break;
  613.                         default:
  614.                                 return; // no radix
  615.                         }
  616.                                
  617.                         // null terminate, then reverse fill string
  618.                         buff[ bi ] = '\0';
  619.                        
  620.                         bit first = true;                              
  621.                         while( bi )
  622.                         {
  623.                                 bi--;
  624.                                 if( val || first )
  625.                                 {
  626.                                         first = false;
  627.                                                                                
  628.                                         if( baseOrBits == 10 )
  629.                                         {
  630.                                                 c = (unsigned char)(val % 10);  // JS - Optimization, use absolute of 10
  631.                                                 val /= 10;      // JS - Optimization, use absolute of 10
  632.                                         }
  633.                                         else
  634.                                         {
  635.                                                 c = val & mask;
  636.                                                 val = ((unsigned int)val) >> baseOrBits;                                               
  637.                                         }
  638.                                        
  639.                                         if( c > 9 )
  640.                                                 c += 55; // convert to hex digits character A-F
  641.                                         else
  642.                                                 c += 48; // convert to digit character 0-9
  643.  
  644.                                 }
  645.                                 else
  646.                                 {
  647.                                         if( sign && (bi == 0 || fill != '0') )
  648.                                         {
  649.                                                 c = sign;
  650.                                                 sign = 0;
  651.                                         }
  652.                                         else
  653.                                                 c = fill;
  654.                                 }
  655.                                
  656.                                 buff[ bi ] = c;
  657.                                
  658.                                 if( pad == 0 && val == 0 && sign == 0 )
  659.                                         break;
  660.                         }
  661.                         // output string to display
  662.                         while( 1 )
  663.                         {
  664.                                 c = buff[ bi ];
  665.                                 if( !c ) break;
  666.                                 _LCD_Write( c );// Display on LCD
  667.                                 bi++;
  668.                         }
  669.                         break;
  670.                 default:
  671.                         _LCD_Write( c );// Display on LCD
  672.                         break;
  673.                 }
  674.         }
  675. }
  676.  
  677. _LCD_TEMPL
  678. void LCD_GotoXy( char x, char y )
  679. {
  680.         // displays memory mapping with two lines:
  681.         // line 1: 0x00
  682.         // line 2: 0x40
  683.        
  684.         // display memory mapping with four lines:
  685.         // line 1: 0x00
  686.         // line 2: 0x40
  687.         // line 3: 0x14
  688.         // line 4: 0x54
  689.        
  690.         _LCD_FunctionMode();
  691.         unsigned char offset = x;
  692.         if( y & 0x01 ) offset += 0x40;
  693.         if( y & 0x02 ) offset += 0x14;         
  694.         _LCD_Write( set_dd_ram + offset );
  695. }
  696.  
  697.  
  698. _LCD_TEMPL
  699. void LCD_Function( char func )
  700. {
  701.         _LCD_FunctionMode();
  702.         _LCD_Write( func );
  703. }
  704.  
  705. ////////////////////////////////////////////////////////////////////////////
  706. // Helpers that hide template arguments
  707. ////////////////////////////////////////////////////////////////////////////
  708. // low level functions
  709. #define lcd_write               LCD_Write<LCD_ARGS>
  710. #define lcd_waitfornotbusy LCD_WaitForNotBusy<LCD_ARGS>
  711. #define lcd_read                LCD_Read<LCD_ARGS>
  712. #define lcd_funcmode    LCD_FunctionMode<LCD_ARGS>
  713. #define lcd_datamode    LCD_DataMode<LCD_ARGS>
  714.  
  715.  
  716. // high level functions - these all set function or data mode as required
  717. #define lcd_setup               LCD_Setup<LCD_ARGS>
  718. #define lprintf                 LCD_Printf<LCD_ARGS>
  719. #define lcd_clear               LCD_Clear<LCD_ARGS>
  720. #define lcd_gotoxy              LCD_GotoXy<LCD_ARGS>
  721. #define lcd_function    LCD_Function<LCD_ARGS>

Desconectado jfh900

  • Moderadores
  • DsPIC30
  • *****
  • Mensajes: 3595
Re: LCD driver universal CCS
« Respuesta #1 en: 17 de Enero de 2009, 09:47:00 »
Hola israelmx

Tu idea es buena y puede ser interesante si efectivamente conseguimos esa universalidad que comentas y que entre todos no dudo que se consiga (sería muy interesante el tratamiento de la inclusión de nuevos caracteres). Pero hablando de tu consulta ¿puedes comentar cual instrucciones no reconoce el CCS?

He visto que tiene el código:

asm nop

Esto hay que sustituirlo por:

#asm nop #endasm

Un saludo
* Cuando hables, procura que tus palabras sean mejores que el silencio.
* 'Todos somos ignorantes, lo que ocurre es que no todos ignoramos las mismas cosas.' Albert Einstein.
* No hay nada peor que un experto para evitar el progreso en un campo
* "La vida es como una novela. No importa que sea larga, sino que esté bien narrada" Seneca
* La vida no se vive por las veces que respiras, sino por los momentos que dejan sin aliento.
* Dios dijo: ∇·E=ρ/ε0 ; ∇·B=0 ; ∇xE=-dB/dt ; ∇xB= μ0ε0dE/dt..y la luz se hizo..!!..

Desde España Jesús

Desconectado israelmx

  • PIC10
  • *
  • Mensajes: 7
Re: LCD driver universal CCS
« Respuesta #2 en: 17 de Enero de 2009, 18:03:34 »
Hola jfh900,


Muchas gracias esperemos que asi sea, por mientras los nop

en vez de sustituirlos por su codigo en asm, se me ocurre manejarlo asi:


delay_cycles(1);   /* No operation */

para mantener el codigo en CCS, estoy trabajando con el y lo que me resulta muy bueno es como manejan los puertos con simples macros, esta es la parte que no he desifardo del todo:


#define _LCD_TEMPL template <  unsigned char InterfaceType,\
                           unsigned char UseBusy,\
                           unsigned int DataPort, unsigned int Data_PortTris,\
                           unsigned int CtrlPort, unsigned int Ctrl_PortTris,\
                           unsigned char RS, unsigned char RW, unsigned char E>

_LCD_TEMPL
inline void LCD_FunctionMode( void )
{
   volatile bool rs@CtrlPort.RS = 0;
}


template es una palabra de C++, no? si no es asi como se esta usando en este caso.

Muchas gracias saludos.

Desconectado pajaro

  • PIC24H
  • ******
  • Mensajes: 1121
Re: LCD driver universal CCS
« Respuesta #3 en: 17 de Mayo de 2009, 10:43:40 »
Hola amigo

Esa idea es muy buena, a mi tambien se me paso por la cabeza, hace un tiempo, jejeje...
yo aporto otra idea: sería posible hacer que esa misma libreria valiara para diferentes tipos de LCD...16x2, 20x2, 20x4, 8x2

que mediante una funcion tu le dijeras el numero de lineas y columnas, o que te lo chequeara automaticamente alguna función.

Felicitaciones por la iniciativa, la inteligencia colectiva es algo muy bueno.

Un saludo.


 

anything