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

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

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

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

Добавлен: 14.06.2025

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

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

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

Communications

441

;line addresses have the high-order bit set

;so as to meet the requirements of controller

;commands.

;

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

;

variables in PIC RAM

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

; Local variables

cblock

0x20

; Start of block

count1

; Counter # 1

count2

; Counter # 2

count3

; Counter # 3

J

; counter J

K

; counter K

store1

; Local storage

store2

; For LCDscroll procedure

LCDcount ; Counter for characters per line

LCDline

; Current display line (0 or 1)

; Keypad processing variables

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

newScan

; 0 if no new scan code detected

;Communications variables ascVal

errorFlags

;Temporary storage used by interrupt handler tempW

tempStatus tempPclath tempFsr endc

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

; P R O G R A M

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x04

InterruptCode:

goto

IntServ

; Interrupt service routine

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

; main program


442

Chapter 14

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

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

;Data memory bank selection bits:

; RP1:RP0

Bank

;

0:0

0

Ports A,B,C,D, and E

;

0:1

1

Tris A,B,C,D, and E

;

1:0

2

;

1:1

3

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

movwf ADCON1

;Port-B, lines are wired to keypad 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

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


Communications

443

;

|

|

|

|

*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

;Initialize serial port for 9600 baud, 8 bits, no parity

;1 stop

call

InitSerial

; Test serial transmission by sending “RDY-”

movlw

‘R’

call

SerialSend

movlw

‘D’

call

SerialSend

movlw

‘Y’

call

SerialSend

movlw

‘-’

call

SerialSend

movlw

0x20

call

SerialSend

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

; Clear character counter and line counter variables

clrf

LCDcount

clrf

LCDline

; Set display address to start of second LCD line

call

line1

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

; scan keypad

;============================================================ ; Keypad switch wiring:


444

Chapter 14

;

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

;

————-

;

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


Communications

445

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 and send 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 itself

btfsc

STATUS,Z ; Is it zero

goto

ScanExit

; 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