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

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

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

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

Добавлен: 14.06.2025

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

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

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

Analog to Digital and Realtime Clocks

603

;value to send with the contents of Port-B

;so as to preserve the 4 low-bits in Port-B

;Logic:

;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

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

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

;blank buffer ;========================

;Procedure to store 16 blank characters in PIC RAM

;buffer starting at address stored in the variable

;pic_ad

blank16

movlw

D’16’

; Setup counter

movwf

count1

movf

pic_ad,w ; First PIC RAM address

movwf

FSR

; Indexed addressing

movlw

0x20

; ASCII space character

storeit

movwf

INDF

; Store blank character in PIC RAM

; buffer using FSR register

decfsz

count1,f

; Done?

goto

incfsr

; no

return

; yes

incfsr

incf

FSR,f

; Bump FSR to next buffer space

goto

storeit


604

Chapter 16

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

;Set address register

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

;ON ENTRY:

;Address of LCD line 1 in constant LCD_1

line1:

bcf

PORTA,E_line

; E line low

bcf

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

PORTA,RS_line

; Setup for data

call

delay_5

; Busy?

return

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

;first text string procedure ;=============================== storeMS1:

;Procedure to store in PIC RAM buffer the message

;contained in the code area labeled msg1

;ON ENTRY:

;variable pic_ad holds address of text buffer

;in PIC RAM

;w register hold offset into storage area

;msg1 is routine that returns the string characters

;and a zero terminator

;index is local variable that hold offset into

;text table. This variable is also used for

;temporary storage of offset into buffer

;ON EXIT:

;Text message stored in buffer

;

;Store offset into text buffer (passed in the w register)

;in temporary variable

movwf

index

; Store w in index

; Store base address of text buffer in FSR

movf

pic_ad,w ; first display RAM address to W

addwf

index,w

; Add offset to address

movwf

FSR

; W to FSR

; Initialize index for text string access

movlw

0

; Start at 0

movwf

index

; Store index in variable

; w still = 0


Analog to Digital and Realtime Clocks

605

get_msg_char:

call

msg1

; Get character from table

; Test for zero terminator

andlw

0x0ff

btfsc

STATUS,Z ; Test zero flag

goto

endstr1

; End of string

;ASSERT: valid string character in w

;store character in text buffer (by FSR)

movwf

INDF

; store in buffer by FSR

incf

FSR,f

; increment buffer pointer

; Restore table character counter from variable

movf

index,w

; Get value into w

addlw

1

; Bump to next character

movwf

index

; Store table index in

variable

goto

get_msg_char

; Continue

endstr1:

return

;Routine for returning message stored in program area

;Message has 10 characters

msg1:

addwf

PCL,f

; Access table

retlw

‘H’

retlw

‘:’

retlw

‘ ‘

retlw

‘ ‘

retlw

‘ ‘

retlw

‘M’

retlw

‘:’

retlw

‘ ‘

retlw

‘ ‘

retlw

‘ ‘

retlw

‘S’

retlw

‘:’

retlw

0

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

;BCD to ASCII decimal

;conversion ;==============================

;ON ENTRY:

;w register has two packed BCD digits

;ON EXIT:

;output variables asc10, and asc1 have

;two ASCII decimal digits

;Routine logic:

;The low order nibble is isolated and the value 30H


606

Chapter 16

;added to convert to ASCII. The result is stored in

;the variable asc1. Then the same is done to the

;high-order nibble and the result is stored in the

;variable asc10

Bcd2asc:

movwf

store1

; Save input

andlw

b’00001111’ ; Clear high nibble

addlw

0x30

; Convert to ASCII

movwf

asc1

; Store result

swapf

store1,w ; Recover input and swap digits

andlw

b’00001111’ ; Clear high nibble

addlw

0x30

; Convert to ASCII

movwf

asc10

; Store result

return

;============================================================ ; 6355 RTC procedures ;============================================================ ;============================

;init RTC ;============================

;Procedure to initialize the real time clock chip. If chip

;is not initialized it will not operate and the values

;read will be invalid.

;Since the 6355 operates in BCD format the stored values must

;be converted to packed BCD.

;According to wiring diagram

;NJU6355 Interface for setting time:

; DAT

PORTB,0

Output

; CLK

PORTB,1

Output

; CE

PORTB,2

Output

; IO

PORTB,3

Output

setRTC:

Bank1

movlw

b’00000000’

; All output bits

movlw

TRISB

Bank0

;Writing to the 6355 requires that the CLK bit be held

;low while the IO and CE lines are high

bcf

PORTB,CLK

; CLK low

call

delay_5

bsf

PORTB,IO ; IO high

call

delay_5

bsf

PORTB,CE ; CE high

; Data is stored in RTC as follows:

;

year

8

bits

(0

to 99)

;

month

8

bits

(1

to 12)


Analog to Digital and Realtime Clocks

607

;

day

8

bits (1 to 31)

;

dayOfWeek

4

bits (1 to 7)

;

hour

8

bits (0 to 23)

;

minutes

8

bits (0 to 59)

;

======

;

Total

44 bits

;Seconds cannot be written to RTC. RTC seconds register

;is automatically initialized to zero

movf

year,w

; Get item from storage

call

bin2bcd

; Convert to BCD

movwf

temp1

call

writeRTC

movf

month,w

call

bin2bcd

movwf

temp1

call

writeRTC

movf

day,w

call

bin2bcd

movwf

temp1

call

writeRTC

movf

dayOfWeek,w

; dayOfWeek of week is 4-bits

call

bin2bcd

movwf

temp1

call

write4RTC

movf

hour,w

call

bin2bcd

movwf

temp1

call

writeRTC

movf

minutes,w

call

bin2bcd

movwf

temp1

call

writeRTC

; Done

bcf

PORTB,CLK

; Hold CLK line low

call

delay_5

bcf

PORTB,CE

; and the CE line

; to the RTC

call

delay_5

bcf

PORTB,IO

; RTC in output mode

return

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

;

read RTC data

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