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

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

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

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

Добавлен: 14.06.2025

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

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

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

508

Chapter 15

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:

;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 movlw b’00001000’

movwf OPTION_REG

;Back to bank 0 Bank0

;Initialize serial Port-for 2400 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


Data EEPROM Programming

509

clrf

LCDcount

clrf

LCDline

; Set display address to start of first LCD line

call

line1

; Store address of display buffer

movlw

0x20

movwf

bufAdd

; Display “Receiving:” message prompt

call

blank20

; Clear buffer

movlw

0x00

; Offset in buffer

call

storeMS1

; Store message at offset

call

display20

; Display message

; Start address of EEPROM

clrf

EEMemAdd

; Setup for display in second line

call

line2

clrf

LCDline

incf

LCDline,f

; Set scroll control for

; line 2

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

;receive serial data, store, and display ;============================================================ receive:

;Call serial receive procedure

call SerialRcv

;HOB of newData register is set if new data

;received

btfss

newData,7

goto

scanExit

; At this point new data was received.

movwf

EEByte

; Save received character

; Display character on LCD

movf

EEByte,w ; Recover character

call

send8

; Display in LCD

call

LCDscroll

; Scroll at end of line

; Store character in EEPROM at location in EEMemAdd

call

EEWrite

; Local procedure

incf

EEMemAdd,f

; Bump to next EEPROM

; Check for <Enter> key (0x0d) and execute display function

movf

EEByte,w ; Recover last received

sublw

0x0d

btfsc

STATUS,Z ; Test if <Enter> key

goto

isEnter

; Go if <Enter>

; Not <Enter> key, continue processing

scanExit:

goto

receive

; Continue

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

;

display EEPROM data


510

Chapter 15

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

;This routine receives control when the <Enter> key is

;received.

;Action:

;1. Clear LCD

;2. Output is set to top LCD line

;3. Characters stored in EEPROM are displayed

;until 0x0d code is detected

isEnter:

call clearLCD

; Clear character counter and line counter variables clrf LCDcount

clrf LCDline

;Read data from EEPROM memory, starting at address 0

;and display on LCD until 0x0d terminator

call

line1

clrf

EEMemAdd ; Start at EEPROM 0

readOne:

call

EERead

; Get character

; Store character

movwf

EEByte

; Save character

; Test for terminator

sublw

0x0d

btfsc

STATUS,Z ; Test if 0x0d

goto

atEnd

; Go if 0x0d

;At this point character read is not 0x0d

;Display on LCD

movf

EEByte,w ; Recover character

; Display character on LCD

call

send8

; Display in LCD

call

LCDscroll

; Scroll at end of line

incf

EEMemAdd,f

; Next EEPROM byte

goto

readOne

; End of execution atEnd:

goto atEnd

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

;============================================================ ; L O C A L P R O C E D U R E S ;============================================================ ;============================================================ ;==========================

;init LCD for 4-bit mode ;========================== initLCD:

;Initialization for Densitron LCD module as follows:


Data EEPROM Programming

511

;4-bit interface

;2 display lines of 16 characters each

;cursor on

;left-to-right increment

;cursor shift right

;no display shift ;=======================|

;

set command mode

|

;=======================|

bcf

PORTE,E_line

; E line low

bcf

PORTE,RS_line

; RS line low

bcf

PORTE,RW_line

; Write mode

call

delay_125

; delay 125 microseconds

movlw

0x28

; 0 0 1 0 1 0 0

0 (FUNCTION SET)

call

send8

; 4-bit send routine

; Set 4-bit mode command must be repeated

movlw

0x28

call

send8

movlw

0x0e

; 0 0 0 0 1 1 1

0 (DISPLAY ON/OFF)

call

send8

movlw

0x06

; 0 0 0 0 0 1 1

0 (ENTRY MODE SET)

call

send8

movlw

0x14

; 0 0 0 1 0 1 0

0 (CURSOR/DISPLAY

;

SHIFT)

call

send8

movlw

0x01

; 0 0 0 0 0 0 0

1 (CLEAR DISPLAY)

;

|___ COMMAND BIT

call

send8

call

delay_5

; Test for busy

return

.;===========================

;

procedure to clear LCD

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

clearLCD:

bcf

PORTE,E_line

; E line low

bcf

PORTE,RS_line

; RS line low

bcf

PORTE,RW_line

; Write mode

call

delay_125

; delay 125 microseconds

movlw

0x01

; 0 0 0 0 0 0 0

1 (CLEAR DISPLAY)

;

|___ COMMAND BIT

call

send8

call

delay_5

; Test for busy

return

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

;Procedure to delay

;42 microseconds


512

Chapter 15

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

delay_125:

movlw

.105

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

.105

; Counter = 105 cycles

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

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