Lcd6Pin.c

The program shows how to measure the frequency on pin RB0 and write it to a LCD-display (by Gerhard Burger).



//------------------------------------------------------------------------------------------------------
// LCD-Library for 2-Line-LCDs 
// Compiler C2C, Pavel Baranov
// Author: G. Burger (onle code adapted from other LCD-libraries)
// Date: May 2003
//------------------------------------------------------------------------------------------------------

/* 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

#ifndef    PinLCD_SEL       #define PinLCD_SEL           2     #endif 
#ifndef    PinLCD_RS        #define PinLCD_RS            1     #endif 
#ifndef    PinLCD_DATA_4    #define PinLCD_DATA_4        3     #endif 
#ifndef    PinLCD_DATA_5    #define PinLCD_DATA_5        4     #endif 
#ifndef    PinLCD_DATA_6    #define PinLCD_DATA_6        5     #endif 
#ifndef    PinLCD_DATA_7    #define PinLCD_DATA_7        6     #endif 

#ifndef    PortLCD_SEL      #define PortLCD_SEL      PORTB     #endif 
#ifndef    PortLCD_RS       #define PortLCD_RS       PORTB     #endif 
#ifndef    PortLCD_DATA_4   #define PortLCD_DATA_4   PORTB     #endif 
#ifndef    PortLCD_DATA_5   #define PortLCD_DATA_5   PORTB     #endif 
#ifndef    PortLCD_DATA_6   #define PortLCD_DATA_6   PORTB     #endif 
#ifndef    PortLCD_DATA_7   #define PortLCD_DATA_7   PORTB     #endif

#ifndef    TrisLCD_SEL      #define TrisLCD_SEL      TRISB     #endif 
#ifndef    TrisLCD_RS       #define TrisLCD_RS       TRISB     #endif 
#ifndef    TrisLCD_DATA_4   #define TrisLCD_DATA_4   TRISB     #endif 
#ifndef    TrisLCD_DATA_5   #define TrisLCD_DATA_5   TRISB     #endif 
#ifndef    TrisLCD_DATA_6   #define TrisLCD_DATA_6   TRISB     #endif 
#ifndef    TrisLCD_DATA_7   #define TrisLCD_DATA_7   TRISB     #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                   */


char LCD_gp;

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


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

   Delay();
   if (dh & 0x80)
            set_bit(PortLCD_DATA_7,PinLCD_DATA_7);
     else clear_bit(PortLCD_DATA_7,PinLCD_DATA_7);
   if (dh & 0x40)
            set_bit(PortLCD_DATA_6,PinLCD_DATA_6);
     else clear_bit(PortLCD_DATA_6,PinLCD_DATA_6);
   if (dh & 0x20)
            set_bit(PortLCD_DATA_5,PinLCD_DATA_5);
     else clear_bit(PortLCD_DATA_5,PinLCD_DATA_5);
   if (dh & 0x10)
            set_bit(PortLCD_DATA_4,PinLCD_DATA_4);
     else clear_bit(PortLCD_DATA_4,PinLCD_DATA_4);
  Delay();
   set_bit(PortLCD_SEL, PinLCD_SEL);
   Delay();
   clear_bit(PortLCD_SEL, PinLCD_SEL);
   clear_wdt();
}


/***********************************/
/* Write a single byte to the LCD  */
/* 4 Bit Mode                      */
/***********************************/
void Write_4_Bit(char dl )
{

//   clear_bit( LCD_CONTROL, LCD_WR );       /* Write mode */

   if (dl & 0x80)
            set_bit(PortLCD_DATA_7,PinLCD_DATA_7);
     else clear_bit(PortLCD_DATA_7,PinLCD_DATA_7);
   if (dl & 0x40)
            set_bit(PortLCD_DATA_6,PinLCD_DATA_6);
     else clear_bit(PortLCD_DATA_6,PinLCD_DATA_6);
   if (dl & 0x20)
            set_bit(PortLCD_DATA_5,PinLCD_DATA_5);
     else clear_bit(PortLCD_DATA_5,PinLCD_DATA_5);
   if (dl & 0x10)
            set_bit(PortLCD_DATA_4,PinLCD_DATA_4);
     else clear_bit(PortLCD_DATA_4,PinLCD_DATA_4);

   Delay();
   set_bit(PortLCD_SEL, PinLCD_SEL);
   Delay();
   clear_bit(PortLCD_SEL, PinLCD_SEL);
   Delay();

   dl <<= 4;

   if (dl & 0x80)
            set_bit(PortLCD_DATA_7,PinLCD_DATA_7);
     else clear_bit(PortLCD_DATA_7,PinLCD_DATA_7);
   if (dl & 0x40)
            set_bit(PortLCD_DATA_6,PinLCD_DATA_6);
     else clear_bit(PortLCD_DATA_6,PinLCD_DATA_6);
   if (dl & 0x20)
            set_bit(PortLCD_DATA_5,PinLCD_DATA_5);
     else clear_bit(PortLCD_DATA_5,PinLCD_DATA_5);
   if (dl & 0x10)
            set_bit(PortLCD_DATA_4,PinLCD_DATA_4);
     else clear_bit(PortLCD_DATA_4,PinLCD_DATA_4);

   Delay();
   set_bit(PortLCD_SEL, PinLCD_SEL);
   Delay();
   clear_bit(PortLCD_SEL, PinLCD_SEL);
   Delay();

}


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

void DataMode(void)
{
   set_bit(PortLCD_RS, PinLCD_RS );
   Delay();
}

/***********************************/
/* Put LCD in Function Mode        */
/***********************************/
void FunctionMode(void)
{
   clear_bit(PortLCD_RS, 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            */
/***********************************/
void LcdSetup(void)
{

  set_bit(STATUS, RP0);    // select the register bank 1
  clear_bit(TrisLCD_SEL,PinLCD_SEL);
  clear_bit(TrisLCD_RS,PinLCD_RS);
  clear_bit(TrisLCD_DATA_4,PinLCD_DATA_4);
  clear_bit(TrisLCD_DATA_5,PinLCD_DATA_5);
  clear_bit(TrisLCD_DATA_6,PinLCD_DATA_6);
  clear_bit(TrisLCD_DATA_7,PinLCD_DATA_7);
  clear_bit(STATUS, RP0);

  clear_bit(PortLCD_SEL,PinLCD_SEL);
  clear_bit(PortLCD_RS,PinLCD_RS);
  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         */
/*******************************************/
void LcdWrite( const char *lcdptr )
{
  char pi;
  char ch;

  pi = 0;
                // Check for end of string
    while( lcdptr[pi] !=  0 )
         {
            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 );
}

//void main()
//{
// LCD_Setup;
//}



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

Copyright © 2002-2006 SourceBoost Technologies