Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 14.06.2025

Просмотров: 8616

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

330

Chapter 13

movlw

d’3’

; Offset into buffer

call

storeMSU

; Set DDRAM address to start of first line

call

line1

; Call procedure to display 16 characters in LCD

call

display16

;========================

;

second LCD line

;========================

call

delay_5

; Wait for termination

call

blank16

; Blank buffer

;Call procedure to store ASCII characters for message

;in text buffer

movlw

d’1’

; Offset into buffer

call

storeUniv

call

line2

; DDRAM address of LCD line 2

call

display16

;=======================

;

done!

;=======================

loopHere:

goto

loopHere

;done

;============================================================ ; initialize LCD for 4-bit mode ;============================================================ initLCD:

;Initialization for Densitron LCD module as follows:

;4-bit interface

;2 display lines of 16 characters each

;cursor on

;left-to-right increment

;cursor shift right

;no display shift

;=======================|

;

set command mode

|

;=======================|

bcf

PORTA,E_line

; E line low

bcf

PORTA,RS_line

; RS line low

bcf

PORTA,RW_line

; Write mode

call

delay_125

; delay 125

microseconds

;***********************|

;

FUNCTION SET

|

;***********************|

movlw

0x28

; 0 0 1 0 1 0 0 0 (FUNCTION SET)

;

| | | |__ font select:

;

| | |

1 = 5x10 in 1/8 or 1/11


LCD Interfacing and Programming

331

;

| | |

0 = 1/16 dc

;

| | |___ Duty cycle select

;

| |

0 = 1/8 or 1/11

;

| |

1 = 1/16

;

| |___ Interface width

;

|

0 = 4 bits

;

|

1 = 8 bits

;

|___ FUNCTION SET COMMAND

call

send8

; 4-bit send routine

; Set 4-bit mode command must be repeated

movlw

0x28

call

send8

;***********************|

; DISPLAY AND CURSOR ON |

;***********************|

movlw

0x0e

; 0 0 0 0 1 1 1 0 (DISPLAY ON/OFF)

;

| | | |___ Blink character

;

| | |

1 = on, 0 = off

;

| | |___ Cursor on/off

;

| |

1 = on, 0 = off

;

| |____ Display on/off

;

|

1 = on, 0 = off

;

|____ COMMAND BIT

call

send8

;***********************|

;

set entry mode

|

;***********************|

movlw

0x06

; 0 0 0 0 0 1 1 0 (ENTRY MODE SET)

;

| | |___ display shift

;

| |

1 = shift

;

| |

0 = no shift

;

| |____ increment mode

;

|

1 = left-to-right

;

|

0 = right-to-left

;

|___ COMMAND BIT

call

send8

;***********************|

; cursor/display shift

|

;***********************|

movlw

0x14

; 0 0 0 1 0 1 0 0 (CURSOR/DISPLAY

;

| | | | | SHIFT)

;

| | | |_|___ don’t care

;

| |_|__ cursor/display shift

;

|

00 = cursor shift left

;

|

01 = cursor shift right

;

|

10 = cursor and display


332

Chapter 13

;

|

shifted left

;

|

11 = cursor and display

;

|

shifted right

;

|___ COMMAND BIT

call

send8

;***********************|

;

clear display

|

;***********************|

movlw

0x01

; 0 0 0 0

0 0 0 1 (CLEAR DISPLAY)

;

|___ COMMAND BIT

call

send8

; Per documentation

call

delay_5

; Test for busy

return

;=======================

;Procedure to delay

;42 microseconds ;======================= delay_125

movlw

D’42’

; Repeat 42 machine cycles

movwf

count1

; Store value in counter

repeat

decfsz

count1,f

; Decrement counter

goto

repeat

; Continue if not 0

return

; End of delay

;=======================

;Procedure to delay

;5 milliseconds ;======================= delay_5:

movlw

D’41’

; Counter = 41

movwf

count2

; Store in variable

delay:

call

delay_125

; Delay

decfsz

count2,f

; 40 times = 5 milliseconds

goto

delay

return

; End of delay

;========================

;

pulse E line

;========================

pulseE

bsf

PORTA,E_line

; Pulse E line

nop

bcf

PORTA,E_line

return


LCD Interfacing and Programming

333

;=============================

;long delay sub-routine

;(for debugging) ;============================= long_delay:

movlw

D’200’

; w = 200 decimal

movwf

J

; J = w

jloop:

movwf

K

; K = w

kloop:

decfsz

K,f

; K = K-1, skip next if zero

goto

kloop

decfsz

J,f

; J = J-1, skip next if zero

goto

jloop

return

;=============================

;LCD display procedure ;=============================

;Sends 16 characters from PIC buffer with address stored

;in variable pic_ad to LCD line previously selected display16

call

delay_5

; Make sure not busy

; Set up for data

bcf

PORTA,E_line

; E line low

bsf

PORTA,RS_line

; RS line high for data

; Set up counter for 16 characters

movlw

D’16’

; Counter = 16

movwf

count3

; Get display address from local variable pic_ad

movf

pic_ad,w ; First display RAM address to W

movwf

FSR

; W to FSR

getchar:

movf

INDF,w

; get character from display RAM

; location pointed to by file select

; register

call

send8

; 4-bit interface routine

; Test for 16 characters displayed

decfsz

count3,f

; Decrement counter

goto

nextchar

; Skipped if done

return

nextchar:

incf

FSR,f

; Bump pointer

goto

getchar

;========================

;send 2 nibbles in

;4-bit mode ;========================


334

Chapter 13

;Procedure to send two 4-bit values to port B lines

;7, 6, 5, and 4. High-order nibble is sent first

;ON ENTRY:

;w register holds 8-bit value to send

send8:

movwf

store1

; Save

original value

call

merge4

; Merge with port B

; Now w has merged byte

movwf

PORTB

; w to

port B

call

pulseE

; Send

data to LCD

; High nibble is sent

movf

store1,w

; Recover byte into w

swapf

store1,w

; Swap

nibbles in w

call

merge4

movwf

PORTB

call

pulseE

; Send

data to LCD

call

delay_125

return

;=================

;merge bits ;=================

;Routine to merge the 4 high-order bits of the

;value to send with the contents of port B

;so as to preserve the 4 low-bits in port B

;Logic:

;AND value with 1111 0000 mask

;AND port B with 0000 1111 mask

;Now low nibble in value and high nibble in

;port B are all 0 bits:

;value = vvvv 0000

;port B = 0000 bbbb

;OR value and port B resulting in:

;

vvvv bbbb

;ON ENTRY:

;w contain value bits

;ON EXIT:

;w contains merged bits

merge4:

andlw

b’11110000’

; ANDing

with

0 clears the

; bit. ANDing

with 1 preserves

; the original value

movwf

store2

;

Save

result

in variable

movf

PORTB,w

;

port

B

to w

register

andlw

b’00001111’

Clear high nibble

in port b

; and preserve low

nibble

iorwf

store2,w ; OR two operands in w

return