Autor Tema: OLED 128 x 64  (Leído 5881 veces)

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

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
OLED 128 x 64
« en: 16 de Marzo de 2015, 19:30:46 »
Hey there

Just to let u know that I'm working on the OLED 128x64 Display with I2C

Here is so far the Library, hopefully next weekend I got it finished.

Hola

Sólo para que tu sabes que estoy trabajando en la pantalla OLED de 128x64 con I2C

Aquí es hasta el momento la Biblioteca, espero que el próximo fin de semana lo tengo terminado.



OLED Display.c
Código: C
  1. /************ OLED Display.c *********/
  2.  
  3. #include "OLED Display.h"
  4.  
  5.  
  6.  
  7.  
  8. char OLEDSetUp ( void )
  9. {
  10.         __delay_ms ( 1 );                               // As OLDE VDD goes high sooner than VCC,
  11.         OLED_RESET_PIN = OFF;                   // we must wait to stabilize VCC
  12.         __delay_ms ( 1 );                               // OLED Reset pin.
  13.         OLED_RESET_PIN = ON;                   
  14.  
  15. // 1. Fundamental Commands.
  16.         OLEDCommand ( OLED_CONTRAST );                          // 0x81
  17.         OLEDCommand ( 0x2f );                                           // was 0x9F.
  18.         OLEDCommand ( OLED_DISPLAY_ALL_ON_RESUME );     // 0xA5
  19.         OLEDCommand ( OLED_NORMAL_DISPLAY );            // 0xA6
  20.         OLEDCommand ( OLED_DISPLAY_ON );                                //--turn on oled panel
  21.  
  22. // 2. Scrolling.
  23.  
  24. // 3. Addressing.
  25.         OLEDCommand ( OLED_MEMORY_MODE );                       // 0x20
  26.         OLEDCommand ( OLED_HORIZONTAL_MODE );
  27.                
  28. // 4. Hardware Configuration.
  29.         OLEDCommand ( OLED_SET_START_LINE | 0x00 );     // line #0
  30.         OLEDCommand ( OLED_SEGMENT_REMAP | 0x01 );      // WTF???
  31.         OLEDCommand ( OLED_SET_MULTIPLEX_RATIO );       // 0xA8
  32.         OLEDCommand ( 0x3F );
  33.         OLEDCommand ( OLED_COM_SCAN_DECREMENT );        // WTF???
  34.         OLEDCommand ( OLED_DISPLAY_OFFSET );            // 0xD3
  35.         OLEDCommand ( 0x0 );                                            // no offset
  36.         OLEDCommand ( OLED_SET_COM_PINS );                      // 0xDA
  37.         OLEDCommand ( 0x12 );
  38.  
  39. // 5. Timing.
  40.         OLEDCommand ( OLED_DISPLAY_CLOCK_RATIO );       // 0xD5
  41.         OLEDCommand ( OLED_CLOCK_RESET_RATIO );
  42.         OLEDCommand ( OLED_SET_PRECHARGE );                     // 0xd9
  43.         OLEDCommand ( OLED_PRECHARGE_RESET );
  44.         OLEDCommand ( OLED_SET_VCOM_H );                        // 0xDB
  45.         OLEDCommand ( 0x40 );                                           // WTF???
  46.  
  47. // 6?. Other
  48.         OLEDCommand ( OLED_CHARGE_PUMP );                       // 0x8D
  49.         OLEDCommand ( OLED_DISABLE_CHARGE );
  50.  
  51.         return 0x00;
  52.         }
  53.        
  54.        
  55.  
  56.        
  57.  
  58.        
  59.  
  60.  
  61. void OLEDCommand ( unsigned char c )
  62. {
  63.         unsigned char command = 0x00;   // Co = 0, D/C=0, followed by 00 0000.
  64.        
  65.         StartI2C ( );
  66.         I2CAddressCommand ( OLED_ADDRESS, I2C_WRITE );
  67.         WriteI2C ( command );
  68.         WriteI2C ( c );
  69.         StopI2C ( );
  70.         }
  71.        
  72.  
  73. /********** prints out a pixel on the GLCD ********/
  74. void OLEDPixel ( int x, int y, char color )
  75. {
  76.         unsigned char *ptr;
  77.         unsigned char page;
  78.         unsigned int axisX;
  79.         unsigned int axisY;
  80.         signed char bitNumber, bitPlace;
  81.         unsigned int address;
  82.  
  83.  
  84.         if ( ( x < ARRAY_WIDHT && y <= ARRAY_LENGHT ) &&
  85.                 ( x >= 0x00 && y >= 0x00 ) )
  86.         {
  87.                 axisY = y / 8;                                                  // Getting the row value.
  88.                 axisX = x;                                                              // Getting the column value.
  89.                 page = abs ( axisY );                                   // Getting the Page number.
  90.                 bitNumber = ( y % 8 );                                  // Getting the bit value on the column.
  91.                
  92. /*              printf ( " \nX:%d, seg#:%d", x, axisX );
  93.                 printf ( " \nY:%d, PAGE:%d", y, page );
  94.                 printf ( " \nbit #:%d", bitNumber );
  95. */
  96.                 int temp;
  97.                 temp =  y / 8;
  98.                 temp *= ARRAY_WIDHT;
  99.                 temp += x;
  100.                 ptr = ( char * ) TEST;
  101.  
  102.         // Set Column Address.
  103.                 OLEDCommand ( OLED_COLUMN_ADDRESS );           
  104.                 OLEDCommand ( axisX );                                  // Column start address.
  105.                 OLEDCommand ( axisX );                                  // Column end address.
  106.         // Set Page Address.
  107.                 OLEDCommand ( OLED_PAGE_ADDRESS );             
  108.                 OLEDCommand ( page );                                   // Page start Address.
  109.                 OLEDCommand ( page );                                   // Page end Address.
  110.                
  111.                 if ( color )    // bitSet ( x, x ) equivalent.
  112.                         OLEDData ( *( ptr + temp ) |=  ( bitWise ( y % 8 ) ) );
  113.  
  114.                 else                    // bitClear ( x, x ) equivalent.       
  115.                         OLEDData ( *( ptr + temp ) &=  ~( bitWise ( y % 8 ) ) );
  116.  
  117.                 }       // If the axies are whithin the OLED margin.
  118.         }       // test Pixel. 
  119.  
  120.  
  121.  
  122. /********** prints out the Data to the GLCD ********/
  123. void OLEDData ( unsigned char c )
  124. {
  125.         unsigned char control = 0x40;   // Co = 0, D/C = 1
  126.  
  127.         StartI2C ( );
  128.         I2CAddressCommand ( OLED_ADDRESS, I2C_WRITE );
  129.         WriteI2C ( control );
  130.         WriteI2C ( c );
  131.         StopI2C ( );
  132.         }
  133.  
  134. /********** prints out the 'color' Data to the GLCD ********/
  135. void OLEDFill ( unsigned char color )
  136. {
  137.         unsigned char *ptr;
  138.         unsigned char i, j;
  139.  
  140. // Set The Display's GDDRAM with the 'color' Data.
  141.         ptr = ( char * ) TEST;
  142.        
  143.         for ( unsigned int k = 0; k < SSD1306_GDDRAM; k++ )
  144.                 *( ptr + k ) = color;
  145.  
  146. // Set the Display with the 'color' Data.
  147.         OLEDCommand ( OLED_LOW_COLUMN_START );          //set lower column address.
  148.         OLEDCommand ( OLED_HIGH_COLUMN_START );         //set higher column address.
  149.         OLEDCommand ( OLED_PAGE_START );                        //set page address.
  150.  
  151.         for ( j = 0; j < 8; j++ )
  152.         {
  153.                 OLEDCommand ( OLED_LOW_COLUMN_START );  //set lower column address.
  154.                 OLEDCommand ( OLED_HIGH_COLUMN_START ); //set higher column address.
  155.                 OLEDCommand ( OLED_PAGE_START + j );    //set page address.
  156.                
  157.                 for ( i = 0; i < 128; i++ )
  158.                         OLEDData ( *( ptr ) );
  159.                 }       // Printing out the rows an then the columns.
  160.  
  161.         }       // OLEDFill ().
  162.  
  163.  
  164.        
  165. /********** prints out on the UART PORT the Arrays ********/
  166. /********** For Testing Purposes ********/
  167. void printArrayTest ( void )
  168. {
  169.         for ( unsigned char i = 0; i < ARRAY_HEIGHT; i++ )     
  170.         {
  171.                 printf ( "\n" );
  172.                 for ( unsigned char j = 0; j < ARRAY_WIDHT; j++ )
  173.                         printf ( "[%2X]", TEST [ i ] [ j ] );
  174.                 }              
  175.         printf ( "\n" );
  176.         }

