HOME > > > > SUB-MENU: Alpha/graphics displays     StumbleUpon.Com Recommend to StumbleUpon

Arduino Code for Sparkfun LCD backpack (page- alpha-graphic-sparkfun-LCD-09352-code.htm)

This is just the code!

The following Arduino code to drive a Sparkfun graphics and text LCD backpack is discussed in detail on the page the link takes you to. (Sparkfun's page about the backpack is at LCD-09352, which is their partnumber for it.)



/* NGL-TestPcb270- NoviceGuard Lite
Version: 25 Feb 18
Started: 25 Feb 18

This code is discussed at...

http://sheepdogguides.com/elec/ser_disp/alpha-graphic-sparkfun-LCD-09352.htm

*/

#include <SoftwareSerial.h>

SoftwareSerial MySerial(9,10);// RX, TX

void setup() {
  //Prepare things...
  Serial.begin(9600);//Leave in
  MySerial.begin(9600);//115200 is the default... try with that, first.
  delay(400);//Give serial line a moment to stablize

/*Change Baud Rate
 * 
 * You MAY not need to do this! And if you do, you will then have to change the
 * MySerial.begin line above. Changes to baud rate are "remembered" across
 * power off/ power on events.
 * 
 * Changing the baud rate is fraught with "Catch-22" pitfalls. If you get in
 * a total muddle, sending a character at 115200 while the backpack is booting
 * is supposed to reset the device to using 115200
 * 
 * If your device seems to "die", sometime after you've changed to a non-
 * standard baud rate, just check that it hasn't reverted to 115200
 * somehow. If this happens to you often, I'd be interested to hear
 * about it... especially if you can figure out why it does it!

Sending "<control>g (0x07)" followed by an ASCII character from "1" to "6" changes the baud rate. The default baud rate is 115,200bps, but the backpack can be set to a variety of communication speeds:
Character  Baud Rate
"1" 4800
"2" 9600
"3" 19200
"4" 38400
"5" 57600
"6" 115200

MySerial.write(124);
delay(20);
  MySerial.write(7);//CHANGE BAUD RATE SETTING
delay(20);
  MySerial.write(0x32);//N.B.... not 2 (for 9600)
*/

//--------------------------------------------
//Exercise the LCD display, try various commands...

MySerial.write(124);
  MySerial.write(byte(0));//Clear screen  

//Start by setting initial cursor position to 0,0
//If cursor set to 0,0, upper left of 1st character
//   printed will be at upper left of LCD

//This may be unnecessary, but does no harm, and
//   is a chance to demonstrate the "set cursor"
//   command. This "cursor" is relevant only to
//   the placement of text on the screen.
//The backpack has limited "wrapping" intelligence.

MySerial.write(124);
  MySerial.write(0x18);//Set cursor X value
  MySerial.write(byte(0));

MySerial.write(124);
  MySerial.write(0x19);//Set cursor Y value
  MySerial.write(byte(0));
delay(80);//Inserting these after set cursor and printlns
//solves some problems with missed characters.

//Put some text on display...
MySerial.println();//(This just to move text down screen...
//... could have been done with a more clever initial Y
//       coordinate.
delay(80);//Insert similar if the text you are getting
   // has the occasional problem. Check you baud rate settings
   // if you are getting nothing or "pure" gibberish.
MySerial.println("Welcome to Sparkfun");
delay(80);
MySerial.println("   LCD-0935");
delay(80);
MySerial.println();
delay(80);
MySerial.println("Keep watching.Things");
delay(80);
MySerial.println("   come and go.");

//Give viewer a moment to read text...
delay(2000);
//  ... before....

//If you leave the following in, the "ink" and "paper"
//  of your display will be reversed each time the
//  program passes this point.
//Normally, you would issue this command once, with an
//  ad hoc program, and then not issue it again. Unless
//  you like the display's mode flipping back and forth
//  all the time!

/*
MySerial.write(124);
  MySerial.write(0x12);//Reverse display mode.
*/

MySerial.write(124);
  MySerial.write(byte(0));//Clear screen  

//Max on line:    xxxxxxxxxxxxxxxxxxxx
MySerial.println("'Graph' will now");
delay(80);
MySerial.println("appear, then go...");

//Give viewer a moment to read text...
delay(1500);
//  ... before....

MySerial.write(124);
  MySerial.write(byte(0));//Clear screen  

//============ Set/Clear Pixels".....
//Put pixels on screen...
/* (all the "random" stuff is just to generate a
* pseudo "graph". The heart of this is...

MySerial.write(124);
  MySerial.write(0x10);//Set or clear pixel.
  MySerial.write(10);//to draw at x coordinate=10, and...
  MySerial.write(20);//y coordinate=20
  MySerial.write(1);//to set pixel

 Make the last line
  MySerial.write(0);//to clear pixel
 */

byte bCount=0;
byte bPrevY=55;
randomSeed(10);
do {
 MySerial.write(124);
  MySerial.write(0x10);//Set or clear pixel.
  MySerial.write(bCount);//x coordinate
  if (random(3)==0) {bPrevY=bPrevY-2;};
  MySerial.write(bPrevY+random(4)-2);//y coordinate
  MySerial.write(1);//to set pixel
  bCount=bCount+1;
 }while (bCount<80);

//*************************************
// give viewer a moment to see the soon-to-disappear
// pixels...

delay(4000);

 //And now remove the pixels we put on screen
 //  a moment ago...
randomSeed(10);
bCount=0;
bPrevY=55;
do {
 MySerial.write(124);
  MySerial.write(0x10);//Set or clear pixel.
  MySerial.write(bCount);//x coordinate
  if (random(3)==0) {bPrevY=bPrevY-2;};
  MySerial.write(bPrevY+random(4)-2);//y coordinate
  MySerial.write(byte(0));//to clear pixel
  bCount=bCount+1;
 }while (bCount<80);

//=========== "Reverse Display".... see discussion at...
//http://sheepdogguides.com/elec/ser_disp/alpha-graphic-sparkfun-LCD-09352.htm
/*
MySerial.write(124);//cmnd
  MySerial.write(0x12);//reverse the display mode
  //The reuslts of this command are remembered across
  //power off/ power on events.
*/  

//=========== "Draw/ Erase Line"...
/*Rem out this block to see the failure of the supposed "erase line" 
 * block, which follows draw one.
MySerial.write(124);
  MySerial.write(0x0C);//Draw/Erase line... with "Draw" chosen
     //by final parameter
  MySerial.write(byte(0x00));//x coordinate of start...
     //remember "byte()" if saying zero
  MySerial.write(10);//y coordinate of start
  MySerial.write(60);//x coordinate of finish
  MySerial.write(30);//y coordinate of finish
  MySerial.write(1);//to make backpack DRAW line.
     //(Zero here to erase... but "erase" doesn't seem to work)
     //May even be creating a problem, if DrawLine is only
     //   using 4 parameters. (Cause problem by leaving an
     //   unused byte in the serial buffer.)

delay(1000);// to give viewer time to see line, before....
*/

MySerial.write(124);
  MySerial.write(0x0C);//Draw/Erase line... with "Erase" chosen
     //by final parameter
  MySerial.write(byte(0x00));//x coordinate of start... remember "byte" if saying zero
  MySerial.write(10);//y coordinate of start
  MySerial.write(60);//x coordinate of finish
  MySerial.write(30);//y coordinate of finish
  MySerial.write(byte(0));//SUPPOSED to make backpack ERASE line.
      //Doesn't seem to have any effect. 0x0C DRAWS regardless of
      //   this final parameter. See notes in DrawLine above.

delay(1000);// to give viewer time to see line, before....

//========= "Draw/Erase Circle".....
/*Rem out this block to see the failure of the supposed "erase circle" 
 * block, which follows draw one.
MySerial.write(124);
  MySerial.write(0x03);//Draw/Erase circle... with "Draw" chosen
     //by final parameter
  MySerial.write(20);//x coordinate of center
  MySerial.write(20);//y coordinate of center
  MySerial.write(12);//radius
  MySerial.write(1);//to make backpack DRAW circle. (Zero here is
    //SUPPOSED to make backpack erase line. Doesn't seem to work.

delay(1000);// to give viewer time to see line, before....
*/    

//Now try to erase just-drawn circle....
MySerial.write(124);
  MySerial.write(0x03);//Draw/Erase circle... with "Erase" chosen
     //by final parameter
  MySerial.write(20);//x coordinate of center
  MySerial.write(20);//y coordinate of center
  MySerial.write(12);//radius
  MySerial.write(byte(0));//SUPPOSED to make backpack ERASE circle.
      //Doesn't seem to have any effect. 0x03 DRAWS regardless of
      //   this final parameter. See notes in DrawLine above.
      
delay(1000);// to give viewer time to see line erased, before....

/*
//(This unnecessary if "erase circle" didn't work... but left in
//   in case "erase circle" eventually fixed!
//Now re-draw it. To see another form of erasing take place.
MySerial.write(124);
  MySerial.write(0x03);//Draw/Erase circle... with "Draw" chosen
  MySerial.write(byte(20));//x coordinate of center
  MySerial.write(20);//y coordinate of center
  MySerial.write(12);//radius
  MySerial.write(byte(1));//to make backpack DRAW circle. (Zero here is
    //SUPPOSED to make backpack erase line. Doesn't seem to work.)
*/


//================= "Draw/Erase Box".............
/*Rem out the following block to see that DrawBox with zero
  * as final parameter still does a DRAW, not an erase.
MySerial.write(124);
  MySerial.write(0x0F);//Draw/Erase OUTLINE of a box, with "Draw" specified.
  MySerial.write(byte(0x00));//x coordinate of upper left
  MySerial.write(15);//y coordinate of upper left
  MySerial.write(40);//x coordinate of lower right
  MySerial.write(30);//y coordinate of lower right
  MySerial.write(1);//to make backpack DRAW box. (Zero here to erase. Or vice versa. May not work!)
  
delay(1000);// to give viewer time to see box outline, before....
*/

MySerial.write(124);
  MySerial.write(0x0F);//"Draw/Erase" box outline, with "Erase" specified.
  MySerial.write(byte(0x00));//x coordinate of upper left
  MySerial.write(15);//y coordinate of upper left
  MySerial.write(40);//x coordinate of lower right
  MySerial.write(30);//y coordinate of lower right
  MySerial.write(byte(0));//SUPPOSED to make backpack ERASE box outline.
      //Doesn't seem to have any effect. 0x05 DRAWS regardless of
      //   this final parameter. See notes in DrawLine above.

delay(1000);// to give viewer time to see that, before....

//============== "Erase a BLOCK, i.e. area, not just outline.....
//    N.B.... this DIFFERENT from "Draw/Erase Box".
MySerial.write(124);
  MySerial.write(0x05);//Erase BLOCK... this is a DIFFERENT command from
 //the "Draw/Erase Box" command. Erase BLOCK (command code 0) erases the
 //contents of a whole rectangle, not just the four lines that outline it.
  MySerial.write(byte(0x00));//x coordinate of upper left
  MySerial.write(10);//y coordinate of upper left
  MySerial.write(40);//x coordinate of lower right
  MySerial.write(25);//y coordinate of lower right
//Note there is no final parameter with this to say "draw or erase".
//Command 0x05, Erase BLOCK is always an erase operation. "A final
//  parameter COULD have been uses to create a "Erease OR FILL block".)

//delay(1000);// to give viewer time to see that, before....
/*Following doesn't work... pity... but it was never suggested by
 * Sparkfun that it might!
MySerial.write(124);
  MySerial.write(0x05);//Erase BLOCK... this is a DIFFERENT command from
 //the "Draw/Erase Box" command. Erase BLOCK (command code 0) erases the
 //contents of a whole rectangle, not just the four lines that outline it.
  MySerial.write(byte(0x00));//x coordinate of upper left
  MySerial.write(10);//y coordinate of upper left
  MySerial.write(40);//x coordinate of lower right
  MySerial.write(25);//y coordinate of lower right
  MySerial.write(byte(0));//to make backpack ERASE if 0, FILL it if 1
*/

//===== final message....

MySerial.write(124);
  MySerial.write(0x18);//Set cursor X value
  MySerial.write(byte(0));
  delay(80);
  
MySerial.write(124);
  MySerial.write(0x19);//Set cursor Y value
  MySerial.write(35);
  delay(100);
  
//                1   5    0    5    0
//Max on line:    xxxxxxxxxxxxxxxxxxxx
MySerial.println("That's it.Study code");
delay(80);
MySerial.println("for reasons for what");
delay(80);
MySerial.println("was shown recently.");
delay(80);

//========= And show off backlight control....
//Backlight should "step down" to "off", and then come back again...
//This uses the "Set Backlight Duty Cycle" command

MySerial.write(124);
  MySerial.write(0x02);//Set Backlight Duty Cycle. (Brightness)
  MySerial.write(80);//use 0 to 100 here, for off to brightest
  delay(800);
  
MySerial.write(124);
  MySerial.write(0x02);//Set Backlight Duty Cycle.
  MySerial.write(60);
  delay(800);
  
MySerial.write(124);
  MySerial.write(0x02);//Set Backlight Duty Cycle.
  MySerial.write(40);
  delay(800);
  
MySerial.write(124);
  MySerial.write(0x02);//Set Backlight Duty Cycle.
  MySerial.write(byte(0));
  delay(2000);

MySerial.write(124);
  MySerial.write(0x02);//Set Backlight Duty Cycle.
  MySerial.write(100);

}//end of setup()

void loop() {
//Empty loop()
}



Remember, please...

The above Arduino code to drive a Sparkfun graphics and text LCD backpack is discussed in detail on the page the link takes you to.



   Search this site                 powered by FreeFind
 
Site Map    What's New    Search


Ad from page's editor: Yes.. I do enjoy compiling these things for you... hope they are helpful. However.. this doesn't pay my bills!!! If you find this stuff useful, (and you run an MS-DOS or Windows PC) please visit my freeware and shareware page, Sheepdog Software (tm), download something, and circulate it for me? At least (please) send an 'I liked the parallel port use page, and I'm from (country/ state)' email? (No... I don't do spam.) Links on your page to this page would also be appreciated!
Click here to visit editor's freeware, shareware page.



Here is how you can contact this page's editor.
Click here to go up to general page about electronics by editor of this page.
Click here to go up to general page about electronic projects by editor of this page.
Why does this page have a script that loads a tiny graphic? I have my web traffic monitored for me by eXTReMe tracker. They offer a free tracker. If you want to try it, check out eXTReMe's site. The Google panels and the search panel are also script based.

Valid HTML 4.01 Transitional Page WILL BE tested for compliance with INDUSTRY (not MS-only) standards, using the free, publicly accessible validator at validator.w3.org


....... P a g e . . . E n d s .....