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

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

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

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

Добавлен: 14.06.2025

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

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

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

758

Appendix D

movf

asc10,w

; same with other digits

movwf

0x2a

movf

asc100,w

movwf

0x29

;Display line

;Set DDRAM address to start of first line

showLine:

; Testing real time clock

;

call

initRTC

; Initialize variables

;

call

Set_Time

; Wait

movlw

.50

movwf

count3

longWait:

call

long_delay

decfsz

count3,f

goto

longWait

;Get variables from RTC

;call Get_Time

over1:

;Store data in EEPROM

clrf

EEMemAdd

;

Address

0

incf

EEMemAdd,f

;

Address

1

;movf Hours,w movlw 0xee movwf EEByte call EEWrite

incf

EEMemAdd,f

; Address 2

movf

min,w

movwf

EEByte

call

EEWrite

incf

EEMemAdd,f

; Address 3

movf

sec,w

movwf

EEByte

call

EEWrite

call

line1

; Call procedure to display 16 characters in LCD call display20

loopHere:

goto loopHere ;done


Supplementary Programs

759

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

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

; 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

bcf

PORTE,RS_line

; RS line low

bcf

PORTE,RW_line

; Write mode

call

delay_125

; delay 125 microseconds

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

;

FUNCTION SET

|

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

movlw

0x28

; 0 0 1

0

1 0 0 0 (FUNCTION SET)

;

|

| | |__ font select:

;

|

| |

1 = 5x10 in 1/8 or 1/11

;

|

| |

0 = 1/16 dc

;

|

| |___

Duty cycle select

;

|

|

0 = 1/8 or 1/11

;

|

|

1 = 1/16

;

|

|___ Interface width

;

|

0

= 4 bits

;

|

1

= 8 bits

;

|___ FUNCTION SET COMMAND

call

send8

; 4-bit

send routine

; Set 4-bit mode command must be repeated

movlw

0x28

call

send8

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

; DISPLAY AND CURSOR ON |

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

movlw

0x0e

; 0 0 0

0 1 1 1 0 (DISPLAY ON/OFF)

;

| | | |___ Blink character


760

Appendix D

;

| | |

1 = on, 0 = off

;

| | |___ Cursor on/off

;

| |

1

= on, 0 = off

;

| |____ Display on/off

;

|

1 = on, 0 = off

;

|____ COMMAND BIT

call

send8

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

;

set entry mode

|

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

movlw

0x06

; 0 0 0 0 0 1 1 0 (ENTRY MODE SET)

;

| | |___

display shift

;

| |

1 = shift

;

| |

0 = no shift

;

| |____

increment mode

;

|

1 = left-to-right

;

|

0 = right-to-left

;

|___ COMMAND BIT

call

send8

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

; cursor/display shift

|

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

movlw

0x14

; 0 0 0 1 0 1 0 0 (CURSOR/DISPLAY

;

| | | | | SHIFT)

;

| | | |_|___

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


Supplementary Programs

761

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

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

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

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

;LCD display procedure ;=============================

;Sends 20 characters from PIC buffer with address stored


762

Appendix D

; in variable pic_ad to LCD line previously selected

display20:

call

delay_5

; Make sure not busy

; Set up for data

bcf

PORTA,E_line

; E line low

bsf

PORTA,RS_line

; RS line high for data

; Set up counter for 20 characters

movlw

D'20'

movwf

count3

; Get display address from local variable pic_ad

movf

pic_ad,w ; First display RAM address to W

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

count3,f

; Decrement counter

goto

nextchar

; Skipped if done

return

nextchar:

incf

FSR,f

; Bump pointer

goto

getchar

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

;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