display.c

24-hour clock with switching times stored in EEPROM and LCD output (HD44780) for PIC16F84A (by Petr Mervart).



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

/*              Data_7 = Port B bit - RB03                                 */
/*              Data_6 = Port B bit - RB02                                 */
/*              Data_5 = Port B bit - RB01                                 */
/*              Data_4 = Port B bit - RB00                                 */
/*                                                                         */
/*                                                                         */
/*             This version is for 4 bit mode                              */
/*                                                                         */
/*                                                                         */
/***************************************************************************/


void LCD_Setup(void);
void LCD_FunctionMode(void);
void LCD_DataMode(void);
void LCD_Write_8_Bit(char);
void LCD_Write_4_Bit(char);
void LCD_Delay(void);
void Lprintf( const char *lcdptr );   //write string 
void LCD_WriteInt(int num);
void LCD_SetPos(char);             // move cursor on position
/* General definitions */

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

/***********************************************************************/
/* Definitions for the LCD interface                                   */
/***********************************************************************/
#define LCD_SEL    0   /* Port A bit 3 ( Enables LCD )                 */
//#define LCD_WR     2   /* Port A bit 2 ( Logic 0 = Write )           */
#define LCD_RS     1   /* Port A bit 1 ( Register select )             */
#define LCD_DATA_4 0   /* LCD BIT 4                                    */
#define LCD_DATA_5 1   /* LCD BIT 5                                    */
#define LCD_DATA_6 2   /* LCD BIT 6                                    */
#define LCD_DATA_7 3   /* LCD BIT 7                                    */
/*                                                                     */
/*                                                                     */
/***********************************************************************/
/* 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(30);                        /* Power up delay */
   LCD_FunctionMode();

   LCD_Write_8_Bit( system_set_4_bit ); /* This sequence resets the LCD */
   delay_ms(5);
   LCD_Write_8_Bit( system_set_4_bit );
   delay_ms(5);
   LCD_Write_8_Bit( system_set_4_bit );
   delay_ms(5);

   LCD_Write_4_Bit( system_set_4_bit );
   delay_ms(2);
   LCD_Write_4_Bit( display_off );
   delay_ms(2);
   LCD_Write_4_Bit( entry_mode );
   delay_ms(2);
   LCD_Write_4_Bit( display_on );
   delay_ms(2);
   LCD_Write_4_Bit( set_dd_ram );
   delay_ms(2);

   LCD_DataMode();

}

/* END OF MAIN */


/***********************************/
/* Put LCD in Function Mode        */
/***********************************/
void LCD_FunctionMode(void)
{
   output_low_port_a( LCD_RS );
   LCD_Delay();
}

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

void LCD_DataMode(void)
{
   output_high_port_a( LCD_RS );
   LCD_Delay();
}

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


   //output_low_port_a( LCD_WR );   /* Write Mode    */
   LCD_Delay();

                                  /* Setup data    */

   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 ); /* Select LCD    */
   LCD_Delay();
   output_low_port_a( LCD_SEL );  /* De-select LCD */
}


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

   //output_low_port_a( LCD_WR );    /* Write Mode           */
   LCD_Delay();

                                  /* Output Higher 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_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 LCD_WriteInt(int num)
{
 // LCD_Write_4_Bit( '0' + (num / 10000));      // desetitisice - max. 99999 dec 
 // LCD_Write_4_Bit( '0' + (num / 1000) % 10 ); // tisice   - max. 9999 dec 
//  LCD_Write_4_Bit( '0' + (num / 100) % 10);   // stovky   - max. 999 dec 
    LCD_Write_4_Bit( '0' + (num / 10));         // desitky  - max. 99 dec
    LCD_Write_4_Bit('0' + (num % 10));       // jednotky - max. 9 dec
}

void LCD_SetPos(char position)
{
     LCD_FunctionMode();
     LCD_Write_4_Bit(position);
    LCD_DataMode();
}



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

Copyright © 2002-2006 SourceBoost Technologies