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

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

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

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

Добавлен: 14.06.2025

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

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

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

732

Appendix D

bcf

STATUS,RP1

ENDM

Bank1

MACRO

; Select RAM bank 1

bsf

STATUS,RP0

bcf

STATUS,RP1

ENDM

Bank2

MACRO

; Select RAM bank 2

bcf

STATUS,RP0

bsf

STATUS,RP1

ENDM

Bank3

MACRO

; Select RAM bank 3

bsf

STATUS,RP0

bsf

STATUS,RP1

ENDM

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

; M A I N P R O G R A M

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

;Wiring:

;LCD data to port D, lines 0 to 7

;E line -> port E, 1

;RW line -> port E, 2

;RS line -> port E, 0

;Set porte D and E for output

;First, initialize port B by clearing latches clrf STATUS

clrf PORTB

;Select bank 1 to tris port D for output Bank1

;Tris port D for output. Port D lines 4 to 7 are wired

;to LCD data lines. Port D lines 0 to 4 are wired to LEDs. movlw B'00000000'

movwf

TRISD

; and port D

;By default port A lines are analog. To configure them

;as digital code must set bits 1 and 2 of the ADCON1

;register (in bank 1)

movlw

0x06

; binary 0000 0110 is code to

; make all

port A

lines digital

movwf

ADCON1

; Port

B, lines are wired to keypad switches, as follows:


Supplementary Programs

733

;7 6 5 4 3 2 1 0

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

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

;rows must be defined as output and columns as input

movlw

b'11110000'

movwf

TRISB

; Tris

port E for output

movlw

B'00000000'

movwf

TRISE

; Tris port E

; Enable port B pullups for switches in OPTION register

;

7

6

5

4

3

2 1 0 <= OPTION bits

;

|

|

|

|

|

|__|__|_____

PS2-PS0 (prescaler bits)

;

|

|

|

|

|

Values for Timer0

;

|

|

|

|

|

000

= 1:2

001 = 1:4

;

|

|

|

|

|

010

= 1:8

011 = 1:16

;

|

|

|

|

|

100

= 1:32

101 = 1:64

;

|

|

|

|

|

110

= 1:128 *111 = 1:256

;

|

|

|

|

|______________

PSA

(prescaler assign)

;

|

|

|

|

*1

=

to WDT

;

|

|

|

|

0

=

to Timer0

;

|

|

|

|_________________

TOSE (Timer0 edge select)

;

|

|

|

*0

=

increment on low-to-high

;

|

|

|

1

=

increment in high-to-low

;

|

|

|____________________

TOCS (TMR0 clock source)

;

|

|

*0

=

internal clock

;

|

|

1

=

RA4/TOCKI bit source

;

|

|_______________________

INTEDG (Edge select)

;

|

*0

=

falling edge

;|__________________________ RBPU (Pullup enable)

;

*0

=

enabled

;

1

=

disabled

movlw

b'00001000'

movwf

OPTION_REG

;Back to bank 0 Bank0

;Clear all output lines movlw b'00000000' movwf portd

movwf

porte

; Wait and initialize HD44780

call

delay_5

; Allow LCD time to initialize itself

call

initLCD

; Then do forced initialization

call

delay_5

; (Wait probably not necessary)

; Set display address to start of second LCD line

call

line1

; Clear character counter and line counter variables

clrf

LCDcount

clrf

LCDline


734

Appendix D

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

;scan keypad ;========================

;Keypad switch wiring:

;

x

x

x

x

<= port B0 |

;

x

x

x

x

<= port B1 |--- ROWS = OUTPUTS

;

x

x

x

x

<= port B2 |

;

x

x

x

x

<= port B3

|

;

|

|

|

|

;

|

|

|

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

;

---------


Supplementary Programs

735

;

0000 1101 <= row 1 is masked off

;

;The row mask, which is initially 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

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

;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

scanExit

; At this point a new scan code is detected


736

Appendix D

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)

;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. Conver 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:

call

send8

; Display routine

call

LCDscroll ; Auto line scrolling procedure

scanExit:

call

long_delay

; Debounce

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

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