Advertisement
Guest User

44780 AVR ASM

a guest
May 26th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. .dseg
  2. dead_lcd_buffer: ;allows to write off screen
  3. .db 0, 0
  4. lcd_display: ;holds ASCII to be sent to lcd
  5. .db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  6. .db 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  7.  
  8. .cseg
  9.  
  10. .equ PORT_LCD = PORTA
  11. .equ DDR_LCD = DDRA
  12. .equ LCD_LED = PA0
  13. .equ LCD_RS = PA1
  14. .equ LCD_E = PA2
  15. .equ LCD_DB4 = PA4
  16. .equ LCD_DB5 = PA5
  17. .equ LCD_DB6 = PA6
  18. .equ LCD_DB7 = PA7
  19.  
  20. .equ LCD_INST = 0
  21. .equ LCD_NYBBLE = 1
  22.  
  23. error_message:
  24. .db " CONTROLLER "
  25. .db " NOT DETECTED "
  26.  
  27. init_lcd:
  28. in r16, DDR_LCD
  29. ldi r17, (1 << LCD_LED) | (1 << LCD_RS) | (1 << LCD_E) | (1 << LCD_DB4) | (1 << LCD_DB5) | (1 << LCD_DB6) | (1 << LCD_DB7)
  30. or r16, r17
  31. out DDR_LCD, r16
  32.  
  33. rcall wait_5_ms
  34. rcall wait_5_ms
  35. rcall wait_5_ms ;wait 15ms after startup
  36.  
  37. ldi r16, 0x03
  38. ldi r17, (1 << LCD_NYBBLE) | (1 << LCD_INST) ;only need to set the first time
  39. rcall send_LCD
  40. rcall wait_5_ms ;write 0x03 and wait 5ms
  41.  
  42. ldi r16, 0x03
  43. rcall send_LCD
  44. rcall wait_160_us ;write 0x03 and wait 160us
  45.  
  46. ldi r16, 0x03
  47. rcall send_LCD
  48. rcall wait_160_us ;write 0x03 and wait 160us
  49.  
  50. ldi r16, 0x02 ;4bit mode
  51. rcall send_LCD
  52. rcall wait_5_ms
  53.  
  54. ldi r16, 1 ;clear display
  55. ldi r17, 1 << LCD_INST
  56. rcall send_LCD
  57. rcall wait_160_us
  58.  
  59. ldi r16, 2 ;return cursor to home position
  60. rcall send_LCD
  61. rcall wait_5_ms
  62.  
  63. ldi r16, 0x20 | 0x08 ;interface legth 4 bit, 2 line display
  64. rcall send_LCD
  65. rcall wait_160_us
  66.  
  67. ldi r16, 8 | 4 | 0 | 0 ;enable display, turn display on, cursor on, blink on
  68. rcall send_LCD
  69. rcall wait_160_us
  70.  
  71. ret
  72.  
  73. update_lcd:
  74. ldi r16, 2 ;return cursor to home position
  75. ldi r17, 1 << LCD_INST
  76. rcall send_LCD
  77. rcall wait_5_ms
  78.  
  79. ldi ZH, high(lcd_display)
  80. ldi ZL, low(lcd_display) ;get pointer to lcd buffer
  81.  
  82. ldi r18, 16 ;send 16 bytes
  83. ldi r17, 0 ;default settings for send_LCD
  84. update_lcd_loop:
  85. ld r16, Z+
  86. rcall send_LCD
  87. rcall wait_160_us
  88. dec r18
  89. brne update_lcd_loop
  90.  
  91. ldi r16, 0x80 | 0x40 ;move cursor to position 0x40
  92. ldi r17, 1 << LCD_INST
  93. rcall send_LCD
  94. rcall wait_160_us
  95.  
  96. ldi r18, 16 ;send 16 bytes
  97. ldi r17, 0 ;default settings for send_LCD
  98. update_lcd_loop2:
  99. ld r16, Z+
  100. rcall send_LCD
  101. rcall wait_160_us
  102. dec r18
  103. brne update_lcd_loop2
  104.  
  105. ret
  106.  
  107. shift_display_screen_2:
  108. ldi r18, 16 ;shift screen 16 times
  109.  
  110. shift_display_screen_2_loop:
  111. ldi r16, 0x18 ;shift screen once to the left
  112. ldi r17, 1 << LCD_INST
  113. rcall send_LCD
  114. rcall wait_160_us
  115.  
  116. dec r18
  117. cpi r18, 0
  118. brne shift_display_screen_2_loop
  119.  
  120. ret
  121.  
  122.  
  123. send_LCD: ;r16 = byte to send, r17 can be NYBBLE or INST, byte and data are default
  124. push r17 ;do not destroy setting, makes multiple sends easier
  125. push r18
  126. push r19
  127.  
  128. in r19, PORT_LCD ;leave the other two bits alone (LED and n/c) by clearing
  129. andi r19, ~((1 << LCD_RS) | (1 << LCD_E) | 0xF0) ;control lines and data bits
  130.  
  131. sbrc r17, LCD_NYBBLE
  132. swap r16
  133.  
  134. send_LCD_loop:
  135. mov r18, r16
  136. andi r18, 0xF0 ;upper nybble
  137. sbrs r17, LCD_INST
  138. sbr r18, 1 << LCD_RS ;set data bit based on r17
  139. or r18, r19 ;restore other two bits (LED and n/c)
  140.  
  141. out PORT_LCD, r18
  142. sbi PORT_LCD, LCD_E
  143. cbi PORT_LCD, LCD_E
  144.  
  145. sbrc r17, LCD_NYBBLE
  146. rjmp send_LCD_end
  147.  
  148. swap r16
  149. sbr r17, 1 << LCD_NYBBLE
  150. rjmp send_LCD_loop ;send the lower nybble if byte mode is set
  151.  
  152. send_LCD_end:
  153. pop r19
  154. pop r18
  155. pop r17
  156. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement