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

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

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

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

Добавлен: 14.06.2025

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

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

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

446

Chapter 14

;(0xa to 0xf) then ASCII conversion requires adding

;0x37

sublw

0x09

; 9 - w

;if w from 0 to 9 then 9 - w = positive (C flag = 1)

;if w = 0xa then 9 - 10 = -1 (C flag = 0)

;if w = 0xc then 9 - 12 = -2 (C flag = 0)

btfss

STATUS,C ; Test carry flag

goto

hexLetter

; Carry clear, must be a

letter

;At this point scan code is a decimal digit in the

;range 0 to 9. Convert to ASCII by adding 0x30

movf

scanCode,w

; Recover scan code

addlw

0x30

; Convert to ASCII

goto

displayDig

hexLetter:

movf

scanCode,w

; Recover scan code

addlw

0x37

; Convert to ASCII

displayDig:

; Store so it can be sent

movwf

ascVal

call

send8

; Display routine

call

LCDscroll

call

long_delay

; Debounce

; Recover ASCII

movf

ascVal,w

call

SerialSend

ScanExit:

goto

keyScan

; Continue

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

;calculate scan code ;==========================

;The column position is added to the row code (stored

;in rowCode register). Sum is the scan code

col0:

movf

rowCode,w

; Row code to w

addlw

0x00

; Add 0

movwf

scanCode

; Final value

incf

newScan,f

; New scan code

return

col1:

movf

rowCode,w

; Row code to w

addlw

0x01

; Add 1

movwf

scanCode

incf

newScan,f

return

col2:

movf

rowCode,w

; Row code to w


Communications

447

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

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

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

;

L O C A L

P R O C E D U R E S

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

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

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

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


448

Chapter 14

;

|___ 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

;

|

shifted left

;

|

11 = cursor and display

;

|

shifted right

;

|___ COMMAND BIT

call send8 ;***********************|


Communications

449

;

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’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

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

long_delay

movlw

D’200’

; w delay count

movwf

J

; J = w


450

Chapter 14

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

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

;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 contains value bits