cambiare pin tx rx su una libreria di sparkfun

salve a tutti!

Ho acquistato su un lcd 160128 con relativo circuito Serial Graphic LCD Backpack della Sparkfun, ecco il link:

https://learn.sparkfun.com/tutorials/serial-graphic-lcd-hookup

Ho installato anche le librerie consigliate per semplificare il tutto:

#include <SerialGraphicLCD.h>

#include <SoftwareSerial.h> (il pin TX di default è il 3)

…e tutto funziona perfettamente se collegate il pin 3 di Arduino con il pin RX del circuito Serial Graphic
LCD Backpack e dissaldate il jumper dietro al circuito.

Sul sito Sparkfun come vedete spiegano tutto perfettamente, però la libreria <SoftwareSerial.h>
serve se avete un Arduino Uno. Nel mio caso utilizzo l’Arduino Mega.

Quindi, semplicemente faccio a meno di installare <SoftwareSerial.h>.

La mia domanda è:

Come faccio a dire alla libreria <SerialGraphicLCD.h> di utilizzare gli altri pin RX,TX di Arduino Mega?

Per esempio utilizzare i pin 18 e 19?

O meglio: utilizzare solo il TX 19 di Arduino Mega, visto che tanto connettere RX non è necessario in
quanto il circuito per il 160128 riceve solo…

grazie anticipatamente a chi mi vuol aiutare. :slight_smile:

Come sai, le varie seriali sulla MEGA hanno un nome che va da Serial1 a Serial3 (quella di base è sempre Serial).

Dato che i metodi della SoftwareSerial hanno lo stesso nome di quelli standard, ti basta [u]eliminare[/u] l'#include della SoftwareSerial, la sua istanza (es. SoftwareSerial mySerial(RX, TX)) e ...

... o correggere a mano tutte le chiamate mettendo al posto del nome della SoftwareSerial (es. mySerial o quello che è) il riferimento alla vera seriale che vuoi usare (Serial1, Serial2, Serial3)

... o aggiungere, se, ad esempio, l'istanza delle SoftwareSerial si chiamava mySerial, in testa una #define mySerial Serial1 (o Serial2 o Serial3 a secondo di quella che vuoi usare).

Guglielmo

Ciao Guglielmo, grazie per la tempestiva risposta…

… o aggiungere, se, ad esempio, l’istanza delle SoftwareSerial si chiamava mySerial, in testa una #define mySerial Serial1 (o Serial2 o Serial3 a secondo di quella che vuoi usare).

quindi ho inserito il frammento di codice:

#include <SparkFunSerialGraphicLCD.h>//inculde the Serial Graphic LCD library
#include <SoftwareSerial.h>
#define mySerial Serial1//questo qua

non capisco cosa ho sbagliato, collego il pin TX 19 del Mega e non funziona.

alternativa:

… o correggere a mano tutte le chiamate mettendo al posto del nome della SoftwareSerial (es. mySerial o quello che è) il riferimento alla vera seriale che vuoi usare (Serial1, Serial2, Serial3)

se non ho capito male questa soluzione mi farebbe risparmiare memoria perché’ non necessita della libreria <SoftwareSerial.h>

qua intendi aprire la libreria stessa: <SparkFunSerialGraphicLCD.h> , per sostituire il riferimento a (Serial1, Serial2, Serial3). Ma come si apre? :fearful:

No, No, non devi modificare solo il tuo codice difatti, DEVI modificare la libreria ... ... devi andare nella tua cartella dove sono le librerie che scarichie ed installi, c*ercare il .h ed il .cpp e modificarli*.

Ah ... la #include della SoftwareSerial la devi eliminare.

Ovvio che ... non è cosa esattamente da "iniziandi" ... ::)

Guglielmo

