LCD a PIC

Jiri Prachar Jiri.Prachar
Středa Březen 17 11:41:56 CET 2004


zdravim
taky sem mel docela problemy s rozchazenim displeje
bylo nutne dodrzet casovani
jinak je mozne kontrolovat busy flag (ale je to asi zbytecna 
komplikace)
jestli vam to pomuze tak tady je trocha kodu pro 2051 atmela
(jsou tam i casy)
Jirka
;
; 	LCD interface for AT89C2051 in 4 bits mode
; dodo@jnc.cz
;

$mod2051

RESET   CODE  000H  ; RESET ADDRESS
TIMER0  CODE  00BH  ; TIMER 0 INTERRUPT VECTOR
TIMER1  CODE  01BH  ; TIMER 1 INTERRUPT VECTOR
EXTERN0 CODE  003H  ; EXTERNAL INTERRUPT 0
EXTERN1 CODE  013H  ; EXTERNAL INTERRUPT 1
SERIAL  CODE  023H  ; SERIAL LINE INTERRUPT VECTOR

enable          equ p1.3
reg_sel         equ p1.2
ledka		equ p3.7

                org RESET
		jmp main

		org 80h
init_lcd:
		clr enable
		clr reg_sel

		mov @R0,#01h	;???neprime  adresovani pro xchd

		mov a, #150	;delay 15ms
		acall wait

                mov a,#00110000b	; init 1
                acall wr_ctrl
		mov a, #41	;delay 4,1ms
		acall wait

                mov a,#00110000b	; init 2
                acall wr_ctrl
		mov a, #1	;delay 100us
		acall wait

                mov a,#00110000b	; init 3
                acall wr_ctrl
		mov a, #41	;delay 4,1ms
		acall wait

                mov a,#00100000b	; set 4-bit operation
                acall wr_ctrl		; from this point we are in 4 bit mode

                mov a,#00101000b	; 4 bits, 2 lines, 5x8 dot matrix
                acall wr_ctrl

                mov a,#00000110b	; increment cursor
                acall wr_ctrl

                mov a,#00001111b	; display on, cursor on, blink on
                acall wr_ctrl

clear_lcd:	mov a,#01h		; clear display
		acall wr_ctrl

		mov a,#02h		; home cursor
		acall wr_ctrl

		ret


wr_ctrl:        clr reg_sel		; select control register
		acall wr_common		; write setup info to register
                ret

wr_data:  	setb reg_sel		; select data register
		acall wr_common		; write data to LCD
                ret

wr_common:	mov @R0,p1		; nacte momentalni stav p1
		xchd a,@R0		; prohodi spodek(4bity) A a p1
		mov p1,a		; a zapis privniho niblu
                setb enable		; strobe enable high
                clr enable		; ... and then bring it low

		xchd a,@R0		; navrat spatky
		swap a			; prohozeni spodku a vrsku A
		xchd a,@R0		; zase prohozeni niblu A a p1
		mov p1,a		; zapis druheho niblu
                setb enable		; strobe enable high
                clr enable		; ... and then bring it low

		acall wait_40u		;wait a while
                ret


outstr:     	clr a
            	movc a,@a+dptr		; get character
            	jz exit			; stop if char == null
            	acall wr_data		; else write it
            	inc dptr		; point to next char
            	sjmp outstr
exit:       	ret

;40 microsecond delay, assumes 12MHz clock
;12MHz/(12clocks/instr)=1 machine cycle per microsecond
;24MHz  40us=35 DJNZ instr (2*0,5 usec) +10 cy.
;12MHz  40us=15 DJNZ instr (2*1 usec) +10 cy.
;9,6MHz 40us=11 DJNZ instr (2*1,250 usec) +10 cy.

WAIT_40U:
	PUSH	1		;save register R1 
	MOV	R1, #35		;20 DJNZ instr = 40us
	DJNZ	R1, $
	nop
	POP	1 		;restore register R1 
	RET

; delay
; ACC*100us delay, assumes 12MHz clock 
;24MHz  100us=99 DJNZ 
;12MHz  100us=49 DJNZ 
;9,6MHz 100us=39 DJNZ 
WAIT:
	PUSH	1     		;save register R1 

WAITLP:	MOV	R1, #39     	;^  DJNZ instr = 100us 
	DJNZ	R1, $		;(2)
	DJNZ	ACC, WAITLP	;(2)

	POP	1		;restore register R1 (2)
	RET

main:
	clr ledka		;zhasni LED
	acall init_lcd

	mov dptr,#prompt	; write a string 
	acall outstr

	setb ledka
	sjmp $


prompt:    	db 'HA Im alive!! > ',0


	end
--------------------------------------------
  dodo@jnc.cz
--------------------------------------------





Další informace o konferenci Hw-list