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

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

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

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

Добавлен: 14.06.2025

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

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

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

598

Chapter 16

thisDig

; Digit counter

endc

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

;

program

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

org

0

; start at address

goto

main

; Space for interrupt handlers

org

0x08

main:

movlw

b’00000000’ ; All lines to output

Bank1

movwf

TRISA

; in Port-A

movwf

TRISB

; and Port-B

Bank0

movlw

b’00000000’

; All outputs ports low

movwf

PORTA

movwf

PORTB

; Wait and initialize HD44780

call

delay_5

; Allow LCD time to initialize

; itself

call

delay_5

call

initLCD

; Then do forced

initialization

call

delay_5

; Wait again

; Store base address of text buffer in PIC RAM

movlw

0x0c

; Start address for buffer

movwf

pic_ad

; to local variable

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

;first LCD line ;======================

;Store 16 blanks in PIC RAM, starting at address stored

;in variable pic_ad

call blank16

;Call procedure to store ASCII characters for message

;in text buffer

movlw

d’0’

; Offset into buffer

call

storeMS1 ; Store message text in buffer

; Initialize real time clock

call

initRTC

; Initialize variables

call

setRTC

; Start clock

call

delay_5

; Wait for operation to

; conclude

newTime:

; Get variables from RTC

call

Get_Time


Analog to Digital and Realtime Clocks

599

call

delay_5

; Wait

movf

hour,w

;

Get hours

call

Bcd2asc

;

Conversion routine

;At this point three ASCII digits are stored in local

;variables. Move digits to display area

movf

asc1,w

; Unit digit

movwf

.15

; Store in buffer

movf

asc10,w

; Same with other digit

movwf

.14

call

delay_5

movf

minutes,w

call

Bcd2asc

; Conversion routine

;At this point three ASCII digits are stored in local

;variables. Move two digits to display area

movf

asc1,w

; Unit digit

movwf

.20

; Store in buffer

movf

asc10,w

; same with other digit

movwf

.19

call

delay_5

movf

seconds,w

call

Bcd2asc

; Conversion routine

; Move digits to display area

movf

asc1,w

; Unit digit

movwf

.25

; Store in buffer

movf

asc10,w

; same with other digit

movwf

.24

call

delay_5

; Set DDRAM address to start of first line

call

line1

; Call procedure to display 16 characters in LCD

call

display16

goto

newTime

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

|

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


600

Chapter 16

bcf

PORTA,E_line

; E line low

bcf

PORTA,RS_line

; RS line low

bcf

PORTA,RW_line

; Write mode

call

delay_125

; delay 125

microseconds

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

;

FUNCTION SET

|

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

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

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

; DISPLAY AND CURSOR ON |

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

movlw

0x0e

; 0 0 0 0 1 1 1 0 (DISPLAY ON/OFF)

call

send8

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

;

set entry mode

|

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

movlw

0x06

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

call

send8

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

; cursor/display shift

|

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

movlw

0x14

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

SHIFT)

call

send8

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

;

clear display

|

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

movlw

0x01

; 0 0 0 0 0 0 0 1 (CLEAR DISPLAY)

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


Analog to Digital and Realtime Clocks

601

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

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

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

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

;Sends 16 characters from PIC buffer with address stored

;in variable pic_ad to LCD line previously selected display16

call

delay_5

; Make sure not busy


602

Chapter 16

; Set up for data

bcf

PORTA,E_line

; E line low

bsf

PORTA,RS_line

; RS line high for data

; Set up counter for 16 characters

movlw

D’16’

; Counter = 16

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

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