OLED Display.h
Código: C
  1. #include        <xc.h>
  2. /************ GLCD 160x80.h *********/
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8.  
  9. #ifndef OLED_RESET_PIN
  10. #define OLED_RESET_PIN          PORTCbits . RC0
  11. #endif
  12.  
  13.  
  14. #define ARRAY_WIDHT             128
  15. #define ARRAY_HEIGHT    7
  16. #define ARRAY_LENGHT    ( 8 * ARRAY_HEIGHT )
  17. #define SSD1306_GDDRAM  ( ARRAY_WIDHT * ARRAY_HEIGHT / 8 )
  18. unsigned char TEST [ ARRAY_HEIGHT ] [ ARRAY_WIDHT ];   
  19.  
  20.  
  21.  
  22. // 1. Fundamental Commands.
  23. #define OLED_CONTRAST                           0x81
  24. #define OLED_DISPLAY_ALL_ON_RESUME      0xA4
  25. #define OLED_DISPLAY_ALL_ON             0xA5
  26. #define OLED_NORMAL_DISPLAY             0xA6
  27. #define OLED_INVERTED_DISPLAY           0xA7
  28. #define OLED_DISPLAY_OFF                        0xAE
  29. #define OLED_DISPLAY_ON                         0xAF
  30.  
  31. // 2. Scrolling
  32.  
  33. // 3. Addressing.
  34. #define OLED_LOW_COLUMN_START           0x00
  35. #define OLED_HIGH_COLUMN_START          0x10
  36. #define OLED_MEMORY_MODE                        0x20
  37. #define OLED_HORIZONTAL_MODE            0x00
  38. #define OLED_VERTICAL_MODE                      0x01
  39. #define OLED_PAGE_MODE                          0x02
  40. #define OLED_COLUMN_ADDRESS                     0x21
  41. #define OLED_PAGE_ADDRESS                       0x22
  42. #define OLED_PAGE_START                         0xB0
  43.  
  44. // 4. Hardware Configuration.
  45. #define OLED_SET_START_LINE                     0x40
  46. #define OLED_SEGMENT_REMAP                      0xA0
  47. #define OLED_SET_MULTIPLEX_RATIO        0xA8
  48. #define OLED_COM_SCAN_INCREMENT         0xC0
  49. #define OLED_COM_SCAN_DECREMENT         0xC8
  50. #define OLED_DISPLAY_OFFSET             0xD3
  51. #define OLED_SET_COM_PINS                       0xDA
  52.  
  53. // 5. Timing.
  54. #define OLED_DISPLAY_CLOCK_RATIO        0xD5
  55. #define OLED_CLOCK_RESET_RATIO          0x80
  56. #define OLED_SET_PRECHARGE                      0xD9
  57. #define OLED_PRECHARGE_RESET            0x22
  58. #define OLED_SET_VCOM_H                         0xDB
  59. #define OLED_VCC_RESET                          0x20
  60.  
  61. // 6?. Other
  62. #define OLED_CHARGE_PUMP                        0x8D
  63. #define OLED_DISABLE_CHARGE                     0x10
  64.  
  65.  
  66. #define bitWise(n) ( 1 << n )
  67.  
  68.  
  69. char OLEDSetUp ( void );
  70. void OLEDCommand ( unsigned char c );
  71. void OLEDPixel ( int x, int y, char color );
  72. void OLEDCommandSend ( char reg, char data );
  73. void OLEDData ( unsigned char c );
  74. void OLEDFill ( unsigned char dat );
  75. void OLEDDisplay ( void );
  76. void OLEDAddress ( int address );
  77.  
  78.  
  79. void testArray ( void );
  80. void clearTest ( void );
  81. void printArrayTest ( void );




...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: OLED 128 x 64
« Respuesta #1 en: 16 de Marzo de 2015, 19:32:21 »
And here is the code... (so far)

Y aquí está el código ... (hasta ahora)

Código: C
  1. // Menu function is requested if the MenuButton is pressed.
  2. //      menu ( );
  3.                
  4.         OLEDSetUp ( );
  5.         OLEDFill ( 0x0 );
  6.  
  7.         for ( unsigned char i = 15; i < 100; i ++ )
  8.                 OLEDPixel ( i, 54, ON );
  9.  
  10.         for ( unsigned char i = 15; i < 100; i ++ )
  11.                 OLEDPixel ( i, 27, ON );
  12.  
  13.         for ( unsigned char i = 1; i < 125; i ++ )
  14.                 OLEDPixel ( i, i, ON );
  15.  
  16.         for ( unsigned char i = 4; i < 50; i ++ )
  17.                 OLEDPixel ( 15, i, ON );
  18.  
  19.  
  20. //      printArrayTest ( );
  21.         for ( unsigned char i = 0; i < 12; i++ )
  22.                 OLEDPixel ( i, i, ON );
  23.         for ( unsigned char i = 0; i < 12; i++ )
  24.                 OLEDPixel ( i, 3, ON );
  25. //      OLEDPixel ( 8, 8, OFF );
  26. /
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re: OLED 128 x 64
« Respuesta #2 en: 16 de Marzo de 2015, 20:24:41 »
thanks for sharing, i never used an OLED display in my life  :(

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: OLED 128 x 64
« Respuesta #3 en: 17 de Marzo de 2015, 00:09:52 »
Ok, Finally I just make it run, it has some bugs, but I'll fix them later (sadly I have a real job LOL)

So far it print, lines, rectangles, circles, text, bars, works on XC8 and it is running the PIC18F45K22

Here are some pics with text, and as soon I got it running good I'll post it

Ok, Finalmente acabo Hacer que funciona, tiene algunos errores, pero lo voy a arreglar más tarde (por desgracia tengo un trabajo de verdad LOL)

Hasta el momento se imprime, líneas, rectángulos, círculos, texto, bares, trabaja en XC8 y se está ejecutando la PIC18F45K22

Aquí están algunas fotos con texto, y tan pronto lo tengo funcionando bien voy a publicar




...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: OLED 128 x 64
« Respuesta #4 en: 17 de Marzo de 2015, 00:25:21 »
Here some Bugs to fix...

Aquí algunos errores para solucionar ...



...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: OLED 128 x 64
« Respuesta #5 en: 29 de Marzo de 2015, 17:00:36 »
Hey there

Finally here is the I2C Library for XC8

Hola

Finalmente aquí está la Biblioteca I2C para XC8



One advantage is that u don't need a Reset Pin, the SetUp will do the job for u
Una ventaja es que u no necesita un pin de restablecimiento, la configuración hará el trabajo para u
« Última modificación: 29 de Marzo de 2015, 17:06:46 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re: OLED 128 x 64
« Respuesta #6 en: 29 de Marzo de 2015, 17:04:48 »
OLED Display.c

Código: C
  1. /************ OLED Display.c *********/
  2.  
  3. #include "OLED Display.h"
  4.  
  5.  
  6.  
  7.  
  8. char OLEDSetUp ( void )
  9. {
  10. #if defined RESET_PIN_NEEDED
  11.         __delay_ms ( 1 );                               // As OLDE VDD goes high sooner than VCC,
  12.         OLED_RESET_PIN = OFF;                   // we must wait to stabilize VCC
  13.         __delay_ms ( 1 );                               // OLED Reset pin.
  14.         OLED_RESET_PIN = ON;                   
  15. #endif
  16.  
  17. // 1. Fundamental Commands.
  18.         OLEDCommand ( OLED_CONTRAST );                          // 0x81
  19.         OLEDCommand ( 0x4F );                                           // was 0x9F. ******
  20.         OLEDCommand ( OLED_DISPLAY_ALL_ON_RESUME );     // 0xA5
  21.         OLEDCommand ( OLED_NORMAL_DISPLAY );            // 0xA6
  22.         OLEDCommand ( OLED_DISPLAY_ON );                        // 0xAF
  23.  
  24. // 2. Scrolling.
  25.  
  26. // 3. Addressing.
  27.         OLEDCommand ( OLED_MEMORY_MODE );                       // 0x20
  28.         OLEDCommand ( OLED_HORIZONTAL_MODE );
  29.                
  30. // 4. Hardware Configuration.
  31.         OLEDCommand ( OLED_SET_START_LINE | 0x00 );     // line #0
  32.         OLEDCommand ( OLED_SEGMENT_REMAP | 0x01 );      // WTF???
  33.         OLEDCommand ( OLED_SET_MULTIPLEX_RATIO );       // 0xA8
  34.         OLEDCommand ( 0x3F );
  35.         OLEDCommand ( OLED_COM_SCAN_DECREMENT );        // WTF???
  36.         OLEDCommand ( OLED_DISPLAY_OFFSET );            // 0xD3
  37.         OLEDCommand ( 0x0 );                                            // no offset
  38.         OLEDCommand ( OLED_SET_COM_PINS );                      // 0xDA
  39.         OLEDCommand ( 0x12 );
  40.  
  41. // 5. Timing.
  42.         OLEDCommand ( OLED_DISPLAY_CLOCK_RATIO );       // 0xD5
  43.         OLEDCommand ( OLED_CLOCK_RESET_RATIO );
  44.         OLEDCommand ( OLED_SET_PRECHARGE );                     // 0xd9
  45.         OLEDCommand ( OLED_PRECHARGE_P1_P2 );           // 0xF1
  46.         OLEDCommand ( OLED_SET_VCOM_H );                        // 0xDB
  47.         OLEDCommand ( 0x40 );                                           // WTF???
  48.  
  49. // 6?. Other
  50.         OLEDCommand ( OLED_CHARGE_PUMP );                       // 0x8D
  51.         OLEDCommand ( OLED_ENABLE_CHARGE );                     // 0x14
  52.  
  53.         OLEDStopScroll ( );
  54.        
  55.         return 0x00;
  56.         }
  57.        
  58.        
  59. void OLEDCommand ( unsigned char c )
  60. {
  61.         unsigned char command = 0x00;   // Co = 0, D/C=0, followed by 00 0000.
  62.        
  63.         StartI2C ( );
  64.         I2CAddressCommand ( OLED_ADDRESS, I2C_WRITE );
  65.         WriteI2C ( command );
  66.         WriteI2C ( c );
  67.         StopI2C ( );
  68.         }
  69.        
  70.  
  71. /********** prints out the Data to the GLCD ********/
  72. void OLEDData ( unsigned char c )
  73. {
  74.         unsigned char control = 0x40;   // Co = 0, D/C = 1
  75.  
  76.         StartI2C ( );
  77.         I2CAddressCommand ( OLED_ADDRESS, I2C_WRITE );
  78.         WriteI2C ( control );
  79.         WriteI2C ( c );
  80.         StopI2C ( );
  81.         }
  82.  
  83.  
  84. /********** prints out a pixel on the GLCD ********/
  85. void OLEDPixel ( int x, int y, char color )
  86. {
  87.         unsigned char *ptr;
  88.         unsigned char page;
  89.         unsigned int axisX;
  90.         unsigned int axisY;
  91.         signed char bitNumber, bitPlace;
  92.         unsigned int address;
  93.  
  94.  
  95.         if ( ( x < ARRAY_WIDHT && y <= ARRAY_LENGHT ) &&
  96.                 ( x >= 0x00 && y >= 0x00 ) )
  97.         {
  98.                 axisY = y / 8;                                                  // Getting the row value.
  99.                 axisX = x;                                                              // Getting the column value.
  100.                 page = abs ( axisY );                                   // Getting the Page number.
  101.                 bitNumber = ( y % 8 );                                  // Getting the bit value on the column.
  102.                
  103. /*              printf ( " \nX:%d, seg#:%d", x, axisX );
  104.                 printf ( " \nY:%d, PAGE:%d", y, page );
  105.                 printf ( " \nbit #:%d", bitNumber );
  106. */
  107.                 int temp;
  108.                 temp =  y / 8;
  109.                 temp *= ARRAY_WIDHT;
  110.                 temp += x;
  111.                 ptr = ( char * ) TEST;
  112.  
  113.         // Set Column Address.
  114.                 OLEDCommand ( OLED_COLUMN_ADDRESS );           
  115.                 OLEDCommand ( axisX );                                  // Column start address.
  116.                 OLEDCommand ( axisX );                                  // Column end address.
  117.         // Set Page Address.
  118.                 OLEDCommand ( OLED_PAGE_ADDRESS );             
  119.                 OLEDCommand ( page );                                   // Page start Address.
  120.                 OLEDCommand ( page );                                   // Page end Address.
  121.                
  122.                 if ( color )    // bitSet ( x, x ) equivalent.
  123. //                      OLEDData ( *( ptr + temp ) |=  ( bitWise ( y % 8 ) ) );
  124.                         *( ptr + temp ) |=  ( bitWise ( y % 8 ) );
  125.  
  126.                 else                    // bitClear ( x, x ) equivalent.       
  127. //                      OLEDData ( *( ptr + temp ) &=  ~( bitWise ( y % 8 ) ) );
  128.                         *( ptr + temp ) &=  ~( bitWise ( y % 8 ) );
  129.  
  130.                 }       // If the axies are whithin the OLED margin.
  131.         }       // test Pixel. 
  132.  
  133.  
  134.  
  135. /********** prints out the 'color' Data to the GLCD ********/
  136. void OLEDFill ( unsigned char color )
  137. {
  138. //      OLEDClearBuffer ( );
  139.  
  140.         unsigned char *ptr;
  141.         unsigned char i, j;
  142.  
  143.         ptr = ( char * ) TEST;
  144.  
  145.         for ( unsigned int i = 0x00; i < ARRAY_HEIGHT; i++ )
  146.                 for ( unsigned int j = 0x00; j < ARRAY_WIDHT; j++ )
  147.                         *( ptr + ( j + i * ARRAY_WIDHT ) ) = color;
  148.  
  149.  
  150. // Set the Display with the 'color' Data.
  151.         OLEDCommand ( OLED_LOW_COLUMN_START );          //set lower column address.
  152.         OLEDCommand ( OLED_HIGH_COLUMN_START );         //set higher column address.
  153.         OLEDCommand ( OLED_PAGE_START );                        //set page address.
  154.  
  155.         for ( j = 0x00; j < ARRAY_HEIGHT; j++ )
  156.         {
  157.                 OLEDCommand ( OLED_LOW_COLUMN_START );  //set lower column address.
  158.                 OLEDCommand ( OLED_HIGH_COLUMN_START ); //set higher column address.
  159.                 OLEDCommand ( OLED_PAGE_START + j );    //set page address.
  160.                
  161.                 for ( i = 0x00; i < ARRAY_WIDHT; i++ )
  162.                         OLEDData ( *( ptr + ( j + i * ARRAY_WIDHT ) ) = color );
  163. //                      OLEDData ( color );
  164.                 }       // Printing out the rows an then the columns.
  165.  
  166.         }       // OLEDFill ( ).
  167.  
  168.  
  169. void OLEDClearBuffer ( void )
  170. {
  171.         unsigned char *ptr;
  172.         unsigned char ColumnAddress, PageAddress;
  173.  
  174.         ptr = ( char * ) TEST;
  175.  
  176.         for ( PageAddress = 0x00; PageAddress < ARRAY_HEIGHT; PageAddress++ )
  177.                 for ( ColumnAddress = 0x00; ColumnAddress < ARRAY_WIDHT; ColumnAddress++ )
  178.                         *( ptr + ( ColumnAddress + PageAddress * ARRAY_WIDHT ) ) = CLEAR;
  179.        
  180.         }      
  181.  
  182.  
  183.  
  184.  
  185. void OLEDUpdateDisplay ( unsigned char clear )
  186. {
  187.         unsigned char ColumnAddress, PageAddress;
  188.         unsigned char *ptr;
  189.        
  190.         ptr = ( char * ) TEST;
  191.  
  192. // Set Page Address.
  193.         OLEDCommand ( OLED_PAGE_ADDRESS );             
  194.         OLEDCommand ( 0x00 );                                   // Page start Address.
  195.         OLEDCommand ( 0x07 );                                   // Page end Address.
  196.  
  197. // Set Column Address.
  198.         OLEDCommand ( OLED_COLUMN_ADDRESS );           
  199.         OLEDCommand ( 0x00 );                                   // Column start address.
  200.         OLEDCommand ( 0x7F );                                   // Column end address.
  201.  
  202.         for ( PageAddress = 0x00; PageAddress < ARRAY_HEIGHT; PageAddress++ )  
  203.         {
  204. // Set Page Address.
  205.                 OLEDCommand ( OLED_PAGE_ADDRESS );             
  206.                 OLEDCommand ( 0x00 + PageAddress );     // Page start Address.
  207.                 OLEDCommand ( 0x07 );                           // Page end Address.
  208. // Set Column Address.
  209.                 OLEDCommand ( OLED_COLUMN_ADDRESS );           
  210.                 OLEDCommand ( 0x00 );                   // Column start address.
  211.                 OLEDCommand ( 0x7F );                   // Column end address.
  212.  
  213.                 StartI2C ( );
  214.                 I2CAddressCommand ( OLED_ADDRESS, I2C_WRITE );
  215.                 WriteI2C ( OLED_SET_START_LINE );
  216.                 for ( ColumnAddress = 0x00; ColumnAddress < ARRAY_WIDHT; ColumnAddress++ )
  217.                         WriteI2C ( *( ptr + ( ( PageAddress * ARRAY_WIDHT) + ColumnAddress ) ) );
  218.                 StopI2C ( );
  219.                 }       // Printing out the rows an then the columns.
  220.  
  221.         if ( clear )                            // if erased DDRAM wanted on every Update.
  222.                 OLEDClearBuffer ( );
  223.        
  224.         }       // OLEDUpdateDisplay.
  225.  
  226.  
  227.        
  228. /********** prints out on the UART PORT the Arrays ********/
  229. /********** For Testing Purposes ********/
  230. void printArrayTest ( void )
  231. {
  232.         for ( unsigned char i = 0; i < ARRAY_HEIGHT; i++ )     
  233.         {
  234.                 printf ( "\n" );
  235.                 for ( unsigned char j = 0; j < ARRAY_WIDHT; j++ )
  236.                         printf ( "[%2X]", TEST [ i ] [ j ] );
  237.                 }              
  238.         printf ( "\n" );
  239.         }
  240.  
  241.  
  242.  
  243. void OLEDInvertedDisplay ( unsigned char complement )
  244. {
  245.         if ( complement )
  246.                 OLEDCommand ( OLED_INVERTED_DISPLAY );
  247.         else
  248.                 OLEDCommand ( OLED_NORMAL_DISPLAY );
  249.         }      
  250.        
  251.  
  252.  
  253.  
  254. /*Continuous Horizontal Scroll Setup */
  255. // The bigger the frequency, the longer the time to scroll.
  256. void OLEDScrollToRight ( unsigned char start, unsigned char stop, unsigned char frequency  )
  257. {
  258.         OLEDCommand ( OLED_RIGHT_HORIZONTAL_SCROLL );
  259.         OLEDCommand ( OLED_DUMMY_BYTE );                                // A, Dummy byte.
  260.         OLEDCommand ( start );                                                  // B, Define start page address.
  261.         OLEDCommand ( frequency );                                              // C, Frame frequency.
  262. // Step D must be larger or equal to B.
  263.         OLEDCommand ( stop );                                                   // D, Define end page address.
  264.         OLEDCommand ( OLED_DUMMY_BYTE );                                // E, Dummy byte (Set as 00h).
  265.         OLEDCommand ( 0xFF );                                                   // F, Dummy byte (Set as ffh)
  266.         OLEDCommand ( OLED_ACTIVATE_SCROLL );                   // Start scrolling.
  267.         }
  268.        
  269. void OLEDScrollToLeft ( unsigned char start, unsigned char stop, unsigned char frequency )
  270. {
  271.         OLEDCommand ( OLED_LEFT_HORIZONTAL_SCROLL );
  272.         OLEDCommand ( OLED_DUMMY_BYTE );                                // A, Dummy byte.
  273.         OLEDCommand ( start );                                                  // B, Define start page address.
  274.         OLEDCommand ( frequency );                                              // C, Frame frequency.
  275. // Step D must be larger or equal to B.
  276.         OLEDCommand ( stop );                                                   // D, Define end page address.
  277.         OLEDCommand ( OLED_DUMMY_BYTE );                                // E, Dummy byte (Set as 00h).
  278.         OLEDCommand ( 0xFF );                                                   // F, Dummy byte (Set as ffh)
  279.         OLEDCommand ( OLED_ACTIVATE_SCROLL );                   // Start scrolling.
  280.         }      
  281.        
  282.        
  283. void OLEDScrollVerticalAndRight ( unsigned char start, unsigned char stop,
  284.                                                                 unsigned char frequency, unsigned char offset )
  285. {
  286.         OLEDCommand ( OLED_VERTICAL_RIGHT );
  287.         OLEDCommand ( OLED_DUMMY_BYTE );                                // A, Dummy byte.
  288.         OLEDCommand ( start );                                                  // B, Define start page address.
  289.         OLEDCommand ( frequency );                                              // C, Frame frequency.
  290. // Step D must be larger or equal to B.
  291.         OLEDCommand ( stop );                                                   // D, Define end page address.
  292. //No continuous vertical scrolling is available.
  293.         OLEDCommand ( offset );                                                 // E, Vertical scrolling offset.
  294.         OLEDCommand ( OLED_ACTIVATE_SCROLL );                   // Start scrolling.
  295.         }
  296.        
  297.        
  298. void OLEDScrollVerticalAndLeft ( unsigned char start, unsigned char stop,
  299.                                                                 unsigned char frequency, unsigned char offset )
  300. {
  301.         OLEDCommand ( OLED_VERTICAL_LEFT );
  302.         OLEDCommand ( OLED_DUMMY_BYTE );                                // A, Dummy byte.
  303.         OLEDCommand ( start );                                                  // B, Define start page address.
  304.         OLEDCommand ( frequency );                                              // C, Frame frequency.
  305. // Step D must be larger or equal to B.
  306.         OLEDCommand ( stop );                                                   // D, Define end page address.
  307. //No continuous vertical scrolling is available.
  308.         OLEDCommand ( offset );                                                 // E, Vertical scrolling offset.
  309.         OLEDCommand ( OLED_ACTIVATE_SCROLL );                   // Start scrolling.
  310.         }      
  311.        
  312.        
  313. void OLEDScrollVertical ( unsigned char startRow, unsigned char stopRow, unsigned char frequency )
  314. {
  315.         OLEDCommand ( OLED_VERTICAL_SCROLL );
  316.         OLEDCommand ( startRow );                                       // A, Define start row address.
  317.         OLEDCommand ( stopRow );                                        // B, Define end row address.
  318.  
  319. //      OLEDCommand ( OLED_VERTICAL_LEFT );
  320. //      OLEDCommand ( OLED_DUMMY_BYTE );                        // A, Dummy byte.
  321. //      OLEDCommand ( startRow );                                       // B, Define start page address.
  322. //      OLEDCommand ( frequency );                                      // C, Frame frequency.
  323. // Step D must be larger or equal to B.
  324. //      OLEDCommand ( stopRow );                                        // D, Define end page address.
  325. //No continuous vertical scrolling is available.
  326. //      OLEDCommand ( 0x01 );                                           // E, Vertical scrolling offset.
  327.  
  328.         OLEDCommand ( OLED_ACTIVATE_SCROLL );           // Start scrolling.
  329.        
  330.         }
  331.  
  332.  
  333. void OLEDStopScroll ( void )
  334. {
  335.         OLEDCommand ( OLED_DEACTIVATE_SCROLL );
  336.         }      
  337.  
  338.        
  339.        
  340.        
  341. void OLEDLine ( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, char color )
  342. {
  343.         int  x, y, addx, addy, dx, dy;
  344.         int P;
  345.         int i;
  346.  
  347.         dx = abs ( ( int ) ( x2 - x1 ) );
  348.         dy = abs ( ( int ) ( y2 - y1 ) );
  349.         x = x1;
  350.         y = y1;
  351.  
  352.         if ( x1 > x2 )
  353.                 addx = -1;
  354.         else
  355.                 addx = 1;
  356.        
  357.         if ( y1 > y2 )
  358.                 addy = -1;
  359.         else
  360.                 addy = 1;
  361.  
  362.         if ( dx >= dy )
  363.         {
  364.                 P = 2 * dy - dx;
  365.  
  366.                 for ( i = 0; i <= dx; ++i )
  367.                 {
  368.                         OLEDPixel ( x, y, color );
  369.  
  370.                         if ( P < 0 )
  371.                         {
  372.                                 P += 2 * dy;
  373.                                 x += addx;
  374.                                 }
  375.                         else
  376.                         {
  377.                                 P += 2 * dy - 2 * dx;
  378.                                 x += addx;
  379.                                 y += addy;
  380.                                 }
  381.                         }
  382.                 }
  383.         else
  384.         {
  385.                 P = 2 * dx - dy;
  386.  
  387.                 for ( i = 0; i <= dy; ++i )
  388.                 {
  389.                         OLEDPixel ( x, y, color );
  390.  
  391.                         if ( P < 0 )
  392.                         {
  393.                                 P += 2 * dx;
  394.                                 y += addy;
  395.                                 }
  396.                         else
  397.                         {
  398.                                 P += 2 * dx - 2 * dy;
  399.                                 x += addx;
  400.                                 y += addy;
  401.                                 }
  402.                         }
  403.                 }
  404.         }
  405.  
  406.  
  407. void OLEDRectangular ( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, char color )
  408. {
  409.         if ( fill )
  410.         {
  411.                 unsigned char y, ymax;                          // Find the y min and max
  412.                
  413.                 if ( y1 < y2 )
  414.                 {
  415.                         y = y1;
  416.                         ymax = y2;
  417.                         }
  418.                 else
  419.                 {
  420.                         y = y2;
  421.                         ymax = y1;
  422.                         }
  423.  
  424.                 for ( ; y <= ymax; ++y )                    // Draw lines to fill the rectangle
  425.                         OLEDLine ( x1, y, x2, y, color );
  426.                 }
  427.         else
  428.         {
  429.                 OLEDLine ( x1, y1, x2, y1, color );      // Draw the 4 sides
  430.                 OLEDLine ( x1, y2, x2, y2, color );
  431.                 OLEDLine ( x1, y1, x1, y2, color );
  432.                 OLEDLine ( x2, y1, x2, y2, color );
  433.                 }
  434.         }
  435.  
  436.  
  437. void OLEDAreaFill ( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, char color )
  438. {
  439.         unsigned char y, ymax;                          // Find the y min and max
  440.        
  441.         if ( y1 < y2 )
  442.         {
  443.                 y = y1;
  444.                 ymax = y2;
  445.                 }
  446.         else
  447.         {
  448.                 y = y2;
  449.                 ymax = y1;
  450.                 }
  451.  
  452.         for ( ; y <= ymax; ++y )                    // Draw lines to fill the rectangle
  453.                 OLEDLine ( x1, y, x2, y, color );
  454.         }      
  455.  
  456.  
  457. void OLEDCircle ( int x, int y, int radius, char fill, char color )
  458. {
  459.         int a, b, P;
  460.         a = 0x00;
  461.         b = radius;
  462.         P = 0x01 - radius;
  463.        
  464.         do
  465.         {
  466.                 if( fill )
  467.                 {
  468.                         OLEDLine ( x - a, y + b, x + a, y + b, color );
  469.                         OLEDLine ( x - a, y - b, x + a, y - b, color );
  470.                         OLEDLine ( x - b, y + a, x + b, y + a, color );
  471.                         OLEDLine ( x - b, y - a, x + b, y - a, color );
  472.                         }
  473.                 else
  474.                 {
  475.                         OLEDPixel ( a + x, b + y, color );
  476.                         OLEDPixel ( b + x, a + y, color );
  477.                         OLEDPixel ( x - a, b + y, color );
  478.                         OLEDPixel ( x - b, a + y, color );
  479.                         OLEDPixel ( b + x, y - a, color );
  480.                         OLEDPixel ( a + x, y - b, color );
  481.                         OLEDPixel ( x - a, y - b, color );
  482.                         OLEDPixel ( x - b, y - a, color );
  483.                 }
  484.  
  485.                 if ( P < 0 )
  486.                         P += 3 + 2 * a++;
  487.                 else
  488.                         P += 5 + 2 * ( a++ - b-- );
  489.                
  490.                 } while ( a <= b );
  491.         }
  492.  
  493.  
  494. void OLEDBar ( int x1, int y1, int x2, int y2, int width, char color )
  495. {
  496.         signed int  x, y, addx, addy, j;
  497.         signed int P, dx, dy, c1, c2;
  498.         char i;
  499.  
  500.         dx = abs ( ( signed int ) ( x2 - x1 ) );
  501.         dy = abs ( ( signed int ) ( y2 - y1 ) );
  502.         x = x1;
  503.         y = y1;
  504.         c1 = -dx * x1 - dy * y1;
  505.         c2 = -dx * x2 - dy * y2;
  506.  
  507.         if ( x1 > x2 )
  508.         {
  509.                 addx = -1;
  510.                 c1 = -dx * x2 - dy * y2;
  511.                 c2 = -dx * x1 - dy * y1;
  512.                 }
  513.         else
  514.                 addx = 1;
  515.        
  516.         if ( y1 > y2 )
  517.         {
  518.                 addy = -1;
  519.                 c1 = -dx*x2 - dy*y2;
  520.                 c2 = -dx*x1 - dy*y1;
  521.                 }
  522.         else
  523.                 addy = 1;
  524.  
  525.         if ( dx >= dy )
  526.         {
  527.                 P = 2*dy - dx;
  528.  
  529.                 for ( i = 0; i <= dx; ++i )
  530.                 {
  531.                         for ( j =- ( width / 2 ); j < width / 2 + width % 2; ++j )
  532.                         {
  533.                                 if ( dx * x + dy * ( y + j ) + c1 >= 0 && dx * x + dy * ( y + j ) + c2 <= 0 )
  534.                                         OLEDPixel ( x, y + j, color );
  535.                                 }
  536.                        
  537.                         if ( P < 0 )
  538.                         {
  539.                                 P += 2 * dy;
  540.                                 x += addx;
  541.                                 }
  542.                 else
  543.                         {
  544.                                 P += 2 * dy - 2 * dx;
  545.                                 x += addx;
  546.                                 y += addy;
  547.                                 }
  548.                         }
  549.                 }
  550.         else
  551.         {
  552.                 P = 2 * dx - dy;
  553.  
  554.                 for ( i = 0; i <= dy; ++i )
  555.                 {
  556.                         if ( P < 0 )
  557.                         {
  558.                                 P += 2 * dx;
  559.                                 y += addy;
  560.                                 }
  561.                         else
  562.                         {
  563.                                 P += 2 * dx - 2 * dy;
  564.                                 x += addx;
  565.                                 y += addy;
  566.                                 }
  567.                        
  568.                         for ( j =- ( width / 2 ); j < width / 2 + width % 2; ++j )
  569.                         {
  570.                                 if ( dx * x + dy * ( y + j ) + c1 >= 0 && dx * x + dy * ( y + j ) + c2 <= 0 )
  571.                                 OLEDPixel ( x + j, y, color );
  572.                                 }
  573.                         }
  574.                 }
  575.         }
  576.  
  577.  
  578.  
  579. void OLEDText ( int x, int y, char* textptr, int size, char color )
  580. {
  581.         unsigned char i, j, k, l, m;                                            // Loop counters
  582.         unsigned char pixelData [ 5 ];                                          // Stores character data
  583.  
  584.         for ( i = 0; textptr [ i ] != 0x00; ++i, ++x )          // Loop through the passed string
  585.         {
  586.                 if ( textptr [ i ] < 'S') // Checks if the letter is in the first text array
  587.                         memcpy ( pixelData, TEXT [ textptr [ i ] - ' '], 5 );
  588.                
  589.                 else if(textptr[i] <= '~') // Check if the letter is in the second array
  590.                 {
  591.                         if ( ( textptr[i] >= 'a' ) && ( textptr[i] <= 'z' ) )
  592.                                 memcpy ( pixelData, TEXT2 [ textptr [ i ] - 'S' - 1 ], 5 );
  593.                        
  594.                         else
  595.                                 memcpy ( pixelData, TEXT2 [ textptr [ i ] - 'S' ], 5 );
  596.  
  597.                         }
  598.                
  599.                 else
  600.                         memcpy ( pixelData, TEXT [ 0 ], 5 );            // Default to space
  601.  
  602.                 if ( x + 5 * size >= ARRAY_WIDHT )                              // Performs character wrapping
  603.                 {      
  604.                         x = 0;                                                                          // Set x at far left position
  605.                         y += 7*size + 1;                                                        // Set y at next position down
  606.                 }
  607.      
  608.                 for ( j = 0; j < 5; ++j, x += size )                    // Loop through character byte data
  609.                 {
  610.                         for ( k = 0; k <7 *size; ++k )                          // Loop through the vertical pixels
  611.                         {
  612.                                 if ( bit_test ( pixelData [ j ], k ) )  // Check if the pixel should be set
  613.                                 {
  614.                                         for ( l = 0; l < size; ++l )            // The next two loops change the
  615.                                         {                                                                       // character's size
  616.                                                 for ( m = 0; m < size; ++m )
  617.                                                 {
  618.                                                         OLEDPixel ( x + m, y + k * size + l, color ); // Draws the pixel
  619.                                                         }
  620.                                                 }
  621.                                         }
  622.                                 }
  623.                         }
  624.                 }
  625.         }


OLED Display.h

Código: C
  1. #include        <xc.h>
  2. /************ OLED Display.h *********/
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. // Commnet this line if the OLED Display needs a Reset Pin
  9. //#define RESET_PIN_NEEDED
  10.  
  11.  
  12.  
  13. #if defined RESET_PIN_NEEDED
  14.         #ifndef OLED_RESET_PIN
  15.         #define OLED_RESET_PIN          PORTCbits . RC0
  16.         #endif
  17. #endif
  18.  
  19.  
  20. #define ARRAY_WIDHT             128
  21. #define ARRAY_HEIGHT    8
  22. #define ARRAY_LENGHT    ( 8 * ARRAY_HEIGHT )
  23. #define SSD1306_GDDRAM  ( ARRAY_WIDHT * ARRAY_HEIGHT / 8 )
  24. unsigned char TEST [ ARRAY_HEIGHT ] [ ARRAY_WIDHT ];   
  25.  
  26.  
  27. #define WHITE                   0x01
  28. #define BLACK                   0x00
  29.  
  30. #define FILL                    0x01
  31. #define NO_FILL                 0x00
  32.  
  33. // 1. Fundamental Commands.
  34. #define OLED_CONTRAST                           0x81
  35. #define OLED_DISPLAY_ALL_ON_RESUME      0xA4
  36. #define OLED_DISPLAY_ALL_ON             0xA5
  37. #define OLED_NORMAL_DISPLAY             0xA6
  38. #define OLED_INVERTED_DISPLAY           0xA7
  39. #define OLED_DISPLAY_OFF                        0xAE
  40. #define OLED_DISPLAY_ON                         0xAF
  41.  
  42. // 2. Scrolling
  43. #define OLED_RIGHT_HORIZONTAL_SCROLL    0x26
  44. #define OLED_LEFT_HORIZONTAL_SCROLL             0x27
  45. #define OLED_5_FRAMES                                   0x00
  46. #define OLED_64_FRAMES                                  0x01
  47. #define OLED_128_FRAMES                                 0x02
  48. #define OLED_256_FRAMES                                 0x03
  49. #define OLED_3_FRAMES                                   0x04
  50. #define OLED_4_FRAMES                                   0x05
  51. #define OLED_25_FRAMES                                  0x06
  52. #define OLED_2_FRAMES                                   0x07
  53. #define OLED_DUMMY_BYTE                                 0x00
  54. #define OLED_VERTICAL_RIGHT                             0x29
  55. #define OLED_VERTICAL_LEFT                              0x2A
  56. #define OLED_DEACTIVATE_SCROLL                  0x2E
  57. #define OLED_ACTIVATE_SCROLL                    0x2F
  58. #define OLED_VERTICAL_SCROLL                    0xA3
  59.  
  60.  
  61. // 3. Addressing.
  62. #define OLED_LOW_COLUMN_START           0x00
  63. #define OLED_HIGH_COLUMN_START          0x10
  64. #define OLED_MEMORY_MODE                        0x20
  65. #define OLED_HORIZONTAL_MODE            0x00
  66. #define OLED_VERTICAL_MODE                      0x01
  67. #define OLED_PAGE_MODE                          0x02
  68. #define OLED_COLUMN_ADDRESS                     0x21
  69. #define OLED_PAGE_ADDRESS                       0x22
  70. #define OLED_PAGE_START                         0xB0
  71.  
  72. // 4. Hardware Configuration.
  73. #define OLED_SET_START_LINE                     0x40
  74. #define OLED_SEGMENT_REMAP                      0xA0
  75. #define OLED_SET_MULTIPLEX_RATIO        0xA8
  76. #define OLED_COM_SCAN_INCREMENT         0xC0
  77. #define OLED_COM_SCAN_DECREMENT         0xC8
  78. #define OLED_DISPLAY_OFFSET             0xD3
  79. #define OLED_SET_COM_PINS                       0xDA
  80.  
  81. // 5. Timing.
  82. #define OLED_DISPLAY_CLOCK_RATIO        0xD5
  83. #define OLED_CLOCK_RESET_RATIO          0x80
  84. #define OLED_SET_PRECHARGE                      0xD9
  85. #define OLED_PRECHARGE_RESET            0x22
  86. #define OLED_PRECHARGE_P1_P2            0xF1
  87. #define OLED_SET_VCOM_H                         0xDB
  88. #define OLED_VCC_RESET                          0x20
  89.  
  90. // 6?. Other
  91. #define OLED_CHARGE_PUMP                        0x8D
  92. #define OLED_DISABLE_CHARGE                     0x10
  93. #define OLED_ENABLE_CHARGE                      0x14
  94.  
  95.  
  96. #define bitWise(n) ( 1 << n )
  97.  
  98. #define SIZE_ONE                                        0x01
  99. #define SIZE_TWO                                        0x02
  100. #define SIZE_THREE                                      0x03
  101.  
  102. #define DDGRAM_CLEAR                            0x01
  103. #define DDGRAM_NO_CLEAR                         0x00
  104.  
  105.  
  106. char OLEDSetUp ( void );
  107. void OLEDCommand ( unsigned char c );
  108. void OLEDPixel ( int x, int y, char color );
  109. void OLEDCommandSend ( char reg, char data );
  110. void OLEDData ( unsigned char c );
  111. void OLEDFill ( unsigned char dat );
  112. void OLEDUpdateDisplay ( unsigned char clear );
  113. void OLEDClearBuffer ( void );
  114.  
  115. void OLEDInvertedDisplay ( unsigned char complement );
  116. void OLEDScrollToRight ( unsigned char start, unsigned char stop, unsigned char frequency  );
  117. void OLEDScrollToLeft ( unsigned char start, unsigned char stop, unsigned char frequency );
  118. void OLEDScrollVerticalAndRight ( unsigned char start, unsigned char stop,
  119.                                                                 unsigned char frequency, unsigned char offset );
  120. void OLEDScrollVerticalAndLeft ( unsigned char start, unsigned char stop,
  121.                                                                 unsigned char frequency, unsigned char offset );
  122. void OLEDScrollVertical ( unsigned char startRow, unsigned char stopRow, unsigned char frequency );     //8!
  123. void OLEDStopScroll ( void );
  124.  
  125.  
  126.  
  127. void testArray ( void );
  128. void clearTest ( void );
  129. void printArrayTest ( void );
  130.  
  131.  
  132. void OLEDLine ( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, char color );
  133. void OLEDRectangular ( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, char color );
  134. void OLEDCircle ( int x, int y, int radius, char fill, char color );
  135. void OLEDBar ( int x1, int y1, int x2, int y2, int width, char color );
  136. void OLEDAreaFill ( unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2, unsigned char fill, char color );
  137. void OLEDText ( int x, int y, char* textptr, int size, char color );
  138.  
  139.  
  140.  
  141.  
  142.  
  143. // CHARACTERS GRTAPIC MODE                      // !!
  144. #if defined ( _PIC16F887_H_ )
  145.         const char TEXT[51][5] =
  146. #elif defined ( _PIC18F45K22_H_ ) || defined ( _PIC18F44K22_H_ )
  147. //      const char TEXT[51][5] @ 0x1000 =
  148. //      __far const char TEXT[51][5] =  
  149.         const char TEXT[51][5] @ 0x1000 =
  150. #endif
  151.                                                 {0x00, 0x00, 0x00, 0x00, 0x00, // SPACE
  152.                          0x00, 0x00, 0x5F, 0x00, 0x00, // !
  153.                          0x00, 0x03, 0x00, 0x03, 0x00, // "
  154.                          0x14, 0x3E, 0x14, 0x3E, 0x14, // #
  155.                          0x24, 0x2A, 0x7F, 0x2A, 0x12, // $
  156.                          0x43, 0x33, 0x08, 0x66, 0x61, // %
  157.                          0x36, 0x49, 0x55, 0x22, 0x50, // &
  158.                          0x00, 0x05, 0x03, 0x00, 0x00, // '
  159.                          0x00, 0x1C, 0x22, 0x41, 0x00, // (
  160.                          0x00, 0x41, 0x22, 0x1C, 0x00, // )
  161.                          0x14, 0x08, 0x3E, 0x08, 0x14, // *
  162.                          0x08, 0x08, 0x3E, 0x08, 0x08, // +
  163.                          0x00, 0x50, 0x30, 0x00, 0x00, // ,
  164.                          0x08, 0x08, 0x08, 0x08, 0x08, // -
  165.                          0x00, 0x60, 0x60, 0x00, 0x00, // .
  166.                          0x20, 0x10, 0x08, 0x04, 0x02, // /
  167.                          0x3E, 0x51, 0x49, 0x45, 0x3E, // 0
  168.                          0x04, 0x02, 0x7F, 0x00, 0x00, // 1
  169.                          0x42, 0x61, 0x51, 0x49, 0x46, // 2
  170.                          0x22, 0x41, 0x49, 0x49, 0x36, // 3
  171.                          0x18, 0x14, 0x12, 0x7F, 0x10, // 4
  172.                          0x27, 0x45, 0x45, 0x45, 0x39, // 5
  173.                          0x3E, 0x49, 0x49, 0x49, 0x32, // 6
  174.                          0x01, 0x01, 0x71, 0x09, 0x07, // 7
  175.                          0x36, 0x49, 0x49, 0x49, 0x36, // 8
  176.                          0x26, 0x49, 0x49, 0x49, 0x3E, // 9
  177.                          0x00, 0x36, 0x36, 0x00, 0x00, // :
  178.                          0x00, 0x56, 0x36, 0x00, 0x00, // ;
  179.                          0x08, 0x14, 0x22, 0x41, 0x00, // <
  180.                          0x14, 0x14, 0x14, 0x14, 0x14, // =
  181.                          0x00, 0x41, 0x22, 0x14, 0x08, // >
  182.                          0x02, 0x01, 0x51, 0x09, 0x06, // ?
  183.                          0x3E, 0x41, 0x59, 0x55, 0x5E, // @
  184.                          0x7E, 0x09, 0x09, 0x09, 0x7E, // A
  185.                          0x7F, 0x49, 0x49, 0x49, 0x36, // B
  186.                          0x3E, 0x41, 0x41, 0x41, 0x22, // C
  187.                          0x7F, 0x41, 0x41, 0x41, 0x3E, // D
  188.                          0x7F, 0x49, 0x49, 0x49, 0x41, // E
  189.                          0x7F, 0x09, 0x09, 0x09, 0x01, // F
  190.                          0x3E, 0x41, 0x41, 0x49, 0x3A, // G
  191.                          0x7F, 0x08, 0x08, 0x08, 0x7F, // H
  192.                          0x00, 0x41, 0x7F, 0x41, 0x00, // I
  193.                          0x30, 0x40, 0x40, 0x40, 0x3F, // J
  194.                          0x7F, 0x08, 0x14, 0x22, 0x41, // K
  195.                          0x7F, 0x40, 0x40, 0x40, 0x40, // L
  196.                          0x7F, 0x02, 0x0C, 0x02, 0x7F, // M
  197.                          0x7F, 0x02, 0x04, 0x08, 0x7F, // N
  198.                          0x3E, 0x41, 0x41, 0x41, 0x3E, // O
  199.                          0x7F, 0x09, 0x09, 0x09, 0x06, // P
  200.                          0x1E, 0x21, 0x21, 0x21, 0x5E, // Q
  201.                          0x7F, 0x09, 0x09, 0x09, 0x76};// R
  202.  
  203. const char TEXT2[44][5]={0x26, 0x49, 0x49, 0x49, 0x32, // S
  204.                          0x01, 0x01, 0x7F, 0x01, 0x01, // T
  205.                          0x3F, 0x40, 0x40, 0x40, 0x3F, // U
  206.                          0x1F, 0x20, 0x40, 0x20, 0x1F, // V
  207.                          0x7F, 0x20, 0x10, 0x20, 0x7F, // W
  208.                          0x41, 0x22, 0x1C, 0x22, 0x41, // X
  209.                          0x07, 0x08, 0x70, 0x08, 0x07, // Y
  210.                          0x61, 0x51, 0x49, 0x45, 0x43, // Z
  211.                          0x00, 0x7F, 0x41, 0x00, 0x00, // [
  212.                                                  0x02, 0x04, 0x08, 0x10, 0x20, // \
  213.                          0x00, 0x00, 0x41, 0x7F, 0x00, // ]
  214.                          0x04, 0x02, 0x01, 0x02, 0x04, // ^
  215.                          0x40, 0x40, 0x40, 0x40, 0x40, // _
  216.                          0x00, 0x01, 0x02, 0x04, 0x00, // `
  217.                          0x20, 0x54, 0x54, 0x54, 0x78, // a
  218.                          0x7F, 0x44, 0x44, 0x44, 0x38, // b
  219.                          0x38, 0x44, 0x44, 0x44, 0x44, // c
  220.                          0x38, 0x44, 0x44, 0x44, 0x7F, // d
  221.                          0x38, 0x54, 0x54, 0x54, 0x18, // e
  222.                          0x04, 0x04, 0x7E, 0x05, 0x05, // f
  223.                          0x08, 0x54, 0x54, 0x54, 0x3C, // g
  224.                          0x7F, 0x08, 0x04, 0x04, 0x78, // h
  225.                          0x00, 0x44, 0x7D, 0x40, 0x00, // i
  226.                          0x20, 0x40, 0x44, 0x3D, 0x00, // j
  227.                          0x7F, 0x10, 0x28, 0x44, 0x00, // k
  228.                          0x00, 0x41, 0x7F, 0x40, 0x00, // l
  229.                          0x7C, 0x04, 0x78, 0x04, 0x78, // m
  230.                          0x7C, 0x08, 0x04, 0x04, 0x78, // n
  231.                          0x38, 0x44, 0x44, 0x44, 0x38, // o
  232.                          0x7C, 0x14, 0x14, 0x14, 0x08, // p
  233.                          0x08, 0x14, 0x14, 0x14, 0x7C, // q
  234.                          0x00, 0x7C, 0x08, 0x04, 0x04, // r
  235.                          0x48, 0x54, 0x54, 0x54, 0x20, // s
  236.                          0x04, 0x04, 0x3F, 0x44, 0x44, // t
  237.                          0x3C, 0x40, 0x40, 0x20, 0x7C, // u
  238.                          0x1C, 0x20, 0x40, 0x20, 0x1C, // v
  239.                          0x3C, 0x40, 0x30, 0x40, 0x3C, // w
  240.                          0x44, 0x28, 0x10, 0x28, 0x44, // x
  241.                          0x0C, 0x50, 0x50, 0x50, 0x3C, // y
  242.                          0x44, 0x64, 0x54, 0x4C, 0x44, // z
  243.                          0x00, 0x08, 0x36, 0x41, 0x41, // {
  244.                          0x00, 0x00, 0x7F, 0x00, 0x00, // |
  245.                          0x41, 0x41, 0x36, 0x08, 0x00, // }
  246.                          0x02, 0x01, 0x02, 0x04, 0x02};// ~


And here is the example....

Código: C
  1. /*
  2. I do not think you can name many great inventions that have been
  3. made by married men.
  4.         NT.
  5. */
  6. #pragma warning disable 1273
  7.  
  8. #include <xc.h>
  9.  
  10. #define _XTAL_FREQ      FREQ_16MHz
  11.  
  12. #include "OLED.c"
  13.  
  14. void drawFrame ( unsigned char x, unsigned char y, unsigned char w, unsigned char h );
  15. void getValues ( void );       
  16.  
  17.  
  18.  
  19. unsigned char OLED_Text_1 [ ] = "OLED Display";
  20. unsigned char OLED_Text_2 [ ] = "128 x 64";
  21. unsigned char OLED_Text_3 [ ] = "LINES";
  22. unsigned char OLED_Text_4 [ ] = "Voltage";
  23. unsigned char OLED_Text_5 [ 0x03 ];
  24. unsigned char OLED_Text_6 [ ] = "Rectangles";
  25. unsigned char OLED_Text_7 [ ] = "Circles";
  26. unsigned char OLED_Text_8 [ ] = "Bars";
  27. unsigned char OLED_Text_9 [ ] = "TEXTS";
  28. unsigned char OLED_Text_10 [ ] = "x1";
  29. unsigned char OLED_Text_11 [ ] = "x2";
  30. unsigned char OLED_Text_12 [ ] = "x3";
  31. unsigned char OLED_Text_13 [ ] = "Scroll To Right";
  32. unsigned char OLED_Text_14 [ ] = "Scroll To Left";
  33. unsigned char OLED_Text_15 [ ] = "Vertical & Right";
  34. unsigned char OLED_Text_16 [ ] = "Vertical & Left";
  35.        
  36.  
  37.                
  38.         unsigned char x1 = 6;
  39.         unsigned char x2 = 6;
  40.         unsigned char y1 = 0;
  41.         unsigned char y2 = 0;
  42.  
  43.  
  44.  
  45. int main ( string [ ], args ) //@ 0x100
  46. {
  47. /* SetUP the uController */
  48.         SetUp ( );
  49.  
  50. // Init variables.
  51.  
  52.         printfUART ( "OLED Display...\n" );
  53.         printfUART ( "V1.0...\n" );
  54.        
  55. // Menu function is requested if the MenuButton is pressed.
  56. //      menu ( );
  57.  
  58.  
  59.         OLEDLine ( 2, 15, 112, 15, WHITE );
  60.         OLEDText ( 2, 20, OLED_Text_1, SIZE_TWO, WHITE );
  61.         OLEDText ( 18, 37, OLED_Text_2, SIZE_TWO, WHITE );
  62.         OLEDLine ( 2, 55, 112, 55, WHITE );
  63.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  64.         delay_ms ( 3000 );
  65.        
  66.         OLEDText ( 28, 5, OLED_Text_3, SIZE_ONE, WHITE );
  67.         OLEDLine ( 12, 15, 112, 58, WHITE );
  68.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  69.         delay_ms ( 1000 );
  70.         OLEDLine ( 22, 10, 102, 43, WHITE );
  71.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  72.         delay_ms ( 1000 );
  73.         OLEDLine ( 12, 58, 112, 58, WHITE );
  74.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  75.         delay_ms ( 1000 );
  76.         OLEDLine ( 12, 53, 112, 18, WHITE );
  77.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  78.         delay_ms ( 1000 );
  79.  
  80.         OLEDText ( 28, 5, OLED_Text_6, SIZE_ONE, WHITE );
  81.         OLEDRectangular ( 2, 17, 82, 25, NO_FILL, WHITE );
  82.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  83.         delay_ms ( 1000 );
  84.         OLEDRectangular ( 12, 12, 42, 55, NO_FILL, WHITE );
  85.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  86.         delay_ms ( 1000 );
  87.         OLEDRectangular ( 22, 22, 36, 35, NO_FILL, WHITE );
  88.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  89.         delay_ms ( 1000 );
  90.         OLEDRectangular ( 43, 23, 112, 61, NO_FILL, WHITE );
  91.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  92.         delay_ms ( 1000 );
  93.  
  94.         OLEDText ( 28, 5, OLED_Text_7, SIZE_ONE, WHITE );
  95.         OLEDCircle ( 25, 35, 15, NO_FILL, WHITE );
  96.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  97.         delay_ms ( 1000 );
  98.         OLEDCircle ( 65, 41, 25, NO_FILL, WHITE );
  99.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  100.         delay_ms ( 1000 );
  101.         OLEDCircle ( 36, 51, 5, NO_FILL, WHITE );
  102.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  103.         delay_ms ( 1000 );
  104.         OLEDCircle ( 56, 10, 15, NO_FILL, WHITE );
  105.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  106.         delay_ms ( 1000 );
  107.  
  108.         OLEDText ( 28, 5, OLED_Text_8, SIZE_ONE, WHITE );
  109.         OLEDBar ( 2, 44, 78, 53, 5, WHITE );
  110.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  111.         delay_ms ( 1000 );
  112.         OLEDBar ( 12, 14, 118, 53, 2, WHITE );
  113.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  114.         delay_ms ( 1000 );
  115.         OLEDBar ( 2, 44, 115, 60, 3, WHITE );
  116.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  117.         delay_ms ( 1000 );
  118.         OLEDBar ( 22, 34, 88, 37, 4, WHITE );
  119.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  120.         delay_ms ( 1000 );
  121.  
  122.         OLEDText ( 28, 5, OLED_Text_9, SIZE_ONE, WHITE );
  123.         OLEDText ( 28, 15, OLED_Text_10, SIZE_ONE, WHITE );
  124.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  125.         delay_ms ( 1000 );
  126.         OLEDText ( 32, 25, OLED_Text_11, SIZE_TWO, WHITE );
  127.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  128.         delay_ms ( 1000 );
  129.         OLEDText ( 38, 40, OLED_Text_12, SIZE_THREE, WHITE );
  130.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  131.         delay_ms ( 1000 );
  132.         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  133.  
  134.         OLEDText ( 38, 40, OLED_Text_13, SIZE_ONE, WHITE );
  135.         OLEDScrollToRight ( 2, 120, OLED_2_FRAMES );
  136.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  137.         delay_ms ( 2000 );
  138.  
  139.         OLEDText ( 38, 40, OLED_Text_14, SIZE_ONE, WHITE );
  140.         OLEDScrollToLeft ( 2, 120, OLED_2_FRAMES );
  141.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  142.         delay_ms ( 2000 );
  143.  
  144.         OLEDText ( 38, 20, OLED_Text_15, SIZE_ONE, WHITE );
  145.         OLEDScrollVerticalAndRight ( 2, 120, OLED_128_FRAMES, 60 );
  146.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  147.         delay_ms ( 2000 );
  148.  
  149.         OLEDText ( 38, 20, OLED_Text_16, SIZE_ONE, WHITE );
  150.         OLEDScrollVerticalAndLeft ( 2, 120, OLED_128_FRAMES, 60 );
  151.         OLEDUpdateDisplay ( DDGRAM_CLEAR );
  152.         delay_ms ( 2000 );
  153.  
  154.         OLEDStopScroll ( );
  155.         drawFrame ( 5, 0, 125, 51 );            // Draw Plot Frame
  156.  
  157.         EnableInterrupt ( GLOBAL_INTERRUPT );
  158.  
  159. /* Main Loop */
  160.         while ( TRUE )
  161.         {
  162.                 if ( interruptFlagTMR0 )
  163.                 {
  164.                         LED_2 ^= SET;           // Toggling a LED.
  165. //                      togglingLED ( );
  166. //                      debuggingTouchSensors ( );
  167. //                      readTouchSensors ( );
  168.  
  169. //                      drawFrame ( 5, 0, 125, 51 );            // Draw Plot Frame
  170.                         getValues ( );
  171.                        
  172.                         OLEDUpdateDisplay ( DDGRAM_NO_CLEAR );
  173.  
  174.                         interruptFlagTMR0 = CLEAR;
  175.                         }       // if interrupt TMR0.
  176.  
  177.                 }       // infinite loop.      
  178.        
  179.         return 0x00;
  180.         }       // void main.
« Última modificación: 29 de Marzo de 2015, 21:35:13 por rotting79 »
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re: OLED 128 x 64
« Respuesta #7 en: 29 de Marzo de 2015, 21:08:08 »
Good joob, thanks again and i only want to say: THAT TURTLE! xD in the video.

Desconectado juaperser1

  • Colaborador
  • DsPIC30
  • *****
  • Mensajes: 2976
Re: OLED 128 x 64
« Respuesta #8 en: 30 de Marzo de 2015, 02:32:40 »
lol is donatello ninja turtle xd, good job with lcd and thanks you for sharing
Visita mi canal para aprender sobre electrónica y programación:

https://www.youtube.com/channel/UCxOYHcAMLCVEtZEvGgPQ6Vw

Desconectado viki2000

  • PIC10
  • *
  • Mensajes: 1
Re:OLED 128 x 64
« Respuesta #9 en: 23 de Octubre de 2015, 08:40:37 »
Hello rotting79,
Congratulations for this job.
I have SainSmart 1.3" SPI Serial 128X64 which is driven by SH1106 and said to have also I2C.
http://www.sainsmart.com/sainsmart-1-3-spi-serial-128x64-blue-oled.html
https://drive.google.com/folderview?id=0BwXmKaSw75eKflRabGRIRDNSNzNOMHB2ZnFHaDVkRzVnRzdQUlV0SzhsQ1d6V2hBbnBTNmc&usp=drive_web
I tested it in SPI with FlowCode, but I have also MPLABX with XC8.
In SPI works as you see below:
https://drive.google.com/file/d/0BwXmKaSw75eKaHZrWTZkeXlVd3c/view?pli=1
except that has some vertical lines on the right side.
The datasheets of some display controllers are here:
SSD1306: https://drive.google.com/file/d/0BwXmKaSw75eKMG9mTFByMnRrZVk/view?pli=1
SH1106: https://drive.google.com/file/d/0BwXmKaSw75eKREhHRkFTZUh5Qm8/view?pli=1
It is clear that SH1106 is 132x64 and not 128x64. Maybe from here the 2 lines on the right side, the difference between 132 and 128.

How can you adapt your library to work with SH1106 or other way said with 132x64?

I have tested a second one from Adafruit Monochrome 1.3" 128x64 OLED graphic display:
http://www.adafruit.com/product/938
https://drive.google.com/folderview?id=0BwXmKaSw75eKfnI5M3RvSjBoZ0Zfa2pYV1VpeWlNbUNyTmNqclBYZTJLUVN6RzktMndjUmc&usp=drive_web
Wiring:
https://learn.adafruit.com/monochrome-oled-breakouts/wiring-1-dot-3-128x64
And has no such problem:
https://drive.google.com/file/d/0BwXmKaSw75eKYWVKZ0lqNGIzeVk/view?pli=1
https://drive.google.com/file/d/0BwXmKaSw75eKUkRIRjQ5UmFXMFE/view?pli=1
I would like to test your library with SaintSmart 1.3“ OLED in I2C.
I am interested to see of the SH1106 with 132x64 has the same problem, not being in fact 128x64.
SaintSmart is  a lot cheaper than Adafruit.
« Última modificación: 23 de Octubre de 2015, 08:48:11 por viki2000 »

Desconectado rotting79

  • PIC12
  • **
  • Mensajes: 91
Re:OLED 128 x 64
« Respuesta #10 en: 30 de Octubre de 2015, 23:43:04 »
hey there viki2000

first of all, I'm a cheap guy, so everything I bought is from  ebay and banggood, that's why I got a bunch of these OLEDs for a really good price, even for a free shipping (just need wait about three weeks), everything comes from Asia, even the AdaFruit stuff, u just pay them in USD dollar but EVERYTHING comes from Asia.

after that I would say the SH1106 data sheet seems almost the same as the SSD1306, if u really know how the I2C works, just need to adapt the commands, also the next lines which are in my OLED.h file are:

#define   ARRAY_WIDHT      128
#define   ARRAY_HEIGHT   8
#define   ARRAY_LENGHT   ( 8 * ARRAY_HEIGHT )
#define SSD1306_GDDRAM   ( ARRAY_WIDHT * ARRAY_HEIGHT / 8 )
unsigned char TEST [ ARRAY_HEIGHT ] [ ARRAY_WIDHT ];   

u need to modified them in order to get acces to the whole screen and avoid the white lines on the right.

here is where I recommend u to look at:
http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=I2C+Serial+128+x+64+OLED&_sop=15


Hola viki2000

primero que nada, soy un tipo barato, así que todo lo que compré es de ebay y banggood, es por eso que tengo un montón de estos OLEDs para un precio realmente bueno, incluso para un envío libre (sólo necesita esperar unas tres semanas), todo viene de Asia, incluso las cosas Adafruit, u sólo ellos pagan en USD dólar, pero todo viene de Asia.

después de eso yo diría que la ficha técnica SH1106 parece casi el mismo que el SSD1306, si u realmente saben cómo funciona el I2C, sólo hay que adaptar los comandos, también las siguientes líneas que están en mi archivo OLED.h son:

#define ARRAY_WIDHT 128
#define ARRAY_HEIGHT 8
#define ARRAY_LENGHT (8 * ARRAY_HEIGHT)
#define SSD1306_GDDRAM (ARRAY_WIDHT * ARRAY_HEIGHT / 8)
PRUEBA unsigned char [ARRAY_HEIGHT] [ARRAY_WIDHT];

u necesidad de ellos modificado con el fin de obtener acceso a toda la pantalla y evitar las líneas blancas de la derecha.

aquí es donde te recomiendo u para mirar:

http://www.ebay.com/sch/i.html?_from=R40&_sacat=0&_nkw=I2C+Serial+128+x+64+OLED&_sop=15
...On November 2nd, a PIC10 became self-aware and decided our fate in __delay_us ( 1 ); ...