Villalba2006

POST_47

Nov 28th, 2016
5,049
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.71 KB | None | 0 0
  1. /*  TITULO: Geolocalización con módulo GPS GY-NEO6MV2 y LCD 1602
  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 comunicar un Display LCD 16x2 con Arduino mediante comunicación I2C,
  14.     para mosrar las coordenadas de latitud y longitud, el valor de la altidud y la hora internacional
  15.     en formato 24h, que nos entrega el módulo GPS GY-NEO6MV2 que hemos conectado a nuestro Arduino a
  16.     través de comunicación serie (TX/RX).
  17.    
  18.    
  19.     ESQUEMA DE CONEXION
  20.    
  21.                                       +-----+
  22.          +----[PWR]-------------------| USB |--+
  23.          |                            +-----+  |
  24.          |         GND/RST2  [ ][ ]            |
  25.          |       MOSI2/SCK2  [ ][ ]  A5/SCL[ ] |   SCL del módulo I2C conectado al LCD 1602
  26.          |          5V/MISO2 [ ][ ]  A4/SDA[ ] |   SDA del módulo I2C conectado al LCD 1602
  27.          |                             AREF[ ] |
  28.          |                              GND[ ] |
  29.          | [ ]N/C                    SCK/13[ ] |  
  30.          | [ ]IOREF                 MISO/12[ ] |  
  31.          | [ ]RST                   MOSI/11[ ]~|  
  32.          | [ ]3V3    +---+               10[ ]~|  
  33.          | [ ]5v    -| A |-               9[ ]~|  
  34.          | [ ]GND   -| R |-               8[ ] |  
  35.          | [ ]GND   -| D |-                    |
  36.          | [ ]Vin   -| U |-               7[ ] |  
  37.          |          -| I |-               6[ ]~|  
  38.          | [ ]A0    -| N |-               5[ ]~|  
  39.          | [ ]A1    -| O |-               4[ ] |   TX del módulo GPS GY-NEO6MV2
  40.          | [ ]A2     +---+           INT1/3[ ]~|   RX del módulo GPS GY-NEO6MV2 a través de R=4,7K Ohms
  41.          | [ ]A3                     INT0/2[ ] |  
  42.          | [ ]A4/SDA  RST SCK MISO     TX>1[ ] |  
  43.          | [ ]A5/SCL  [ ] [ ] [ ]      RX<0[ ] |  
  44.          |            [ ] [ ] [ ]              |
  45.          |  UNO_R3    GND MOSI 5V  ____________/
  46.           \_______________________/
  47.  
  48.   NOTAS:
  49.    - La alimentación y la masa del módulo LCM 1602 I2C V1 van directamente conectadas a VCC (+5V) y GND
  50.      respectivamente.
  51.    - Conexiones del módulo GPS GY-NEO6MV2:  
  52.        - VCC --> +3,3V de Arduino.
  53.        - GND --> GND de Arduino.
  54.        - RX --> a GND a través de una R=10K Ohms.
  55. */
  56.  
  57.   // Incluimos las librerías
  58.   #include <SoftwareSerial.h>        
  59.   #include <LiquidCrystal_I2C.h>    
  60.   #include <Wire.h>                  
  61.   #include <TinyGPS.h>              
  62.  
  63.   // Declaración de los objetos
  64.   TinyGPS gps;
  65.   SoftwareSerial ss(4, 3); // Rx = 4 y Tx = 3
  66.  
  67.   // Terminales de conexión del LCD
  68.   //                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
  69.   LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
  70.  
  71.   // Variables internas del programa
  72.   static void smartdelay(unsigned long ms);
  73.   static void print_float(float val, float invalid, int len, int prec);
  74.   static void print_int(unsigned long val, unsigned long invalid, int len);
  75.   static void print_date(TinyGPS &gps);
  76.   static void print_str(const char *str, int len);
  77.  
  78.   float flat, flon;
  79.    
  80.   void setup()
  81.   {
  82.     lcd.begin(16,2); // Inicializamos el LCD para 16x2
  83.     lcd.backlight(); // Encendemos la retroiluminación del LCD
  84.    
  85.     inicio_LCD(); // LLamamos a la función que inicia el LCD
  86.    
  87.     ss.begin(9600); // Comienza la comunicación serie con el GPS
  88.   }
  89.  
  90.   void loop()
  91.   {
  92.     // Obtenemos las coordenadas de latitud y la longitud en grados
  93.     gps.f_get_position(&flat, &flon);
  94.    
  95.     // Presentamos la latitud
  96.     lcd.clear();                      
  97.     lcd.setCursor(0, 0);              
  98.     lcd.print("<<< LATITUD >>>");
  99.     lcd.setCursor(0, 1);
  100.     print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
  101.    
  102.     smartdelay(4000); // Retardo de 4 segundos
  103.    
  104.     // Presentamos la longitud
  105.     lcd.clear();                      
  106.     lcd.setCursor(0, 0);              
  107.     lcd.print("<<< LONGITUD >>>");
  108.     lcd.setCursor(0, 1);
  109.     print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 11, 6);
  110.    
  111.     smartdelay(4000); // Retardo de 4 segundos
  112.    
  113.     // Presentamos la hora
  114.     lcd.clear();                      
  115.     lcd.setCursor(0, 0);              
  116.     lcd.print("<<< HORA >>>");
  117.     lcd.setCursor(0, 1);
  118.     print_date(gps);
  119.    
  120.     smartdelay(4000); // Retardo de 4 segundos
  121.    
  122.     // Presentamos la altitud
  123.     lcd.clear();                      
  124.     lcd.setCursor(0, 0);              
  125.     lcd.print("<<< ALTITUD >>>");
  126.     lcd.setCursor(0, 1);
  127.     print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 7, 2);
  128.    
  129.     smartdelay(4000); // Retardo de 4 segundos
  130.   }
  131.  
  132.   // Función del retardo
  133.   static void smartdelay(unsigned long ms)
  134.   {
  135.     unsigned long start = millis();
  136.     do
  137.     {
  138.       while (ss.available())
  139.         gps.encode(ss.read());
  140.     } while (millis() - start < ms);
  141.   }
  142.  
  143.   // Función para imprimir valores con coma flotante (float)
  144.   static void print_float(float val, float invalid, int len, int prec)
  145.   {
  146.     if (val == invalid)
  147.     {
  148.       while (len-- > 1)
  149.         lcd.print('*');
  150.       lcd.print(' ');
  151.     }
  152.     else
  153.     {
  154.       lcd.print(val, prec);
  155.       int vi = abs((int)val);
  156.       int flen = prec + (val < 0.0 ? 2 : 1);
  157.       flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  158.       for (int i=flen; i<len; ++i)
  159.         lcd.print(' ');
  160.     }
  161.     smartdelay(0);
  162.   }
  163.  
  164.   // Función para imprimir la hora
  165.   static void print_date(TinyGPS &gps)
  166.   {
  167.     int year;
  168.     byte month, day, hour, minute, second, hundredths;
  169.     unsigned long age;
  170.     gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
  171.     if (age == TinyGPS::GPS_INVALID_AGE)
  172.       lcd.print("****************");
  173.     else
  174.     {
  175.       char sz[32];
  176.       sprintf(sz, "%02d:%02d:%02d ",hour, minute, second);
  177.       lcd.print(sz);
  178.     }
  179.     smartdelay(0);
  180.   }
  181.  
  182.   // Función que inicializa el LCD 1602
  183.   void inicio_LCD()                        
  184.   {                                              
  185.       lcd.clear();                      
  186.       lcd.setCursor(0, 0);              
  187.       lcd.print("Modulo GPS");          
  188.       lcd.setCursor(0, 1);              
  189.       lcd.print("& Display I2C... ");  
  190.       delay(3000);                      
  191.       lcd.clear();                      
  192.       lcd.setCursor(0, 0);              
  193.       lcd.print("Iniciando....");      
  194.       delay(4000);                      
  195.       lcd.clear();                      
  196.   }
Advertisement
Add Comment
Please, Sign In to add comment