Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.32 KB | None | 0 0
  1. ;
  2. ; Uppgift6_LCD.asm
  3. ;
  4. ; Created: 2017-03-28 15:03:18
  5. ; Author : landf
  6. ;
  7. .include "m328pdef.inc"
  8. .def temp=r16 ;tmp
  9. .def tempinst=r17;
  10.  
  11. .equ fclk = 1000000 ;System clock frequency
  12. .equ EOS = 0 ;End of string
  13. .cseg
  14. .org 0
  15.  
  16.  
  17.  
  18. ;--------------------interface---------------
  19. .equ lcd_D7_bit = PORTC3
  20. .equ lcd_D6_bit = PORTC2
  21. .equ lcd_D5_bit = PORTC1
  22. .equ lcd_D4_bit = PORTC0
  23. .equ lcd_E_bit = PORTC4
  24. .equ lcd_RS_bit = PORTC5
  25.  
  26. ; LCD module information
  27. .equ lcd_LineOne = 0x00 ; start of line 1
  28. .equ lcd_LineTwo = 0x10 ; start of line 2
  29. .equ lcd_LineThree = 0x20 ; start of line 2
  30.  
  31.  
  32.  
  33. ; LCD instructions
  34. ;power control-0b01010000 ; follower-0b01101100 ; constrast-0b01111100 ;function2-0b00101000 ;onoff-0b00001111 ;cleardisplay-0b00000001 ;entrymodeset-0b00000110 ;
  35. .equ lcd_FunctionReset = 0b00110000
  36. .equ lcd_4bitFunctionSet = 0b00100000
  37. .equ lcd_lcd4.2bitFunction = 0b00101000
  38. .equ lcd_BiasSet = 0b00011101
  39. .equ lcd_PowerControl = 0b01010000
  40. .equ lcd_Follower = 0b01101100
  41. .equ lcd_Constrast = 0b01111100
  42. .equ lcd_OnOff = 0b00001111
  43. .equ lcd_ClearDisplay = 0b00000001
  44. .equ lcd_EntryModeSet = 0b00000110
  45. .equ lcd_SetCursor = 0b10000000
  46.  
  47. ;----------INIT-----------------
  48.  
  49.  
  50. rjmp main
  51.  
  52. pogchamp:
  53. .db "pogchamp ",EOS
  54.  
  55. hello:
  56. .db "hello",EOS
  57.  
  58. anotherone:
  59. .db "anotherone ",EOS
  60.  
  61. ;===================================Program Start======================================
  62. main:
  63.  
  64. ; init stack pointer
  65. ldi temp,low(RAMEND)
  66. out SPL,temp
  67. ldi temp,high(RAMEND)
  68. out SPH,temp
  69.  
  70. ; set bit in data pins
  71. sbi DDRC, lcd_D7_bit
  72. sbi DDRC, lcd_D6_bit
  73. sbi DDRC, lcd_D5_bit
  74. sbi DDRC, lcd_D4_bit
  75.  
  76. ; set bit in control pins
  77. sbi DDRC, lcd_E_bit
  78. sbi DDRC, lcd_RS_bit
  79.  
  80. call lcd_init
  81.  
  82. ; display the first line of information
  83. ldi ZH, high(hello) ; point to the information that is to be displayed
  84. ldi ZL, low(hello)
  85. ldi tempinst, lcd_LineOne ; point to where the information should be displayed
  86. call lcd_write_string_4d
  87.  
  88. ldi ZH, high(pogchamp)
  89. ldi ZL, low(pogchamp)
  90. ldi tempinst, lcd_LineTwo
  91. call lcd_write_string_4d
  92.  
  93. ldi ZH, high(anotherone)
  94. ldi ZL, low(anotherone)
  95. ldi tempinst, lcd_LineThree
  96. call lcd_write_string_4d
  97.  
  98.  
  99. ;===================================Program End======================================
  100.  
  101.  
  102. ;-------Subroutines-------------
  103.  
  104. ;===================================Delays Start======================================
  105.  
  106.  
  107. delayYx1mS:
  108. call delay1mS ; delay for 1 mS
  109. sbiw YH:YL, 1 ; update the the delay counter
  110. brne delayYx1mS ; counter is not zero
  111.  
  112. ; arrive here when delay counter is zero (total delay period is finished)
  113. ret
  114.  
  115. ; ---------------------------------------------------------------------------
  116.  
  117.  
  118.  
  119. delayTx1mS:
  120. call delay1mS ; delay for 1 mS
  121. dec temp ; update the delay counter
  122. brne delayTx1mS ; counter is not zero
  123.  
  124. ; arrive here when delay counter is zero (total delay period is finished)
  125. ret
  126.  
  127. ; ---------------------------------------------------------------------------
  128.  
  129. delay1mS:
  130. push YL ; [2] preserve registers
  131. push YH ; [2]
  132. ldi YL, low (((fclk/1000)-18)/4) ; [1] delay counter
  133. ldi YH, high(((fclk/1000)-18)/4) ; [1]
  134.  
  135. delay1mS_01:
  136. sbiw YH:YL, 1 ; [2] update the the delay counter
  137. brne delay1mS_01 ; [2] delay counter is not zero
  138.  
  139. ; arrive here when delay counter is zero
  140. pop YH ; [2] restore registers
  141. pop YL ; [2]
  142. ret ; [4]
  143.  
  144. ; ---------------------------------------------------------------------------
  145.  
  146.  
  147. delayTx1uS:
  148. call delay1uS ; delay for 1 uS
  149. dec temp ; decrement the delay counter
  150. brne delayTx1uS ; counter is not zero
  151.  
  152. ; arrive here when delay counter is zero (total delay period is finished)
  153. ret
  154.  
  155. ; ---------------------------------------------------------------------------
  156.  
  157.  
  158. delay1uS:
  159. push temp ; [2] these instructions do nothing except consume clock cycles
  160. pop temp ; [2]
  161. push temp ; [2]
  162. pop temp ; [2]
  163. ret ; [4]
  164.  
  165. ;===================================Delays End======================================
  166.  
  167. ;===================================Write Start======================================
  168. ;data till hög/låg nibble
  169. lcd_write:
  170.  
  171.  
  172. ; write the data
  173. ;
  174. sbi PORTC, lcd_E_bit ; Enable pin high
  175.  
  176. ; set up D7
  177. sbi PORTC, lcd_D7_bit ; assume that the D7 data is '1'
  178. sbrs tempinst, 7 ; check the actual data value
  179. cbi PORTC, lcd_D7_bit ; arrive here only if the data was actually '0'
  180.  
  181. ; set up D6
  182. sbi PORTC, lcd_D6_bit ; repeat for each data bit
  183. sbrs tempinst, 6
  184. cbi PORTC, lcd_D6_bit
  185.  
  186. ; set up D5
  187. sbi PORTC, lcd_D5_bit
  188. sbrs tempinst, 5
  189. cbi PORTC, lcd_D5_bit
  190.  
  191. ; set up D4
  192. sbi PORTC, lcd_D4_bit
  193. sbrs tempinst, 4
  194. cbi PORTC, lcd_D4_bit
  195.  
  196. cbi PORTC, lcd_E_bit ; Enable pin low data in
  197. ldi temp, 1
  198. call delayTx1uS
  199.  
  200.  
  201. ret
  202.  
  203. ; -------------------------lcd_write_4bitinstruction----------------------------
  204. lcd_write_4binstruction:
  205. cbi PORTC, lcd_RS_bit ; rs low 2,1
  206. cbi PORTC, lcd_E_bit ; e low 2,2
  207. call lcd_write ; high nibble 2,3 2,4
  208. swap tempinst ; swap high and low nibbles
  209. nop
  210. call lcd_write ; low nibble write
  211. ret
  212.  
  213. ;==================================Write End===================================
  214. ;====================================Init Start================================
  215. ;------------------------------lcd_init-----------------------------------
  216. lcd_init:
  217. ; Power-up delay
  218.  
  219. ldi temp, 40 ; initial 40 msec delay
  220. call delayTx1mS
  221. ;
  222. cbi PORTC, lcd_RS_bit ;
  223. cbi PORTC, lcd_E_bit
  224.  
  225. ldi tempinst, lcd_FunctionReset ; 1
  226. call lcd_write
  227. ldi temp, 4 ; >4mSec delay
  228. call delayTx1mS
  229.  
  230.  
  231. ldi tempinst, lcd_FunctionReset ; 2
  232. call lcd_write
  233. ldi temp, 3 ; >40 uS delay
  234. call delayTx1uS
  235.  
  236.  
  237. ldi tempinst, lcd_FunctionReset ; 3
  238. call lcd_write
  239. ldi temp, 3 ; >40 uS delay
  240. call delayTx1uS
  241.  
  242. ldi tempinst, lcd_4bitFunctionSet ; 3
  243. call lcd_write
  244. ldi temp, 3 ; >40 uS delay
  245. call delayTx1uS
  246.  
  247. ; Function Set instruction
  248. ldi tempinst, 0b00101001 ; function set db4 0
  249. call lcd_write_4binstruction
  250. ldi temp, 3 ; 40 uS delay
  251. call delayTx1uS
  252.  
  253. ldi tempinst, 0b00011101 ; biasset
  254. call lcd_write_4binstruction
  255. ldi temp, 3 ; 40 uS delay
  256. call delayTx1uS
  257.  
  258. ldi tempinst, 0b01010000 ; power control
  259. call lcd_write_4binstruction
  260. ldi temp, 3 ; 40 uS delay
  261. call delayTx1uS
  262.  
  263. ldi tempinst, 0b01101100 ; follower control
  264. call lcd_write_4binstruction
  265. ldi temp, 3 ; 40 uS delay
  266. call delayTx1uS
  267.  
  268. ldi tempinst, 0b01111100 ; constrast
  269. call lcd_write_4binstruction
  270. ldi temp, 3 ; 40 uS delay
  271. call delayTx1uS
  272.  
  273.  
  274. ldi tempinst, 0b00101000 ; function2
  275. call lcd_write_4binstruction
  276. ldi temp, 3 ; 40 uS delay (min)
  277. call delayTx1uS
  278.  
  279. ldi tempinst, 0b00001111 ; onoff
  280. call lcd_write_4binstruction
  281. ldi temp, 3 ; 40 uS delay (min)
  282. call delayTx1uS
  283.  
  284. ldi tempinst, 0b00000001 ; cleardisplay
  285. call lcd_write_4binstruction
  286. ldi temp, 3 ; 40 uS delay (min)
  287. call delayTx1uS
  288.  
  289. ldi tempinst, 0b00000110 ; entrymodeset
  290. call lcd_write_4binstruction
  291. ldi temp, 3 ; 40 uS delay (min)
  292. call delayTx1uS
  293.  
  294.  
  295.  
  296. ret
  297.  
  298. ;====================================Init End================================
  299.  
  300. ;====================================OutString Start=========================
  301.  
  302.  
  303. lcd_write_string_4d:
  304. ; preserve registers
  305. push ZH ; preserve pointer registers
  306. push ZL
  307.  
  308. ; fix up the pointers for use with the 'lpm' instruction
  309. lsl ZL ; shift the pointer one bit left for the lpm instruction
  310. rol ZH
  311.  
  312. ; set up the initial DDRAM address
  313. ori tempinst, lcd_SetCursor ; convert the plain address to a set cursor instruction
  314. call lcd_write_4binstruction ; set up the first DDRAM address
  315. ldi temp, 80 ; 40 uS delay (min)
  316. call delayTx1uS
  317.  
  318. ; write the string of characters
  319. lcd_write_string_4d_01:
  320. lpm tempinst, Z+ ; get a character
  321. cpi tempinst, 0 ; check for end of string
  322. breq lcd_write_string_4d_02 ; done
  323.  
  324. ; arrive here if this is a valid character
  325. call lcd_write_character_4d ; display the character
  326. ldi temp, 80 ; 40 uS delay (min)
  327. call delayTx1uS
  328. rjmp lcd_write_string_4d_01 ; not done, send another character
  329.  
  330. ; arrive here when all characters in the message have been sent to the LCD module
  331. lcd_write_string_4d_02:
  332. pop ZL ; restore pointer registers
  333. pop ZH
  334. ret
  335.  
  336.  
  337.  
  338. lcd_write_character_4d:
  339. sbi PORTC, lcd_RS_bit ; select the Data Register (RS high)
  340. cbi PORTC, lcd_E_bit ; make sure E is initially low
  341. call lcd_write ; write the upper 4-bits of the data
  342. swap tempinst ; swap high and low nibbles
  343. call lcd_write ; write the lower 4-bits of the data
  344. ret
  345.  
  346.  
  347.  
  348. ;====================================OutString End==============================
  349.  
  350. stop: ;Finished.
  351. rjmp stop
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362. rjmp stop
  363.  
  364. ;Port C: 0,1,2,3,4,5,6,7
  365. ; 0 =
  366. ;PC0 = D4
  367. ;PC1 = D5
  368. ;PC2 = D6
  369. ;PC3 = D7
  370. ;PC4 = Enable
  371. ;PC5 = RS
  372.  
  373. ; -- rw går till jord
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement