Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.11 KB | None | 0 0
  1. /*******************************************************************************
  2.  
  3.   LCD_DISPLAY.h
  4.  
  5.   Displays Character string on LCD display module
  6.   4 bit mode
  7.  
  8.  
  9. *******************************************************************************/
  10.  
  11. #include "iom16c62p.h"
  12.  
  13. #define RS_PIN               p2_0  /* RS Register Select pin */
  14. #define EN_PIN               p2_1  /* Display Enable pin */
  15. #define DATA_PORT            p2    /* Data bus port */
  16. #define DATA_PORT_MASK       0xF0  /* Bit mask from entire port */ 
  17. #define DATA_PORT_SHIFT      4     /* Number of bits data needs to shift */    
  18. #define DATA_WR              1
  19. #define CTRL_WR              0
  20.  
  21. #define DELAY_TIMING          0x01  /* Set to ensure base delay of 1mS minimum */
  22. #define NUMB_CHARS_PER_LINE   8     /* number of lines on the LCD display */
  23. #define MAXIMUM_LINES         2     /* Maximum charactors per line of LCD display. */      
  24. #define LCD_LINE1             0
  25. #define LCD_LINE2             16
  26.  
  27. #define LCD_CLEAR             0x01  /* Clear LCD display and home cursor */
  28. #define LCD_HOME_L1           0x80  /* move cursor to line 1 */
  29. #define LCD_HOME_L2           0xC0  /* move cursor to line 2 */
  30. #define CURSOR_MODE_DEC       0x04  /* Cursor auto decrement after R/W */
  31. #define CURSOR_MODE_INC       0x06  /* Cursor auto increment after R/W */
  32. #define FUNCTION_SET          0x28  /* Setup, 4 bits,2 lines, 5X7 */
  33. #define LCD_CURSOR_ON         0x0E  /* Display ON with Cursor */
  34. #define LCD_CURSOR_OFF        0x0C  /* Display ON with Cursor off */
  35. #define LCD_CURSOR_BLINK      0x0D  /* Display on with blinking cursor */
  36. #define LCD_CURSOR_LEFT       0x10  /*Move Cursor Left One Position */
  37. #define LCD_CURSOR_RIGHT      0x14  /* Move Cursor Right One Position */
  38. #define LCD_DISPLAY_ON        0x04
  39. #define LCD_TWO_LINE          0x08
  40. #define SET_BIT_HIGH          1                
  41. #define SET_BIT_LOW       0
  42.  
  43. void InitialiseDisplay( void );
  44. void DisplayString(unsigned char position, char *string);
  45. void LCD_write(unsigned char data_or_ctrl, unsigned char value);
  46. void LCD_nibble_write(unsigned char data_or_ctrl, unsigned char value);
  47. void DisplayDelay(unsigned long int units);
  48.  
  49.  
  50. /*****************************************************************************
  51. Name:           InitialiseDisplay
  52. Description:    Intializes the LCD display.
  53. *****************************************************************************/
  54.  
  55. void InitialiseDisplay( void )
  56. {
  57.     pd2=0xFF;  //Sets port P2 to output
  58.         /* Power Up Delay for LCD Module */
  59.     EN_PIN = SET_BIT_HIGH;
  60.     DisplayDelay(7000);
  61.     EN_PIN = SET_BIT_LOW;
  62.  
  63.     /* Display initialises in 8 bit mode - so send one write (seen as 8 bit)
  64.     to set to 4 bit mode. */
  65.     /* Function Set */
  66.     LCD_nibble_write(CTRL_WR,0x03);
  67.     //LCD_nibble_write(CTRL_WR,0x03);
  68.     DisplayDelay(39);
  69.  
  70.     /* Configure display */
  71.     //LCD_nibble_write(CTRL_WR,0x03);
  72.     LCD_nibble_write(CTRL_WR,0x02);
  73.     LCD_nibble_write(CTRL_WR,(LCD_DISPLAY_ON | LCD_TWO_LINE ));
  74.     LCD_nibble_write(CTRL_WR,(LCD_DISPLAY_ON | LCD_TWO_LINE ));
  75.     DisplayDelay(39);
  76.  
  77.     /* Display ON/OFF control */
  78.     LCD_write(CTRL_WR,LCD_CURSOR_BLINK);
  79.     DisplayDelay(39);
  80.  
  81.     /* Display Clear */
  82.     LCD_write(CTRL_WR,LCD_CLEAR);
  83.     DisplayDelay(1530);
  84.  
  85.     /* Entry Mode Set */
  86.     LCD_write(CTRL_WR,0x06);
  87.     LCD_write(CTRL_WR,LCD_HOME_L1);
  88. }
  89.  
  90. /*****************************************************************************
  91. Name:           DisplayString  
  92. Parameters:     position    Line number of display
  93.                 string      Pointer to data to be written to display.
  94.                             Last character should be null.
  95. Returns:        none
  96. Description:    This function controls LCD writes to line 1 or 2 of the LCD.
  97.                 You need to use the defines LCD_LINE1 and LCD_LINE2 in order
  98.                 to specify the starting position.
  99.                 For example, to start at the 2nd position on line 1...
  100.                    DisplayString(LCD_LINE1 + 1, "Hello")
  101. *****************************************************************************/
  102. void DisplayString(unsigned char position,  char *string)
  103. {
  104.     static unsigned char next_pos = 0xFF;
  105.  
  106.     /* Set line position if needed. We don't want to if we don't need
  107.        to because LCD control operations take longer than LCD data
  108.        operations. */
  109.     if( next_pos != position)
  110.     {
  111.         if(position < LCD_LINE2)
  112.         {
  113.             /* Display on Line 1 */
  114.             LCD_write(CTRL_WR, (unsigned char)(LCD_HOME_L1 + position) );
  115.         }
  116.         else
  117.         {
  118.             /* Display on Line 2 */
  119.             LCD_write(CTRL_WR, (unsigned char)(LCD_HOME_L2 + position - LCD_LINE2) );
  120.         }
  121.         /* set position index to known value */
  122.         next_pos = position;       
  123.     }
  124.  
  125.     do
  126.     {
  127.         LCD_write(DATA_WR,*string++);
  128.         /* increment position index */
  129.         next_pos++;            
  130.     }
  131.     while(*string);
  132.  
  133.  
  134. }
  135.  
  136. /*****************************************************************************
  137. Name:           LCD_write
  138. Parameters:     value - the value to write
  139.                 data_or_ctrl - To write value as DATA or CONTROL
  140.                                 1 = DATA
  141.                                 0 = CONTROL
  142. Returns:        none
  143. Description:    Writes data to display. Sends command to display.  
  144. *****************************************************************************/
  145. void LCD_write(unsigned char data_or_ctrl, unsigned char value)
  146. {
  147.     /* Write upper nibble first */
  148.     LCD_nibble_write(data_or_ctrl, (value & 0xF0) >> 4);
  149.     /* Write lower nibble second */
  150.     LCD_nibble_write(data_or_ctrl, (value & 0x0F));
  151. }
  152.  
  153. /*****************************************************************************
  154. Name:           LCD_nibble_write
  155. Parameters:     value - the value to write
  156.                 data_or_ctrl - To write value as DATA or CONTROL
  157.                                 1 = DATA
  158.                                 0 = CONTROL
  159. Returns:        none
  160. Description:    Writes data to display. Sends command to display.  
  161. *****************************************************************************/
  162. void LCD_nibble_write(unsigned char data_or_ctrl, unsigned char value)
  163. {
  164.     unsigned char ucStore;
  165.     /* Set Register Select pin high for Data */
  166.     if (data_or_ctrl == DATA_WR)
  167.     {
  168.         RS_PIN = SET_BIT_HIGH;
  169.     }
  170.     else
  171.     {
  172.         RS_PIN = SET_BIT_LOW;
  173.     }
  174.     /* There must be 40ns between RS write and EN write */
  175.     DisplayDelay(1);                   
  176.     /* EN enable chip (HIGH) */
  177.     EN_PIN = SET_BIT_HIGH;
  178.     /* Tiny delay */           
  179.     DisplayDelay(1);
  180.     /* Clear port bits used */ 
  181.     ucStore = DATA_PORT;
  182.     ucStore &= ~DATA_PORT_MASK;
  183.     /* OR in data */   
  184.     ucStore |= ((value << DATA_PORT_SHIFT) & DATA_PORT_MASK );
  185.     /* Write data to port */   
  186.     DATA_PORT = ucStore;
  187.     /* write delay while En High */            
  188.     DisplayDelay(20);
  189.     /* Latch data by dropping EN */                
  190.     EN_PIN = SET_BIT_LOW;
  191.     /* Data hold delay */              
  192.     DisplayDelay(20);                  
  193.     if (data_or_ctrl == CTRL_WR)
  194.     {
  195.         /* Extra delay needed for control writes */
  196.         DisplayDelay(0x7FF);               
  197.     }              
  198. }
  199. /*****************************************************************************
  200. Name:          DisplayDelay
  201. Parameters:    units - Approximately in microseconds                  
  202. Returns:       none
  203. Description:   Delay routine for LCD display.  
  204. *****************************************************************************/
  205. void DisplayDelay(unsigned long int units)
  206. {
  207.     unsigned long counter = units * DELAY_TIMING;
  208.     while(counter--)
  209.     {
  210.         asm("nop");
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement