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

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

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

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

Добавлен: 14.06.2025

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

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

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

Supplementary Programs

711

LCDcount ; Counter for characters per line LCDline ; Current display line (0 or 1) endc

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

;Common RAM area ;==============================

;These GPRs can be accessed from any bank.

;15 bytes are available, from 0x70 to 0x7f

cblock

0x70

; Communications variables

newData

; not 0 if new data received

ascVal

errorFlags

; EEPROM-related variables

EEMemAdd ; EEPROM address to access

EEByte

; Data byte to write

endc

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

; P R O G R A M

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

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

;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 digital movwf ADCON1


712

Appendix D

;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

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


Supplementary Programs

713

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

; Initialize I2C EEPROM operation

call

SetupI2C

; Local procedure

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

;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

WriteI2C

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

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


714

Appendix D

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

ReadI2C

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

;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


Supplementary Programs

715

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