SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By tlogic
#101024
After a lot of hours... testing and writing code i did my LCD work! Because it was difficult for me to get it work, i thought that it would be a good idea, to share my code with you, who maybe trying to do the same think with me. Interface a HD44780 to an ATtiny2313. The source code is writen in assembly, it is tested and it works!. Copy it and test it yourself!

I use all PORTB pins for data bus and PORTD pins 4, 5, 6 for E, RS, RW
Code: Select all
.NOLIST ; Don't list the following in the list file
.INCLUDE "tn2313def.inc" ; Import of the file
.LIST ; Switch list on again

.equ	 E=4
.equ	RS=5
.equ	RW=6

.def	temp1= r16
.def	temp2= r17

.cseg
.org	0
rjmp	RESET

rjmp	RESET
rjmp	RESET

RESET:
	;Init Stack Pointer
	ldi	r16, low(RAMEND)
	out	spl, temp1

	;Configure Pins
	ldi temp1,0xFF
	out DDRB, temp1

	ldi	temp1,0x71
	out	DDRD, temp1

	;Wait for LCD to boot
	rcall	wait_LCD

	;Configure LCD
	rcall	init_LCD

       ;Write Char A to LCD	
       ldi     temp1,'A'
	rcall	write_LCD

	;Main program
	main:
               nop
	rjmp	main

	;***********************************************
	;**************** Initialize LCD ***************
	;***********************************************
	init_LCD:
		
		ldi	temp1,0b00111000	       ;8-bit, 2-lines, 5x8 dots
		rcall	command_LCD

		ldi	temp1,0b00001111 	;Display on, show Cursor, blink
		rcall	command_LCD

		ldi	temp1,0b00000110 	;cursor increament, No display shift 
		rcall	command_LCD

		ldi	temp1,0x01	                ;Return Home
		rcall	command_LCD
	ret

	;***********************************************
	;*************** Commands to LCD *************
	;***********************************************
	command_LCD:

		sbi	PORTD,E		;Set E

		cbi	PORTD,RW	;clear RW bit for write mode
		cbi	PORTD,RS	        ;clear RS bit for command mode

		out	PORTB,temp1
		
		cbi	PORTD,E

		rcall	wait_LCD
	ret


	;***********************************************
	;***************** Write to LCD ****************
	;***********************************************
	write_LCD:

		sbi	PORTD,E		;Set E

		cbi	PORTD,RW	;Clear RW bit for writing
		sbi	PORTD,RS  	;Set RS bit for data

		out PORTB,temp1	;Put data to data bus

		cbi	PORTD,E

		rcall	wait_LCD
	ret


	;***********************************************
	;***************** Wait for LCD ****************
	;***********************************************
	wait_LCD:
	

		sbi	PORTD,RW	;turn in read mode busy flag
		cbi	PORTD,RS

		ldi	temp1,0x00	;turn PortB input
		out	DDRB,r16
		
		sbi	PORTD,E    	;take E high	
		nop
		in	temp2,PINB	;get address and busy flag

		cbi	PORTD,E

		ldi	temp1,0xFF	;turn PortB output again
		out	DDRB,r16
		
		cbi	PORTD,RW

		sbrc temp2,7
		rjmp wait_LCD

		sbi	PORTD,E

	ret