Villalba2006

POST_33

Nov 25th, 2016
2,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.68 KB | None | 0 0
  1. /*  TITULO: Como utilizar la pantalla LCD de un NOKIA 5110.
  2.  
  3.     AUTOR:
  4.    
  5.     MARIANO DEL CAMPO GARCÍA (@2016) --> INGENIERO TÉCNICO INDUSTRIAL ESPECIALIDAD ELECTRÓNICA
  6.     - FACEBOOK: https://www.facebook.com/mariano.delcampogarcia
  7.     - TWITTER: https://twitter.com/MarianoCampoGa
  8.     - CORREO: [email protected]
  9.    
  10.    
  11.     DESCRIPCIÓN DEL PROGRAMA
  12.    
  13.     Con este programa vamos a ser capaces de visualizar una serie de formas y caracteres, a través
  14.     del LCD de NOKIA 5110, previamente programados mediante las librerías de "Adafruit" (fabricante
  15.     de componentes electrónicos para Arduino).
  16.    
  17.    
  18.     ESQUEMA DE CONEXION
  19.    
  20.                                       +-----+
  21.          +----[PWR]-------------------| USB |--+
  22.          |                            +-----+  |
  23.          |         GND/RST2  [ ][ ]            |
  24.          |       MOSI2/SCK2  [ ][ ]  A5/SCL[ ] |  
  25.          |          5V/MISO2 [ ][ ]  A4/SDA[ ] |  
  26.          |                             AREF[ ] |
  27.          |                              GND[ ] |
  28.          | [ ]N/C                    SCK/13[ ] |  
  29.          | [ ]IOREF                 MISO/12[ ] |   RST del LCD NOKIA 5110 a través de R=10K ohms
  30.          | [ ]RST                   MOSI/11[ ]~|   CE del LCD NOKIA 5110 a través de R=10K ohms
  31.          | [ ]3V3    +---+               10[ ]~|   DC del LCD NOKIA 5110 a través de R=10K ohms
  32.          | [ ]5v    -| A |-               9[ ]~|   Din del LCD NOKIA 5110 a través de R=10K ohms
  33.          | [ ]GND   -| R |-               8[ ] |   Clk del LCD NOKIA 5110 a través de R=10K ohms
  34.          | [ ]GND   -| D |-                    |
  35.          | [ ]Vin   -| U |-               7[ ] |  
  36.          |          -| I |-               6[ ]~|  
  37.          | [ ]A0    -| N |-               5[ ]~|  
  38.          | [ ]A1    -| O |-               4[ ] |    
  39.          | [ ]A2     +---+           INT1/3[ ]~|    
  40.          | [ ]A3                     INT0/2[ ] |  
  41.          | [ ]A4/SDA  RST SCK MISO     TX>1[ ] |  
  42.          | [ ]A5/SCL  [ ] [ ] [ ]      RX<0[ ] |  
  43.          |            [ ] [ ] [ ]              |
  44.          |  UNO_R3    GND MOSI 5V  ____________/
  45.           \_______________________/
  46.  
  47.  
  48.   NOTAS:
  49.  
  50.    - Conexión de la alimentación del LCD NOKIA 5110:
  51.        - VCC --> +3.3V
  52.        - GND --> GND
  53.        - BL (BackLight) --> +3.3V.
  54. */
  55.  
  56.   // Incluimos librerías
  57.   #include <SPI.h>
  58.   #include <Adafruit_GFX.h>
  59.   #include <Adafruit_PCD8544.h>
  60.  
  61.   // Hay que poner una resistencia de 10K ohms en los siguientes pines para limitar el voltaje a 3V
  62.   Adafruit_PCD8544 display = Adafruit_PCD8544(8, 9, 10, 11, 12);
  63.  
  64.   // Variables del programa
  65.   #define NUMFLAKES 10
  66.   #define XPOS 0
  67.   #define YPOS 1
  68.   #define DELTAY 2
  69.  
  70.   #define LOGO16_GLCD_HEIGHT 16
  71.   #define LOGO16_GLCD_WIDTH  16
  72.  
  73.   // Mapa de bits del logo de Adafruit
  74.   static const unsigned char PROGMEM logo16_glcd_bmp[] =
  75.   {
  76.     B00000000, B11000000,
  77.     B00000001, B11000000,
  78.     B00000001, B11000000,
  79.     B00000011, B11100000,
  80.     B11110011, B11100000,
  81.     B11111110, B11111000,
  82.     B01111110, B11111111,
  83.     B00110011, B10011111,
  84.     B00011111, B11111100,
  85.     B00001101, B01110000,
  86.     B00011011, B10100000,
  87.     B00111111, B11100000,
  88.     B00111111, B11110000,
  89.     B01111100, B11110000,
  90.     B01110000, B01110000,
  91.     B00000000, B00110000
  92.   };
  93.  
  94.   void setup()  
  95.   {
  96.     Serial.begin(9600); // Comienzo de la comunicación serie
  97.     display.begin(); // Inicializamos el LCD NOKIA 5110
  98.     display.setContrast(50); // Contraste del LCD NOKIA 5110
  99.     display.display(); // Muestra el logo por pantalla
  100.     delay(2000);
  101.     display.clearDisplay(); // Se borra la pantalla
  102.  
  103.     // Dibuja un pixel
  104.     display.drawPixel(10, 10, BLACK);
  105.     display.display();
  106.     delay(2000);
  107.     display.clearDisplay();
  108.  
  109.     // Dibuja líneas
  110.     testdrawline();
  111.     display.display();
  112.     delay(2000);
  113.     display.clearDisplay();
  114.  
  115.     // Dibuja rectángulos
  116.     testdrawrect();
  117.     display.display();
  118.     delay(2000);
  119.     display.clearDisplay();
  120.  
  121.     // Dibuja rectángulos rellenos
  122.     testfillrect();
  123.     display.display();
  124.     delay(2000);
  125.     display.clearDisplay();
  126.  
  127.     // Dibuja círculos
  128.     testdrawcircle();
  129.     display.display();
  130.     delay(2000);
  131.     display.clearDisplay();
  132.  
  133.     // Dibuja un círculo relleno de 10 px de radio
  134.     display.fillCircle(display.width()/2, display.height()/2, 10, BLACK);
  135.     display.display();
  136.     delay(2000);
  137.     display.clearDisplay();
  138.    
  139.     // Dibuja rectángulos redondeados
  140.     testdrawroundrect();
  141.     delay(2000);
  142.     display.clearDisplay();
  143.  
  144.     // Dibuja rectángulos redondeados y rellenos
  145.     testfillroundrect();
  146.     delay(2000);
  147.     display.clearDisplay();
  148.  
  149.     // Dibuja triángulos
  150.     testdrawtriangle();
  151.     delay(2000);
  152.     display.clearDisplay();
  153.    
  154.     // Dibuja triángulos rellenos
  155.     testfilltriangle();
  156.     delay(2000);
  157.     display.clearDisplay();
  158.  
  159.     // Dibuja los primeros 12 caracteres de la fuente
  160.     testdrawchar();
  161.     display.display();
  162.     delay(2000);
  163.     display.clearDisplay();
  164.  
  165.     // Muestra texto
  166.     display.setTextSize(1);
  167.     display.setTextColor(BLACK);
  168.     display.setCursor(0,0);
  169.     display.println("Hola, mundo!");
  170.     display.setTextColor(WHITE, BLACK);
  171.     display.println(3.141592);
  172.     display.setTextSize(2);
  173.     display.setTextColor(BLACK);
  174.     display.print("0x"); display.println(0xDEADBEEF, HEX);
  175.     display.display();
  176.     delay(2000);
  177.  
  178.     // Rotación de la pantalla 90º
  179.     display.clearDisplay();
  180.     display.setRotation(1);
  181.     display.setTextSize(1);
  182.     display.setTextColor(BLACK);
  183.     display.setCursor(0,0);
  184.     display.println("Rotacion");
  185.     display.setTextSize(2);
  186.     display.println("Ejemplo!");
  187.     display.display();
  188.     delay(2000);
  189.  
  190.     // Pantalla sín rotación
  191.     display.setRotation(0);
  192.  
  193.     // Mapa de bits en miniatura
  194.     display.clearDisplay();
  195.     display.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16, 1);
  196.     display.display();
  197.  
  198.     // Invierte la pantalla
  199.     display.invertDisplay(true);
  200.     delay(1000);
  201.     display.invertDisplay(false);
  202.     delay(1000);
  203.  
  204.     // Dibuja un mapa de bits y anima su movimiento
  205.     testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_WIDTH, LOGO16_GLCD_HEIGHT);
  206.   }
  207.  
  208.  
  209.   void loop()
  210.   {
  211.    
  212.   }
  213.  
  214.   // Función para dibujar mapas de bits
  215.   void testdrawbitmap(const uint8_t *bitmap, uint8_t w, uint8_t h)
  216.   {
  217.     uint8_t icons[NUMFLAKES][3];
  218.     randomSeed(666);    
  219.     for (uint8_t f=0; f< NUMFLAKES; f++)
  220.     {
  221.       icons[f][XPOS] = random(display.width());
  222.       icons[f][YPOS] = 0;
  223.       icons[f][DELTAY] = random(5) + 1;
  224.      
  225.       Serial.print("x: ");
  226.       Serial.print(icons[f][XPOS], DEC);
  227.       Serial.print(" y: ");
  228.       Serial.print(icons[f][YPOS], DEC);
  229.       Serial.print(" dy: ");
  230.       Serial.println(icons[f][DELTAY], DEC);
  231.     }
  232.  
  233.     while (1)
  234.     {
  235.       for (uint8_t f=0; f< NUMFLAKES; f++)
  236.       {
  237.         display.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, BLACK);
  238.       }
  239.       display.display();
  240.       delay(200);
  241.       for (uint8_t f=0; f< NUMFLAKES; f++)
  242.       {
  243.         display.drawBitmap(icons[f][XPOS], icons[f][YPOS],  logo16_glcd_bmp, w, h, WHITE);
  244.         icons[f][YPOS] += icons[f][DELTAY];
  245.         if (icons[f][YPOS] > display.height())
  246.         {
  247.     icons[f][XPOS] = random(display.width());
  248.     icons[f][YPOS] = 0;
  249.     icons[f][DELTAY] = random(5) + 1;
  250.         }
  251.       }
  252.      }
  253.   }
  254.  
  255.   // Función para dibujar caracteres
  256.   void testdrawchar(void)
  257.   {
  258.     display.setTextSize(1);
  259.     display.setTextColor(BLACK);
  260.     display.setCursor(0,0);
  261.  
  262.     for (uint8_t i=0; i < 168; i++)
  263.     {
  264.       if (i == '\n') continue;
  265.       display.write(i);
  266.  
  267.     }    
  268.     display.display();
  269.   }
  270.  
  271.   // Función para dibujar círculos
  272.   void testdrawcircle(void)
  273.   {
  274.     for (int16_t i=0; i<display.height(); i+=2)
  275.     {
  276.       display.drawCircle(display.width()/2, display.height()/2, i, BLACK);
  277.       display.display();
  278.     }
  279.   }
  280.  
  281.   // Función para dibujar rectángulos rellenos
  282.   void testfillrect(void)
  283.   {
  284.     uint8_t color = 1;
  285.     for (int16_t i=0; i<display.height()/2; i+=3)
  286.     {
  287.       display.fillRect(i, i, display.width()-i*2, display.height()-i*2, color%2);
  288.       display.display();
  289.       color++;
  290.     }
  291.   }
  292.  
  293.   // Función para dibujar triángulos
  294.   void testdrawtriangle(void)
  295.   {
  296.     for (int16_t i=0; i<min(display.width(),display.height())/2; i+=5)
  297.     {
  298.       display.drawTriangle(display.width()/2, display.height()/2-i,
  299.                        display.width()/2-i, display.height()/2+i,
  300.                        display.width()/2+i, display.height()/2+i, BLACK);
  301.       display.display();
  302.     }
  303.   }
  304.  
  305.   // Función para dibujar triángulos rellenos
  306.   void testfilltriangle(void)
  307.   {
  308.     uint8_t color = BLACK;
  309.     for (int16_t i=min(display.width(),display.height())/2; i>0; i-=5)
  310.     {
  311.       display.fillTriangle(display.width()/2, display.height()/2-i,
  312.                        display.width()/2-i, display.height()/2+i,
  313.                        display.width()/2+i, display.height()/2+i, color);
  314.       if (color == WHITE) color = BLACK;
  315.       else color = WHITE;
  316.       display.display();
  317.     }
  318.   }
  319.  
  320.   // Función para dibujar rectángulos redondeados
  321.   void testdrawroundrect(void)
  322.   {
  323.     for (int16_t i=0; i<display.height()/2-2; i+=2)
  324.     {
  325.       display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, BLACK);
  326.       display.display();
  327.     }
  328.   }
  329.  
  330.   // Función para dibujar rectángulos redondeados y rellenos
  331.   void testfillroundrect(void)
  332.   {
  333.     uint8_t color = BLACK;
  334.     for (int16_t i=0; i<display.height()/2-2; i+=2)
  335.     {
  336.       display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, display.height()/4, color);
  337.       if (color == WHITE) color = BLACK;
  338.       else color = WHITE;
  339.       display.display();
  340.     }
  341.   }
  342.    
  343.   // Función para dibujar rectángulos  
  344.   void testdrawrect(void)
  345.   {
  346.     for (int16_t i=0; i<display.height()/2; i+=2)
  347.     {
  348.       display.drawRect(i, i, display.width()-2*i, display.height()-2*i, BLACK);
  349.       display.display();
  350.     }
  351.   }
  352.  
  353.   // Función para dibujar líneas
  354.   void testdrawline()
  355.   {  
  356.     for (int16_t i=0; i<display.width(); i+=4)
  357.     {
  358.       display.drawLine(0, 0, i, display.height()-1, BLACK);
  359.       display.display();
  360.     }
  361.     for (int16_t i=0; i<display.height(); i+=4)
  362.     {
  363.       display.drawLine(0, 0, display.width()-1, i, BLACK);
  364.       display.display();
  365.     }
  366.     delay(250);
  367.    
  368.     display.clearDisplay();
  369.     for (int16_t i=0; i<display.width(); i+=4)
  370.     {
  371.       display.drawLine(0, display.height()-1, i, 0, BLACK);
  372.       display.display();
  373.     }
  374.     for (int8_t i=display.height()-1; i>=0; i-=4)
  375.     {
  376.       display.drawLine(0, display.height()-1, display.width()-1, i, BLACK);
  377.       display.display();
  378.     }
  379.     delay(250);
  380.    
  381.     display.clearDisplay();
  382.     for (int16_t i=display.width()-1; i>=0; i-=4)
  383.     {
  384.       display.drawLine(display.width()-1, display.height()-1, i, 0, BLACK);
  385.       display.display();
  386.     }
  387.     for (int16_t i=display.height()-1; i>=0; i-=4)
  388.     {
  389.       display.drawLine(display.width()-1, display.height()-1, 0, i, BLACK);
  390.       display.display();
  391.     }
  392.     delay(250);
  393.  
  394.     display.clearDisplay();
  395.     for (int16_t i=0; i<display.height(); i+=4)
  396.     {
  397.       display.drawLine(display.width()-1, 0, 0, i, BLACK);
  398.       display.display();
  399.     }
  400.     for (int16_t i=0; i<display.width(); i+=4)
  401.     {
  402.       display.drawLine(display.width()-1, 0, i, display.height()-1, BLACK);
  403.       display.display();
  404.     }
  405.     delay(250);
  406.   }
Advertisement
Add Comment
Please, Sign In to add comment