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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

737

addlw

0x02

; Add 2

movwf

scanCode

incf

newScan,f

return

col3:

movf

rowCode,w

; Row code to w

addlw

0x03

; Add 3

movwf

scanCode

incf

newScan,f

return

;============================================================ ; 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

porte,E_line

; E line low

bcf

porte,RS_line

; RS line low

bcf

porte,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

;

|

| |

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


738

Appendix D

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

;

| |____ cursor increment

;

|

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

;

|

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


Supplementary Programs

739

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'42'

; 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

porte,E_line

; Pulse E line

Nop

bcf

porte,E_line

return

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

;long delay sub-routine

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

movlw

D'200'

; w delay count

movwf

J

; J = w

jloop:

movwf

K

;

K

=

w

kloop:

decfsz

K,f

;

K

=

K-1, skip next if zero

goto

kloop


740

Appendix D

decfsz

J,f

; J = J-1, skip next if zero

goto

jloop

return

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

;send 2 nibbles in

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

;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

portd

; w to port D

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

portd

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


Supplementary Programs

741

; the original value

movwf

store2

; Save result in variable

movf

portd,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

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

;Set address register

;to LCD line 1 ;========================

;ON ENTRY:

;Address of LCD line 2 in constant LCD_2

line1:

bcf

porte,E_line

; E line low

bcf

porte,RS_line

; RS line low, setup for

; control

call

delay_5

; Busy?

; Set to second display line

movlw

LCD_1

; Address with

high-bit set

call

send8

; Set RS line for data

bsf

porte,RS_line

; RS = 1 for data

call

delay_5

; Busy?

return

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

;Set address register

;to LCD line 2 ;========================

;ON ENTRY:

;Address of LCD line 2 in constant LCD_2

line2:

bcf

porte,E_line

; E line low

bcf

porte,RS_line

; RS line low, setup for

; control

call

delay_5

; Busy?

; Set to second display line

movlw

LCD_2

; Address with high-bit set

call

send8

; Set RS line for data

bsf

porte,RS_line

; RS = 1 for data

call

delay_5

; Busy?

return

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