All pastes #490569 Raw Edit

Someone

public text v1 · immutable
·published 2007-05-16 02:26 UTC
rendered paste body
/***************************************************************************/ 
/*                                                                         */ 
/*  LcdTest.c                                                              */ 
/*             LCD interface implementation                                */ 
/*                                                                         */ 
/*   Version : 1.3                                                         */ 
/*                                                                         */ 
/*                                                                         */ 
/*         By: J. Winpenny                                                 */ 
/*                                                                         */ 
/*       Date: 19/7/1999                                                   */ 
/*                                                                         */ 
/*                                                                         */ 
/*      Mode : HD44780 type LCD displays                                   */ 
/*                                                                         */ 
/* Interface :     SEL = Port A bit 3                                      */ 
/*                  WR = Port A bit 2                                      */ 
/*                  RS = Port A bit 0                                      */ 
/*              Data_7 = Port B bit 7                                      */ 
/*              Data_6 = Port B bit 6                                      */ 
/*              Data_5 = Port B bit 5                                      */ 
/*              Data_4 = Port B bit 4                                      */ 
/*                                                                         */ 
/*     Notes : Requires "Delays.c"                                         */ 
/*                                                                         */ 
/*             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); 
  

/* General definitions */ 

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

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

/* PIC PORT SETUP */ 

#define TxMode       0x40   /* Set option reg TMR0 interupt            */ 
                            /* source from counter.                    */ 
                            /* Max speed prescaler ( 1:2 )             */ 
                            /* ( Not needed for LCD )                  */ 
  
#define PortAConfig  0x10   /* Configure port A ( 1 = input )          */ 
                            /* RA4/RTCC = Input / RA2 = Input          */ 
  
#define PortBConfig  0x0B   /* Configure port B                        */ 
  
  

/* START OF MAIN */ 
  

void main(void) 
{ 

  char a; 
  char b; 

/* First setup PIC Ports and Interrupts */ 

  set_bit( STATUS, RP0 ); 
  OPTION_REG = TxMode;              // Set Option register. 
  set_tris_a( PortAConfig);         // Setup ports 
  set_tris_b( PortBConfig ); 
  clear_bit( STATUS, RP0 ); 
  output_low_port_a( LCD_SEL );     // Disable LCD 
  disable_interrupt( GIE );          // Enable Global Interrupts 
  
/* End of Setup */ 
  

  LCD_Setup();                /* Setup the LCD device   */ 
  
  LCD_Write_4_Bit('L');       /* Display a test message */ 
  LCD_Write_4_Bit('C'); 
  LCD_Write_4_Bit('D'); 
  
  LCD_Write_4_Bit(' '); 
  
  LCD_Write_4_Bit('T'); 
  LCD_Write_4_Bit('e'); 
  LCD_Write_4_Bit('s'); 
  LCD_Write_4_Bit('t'); 
  
  delay_s(10); 
  
 while( 1 ) 
  { 
    LCD_FunctionMode(); 
    LCD_Write_4_Bit(clear_lcd); 
    LCD_DataMode(); 
  
    a = 'a'; 
  
    for ( b = 0; b < 16; b++ )     /* Write "abcdefghijklmnop" on line 1 */ 
        { 
          LCD_Write_4_Bit(a++); 
          delay_ms(250); 
        } 
  
    LCD_FunctionMode(); 
    LCD_Write_4_Bit(set_dd_line2); /* Goto line 2 */ 
    LCD_DataMode(); 
  
    for ( b = 0; b < 10; b++ )     /* Write "qrstuvwxyz" on line 2 */ 
        { 
          LCD_Write_4_Bit(a++); 
          delay_ms(250); 
        } 
  
    delay_s(5); 
  
  } 
  

} 
  
  

/***********************************/ 
/* Setup the lcd device            */ 
/***********************************/ 
void LCD_Setup(void) 
{ 
 /* Reset the LCD */ 
  
 /* WARNING, AFFECTS ALL OF PORT A */ 
  
   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 Display     */ 
/***********************************/ 
void LCD_Delay(void) 
{ 

  delay_ms(1); 

}