... ho guardato SparkFunSerialGraphicLCD.cpp (... che è l'unico da modificare) ... è piuttosto semplice ...

Righe 8 .. 13:

#include "Arduino.h"
#include "SparkFunSerialGraphicLCD.h"
#include "SoftwareSerial.h"

//initialize an instance of the SoftwareSerial library 
SoftwareSerial serial(2,3);//change these two pin values to whichever pins you wish to use (RX, TX)

... elimina le righe relative alla SoftwareSerial (riga 10, 12 e 13) e aggungi :

#define serial Serial1

(... oppure Serial2 oppure Serial3 ... a seconda di quella che vuoi usare).

Nel tuo programma elimina anche li ogni riferimento a SoftwareSerial.

Guglielmo

P.S.. Ricorda sempre che : Serial: 0 (RX) and 1 (TX); Serial 1: 19 (RX) and 18 (TX); Serial 2: 17 (RX) and 16 (TX); Serial 3: 15 (RX) and 14 (TX).

Grazie mille,mi è tutto perfettamente chiaro, però è come se non riuscissi a rendere effettive le modifiche al file cpp. Lo apro con blocco note o con word…
E’ per caso questo il problema?

Il file specialmente con blocco note si visualizza molto male e magari involontariamente inserisco degli errori…

mentre il mio programma (per il momento), è semplicemente il demo che si trova all’interno della libreria.

Qui l’inizio del programma:

#include <SparkFunSerialGraphicLCD.h>//inculde the Serial Graphic LCD library
#include <SoftwareSerial.h>
//Despite this being inculdeing in the library file, 
//it needs to be added here as well to get the sketch to compile


//This demo code was created for both the 128x64 and the 160x128 pixel LCD. 
//Thus, these maximum limits are set to allow this code to work on both sizes of LCD.
//The default size is the 128x64, but you can delete those values and uncomment
//the other values if you have a 160x128 pixel LCD. If you are writing code for just 
//one size LCD, you can omit this information entirely and just use numerical values
//in place of these variable names. 

#define maxX 127//159 
#define maxY 63 //127
//Each maximum value is one less than the stated value to account for position 0,0
//Thus, position 127 is actually the 128th pixel. 

//Create an instance of the LCD class named LCD. We will use this instance to call all the 
//subsequent LCD functions
LCD LCD;

void setup()
{
//we're just going to run through a bunch of demos to show the functionality of the LCD.

La modifica, da te suggerita ,risulta essere il commento della seconda riga con davanti le sbarrette //:

//#include <SoftwareSerial.h>

in piu’ nel mio caso, ovviamente, con il 160128 lcd:

#define maxX 159
#define maxY 127

mentre la modifica eseguita con blocco note al file cpp risulta essere:

attenzione,da notare: /* subito prima di #include “SoftwareSerial.h” con relativa chiusura */ e poi #define serial Serial1

ecco qui il codice copia incolla da blocco note (che tra l’altro qui si visualizza gia’ meglio):

/**********************************************
Graphic Serial LCD Libary Main File
Joel Bartlett
SparkFun Electronics 
9-25-13

**********************************************/
#include "Arduino.h"
#include "SparkFunSerialGraphicLCD.h"/*
#include "SoftwareSerial.h"

//initialize an instance of the SoftwareSerial library 
SoftwareSerial serial(2,3);//change these two pin values to whichever pins you wish to use (RX, TX)*/

#define serial Serial1 



LCD::LCD()
{
serial.begin(115200);

}
//-------------------------------------------------------------------------------------------
void LCD::printStr(char Str[78])//26 characters is the length of one line on the LCD
{
serial.print(Str);
//if you need to print longer strings, change the size of this array here and in the .h file
}
//-------------------------------------------------------------------------------------------
void LCD::printNum(int num)//can't convert ints to strings so this is just for printing ints
{
serial.print(num);
}
//-------------------------------------------------------------------------------------------
void LCD::nextLine()//prints new line
{
serial.println();
}
//-------------------------------------------------------------------------------------------
void LCD::clearScreen()
{
 //clears the screen, you will use this a lot!
 serial.write(0x7C);
 serial.write((byte)0);

In ogni caso il demo viene compilato e caricato e collego TX 19 del Mega… Pero’ sul monitor non si visualizza piu’ la dimostrazione del programma demo.

E’ tutto il giorno che vado dietro a sta cosa e non trovo l’errore… :confused:

  1. Ma lo leggi quello che scrivo ? ? ? :o :o :o Rileggi ATTENTAMENTE il mio post #4 e fai quello che c’è scritto …

  2. Strano, con NotePad si dovrebbe vedere bene … non hai un altro editor di TXT (non Word o altre cose che inseriscono caratteri di controllo) ?

  3. infine … ti ricordo che in conformità al regolamento, punto 7, devi editare il tuo post (in basso a destra del post, bottone More → Modify) e racchiudere il codice all’interno dei tag CODE (… sono quelli che in edit inserisce il bottone fatto così: </>, tutto a sinistra).

Guglielmo

Ciao Guglielmo perdonami,

Ho modificato il post, spero vada bene.

In piu' ho trovato un editor di testo EditPad Lite 7. Adesso si vede bene...

Ritornando al problema...

Ho eseguito alla lettera quello che mi hai scritto al post #4.

Allego libreria cpp modificata: (per intero)

/**********************************************
Graphic Serial LCD Libary Main File
Joel Bartlett
SparkFun Electronics 
9-25-13

**********************************************/
#include "Arduino.h"
#include "SparkFunSerialGraphicLCD.h"


#define serial Serial1 


LCD::LCD()
{
 serial.begin(115200);

}
//-------------------------------------------------------------------------------------------
void LCD::printStr(char Str[78])//26 characters is the length of one line on the LCD
{
 serial.print(Str);
 //if you need to print longer strings, change the size of this array here and in the .h file
}
//-------------------------------------------------------------------------------------------
void LCD::printNum(int num)//can't convert ints to strings so this is just for printing ints
{
 serial.print(num);
}
//-------------------------------------------------------------------------------------------
void LCD::nextLine()//prints new line
{
 serial.println();
}
//-------------------------------------------------------------------------------------------
void LCD::clearScreen()
{
  //clears the screen, you will use this a lot!
  serial.write(0x7C);
  serial.write((byte)0); //CTRL @
  //can't send LCD.write(0) or LCD.write(0x00) because it's interprestted as a NULL
}
//-------------------------------------------------------------------------------------------
void LCD::toggleReverseMode()
{
  //Everything that was black is now white and vise versa
  serial.write(0x7C);
  serial.write(0x12); //CTRL r
}
//-------------------------------------------------------------------------------------------
void LCD::toggleSplash()
{
  //turns the splash screen on and off, the 1 second delay at startup stays either way.
  serial.write(0x7C);
  serial.write(0x13); //CTRL s
}
//-------------------------------------------------------------------------------------------
void LCD::setBacklight(byte duty)
{
  //changes the back light intensity, range is 0-100.
  serial.write(0x7C);
  serial.write(0x02); //CTRL b
  serial.write(duty); //send a value of 0 - 100
}
//-------------------------------------------------------------------------------------------
void LCD::setBaud(byte baud)
{
  //changes the baud rate.
  serial.write(0x7C);
  serial.write(0x07); //CTRL g
  serial.write(baud); //send a value of 49 - 54
  delay(100);

/*
“1” = 4800bps - 0x31 = 49
“2” = 9600bps - 0x32 = 50
“3” = 19,200bps - 0x33 = 51
“4” = 38,400bps - 0x34 = 52
“5” = 57,600bps - 0x35 = 53
“6” = 115,200bps - 0x36 = 54
*/

  //these statements change the SoftwareSerial baud rate to match the baud rate of the LCD. 
  if(baud == 49)
  {
 serial.end();
 serial.begin(4800);
  }
  if(baud == 50)
  {
 serial.end();
 serial.begin(9600);
  }
  if(baud == 51)
  {
 serial.end();
 serial.begin(19200);
  }
  if(baud == 52)
  {
 serial.end();
 serial.begin(38400);
  }
  if(baud == 53)
  {
 serial.end();
 serial.begin(57600);
  }
  if(baud == 54)
  {
 serial.end();
 serial.begin(115200);
  }
}
//-------------------------------------------------------------------------------------------
void LCD::restoreDefaultBaud()
{
//This function is used to restore the default baud rate in case you change it 
//and forget to which rate it was changed. 


serial.end();//end the transmission at whatever the current baud rate is

//cycle through every other possible buad rate and attemp to change the rate back to 115200
serial.begin(4800);
serial.write(0x7C);
serial.write(0x07);
serial.write(54);//set back to 115200
serial.end();

serial.begin(9600);
serial.write(0x7C);
serial.write(0x07);
serial.write(54);//set back to 115200
serial.end();

serial.begin(19200);
serial.write(0x7C);
serial.write(0x07);
serial.write(54);//set back to 115200
serial.end();

serial.begin(38400);
serial.write(0x7C);
serial.write(0x07);
serial.write(54);//set back to 115200
serial.end();

serial.begin(57600);
serial.write(0x7C);
serial.write(0x07);
serial.write(54);//set back to 115200
serial.end();

serial.begin(115200);
delay(10);
serial.write(0x7C);
serial.write((byte)0); //clearScreen
serial.print("Baud restored to 115200!");
delay(5000);

}
//-------------------------------------------------------------------------------------------
void LCD::demo()
{
  //Demonstartes all the capabilities of the LCD
  serial.write(0x7C);
  serial.write(0x04);//CTRL d
}
//-------------------------------------------------------------------------------------------
void LCD::setX(byte posX) //0-127 or 0-159 pixels
{
  //Set the X position 
  serial.write(0x7C);
  serial.write(0x18);//CTRL x
  serial.write(posX);

//characters are 8 pixels tall x 6 pixels wide
//The top left corner of a char is where the x/y value will start its print
//For example, if you print a char at position 1,1, the bottom right of your char will be at position 7,9.
//Therefore, to print a character in the very bottom right corner, you would need to print at the coordinates 
//x = 154 , y = 120. You should never exceed these values.


// Here we have an example using an upper case 'B'. The star is where the character starts, given a set 
//of x,y coordinates. # represents the blocks that make up the character, and _ represnets the remaining 
//unused bits in the char space. 
//    *###__
//    #   #_
//    #   #_
//    ####__
//    #   #_
//    #   #_
//    ####__
//    ______
}
//-------------------------------------------------------------------------------------------
void LCD::setY(byte posY)//0-63 or 0-127 pixels
{
  //Set the y position 
  serial.write(0x7C);
  serial.write(0x19);//CTRL y
  serial.write(posY);
  
}
//-------------------------------------------------------------------------------------------
void LCD::setHome()
{
  serial.write(0x7C);
  serial.write(0x18); 
  serial.write((byte)0);//set x back to 0
  
  serial.write(0x7C);
  serial.write(0x19); 
  serial.write((byte)0);//set y back to 0
}
//-------------------------------------------------------------------------------------------
void LCD::setPixel(byte x, byte y, byte set)
{
  serial.write(0x7C);
  serial.write(0x10);//CTRL p
  serial.write(x);
  serial.write(y);
  serial.write(0x01);
  delay(10);
}
//-------------------------------------------------------------------------------------------
void LCD::drawLine(byte x1, byte y1, byte x2, byte y2, byte set)
{
  //draws a line from two given points. You can set and reset just as the pixel function. 
  serial.write(0x7C);
  serial.write(0x0C);//CTRL l 
  serial.write(x1);
  serial.write(y1);
  serial.write(x2);
  serial.write(y2);
  serial.write(0x01);
  delay(10);

}
//-------------------------------------------------------------------------------------------
void LCD::drawBox(byte x1, byte y1, byte x2, byte y2, byte set)
{
  //draws a box from two given points. You can set and reset just as the pixel function. 
  serial.write(0x7C);
  serial.write(0x0F);//CTRL o 
  serial.write(x1);
  serial.write(y1);
  serial.write(x2);
  serial.write(y2);
  serial.write(0x01);
  delay(10);

}
//-------------------------------------------------------------------------------------------
void LCD::drawCircle(byte x, byte y, byte rad, byte set)
{
//draws a circle from a point x,y with a radius of rad. 
//Circles can be drawn off-grid, but only those pixels that fall within the 
//display boundaries will be written.
  serial.write(0x7C);
  serial.write(0x03);//CTRL c 
  serial.write(x);
  serial.write(y);
  serial.write(rad);
  serial.write(0x01);
  delay(10);

}
//-------------------------------------------------------------------------------------------
void LCD::eraseBlock(byte x1, byte y1, byte x2, byte y2)
{
  //This is just like the draw box command, except the contents of the box are erased to the background color
  serial.write(0x7C);
  serial.write(0x05);//CTRL e 
  serial.write(x1);
  serial.write(y1);
  serial.write(x2);
  serial.write(y2);
  delay(10);

}

Ok, bene, chiaramente anche nel tuo programma NON dovrai più includere la SoftwareSerial ... ... prova e vedi se va ::)

Guglielmo

P.S.: Attenzione a non confondere i pin TX ed RX ... mi raccomando o .. non funziona nulla :D :D :D

E’ quello che ho fatto:

ti inserisco un po’ del programma demo (modificato, manca softwareSerial.h):

/*********************************************************
Serial Graphic LCD Library Demo
Joel Bartlett
SparkFun Electronics
October 14, 2013

License: 
This code is beerware: feel free to use it, with or without attribution, 
in your own projects. If you find it helpful, buy me a beer next time you 
see me at the local pub.

This is an example sketch to accompany the Serial Graphic LCD Library. 
This is intended for use with SparkFun's Serial Graphic LCDs: the 128x64 pixel,
the 160x128 pixel, and the Graphic LCD Serial Backpack. There is no guarantee 
that this code will work on any other LCDs or backpacks. 

This code was written with Arduino 1.0.5 (available at arduino.cc) It should 
work with any Arduino IDE version from 1.0 and on. If you are using 
Arduino 0023 or an older version, this will not compile.

Hardware:
You'll need one of the LCDs mentioned above as well as an Arduino compatible 
board running at 5V, and some jumper wires to connect the two. 

Connections are as follows:
__________________
Arduino | Backpack|
________|_________|
   5V   |  Vin    |
  GND   |  GND    |
   TX   |  RX     |
__________________

***********************************************************/

#include <SparkFunSerialGraphicLCD.h>//inculde the Serial Graphic LCD library

//Despite this being inculdeing in the library file, 
//it needs to be added here as well to get the sketch to compile


//This demo code was created for both the 128x64 and the 160x128 pixel LCD. 
//Thus, these maximum limits are set to allow this code to work on both sizes of LCD.
//The default size is the 128x64, but you can delete those values and uncomment
//the other values if you have a 160x128 pixel LCD. If you are writing code for just 
//one size LCD, you can omit this information entirely and just use numerical values
//in place of these variable names. 

#define maxX 159 
#define maxY 127
//Each maximum value is one less than the stated value to account for position 0,0
//Thus, position 127 is actually the 128th pixel. 

//Create an instance of the LCD class named LCD. We will use this instance to call all the 
//subsequent LCD functions
LCD LCD;

void setup()
{
//we're just going to run through a bunch of demos to show the functionality of the LCD.

delay(1200);///wait for the one second spalsh screen before anything is sent to the LCD.

LCD.setHome();//set the cursor back to 0,0.
LCD.clearScreen();//clear anything that may have been previously printed ot the screen.
delay(10);
  
LCD.printStr("Commence Arduino Demo Mode");
delay(1500);

//Each of these functions is explained in great detail below. 
helloWorld();

reverseModeDemo();

backlightDemo();

baudDemo();

counter();

tempAndHumidity();

XYDemo();

pixelDemo();

lineDemo();

boxDemo();

circleDemo();

eraseDemo();

demoDemo();

//This function should only be used if you accidentally changed the baud rate or if you forgot to what rate it was changed. 
//LCD.restoreDefaultBaud();
}
//-------------------------------------------------------------------------------------------
void loop()
{
//nothing in loop since we are just running through each demo once

}
//-------------------------------------------------------------------------------------------
void reverseModeDemo()
{
//Each LCD has a reverse mode built into it. The toggleReverseMode() function swaps the colors of the background and the pixels.
//For example, on the large display, the text is usually white on a blue background, but you could swap that to blue text on a white background. 
//This setting is stored and will remian even after power has been disconnected. 
  
LCD.setHome();
LCD.clearScreen();
LCD.printStr("Toggle reverse mode");
delay(2000);
  
LCD.clearScreen();
LCD.toggleReverseMode();
LCD.printStr("Reverse Mode On");
delay(1000);

LCD.clearScreen();
LCD.toggleReverseMode();
LCD.printStr("Reverse Mod

il pin è il 19 TX ma non funziona…

didipi: il pin è il 19 TX ma non funziona...

Sicuro ? ... controlla il reference della MEGA ...

Guglielmo

:blush:

guardate la differenza tra Arduino Mega originale e Arduino Mega tarocco…

Utilizzavo il tarocco per gli esperimenti, cosi se anche lo brucio…

Comunque, problema risolto, adesso funziona tutto.

Infinite grazie Guglielmo per la tua pazienza e disponibilita’.

Davide.

didipi: guardate la differenza tra Arduino Mega originale e Arduino Mega tarocco....

:D :D :D ... mai fidarsi delle serigrafie, ma leggere sempre i references ;)

Beh ... meno male che funziona ... :)

Guglielmo