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

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

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

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

Добавлен: 14.06.2025

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

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

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

742

Appendix D

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

;Procedure to count the number of characters displayed on

;each LCD line. If the number reaches the value in the

;constant LCDlimit, then display is scrolled to the second

;LCD line. If at the end of the second line, then LCD is

;reset to the first line.

LCDscroll:

incf

LCDcount,f

; Bump counter

; Test for line limit

movf

LCDcount,w

sublw

LCDlimit

; Count minus limit

btfss

STATUS,z

; Is count - limit = 0

goto

scrollExit

; Go if not at end of line

;At this point the end of the LCD line was reached

;Test if this is also the end of the second line movf LCDline,w

sublw

0x01

; Is it line 1?

btfsc

STATUS,z

; Is LCDline minus 1 = 0?

goto

line2End

; Go if end of second line

; At this point it is the end of the top LCD line

call

line2

; Scroll to second line

clrf

LCDcount

; Reset counter

incf

LCDline,f

; Bump line counter

goto

scrollExit

; End of second LCD line

line2End:

call

initLCD

; Reset

clrf

LCDcount

; Clear counters

clrf

LCDline

call

line1

; Display to first line

scrollExit:

return

end


Supplementary Programs

743

;File name: KeyPad.asm

;Last revision: May 12, 2006

;Author: Julio Sanchez

;

;Description:

;Program to scan a 4 x 4 keypad

;Keypad switch wiring (values are scan codes):

;--- KEYPAD --

;

0

1

2

3

<= port B0 |

;

4

5

6

7

<= port B1 |--- ROWS = OUTPUTS

;

8

9

A

B

<= port B2 |

;

C

D

E

F

<= port B3

|

;

|

|

|

|

;

|

|

|

|_____ port B4

|

;

|

|

|_________ port B5

|--- COLUMNS = INPUTS

;

|

|_____________ port B6

|

;|_________________ port B7 |

;Key press action generates a scan code in the range

;0x0 to 0xf, starting at the top-left pushbutton and

;increasing left-to-right and from the top down.

;

;Scan code is displayed in LEDs wired to port D,

;lines 0, 1, 2, and 3

;WARNING:

;Code assumes 4Mhz clock. Delay routines must be

;edited for faster clock

;

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

;16F877 switches ;===========================

;Switches used in __config directive:

;

_CP_ON

Code protection ON/OFF

; *

_CP_OFF

;

*

_PWRTE_ON

Power-up timer ON/OFF

;_PWRTE_OFF

;

_BODEN_ON

Brown-out reset enable ON/OFF

; *

_BODEN_OFF

;

*

_PWRTE_ON

Power-up timer enable ON/OFF

;_PWRTE_OFF

;

_WDT_ON

Watchdog timer

ON/OFF

; * _WDT_OFF

;

_LPV_ON

Low voltage IC

programming enable ON/OFF

; * _LPV_OFF

;

_CPD_ON

Data EE memory

code protection ON/OFF

;* _CPD_OFF

;OSCILLATOR CONFIGURATIONS:


744

Appendix D

;

_LP_OSC

Low power crystal occilator

;

_XT_OSC

External parallel resonator/crystal oscillator

; * _HS_OSC

High speed crystal resonator

;

_RC_OSC

Resistor/capacitor oscillator

; |

(simplest, 20% error)

;|

;|_____ * indicates setup values presently selected

processor

16f877

; Define processor

#include <p16f877.inc>

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON &

_HS_OSC & _WDT_OFF & _LVP_OFF & _CPD_OFF

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

;

PIC register equates

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

Status

equ

0x03

c

equ

0

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

;

variables in PIC RAM

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

cblock

0x20

J

; Counters

K

keyMask

; For keypad processing

rowMask

; For masking-off key rows

rowCode

; Row addend for calculating scan code

RowCount

; Counter for key rows (0 to 3)

ScanCode

; Final key code

endc

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

;

program

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

; First, initialize port B by clearing latches

clrf

STATUS

clrf

PORTB

; Select bank 1 to tris port D for output

bcf

STATUS,RP1

; Clear banks 2/3 selector

bsf

STATUS,RP0

; Select bank 1 for tris

registers

; Tris port D for output. Port D is wired to LEDs.


Supplementary Programs

745

movlw

B'00000000'

movwf

TRISD

; and port D

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

;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

; Enable port B pullups for switches in OPTION register

movlw

b'00001000'

movwf

OPTION_REG

; Back to bank 0

bcf

STATUS,RP0

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

;Monitor switches and

;toggle LEDS ;========================

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

;LEDS are wired to port D, lines 0, 1, 2, and 3

;Test switches

;First, all LEDs off

movlw

b'00000000'

movwf

PORTD

; Place in port

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


746

Appendix D

clrf

rowCode

; First row is code 0

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