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

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

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

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

Добавлен: 14.06.2025

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

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

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

Communications

431

;

| | | |_|___

don’t care

;

| |_|__

cursor/display shift

;

|

00 =

cursor shift left

;

|

01 =

cursor shift right

;

|

10 =

cursor and display

;

|

shifted left

;

|

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’42’

; 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


432

Chapter 14

bsf

PORTE,E_line

; Pulse E line

nop

bcf

PORTE,E_line

return

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

;

long delay sub-routine

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

long_delay

movlw

D’200’

; w delay count

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

PORTD

; w to Port D

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

PORTD

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:


Communications

433

;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

;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

PORTD,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 2 ;==========================

;ON ENTRY:

;Address of LCD line 2 in constant LCD_2

line2:

bcf

PORTE,E_line

bcf

PORTE,RS_line

control

call

delay_5

; Set to second display line

movlw

LCD_2

call

send8

; Set RS line for data

bsf

PORTE,RS_line

call

delay_5

return

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

;E line low

;RS line low, setup for

;Busy?

;Address with high-bit set

;RS = 1 for data

;Busy?

;Set address register

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

;ON ENTRY:

;Address of LCD line 1 in constant LCD_1


434

Chapter 14

line1:

bcf

PORTE,E_line

; E line low

bcf

PORTE,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

PORTE,RS_line

; Setup for data

call

delay_5

; Busy?

return

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

;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


Communications

435

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

; communications procedures

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

;Initizalize serial port for 2400 baud, 8 bits, no parity,

;1 stop

InitSerial:

Bank1

; Macro to select bank1

;Bits 6 and 7 of Port C are multiplexed as TX/CK and RX/DT

;for USART operation. These bits must be set to input in the

;TRISC register

movlw

b’11000000’

; Bits for TX and RX

iorwf

TRISC,f

; OR into Trisc register

; The asynchronous baud rate is calculated as follows:

;

Fosc

;

ABR = ---------

;

S*(x+1)

;Where x is value in the SPBRG register and S is 64 if the high

;baud rate select bit (BRGH) in the TXSTA control register is

;clear, and 16 if the BRGH bit is set. For setting to 9600 baud

;using a 4Mhs oscillator at a high-speed baud rate the formula

;is:

; 4,000,000 4,000,000

;--------- = --------- = 9,615 baud (0.16% error)

;

16*(25+1)

416

;

; At slow speed (BRGH = 0)

; 4,000,000 4,000,000

;--------- = --------- = 2,403.85 (0.16% error)

;

64*(25+1)

1,664

;

movlw

spbrgVal

; Value in spbrgVal = 25

movwf

SPBRG

; Place in baud rate generator

; TXSTA (Transmit Status

and Control

Register) bit map:

;

7

6

5

4

3

2

1

0 <== bits

;

|

|

|

|

|

|

|

|______

TX9D 9nth data bit on

;

|

|

|

|

|

|

|

?

(used for parity)

;

|

|

|

|

|

|

|_________

TRMT Transmit Shift Register

;

|

|

|

|

|

|

1 =

TSR empty

;

|

|

|

|

|

|

*

0 =

TSR full

;

|

|

|

|

|

|____________

BRGH High Speed Baud Rate

;

|

|

|

|

|

(Asynchronous mode only)

;

|

|

|

|

|

1 =

high speed (* 4)

;

|

|

|

|

|

*

0 =

low speed

;

|

|

|

|

|__________ NOT USED

;

|

|

|

|_____________ SYNC

USART Mode Select

;

|

|

|

1 = synchronous mode

;

|

|

|

* 0 = asynchronous mode

;

|

|

|________________ TXEN

Transmit Enable