Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;
- ; Uppgift6_LCD.asm
- ;
- ; Created: 2017-03-28 15:03:18
- ; Author : landf
- ;
- .include "m328pdef.inc"
- .def temp=r16 ;tmp
- .def tempinst=r17;
- .equ fclk = 1000000 ;System clock frequency
- .equ EOS = 0 ;End of string
- .cseg
- .org 0
- ;--------------------interface---------------
- .equ lcd_D7_bit = PORTC3
- .equ lcd_D6_bit = PORTC2
- .equ lcd_D5_bit = PORTC1
- .equ lcd_D4_bit = PORTC0
- .equ lcd_E_bit = PORTC4
- .equ lcd_RS_bit = PORTC5
- ; LCD module information
- .equ lcd_LineOne = 0x00 ; start of line 1
- .equ lcd_LineTwo = 0x10 ; start of line 2
- .equ lcd_LineThree = 0x20 ; start of line 2
- ; LCD instructions
- ;power control-0b01010000 ; follower-0b01101100 ; constrast-0b01111100 ;function2-0b00101000 ;onoff-0b00001111 ;cleardisplay-0b00000001 ;entrymodeset-0b00000110 ;
- .equ lcd_FunctionReset = 0b00110000
- .equ lcd_4bitFunctionSet = 0b00100000
- .equ lcd_lcd4.2bitFunction = 0b00101000
- .equ lcd_BiasSet = 0b00011101
- .equ lcd_PowerControl = 0b01010000
- .equ lcd_Follower = 0b01101100
- .equ lcd_Constrast = 0b01111100
- .equ lcd_OnOff = 0b00001111
- .equ lcd_ClearDisplay = 0b00000001
- .equ lcd_EntryModeSet = 0b00000110
- .equ lcd_SetCursor = 0b10000000
- ;----------INIT-----------------
- rjmp main
- pogchamp:
- .db "pogchamp ",EOS
- hello:
- .db "hello",EOS
- anotherone:
- .db "anotherone ",EOS
- ;===================================Program Start======================================
- main:
- ; init stack pointer
- ldi temp,low(RAMEND)
- out SPL,temp
- ldi temp,high(RAMEND)
- out SPH,temp
- ; set bit in data pins
- sbi DDRC, lcd_D7_bit
- sbi DDRC, lcd_D6_bit
- sbi DDRC, lcd_D5_bit
- sbi DDRC, lcd_D4_bit
- ; set bit in control pins
- sbi DDRC, lcd_E_bit
- sbi DDRC, lcd_RS_bit
- call lcd_init
- ; display the first line of information
- ldi ZH, high(hello) ; point to the information that is to be displayed
- ldi ZL, low(hello)
- ldi tempinst, lcd_LineOne ; point to where the information should be displayed
- call lcd_write_string_4d
- ldi ZH, high(pogchamp)
- ldi ZL, low(pogchamp)
- ldi tempinst, lcd_LineTwo
- call lcd_write_string_4d
- ldi ZH, high(anotherone)
- ldi ZL, low(anotherone)
- ldi tempinst, lcd_LineThree
- call lcd_write_string_4d
- ;===================================Program End======================================
- ;-------Subroutines-------------
- ;===================================Delays Start======================================
- delayYx1mS:
- call delay1mS ; delay for 1 mS
- sbiw YH:YL, 1 ; update the the delay counter
- brne delayYx1mS ; counter is not zero
- ; arrive here when delay counter is zero (total delay period is finished)
- ret
- ; ---------------------------------------------------------------------------
- delayTx1mS:
- call delay1mS ; delay for 1 mS
- dec temp ; update the delay counter
- brne delayTx1mS ; counter is not zero
- ; arrive here when delay counter is zero (total delay period is finished)
- ret
- ; ---------------------------------------------------------------------------
- delay1mS:
- push YL ; [2] preserve registers
- push YH ; [2]
- ldi YL, low (((fclk/1000)-18)/4) ; [1] delay counter
- ldi YH, high(((fclk/1000)-18)/4) ; [1]
- delay1mS_01:
- sbiw YH:YL, 1 ; [2] update the the delay counter
- brne delay1mS_01 ; [2] delay counter is not zero
- ; arrive here when delay counter is zero
- pop YH ; [2] restore registers
- pop YL ; [2]
- ret ; [4]
- ; ---------------------------------------------------------------------------
- delayTx1uS:
- call delay1uS ; delay for 1 uS
- dec temp ; decrement the delay counter
- brne delayTx1uS ; counter is not zero
- ; arrive here when delay counter is zero (total delay period is finished)
- ret
- ; ---------------------------------------------------------------------------
- delay1uS:
- push temp ; [2] these instructions do nothing except consume clock cycles
- pop temp ; [2]
- push temp ; [2]
- pop temp ; [2]
- ret ; [4]
- ;===================================Delays End======================================
- ;===================================Write Start======================================
- ;data till hög/låg nibble
- lcd_write:
- ; write the data
- ;
- sbi PORTC, lcd_E_bit ; Enable pin high
- ; set up D7
- sbi PORTC, lcd_D7_bit ; assume that the D7 data is '1'
- sbrs tempinst, 7 ; check the actual data value
- cbi PORTC, lcd_D7_bit ; arrive here only if the data was actually '0'
- ; set up D6
- sbi PORTC, lcd_D6_bit ; repeat for each data bit
- sbrs tempinst, 6
- cbi PORTC, lcd_D6_bit
- ; set up D5
- sbi PORTC, lcd_D5_bit
- sbrs tempinst, 5
- cbi PORTC, lcd_D5_bit
- ; set up D4
- sbi PORTC, lcd_D4_bit
- sbrs tempinst, 4
- cbi PORTC, lcd_D4_bit
- cbi PORTC, lcd_E_bit ; Enable pin low data in
- ldi temp, 1
- call delayTx1uS
- ret
- ; -------------------------lcd_write_4bitinstruction----------------------------
- lcd_write_4binstruction:
- cbi PORTC, lcd_RS_bit ; rs low 2,1
- cbi PORTC, lcd_E_bit ; e low 2,2
- call lcd_write ; high nibble 2,3 2,4
- swap tempinst ; swap high and low nibbles
- nop
- call lcd_write ; low nibble write
- ret
- ;==================================Write End===================================
- ;====================================Init Start================================
- ;------------------------------lcd_init-----------------------------------
- lcd_init:
- ; Power-up delay
- ldi temp, 40 ; initial 40 msec delay
- call delayTx1mS
- ;
- cbi PORTC, lcd_RS_bit ;
- cbi PORTC, lcd_E_bit
- ldi tempinst, lcd_FunctionReset ; 1
- call lcd_write
- ldi temp, 4 ; >4mSec delay
- call delayTx1mS
- ldi tempinst, lcd_FunctionReset ; 2
- call lcd_write
- ldi temp, 3 ; >40 uS delay
- call delayTx1uS
- ldi tempinst, lcd_FunctionReset ; 3
- call lcd_write
- ldi temp, 3 ; >40 uS delay
- call delayTx1uS
- ldi tempinst, lcd_4bitFunctionSet ; 3
- call lcd_write
- ldi temp, 3 ; >40 uS delay
- call delayTx1uS
- ; Function Set instruction
- ldi tempinst, 0b00101001 ; function set db4 0
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay
- call delayTx1uS
- ldi tempinst, 0b00011101 ; biasset
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay
- call delayTx1uS
- ldi tempinst, 0b01010000 ; power control
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay
- call delayTx1uS
- ldi tempinst, 0b01101100 ; follower control
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay
- call delayTx1uS
- ldi tempinst, 0b01111100 ; constrast
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay
- call delayTx1uS
- ldi tempinst, 0b00101000 ; function2
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay (min)
- call delayTx1uS
- ldi tempinst, 0b00001111 ; onoff
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay (min)
- call delayTx1uS
- ldi tempinst, 0b00000001 ; cleardisplay
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay (min)
- call delayTx1uS
- ldi tempinst, 0b00000110 ; entrymodeset
- call lcd_write_4binstruction
- ldi temp, 3 ; 40 uS delay (min)
- call delayTx1uS
- ret
- ;====================================Init End================================
- ;====================================OutString Start=========================
- lcd_write_string_4d:
- ; preserve registers
- push ZH ; preserve pointer registers
- push ZL
- ; fix up the pointers for use with the 'lpm' instruction
- lsl ZL ; shift the pointer one bit left for the lpm instruction
- rol ZH
- ; set up the initial DDRAM address
- ori tempinst, lcd_SetCursor ; convert the plain address to a set cursor instruction
- call lcd_write_4binstruction ; set up the first DDRAM address
- ldi temp, 80 ; 40 uS delay (min)
- call delayTx1uS
- ; write the string of characters
- lcd_write_string_4d_01:
- lpm tempinst, Z+ ; get a character
- cpi tempinst, 0 ; check for end of string
- breq lcd_write_string_4d_02 ; done
- ; arrive here if this is a valid character
- call lcd_write_character_4d ; display the character
- ldi temp, 80 ; 40 uS delay (min)
- call delayTx1uS
- rjmp lcd_write_string_4d_01 ; not done, send another character
- ; arrive here when all characters in the message have been sent to the LCD module
- lcd_write_string_4d_02:
- pop ZL ; restore pointer registers
- pop ZH
- ret
- lcd_write_character_4d:
- sbi PORTC, lcd_RS_bit ; select the Data Register (RS high)
- cbi PORTC, lcd_E_bit ; make sure E is initially low
- call lcd_write ; write the upper 4-bits of the data
- swap tempinst ; swap high and low nibbles
- call lcd_write ; write the lower 4-bits of the data
- ret
- ;====================================OutString End==============================
- stop: ;Finished.
- rjmp stop
- rjmp stop
- ;Port C: 0,1,2,3,4,5,6,7
- ; 0 =
- ;PC0 = D4
- ;PC1 = D5
- ;PC2 = D6
- ;PC3 = D7
- ;PC4 = Enable
- ;PC5 = RS
- ; -- rw går till jord
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement