Thursday, November 8, 2012

LCD 16x2 PIC16F690

HD44780 - LCD

On today's article we are going to use an lcd 16x2 with Hitachi HD44780 LCD controller.
wikipedia image

It is necessary before we proceed with schematics and code to explain the basics of these lcds.

Pins

lcd display 16x2

  1. Vss - Ground
  2. Vdd - 5 volts
  3. Vee - contrast adjustment
  4. RS - register select
  5. RW - read or write
  6. E - enable
  7. D0-D7: is the data/command pins
  8. Vdd - backlight anode (optional)
  9. Vss - backlight cathode (optional)
The first 3 pins is just for power. 

The fourth pin RS, is the register select pin. If this is set (RS=1) it means that the data register is selected and the display is now waiting for data. But if RS is unset (RS=0) the command register is selected and the Lcd is waiting for commands to be appeared between D0-D7 pins. That pin it is very important because throughout our projects is very common to send both data and commands.

The fifth pin RW,is the read/write register. When is set (RW=1) we can read from the lcd (rare but is it possible), and when is unset (RW=0) we can write to lcd.

The sixth pin E, enable, it is used as a flag for the lcd to show that the data/command on the D0-D7 pins is valid, and the data/command will be latched into lcd controller as soon as this pin change from high to low. For example if i want to write 10001001, i should move to PORTC the value 10001001, and the data will be valid only when a high to low pulse appears to Enable pin.

The seventh is the data/command pins D0-D7. The valid commands is available in the datasheet of the display, and the data it will be ascii code of the character. For example if i want to write the letter "A" on the display i should apply to the D0-D7 pins the code 0x41 which the ascii equivalent code of "A".

Initialization

The initialization it can be happen by following the flow chart below:


Every part of the flow chart is a command we can send to the display based on the table below:


Keep in mind that in order to send a command (or data) we have to follow the flowchart below:
Here is the initialization code:


  1. Lcd_Init
  2. ;RB4 -->  RS
  3. ;RB5 -->  RW
  4. ;RB6 -->  E
  5. ;RC0-RC7  --> D0-D7

  6. ;wait for the LCD display to boot up
  7. call Delay100ms
  8. ;ready for command
  9. bcf PORTB, 4

  10. ;1. Clear Display and Home the Cursor
  11. ;movlw b'00000001'
  12. movwf PORTC
  13. bsf PORTB, 6
  14. call Delay1ms
  15. bcf PORTB, 6
  16. call Delay1ms

  17. ;2. Return Cursor and LCD to Home Position
  18. movlw b'00000010'
  19. movwf PORTC
  20. bsf PORTB, 6
  21. call Delay1ms
  22. bcf PORTB, 6
  23. call Delay1ms

  24. ;3. Set Cursor Move Direction
  25. ;0-0-0-0-0-1-ID-S
  26. ;ID - Increment the Cursor After Each Byte Written to Display if Set
  27. ;S - Shift Display when Byte Written to Display
  28. movlw b'00000110'
  29. movwf PORTC
  30. bsf PORTB, 6
  31. call Delay1ms
  32. bcf PORTB, 6
  33. call Delay1ms

  34. ;4. Enable Display/Cursor
  35. ;0-0-0-0-1-D-C-B
  36. ;D - Turn Display On(1)/Off(0)
  37. ;C - Turn Cursor On(1)/Off(0)
  38.   ;B - Cursor Blink On(1)/Off(0)
  39. movlw b'00001111'
  40. movwf PORTC
  41. bsf PORTB, 6
  42. call Delay1ms
  43. bcf PORTB, 6
  44. call Delay1ms

  45. ;5. Move Cursor/Shift Display
  46. ;0-0-0-1-SC-RL-x-x
  47. ;SC - Display Shift On(1)/Off(0)
  48.   ;RL - Direction of Shift Right(1)/Left(0)
  49. movlw b'00010100'
  50. movwf PORTC
  51. bsf PORTB, 6
  52. call Delay1ms
  53. bcf PORTB, 6
  54. call Delay1ms
  55. ;6. Set Interface Length
  56. ;0-0-1-DL-N-F-*-*
  57. ;DL - Set Data Interface Length 8(1)/4(0)
  58. ;N - Number of Display Lines 1(0)/2(1)
  59. ;F - Character Font 5x10(1)/5x7(0)
  60. movlw b'00111100'
  61. movwf PORTC
  62. bsf PORTB, 6
  63. call Delay1ms
  64. bcf PORTB, 6
  65. call Delay1ms

  66. ;7.
  67. bcf PORTB, 5
  68. movlw 0x80
  69. movwf PORTC
  70. bsf PORTB, 6
  71. call Delay1ms
  72. bcf PORTB, 6
  73. call Delay1ms

  74. return

Schematic

So based on the explanation above, we created the following schematic:


Message

And after the initialization ends, we are now ready to write the message we want on the screen:


  1. ...
  2. SPACE_l EQU 0x20
  3. A_l EQU 0x41
  4. B_l EQU 0x42
  5. C_l EQU 0x43
  6. D_l EQU 0x44
  7. E_l EQU 0x45
  8. F_l EQU 0x46
  9. G_l EQU 0x47
  10. H_l EQU 0x48
  11. I_l EQU 0x49
  12. J_l EQU 0x4A
  13. K_l EQU 0x4B
  14. L_l EQU 0x4C
  15. M_l EQU 0x4D
  16. N_l EQU 0x4E
  17. O_l EQU 0x4F
  18. P_l EQU 0x50
  19. Q_l EQU 0x51
  20. R_l EQU 0x52
  21. S_l EQU 0x53
  22. T_l EQU 0x54
  23. U_l EQU 0x55
  24. V_l EQU 0x56
  25. W_l EQU 0x57
  26. X_l EQU 0x58
  27. Y_l EQU 0x59
  28. Z_l EQU 0x5A
  29. ...

  30. ;write data
  31. bsf PORTB, 4

  32. movlw H_l
  33. movwf PORTC
  34. bsf PORTB, 6
  35. call Delay100us
  36. bcf PORTB, 6

  37. movlw E_l
  38. movwf PORTC
  39. bsf PORTB, 6
  40. call Delay100us
  41. bcf PORTB, 6

  42. movlw L_l
  43. movwf PORTC
  44. bsf PORTB, 6
  45. call Delay100us
  46. bcf PORTB, 6

  47. movlw L_l
  48. movwf PORTC
  49. bsf PORTB, 6
  50. call Delay100us
  51. bcf PORTB, 6

  52. movlw O_l
  53. movwf PORTC
  54. bsf PORTB, 6
  55. call Delay100us
  56. bcf PORTB, 6

  57. ;move cursor to line 2
  58. bcf PORTB, 4 ;write command
  59. bcf PORTB, 5
  60. movlw 0xC0
  61. movwf PORTC
  62. bsf PORTB, 6
  63. call Delay1ms
  64. bcf PORTB, 6
  65. call Delay1ms

  66. ;write data
  67. bsf PORTB, 4

  68. movlw J_l
  69. movwf PORTC
  70. bsf PORTB, 6
  71. call Delay100us
  72. bcf PORTB, 6

  73. movlw A_l
  74. movwf PORTC
  75. bsf PORTB, 6
  76. call Delay100us
  77. bcf PORTB, 6

  78. movlw C_l
  79. movwf PORTC
  80. bsf PORTB, 6
  81. call Delay100us
  82. bcf PORTB, 6

  83. movlw K_l
  84. movwf PORTC
  85. bsf PORTB, 6
  86. call Delay100us
  87. bcf PORTB, 6

  88. movlw SPACE_l
  89. movwf PORTC
  90. bsf PORTB, 6
  91. call Delay100us
  92. bcf PORTB, 6

  93. movlw S_l
  94. movwf PORTC
  95. bsf PORTB, 6
  96. call Delay100us
  97. bcf PORTB, 6

  98. movlw P_l
  99. movwf PORTC
  100. bsf PORTB, 6
  101. call Delay100us
  102. bcf PORTB, 6

  103. movlw A_l
  104. movwf PORTC
  105. bsf PORTB, 6
  106. call Delay100us
  107. bcf PORTB, 6

  108. movlw R_l
  109. movwf PORTC
  110. bsf PORTB, 6
  111. call Delay100us
  112. bcf PORTB, 6

  113. movlw R_l
  114. movwf PORTC
  115. bsf PORTB, 6
  116. call Delay100us
  117. bcf PORTB, 6
  118. movlw O_l
  119. movwf PORTC
  120. bsf PORTB, 6
  121. call Delay100us
  122. bcf PORTB, 6

  123. movlw W_l
  124. movwf PORTC
  125. bsf PORTB, 6
  126. call Delay100us
  127. bcf PORTB, 6

  128. loop
  129. goto loop

All the files can be found here...

Thank you...

No comments:

Post a Comment