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

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

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

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

Добавлен: 14.06.2025

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

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

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

426

Chapter 14

;

|

|

|

|_____ port B4

|

;

|

|

|_________

port

B5

|—- COLUMNS = INPUTS

;

|

|_____________

port

B6

|

;|_________________ port B7 |

;Switches are connected to Port-B lines

;Clear scan code register

clrf scanCode ;============================

;scan keypad and display ;============================ keyScan:

;Port-B, lines are wired to pushbutton switches, as follows:

;7 6 5 4 3 2 1 0

;| | | | |_|_|_|_____ switch rows (output)

;|_|_|_|_____________ switch columns (input)

;Keypad processing:

;switch rows are successively grounded (row = 0)

;Then column values are tested. If a column returns 0

;in a 0 row, that switch is down.

;Initialize row code addend

clrf

rowCode

; First row is code 0

clrf

newScan

; No new scan code detected

; Initialize row count

movlw

D’4’

; Four rows

movwf

rowCount

; Register variable

movlw

b’11111110’

; All set but LOB

movwf

rowMask

keyLoop:

;Initialize row eliminator mask:

;The row mask is ANDed with the key mask to successively

;mask-off each row, for example:

;

;

|———- row 3

;

||——— row 2

;

|||——- row 1

;

||||—— row 0

;

0000

1111

<= key

mask

;

AND

1111

1101

<= mask for row 1

;

————-

;

0000

1101

<= row

1 is masked off

;

;The row mask, which is initally 1111 1110, is rotated left

;through the carry in order to mask off the next row

movlw

b’00001111’

;

Mask off

all lines

movwf

keyMask

;

To local

register

; Set row mask for current row

movf

rowMask,w

;

Mask to w

andwf

keyMask,f

;

Update key mask


Communications

427

movf

keyMask,w

; Key mask to w

movwf

PORTB

; Mask-off Port-B lines

; Read Port-B lines 4 to 7 (columns are input)

btfss

PORTB,4

call

col0

; Key column procedures

btfss

PORTB,5

call

col1

btfss

PORTB,6

call

col2

btfss

PORTB,7

call

col3

; Index to next row by adding 4 to row code

movf

rowCode,w

; Code to w

addlw

D’4’

movwf

rowCode

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

;shift row mask ;=========================

;Set the carry flag

bsf

STATUS,C

rlf

rowMask,f

; Rotate mask bits in storage

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

;end of keypad? ;=========================

;Test for last key row (maximum count is 4) decfsz rowCount,f ; Decrement counter goto keyLoop

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

;============================================================ ; display, send, and receive data ;============================================================ ;============================================================

;At this point all keys have been tested.

;Variable newScan = 0 if no new scan code detected, else

;variable scanCode holds scan code

movf

newScan,f

; Copy onto intsef (sets Z

; flag)

btfsc

STATUS,Z ; Is it zero

goto

receive

; At this point a new scan code is detected

movf

scanCode,w

; To w

;If scan code is in the range 0 to 9, that is, a decimal

;digit, then ASCII conversion consists of adding 0x30.

;If the scan code represents one of the hex letters

;(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)


428

Chapter 14

;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

goto

scanExit

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

;receive serial data ;========================== receive:

;Call serial receive procedure

call SerialRcv

;HOB of newData register is set if new data

;received

btfss

newData,7

goto

scanExit

; At this point new data was received

call

send8

; Display in LCD

call

LCDscroll

; Scroll at end of line

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 (clearly not

necessary)

movwf

scanCode ; Final value

incf

newScan,f

; New scan code


Communications

429

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

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

|


430

Chapter 14

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

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

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)