display.c

Example code for 16F819(8xx), which measures temperature via SMT160-30, shows it on LCD and communicates with RS232 (sends values of temperature to terminal) (by Petr Mervart).




/*-------------------------------------------------------------------------*/
/* Mode : HD44780 type LCD                                                 */
/*                                                                         */
/* Interface :     SEL = Port A bit - RA01                                 */
/*                  WR = Port A bit - to GND                            */
/*                  RS = Port A bit - RA02                                 */

/*              Data_7 = Port B bit - RB06                                 */
/*              Data_6 = Port B bit - RB05                                 */
/*              Data_5 = Port B bit - RB04                                 */
/*              Data_4 = Port B bit - RB03                                 */
/*                                                                         */
/*                                                                         */
/*             This version is for 4 bit mode                              */
/*                                                                         */
/*                                                                         */

void LCD_Setup(void);
void LCD_Write_4_Bit(char);
void LCD_Delay(void);
void Lprintf( const char *lcdptr );
void Putch(char znak);
void SetPos( char znak );
void WriteInt4(int num);
void WriteInt1(int num);
void WriteInt2(int num);
void WriteInt3(int num);
void LCD_SetPos(char);
/* General definitions */

//#define TRISA 5
//#define TRISB 6
//#define PORTA 5
//#define PORTB 6

/***********************************************************************/
/* Definitions for the LCD interface                                   */
/***********************************************************************/
#define LCD_SEL    1   /* Port A1 ( Enables LCD )                      */
//#define LCD_WR     2   /* Port A bit  2 ( Logic 0 = Write )          */
#define LCD_RS     2   /* Port A2 (     Register select )              */
#define LCD_DATA_4 3   /* LCD BIT 4    PORTB3                          */
#define LCD_DATA_5 4   /* LCD BIT 5        B4                          */
#define LCD_DATA_6 5   /* LCD BIT 6        B5                          */
#define LCD_DATA_7 6   /* LCD BIT 7        B6                          */
/*                                                                     */
/*                                                                     */
/***********************************************************************/
/* LCD Commands ( Refer to LCD Data Sheet )                            */
/* Standard command should work with most common devices               */
/***********************************************************************/
/*                                                                     */
#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                    */

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


/***********************************/
/* Setup the lcd device            */
/***********************************/
void LCD_Setup(void)
{
 /* Reset the LCD */
   delay_ms(200);                      /* Power up delay */
   output_low_port_a( LCD_RS );        //function mode

   set_bit(PORTB,LCD_DATA_5);          // 0010b to DATA7-DATA4

   output_high_port_a( LCD_SEL ); /* Select LCD    */
   output_low_port_a( LCD_SEL );  /* De-select LCD */
   delay_ms(5);

   output_high_port_a( LCD_SEL ); /* Select LCD    */
   output_low_port_a( LCD_SEL );  /* De-select LCD */
   delay_ms(5);

   output_high_port_a( LCD_SEL ); /* Select LCD    */
   output_low_port_a( LCD_SEL );  /* De-select LCD */
   delay_ms(5);

   LCD_Write_4_Bit( system_set_4_bit );
   delay_ms(5);

   LCD_Write_4_Bit( 0x08 );  //display_off
   delay_ms(2);
   LCD_Write_4_Bit( 0x06 );  //entry_mode 
   delay_ms(2);
   LCD_Write_4_Bit( 0x0c );  //display_on
   delay_ms(2);
   LCD_Write_4_Bit( 0x80 );  //set_dd_ram
   delay_ms(2);

   output_high_port_a( LCD_RS );   //data mode

}

/***********************************/
/* Write a single byte to the LCD  */
/* 4 Bit Mode                      */
/***********************************/
void LCD_Write_4_Bit(char d )
{
                                // Output Higher 4 bits 
   LCD_Delay();
   if ( d & 0x80 )
        output_high_port_b( LCD_DATA_7 );
   else output_low_port_b( LCD_DATA_7 );

   if ( d & 0x40 )
        output_high_port_b( LCD_DATA_6 );
   else output_low_port_b( LCD_DATA_6 );


   if ( d & 0x20 )
        output_high_port_b( LCD_DATA_5 );
   else output_low_port_b( LCD_DATA_5 );


   if ( d & 0x10 )
        output_high_port_b( LCD_DATA_4 );
   else output_low_port_b( LCD_DATA_4 );

   //LCD_Delay();
   output_high_port_a( LCD_SEL );
   //LCD_Delay();
   output_low_port_a( LCD_SEL );   /* Clock in the data    */
  // LCD_Delay();

   d <<= 4;                        /* Output Lower 4 bits  */

   if ( d & 0x80 )
        output_high_port_b( LCD_DATA_7 );
   else output_low_port_b( LCD_DATA_7 );

   if ( d & 0x40 )
        output_high_port_b( LCD_DATA_6 );
   else output_low_port_b( LCD_DATA_6 );

   if ( d & 0x20 )
        output_high_port_b( LCD_DATA_5 );
   else output_low_port_b( LCD_DATA_5 );

   if ( d & 0x10 )
        output_high_port_b( LCD_DATA_4 );
   else output_low_port_b( LCD_DATA_4 );


  // LCD_Delay();
   output_high_port_a( LCD_SEL );
  // LCD_Delay();
   output_low_port_a( LCD_SEL );   /* Clock in the data    */

}

/***********************************/
/* LCD timing delay                */
/* Adjust for your LCD             */
/***********************************/
void LCD_Delay(void)
{
 delay_ms(1);
}

/*******************************************/
/* Write a const string to the LCD         */
/*******************************************/
void Lprintf( const char *lcdptr )
{
  char pi;
  pi = 0;
                // Check for end of string
    while( lcdptr[pi] != 0)
         {
             LCD_Write_4_Bit(lcdptr[pi++]);// Display on LCD 
         }
}

void SetPos(char znak)
{
   output_low_port_a( LCD_RS );   // function mode
   LCD_Write_4_Bit(znak);        // Display 1 byte on LCD 
   output_high_port_a( LCD_RS );
}

void Putch(char znak)
{
   LCD_Write_4_Bit(znak);        // Display 1 byte on LCD 
}

void WriteInt4(int num)
{
    LCD_Write_4_Bit( '0' + (num / 1000) % 10 ); // tisice   - max. 9999 dec 
    LCD_Write_4_Bit( '0' + (num / 100) % 10);
    LCD_Write_4_Bit( '0' + (num / 10) % 10 );
    LCD_Write_4_Bit( '0' + num % 10 );

}

void WriteInt1(int num)
{
   LCD_Write_4_Bit( '0' + num % 10 );
}

void WriteInt2(int num)
{
   LCD_Write_4_Bit( '0' + (num / 10));
   LCD_Write_4_Bit( '0' + num % 10 );
}

void WriteInt3(int num)
{
   LCD_Write_4_Bit( '0' + (num / 100) % 10);
   LCD_Write_4_Bit( '0' + (num / 10) % 10 );
   LCD_Write_4_Bit( '0' + num % 10 );
}

void LCD_SetPos(char position)
{
     output_low_port_a( LCD_RS );    //function mode
     LCD_Write_4_Bit(position);
    output_high_port_a( LCD_RS );   //data mode
}



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

Copyright © 2002-2006 SourceBoost Technologies