Lcd3Pin.c

The program shows how to use the 3-Pin Lcd-Library (by Gerhard Burger).



//********************************************************************
// Library for display control over a serial shift register
// Author: G. Burger
// Compiler: C2C, Pavel Baranov
//********************************************************************
/* Definitions for the LCD interface */


#ifndef PORTB               #define PORTB             0x06     #endif
#ifndef TRISB               #define TRISB             0x06     #endif
#ifndef PORTA               #define PORTA             0x05     #endif
#ifndef TRISA               #define TRISA             0x05     #endif


//--------------------Konfiguration der Hardwarepins am PIC (Default-Werte PA0 - PA2)
#ifndef   PinSrData        #define PinSrData                0   #endif 
#ifndef   BitSrData        #define BitSrData             0x01   #endif 
#ifndef   TrisSrData       #define TrisSrData           TRISA   #endif 
#ifndef   PortSrData       #define PortSrData           PORTA   #endif 

#ifndef   PinSrDSet        #define  PinSrDSet               1   #endif 
#ifndef   BitSrDSet        #define  BitSrDSet            0x02   #endif 
#ifndef   TrisSrDSet       #define  TrisSrDSet          TRISA   #endif 
#ifndef   PortSrDSet       #define  PortSrDSet          PORTA   #endif 

#ifndef   PinSrClk         #define  PinSrClk                2   #endif 
#ifndef   BitSrClk         #define  BitSrClk             0x04   #endif 
#ifndef   TrisSrClk        #define  TrisSrClk           TRISA   #endif 
#ifndef   PortSrClk        #define  PortSrClk           PORTA   #endif 
//--------------------Konfiguration der Hardwarepins am PIC



#ifndef   PinLCD_SEL       #define PinLCD_SEL            0x04   #endif 
#ifndef   PinLCD_WR        #define PinLCD_WR             0x02   #endif 
#ifndef   PinLCD_RS        #define PinLCD_RS             0x01   #endif 
#ifndef   PinLCD_DATA_4    #define PinLCD_DATA_4            4   #endif 
#ifndef   PinLCD_DATA_5    #define PinLCD_DATA_5            5   #endif 
#ifndef   PinLCD_DATA_6    #define PinLCD_DATA_6            6   #endif 
#ifndef   PinLCD_DATA_7    #define PinLCD_DATA_7            7   #endif 


#define clear_lcd         0x01 /* Clear Display                       */
#define return_home       0x02 /* Cursor to Home position             */
#define entry_mode        0x06 /* Normal entry mode                   */
#define entry_mode_shift  0x07 /* - with shift                        */
#define system_set_8_bit  0x38 /* 8 bit data mode 2 line ( 5x7 font ) */
#define system_set_4_bit  0x28 /* 4 bit data mode 2 line ( 5x7 font ) */
#define display_on        0x0c /* Switch ON Display                   */
#define display_off       0x08 /* Cursor plus blink                   */
#define set_dd_line1      0x80 /* Line 1 position 1                   */
#define set_dd_line2      0xC0 /* Line 2 position 1                   */
#define set_dd_ram        0x80 /* Line 1 position 1                   */
#define write_data        0x00 /* With rs = 1                         */
#define cursor_on         0x0E /* Switch Cursor ON                    */
#define cursor_off        0x0C /* Switch Cursor OFF                   */


unsigned char SRData; // Daten im Schieberegister
char LCD_gp;


