• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Adding Print Class features to my old Arduino 2-pin 8-bit LCD Backpack Demo

Status
Not open for further replies.

Mike - K8LH

Well-Known Member
I revised the Arduino Demo for my 2-pin 8-bit LCD Backpack to use Print Class services after coming across a simple concise example on David Johson-Davies blog. The original program used 442 bytes of memory and featured Init(), Cmd(), Data(), and Str() functions, as well as an overload Str() function for flash string helper strings. The revised program grew to 634 bytes but now also includes number formatting capabilities from Print Class.

I hope someone finds this demo useful.

Cheerful regards, Mike

Code:
  /******************************************************************
   *  Mike McLaren's 2-pin 8-bit 74HC595/74HC164 LCD Backpack Demo  *
   *                                                                *
   *  16-Sep-18    Arduino 1.8.6 / Arduino Nano                     *
   ******************************************************************/

   #define line1 (0x80+0x00)    // set DDRAM command + line 1 address
   #define line2 (0x80+0x40)    // set DDRAM command + line 2 address
   #define line3 (0x80+0x14)    // set DDRAM command + line 3 address
   #define line4 (0x80+0x54)    // set DDRAM command + line 4 address

   char Scramble [] = "AAAAAAAAA";
 
   #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~(1<<bit))  // clear bit
   #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= 1<<bit)     // set bit

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

   #define clk PORTC2           // 'clk' on PC2 (A2) pin
   #define lat PORTC3           // 'lat' on PC3 (A3) pin

   class LCD : public Print
   { public:                    //
       void init();             //
       void cmd(uint8_t);       //
       size_t write(uint8_t);   //
     private:                   //
       bool rs = 0;             // lcd 'rs' flag
   };

   void LCD::init ()
   { DDRC = 1<<clk|1<<lat;      // make 'clk' and 'lat' outputs
     cbi(PORTC,lat);            // lat = 0
     sbi(PORTC,clk);            // clk = 1
     _delay_ms(100);            // LCD power-on delay
     cmd(0x38);                 // 8-bit, 2-lines, 5x7 font
     cmd(0x0C);                 // display on, currsor & blink off
     cmd(0x06);                 // cursor inc, shift off
     cmd(0x01);                 // clear display
     _delay_ms(2);              // required delay
   }

   size_t LCD::write (uint8_t work)
   { uint8_t bitctr = 8;        //
     uint8_t SaveSREG = SREG;   //
     do                         // start of bit clock cycle
     { if(!(work & 128))        // if b7 is a '0' bit
         cbi(PORTC,clk);        // start clock cycle for a '0'
       _delay_us(9);            // charge or drain cap to 3t
       cli();                   // interrupts off
       cbi(PORTC,clk);          // start clock cycle for a '1'
       sbi(PORTC,clk);          // clock out the '0' or '1'
       SREG = SaveSREG;         // restore SREG & irq enable bit
       work <<= 1;              // prep for next bit
     } while(--bitctr);         // until all 8 bits clocked out
     if(!rs) cbi(PORTC,clk);    // make clk pin = rs flag
     sbi(PORTC,lat);            // latch 595, pulse LCD 'E' pin
     cbi(PORTC,lat);            //  "
     sbi(PORTC,clk);            // always leave clk pin high
     return 1;                  //
   }

   void LCD::cmd (uint8_t c)
   { rs = 0; write(c); rs = 1;  //
   }                            //

  /******************************************************************
   *  main                                                          *
   ******************************************************************/

   LCD lcd;                     // create instance 'lcd' of LCD class

   int main()                   //
   {
     lcd.init();                // 8-bit mode, cursor inc & off
  /*                                                                *
   *  send a couple 'flash string helper' strings                   *
   *                                                                */
     lcd.print(F("K8LH Backpack")); // (44) 1st instance
     lcd.print(F(" v2"));           // (16) 2nd instance

     lcd.cmd(line2+2);          // line 2, tab 2 (6 bytes)
     lcd.print(Scramble);       // (40 bytes) 1st instance
     lcd.cmd(line2+1);          // line 2, tab 1 (6 bytes)
     lcd.print(Scramble);       // (8 bytes) 2nd instance

     while(1)                   // loop forever
     {                          //
     }                          //
   }                            //
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top