CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

Sparkfun Serial LCD Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 929
Location: Panama

View user's profile Send private message

Sparkfun Serial LCD Driver
PostPosted: Tue Oct 04, 2016 1:54 pm     Reply with quote

Hi All,

I wrote this little helper driver for the Sparkfun 4x20 Serial LCD.
This makes writing to the LCD more natural or CCS like.

Simply put: It automates the use of '\r' - '\n' - '\f'.

I'm using it with software serial.

Streamception.

So far it seems to be working as expected, with proper text wrapping.
Feel free to improve.

yes: that particular screen was a PIC16F88 onboard, which could easily be reprogrammed with Flex_LCD and controlled serially with custom code and improvements and blackjack and hookers.... yes.

Edit: 12/10/2016 - Slight correction to the "Select_Line(x,y)" function.
Code:

//*************************** Sparkfun Serial LCD Driver**************************
// Author: Gabriel Barrios
// Date: 04/10/2016
// Panama, Rep. Panama.
//********************************************************************************
// This driver controls the Sparkfun 20x4 Serial LCD.
// It automates the use of '\r' - '\n' - '\f'.
// This is because I found it terribly inconvenient to send control commands
// mid code instead of just pushing a '\r' and the end of my printfs.
// The Driver includes other little comfort codes like cursor positioning, toggling
// of the Cursor and Backlight On/Off control.
// A small Splash Screen changer function is included too.
//
// NOTE: when printing to the screen, since the printf calls this "middle man" function
// and not an actual stream, prints are done with "printf" and NOT "fprintf"
//
//***************************** Examples *****************************************
//   On your main or elseware:
//
//        Select_Line(2,0);   //place cursor at line 3,position 0
//         printf(Serial_LCD,"\fTemp:-2.67C\r\n");   //print your stuff
//         Backlight(1);   //Backlight On


//LCD Control Special Characters
#define LCD_CTRL 0xFE      //LCD Cursor/Text Special Character
#define LCD_SETTINGS 0x7C    //LCD Settings Special Character

//LCD Instructions to be used with above Special Characters
#define CLEAR_DISPLAY 0x01
#define CURSOR_ON 0x0D
#define CURSOR_OFF 0x0C
#define BACKLIGHT_ON 0x80
#define BACKLIGHT_OFF 0x9D

// Define the Stream the LCD is going to use.
#use rs232(baud=9600, xmit=PIN_C4, rcv=PIN_F2, ERRORS,STREAM=LCD)//LCD

// Globals to track the cursor
int Current_Line=0;
int Cursor_Position=0;
int Cursor_Tracker=0;

// Function Definitions
void Serial_LCD(char);
void Blink_Cursor(int1);
void Select_Line(char, char);
void Backlight(int1 State);
void Splash_Screen();

//*********************************************************************************
//                     Write New Splash Screen
//*********************************************************************************
// Call this Function ONCE in your code, and then dont call ever again unless you
// want to change the splash screen again.
// These changes will save on the Screens EEPROM and survive power cycles
void Splash_Screen()
{
delay_ms(2000);
printf(Serial_LCD,"\f");
delay_ms(2000);
Select_Line(0,0);
printf(Serial_LCD,"  ALL YOUR BASE ARE ");
Select_Line(1,0);
printf(Serial_LCD,"    BELONG TO US    ");
delay_ms(2000);
fprintf(LCD,"%c%c",0x7C,0x0A);
delay_ms(2000);
}
//*********************************************************************************
//                        Backlight Control
//*********************************************************************************
// Call it with a 1 or 0 to toggle the Backlight LED
void Backlight(int1 State)
{
   if(State==1)
   fprintf(LCD,"%c%c",LCD_SETTINGS,BACKLIGHT_ON);
   else
   fprintf(LCD,"%c%c",LCD_SETTINGS,BACKLIGHT_OFF);
}
//*********************************************************************************
//                        Cursor Blink
//*********************************************************************************
// Call it with a 1 or 0 to enable or disable cursor blink
void Blink_Cursor(int1 State)
{
   if(State==1)
   fprintf(LCD,"%c%c",LCD_CTRL,CURSOR_ON);
   else
   fprintf(LCD,"%c%c",LCD_CTRL,CURSOR_OFF);
}

//*********************************************************************************
//                     Cursor Position/Goto_xy
//*********************************************************************************
// Call it with a line number (0-3) and a row number (0-19)
void Select_Line(char line, char cursor)
{
   if(line==0)Cursor_Position=128+cursor;
   if(line==1)Cursor_Position=192+cursor;
   if(line==2)Cursor_Position=148+cursor;
   if(line==3)Cursor_Position=212+cursor;
   Current_line=line;
   Cursor_Tracker=cursor;
   fprintf(LCD,"%c%c",LCD_CTRL,Cursor_Position);
}

//*********************************************************************************
//                        Printf Control
//*********************************************************************************
// This function enables the use of regular CCS print format chars like \r \n \f.
// It also handles the Wrap arounds so that these characters keep working after the
// screen has automatically wrapped.
void Serial_LCD(char next_char)
{
   switch (next_char)
   {
      case '\f':
      {
         fprintf(LCD,"%c%c",LCD_CTRL,CLEAR_DISPLAY);
         Current_line=0;
         Cursor_Position=128;
         Cursor_Tracker=0;
         delay_ms(100);
         break;
      }
      case '\r':
      {
         if(Current_line==0)Cursor_Position=128;
         if(Current_line==1)Cursor_Position=192;
         if(Current_line==2)Cursor_Position=148;
         if(Current_line==3)Cursor_Position=212;
         Cursor_Tracker=0;
         fprintf(LCD,"%c%c",LCD_CTRL,Cursor_Position);
         delay_ms(100);
         break;
      }
      case '\n':
      {
         if(Current_line==0)Cursor_Position=192+Cursor_Tracker;
         if(Current_line==1)Cursor_Position=148+Cursor_Tracker;
         if(Current_line==2)Cursor_Position=212+Cursor_Tracker;
         if(Current_line==3)Cursor_Position=128+Cursor_Tracker;
         Current_line++;
         if(Current_Line>3)Current_line=0;
         fprintf(LCD,"%c%c",LCD_CTRL,Cursor_Position);
         delay_ms(100);
         break;
      }
      default:
      {
         fputc(next_char,LCD);
         Cursor_Tracker++;
         if(Cursor_Tracker>20)
         {
            Cursor_Tracker=0;
            if(Current_line==0){Cursor_Position=192;Current_line=1;}
            else if(Current_line==1){Cursor_Position=148;Current_line=2;}
            else if(Current_line==2){Cursor_Position=212;Current_line=3;}
            else {Cursor_Position=128;Current_line=0;}
         }
         break;
      }
   }
}


Enjoy.

G.
_________________
CCS PCM 4.135 - PIC: 16F886, 88', 876' 877A
CCS PCH 5.012 - PIC: 18F97j60
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group