void WriteSR ()
{
  clear_bit(PortSrClk,PinSrClk);
  clear_bit(PortSrDSet,PinSrDSet);

  //adata = data;
  if (SRData & 0x80 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_bit(PortSrClk,PinSrClk);

  if (SRData & 0x40 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_bit(PortSrClk,PinSrClk);


  if (SRData & 0x20 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_bit(PortSrClk,PinSrClk);


  if (SRData & 0x10 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_wdt(); clear_bit(PortSrClk,PinSrClk);


  if (SRData & 0x08 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_wdt(); clear_bit(PortSrClk,PinSrClk);


  if (SRData & 0x04 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_wdt(); clear_bit(PortSrClk,PinSrClk);


  if (SRData & 0x02 != 0) set_bit(PortSrData,PinSrData);
             else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_wdt(); clear_bit(PortSrClk,PinSrClk);


  if (SRData & 0x01 != 0) set_bit(PortSrData,PinSrData);
                   else clear_bit(PortSrData,PinSrData);
  set_bit(PortSrClk,PinSrClk); clear_wdt(); clear_bit(PortSrClk,PinSrClk);

  set_bit(PortSrDSet,PinSrDSet);
  clear_wdt();
  clear_bit(PortSrDSet,PinSrDSet);
  clear_wdt();

}

void set_bitLcd(unsigned char BitNr)
{
  SRData = SRData | BitNr;
  WriteSR();
}

void clear_bitLcd(unsigned char BitNr)
{
  SRData = SRData & ~BitNr;
  WriteSR();
}



/***********************************/
/* LCD timing delay                */
/* Adjust for your LCD             */
/***********************************/
void Delay(void)
{
   delay_us(255);
   clear_wdt();
}


/***********************************/
/* Write a single byte to the LCD  */
/* 8 Bit Mode                      */
/***********************************/
void Write_8_Bit( char dh )
{

   Delay();

   SRData = SRData & 0x0F;

   SRData = SRData & dh;
   if (dh & 0x80)
            set_bit(SRData,PinLCD_DATA_7);
     else clear_bit(SRData,PinLCD_DATA_7);
   if (dh & 0x40)
            set_bit(SRData,PinLCD_DATA_6);
     else clear_bit(SRData,PinLCD_DATA_6);
   if (dh & 0x20)
            set_bit(SRData,PinLCD_DATA_5);
     else clear_bit(SRData,PinLCD_DATA_5);
   if (dh & 0x10)
            set_bit(SRData,PinLCD_DATA_4);
     else clear_bit(SRData,PinLCD_DATA_4);

   WriteSR();
   set_bitLcd(PinLCD_SEL);
   Delay();
   clear_bitLcd(PinLCD_SEL);
   Delay();
   clear_wdt();
}


/***********************************/
/* Write a single byte to the LCD  */
/* 4 Bit Mode                      */
/***********************************/
void Write_4_Bit(char dl )
{
   if (dl & 0x80)
            set_bit(SRData,PinLCD_DATA_7);
     else clear_bit(SRData,PinLCD_DATA_7);
   if (dl & 0x40)
            set_bit(SRData,PinLCD_DATA_6);
     else clear_bit(SRData,PinLCD_DATA_6);
   if (dl & 0x20)
            set_bit(SRData,PinLCD_DATA_5);
     else clear_bit(SRData,PinLCD_DATA_5);
   if (dl & 0x10)
            set_bit(SRData,PinLCD_DATA_4);
     else clear_bit(SRData,PinLCD_DATA_4);
   WriteSR();
   Delay();
   set_bitLcd(PinLCD_SEL);
   Delay();
   clear_bitLcd(PinLCD_SEL);
   Delay();


   dl <<= 4;

   if (dl & 0x80)
            set_bit(SRData,PinLCD_DATA_7);
     else clear_bit(SRData,PinLCD_DATA_7);
   if (dl & 0x40)
            set_bit(SRData,PinLCD_DATA_6);
     else clear_bit(SRData,PinLCD_DATA_6);
   if (dl & 0x20)
            set_bit(SRData,PinLCD_DATA_5);
     else clear_bit(SRData,PinLCD_DATA_5);
   if (dl & 0x10)
            set_bit(SRData,PinLCD_DATA_4);
     else clear_bit(SRData,PinLCD_DATA_4);
   WriteSR();
   Delay();
   set_bitLcd(PinLCD_SEL);
   Delay();
   clear_bitLcd(PinLCD_SEL);
   Delay();
}


/***********************************/
/* Put LCD in Data Mode            */
/***********************************/

void DataMode(void)
{
   set_bitLcd(PinLCD_RS );
   Delay();
}

/***********************************/
/* Put LCD in Function Mode        */
/***********************************/
void FunctionMode(void)
{
   clear_bitLcd(PinLCD_RS );
   Delay();
}


/***********************************/
/* Clear LCD Screen                */
/***********************************/
void LcdClear(void)
{
   FunctionMode();
   Write_4_Bit(clear_lcd);
   DataMode();
}


void Set8BitMode(void)
{
  FunctionMode();
  Write_4_Bit(system_set_8_bit);
  DataMode();
}

/***********************************/
/* Setup the lcd device            */
/***********************************/
extern void LcdSetup(void)
{

  set_bit(STATUS, RP0);    // select the register bank 1
    clear_bit(TrisSrClk,PinSrClk);
    clear_bit(TrisSrData,PinSrData);
    clear_bit(TrisSrDSet,PinSrDSet);
  clear_bit(STATUS, RP0);

  SRData=0;
  WriteSR();

  Delay();

  Set8BitMode();

  delay_ms(30);
  FunctionMode();
  Write_8_Bit( system_set_4_bit ); /* This sequence resets the LCD */
  delay_ms(5);
  Write_8_Bit( system_set_4_bit );
  delay_ms(5);
  Write_8_Bit( system_set_4_bit );
  delay_ms(5);

  Write_4_Bit( system_set_4_bit );
  delay_ms(5);

  Write_4_Bit( display_off );
  delay_ms(2);
  Write_4_Bit( entry_mode );
  delay_ms(2);
  Write_4_Bit( display_on );
  delay_ms(2);
  Write_4_Bit( set_dd_ram );
  delay_ms(2);
  LcdClear();

  DataMode();

}



/***********************************/
/* Set the cursor position         */
/***********************************/
void LcdSetPos(char Pos)
{

   FunctionMode();
   Write_4_Bit( 0x80 + Pos );
   DataMode();

}

/***********************************/
/* Set Position to line 1          */
/***********************************/
void LcdLine1(void)
{
   FunctionMode();
   Write_4_Bit( set_dd_line1 );
   DataMode();
}

/***********************************/
/* Set Position to line 2          */
/***********************************/
void LcdLine2(void)
{
   FunctionMode();
   Write_4_Bit( set_dd_line2 );
   DataMode();
}

/*******************************************/
/* Clear Line 1                            */
/*******************************************/
void LcdClearLine1(void)
{

   LcdLine1();

   for( LCD_gp = 0; LCD_gp < 16; LCD_gp++ )
      {
          Write_4_Bit(' ');
      }

   LcdLine1();
}

/*******************************************/
/* Clear Line 2                            */
/***************************** **************/
void LcdClearLine2(void)
{
  LcdLine2();
  for( LCD_gp = 0; LCD_gp < 16; LCD_gp++ )
  {
    Write_4_Bit(' ');
  }
  LcdLine2();
}

/*******************************************/
/* Write a const string to the LCD         */
/*  3 Bytes if used
/*******************************************/
void LcdWrite( const char *lcdptr )
{
  char pi;
  char ch;
  pi = 0;
  while(lcdptr[pi] !=  0)                 // Check for end of string
  {
    ch = lcdptr[pi++];
    Write_4_Bit(ch);// Display on LCD 
 }
}

/* Write an Integer value to LCD      */
void LcdWriteInt(int num)
{
  Write_4_Bit( '0' + (num / 10000));
  Write_4_Bit( '0' + (num / 1000) % 10 );
  Write_4_Bit( '0' + (num / 100) % 10);
  Write_4_Bit( '0' + (num / 10) % 10 );
  Write_4_Bit( '0' + num % 10 );
}



http://www.sourceboost.com/home.html

Copyright © 2002-2006 SourceBoost Technologies