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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

695

;

|

11 = cursor and display

;

|

shifted right

;

|___ COMMAND BIT

call

send8

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

;

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'41'

; 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

PORTA,E_line

; Pulse E line

nop

bcf

PORTA,E_line

return

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

; long delay sub-routine


696

Appendix D

;

(for debugging)

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

long_delay

movlw

D'200' ; w = 200 decimal

movwf

J

; J = w

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

PORTB

;

w to

port B

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

PORTB

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


Supplementary Programs

697

;ON ENTRY:

;w contain value bits

;ON EXIT:

;w contains merged bits

merge4:

andlw

b'11110000'

; ANDing with

0 clears the

; bit. ANDing

with 1 preserves

; the original value

movwf

store2

; Save result

in variable

movf

PORTB,w

; port B to w

register

andlw

b'00001111' ; Clear high nibble in port b

; and preserve low nibble

iorwf

store2,w ; OR two operands in w

return

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

;Set address register

;to LCD line 1 ;========================

;ON ENTRY:

;Address of LCD line 1 in constant LCD_1

line1:

bcf

PORTA,E_line

; E line low

bcf

PORTA,RS_line

; RS line low, set up for

; control

call

delay_5

; busy?

; Set to second display line

movlw

LCD_1

; Address and command bit

call

send8

; 4-bit routine

; Set RS line for data

bsf

PORTA,RS_line

; Setup for data

call

delay_5

; Busy?

; Clear buffer and pointer

call

blankBuf

clrf

bufPtr

; Clear

return

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

;Set address register

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

;ON ENTRY:

;Address of LCD line 2 in constant LCD_2

line2:

bcf

PORTA,E_line

; E line low

bcf

PORTA,RS_line

;

RS line low, setup for

control

call

delay_5

;

Busy?

; Set to second display line


698

Appendix D

movlw

LCD_2

; Address with high-bit set

call

send8

; Set RS line for data

bsf

PORTA,RS_line

; RS = 1 for data

call

delay_5

; Busy?

; Clear buffer and pointer

call

blankBuf

clrf

bufPtr

return

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

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

;second line is scrolled to the first line and display

;continues at the start of the second line

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

;Scroll second line to first line. Characters to be

;scrolled are stored in buffer starting at address 0x0c.

;16 characters are to be moved

;First clear LCD

call initLCD

call

delay_5

; Make sure not busy

; Set up for data

bcf

PORTA,E_line

; E line low


Supplementary Programs

699

bsf

PORTA,RS_line

; RS line high for data

; Set up counter for 16 characters

movlw

D'16'

; Counter = 16

movwf

count2

; Get address of storage buffer

movlw

0x0c

movwf

FSR

;

W to FSR

getchar:

movf

INDF,w

;

get character from display RAM

;

location pointed to by file select

;

register

call

send8

;

4-bit interface routine

; Test for 16 characters displayed

decfsz

count2,f ;

Decrement counter

goto

nextchar ;

Skipped if done

; At this point scroll operation has concluded

clrf

LCDcount ;

Clear counters

; Stay at line 2

clrf

LCDline

incf

LCDline,f

call

line2

;

Set for second line

scrollExit:

return

nextchar:

incf

FSR,f

;

Bump pointer

goto

getchar

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

;clear line buffer ;============================

;Use indirect addressing to store 16 blanks in the

;buffer located at 0x0c

blankBuf:

Bank0

movlw

0x0c

; Pointer to RAM

movwf

FSR

; To index register

blank16:

clrf

INDF

; Clear memory pointed at by FSR

incf

FSR,f

; Bump pointer

btfss

FSR,4

; 000x0000 when bit 4 is set

; count reached 16

goto

blank16

return

;============================================================ ; initialize for TTY ;============================================================ ; Procedure to initialize RS232 reception