Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8613
Скачиваний: 0
608 |
Chapter 16 |
;Procedure to read the current time from the RTC and store
;data (in packed BCD format) in local time registers.
;According to wiring diagram
;NJU6355 Interface for read operations:
; DAT |
PORTB,0 |
Input |
; CLK |
PORTB,1 |
Output |
; CE |
PORTB,2 |
Output |
; IO |
PORTB,3 |
Output |
Get_Time |
||
; Clear Port-B |
||
movlw |
b’00000000’ |
|
movwf |
PORTB |
;Make data line input Bank1
movlw b’00000001’ movwf TRISB
Bank0
;Reading RTC data requires that the IO line be low and the
;CE line be high. CLK line is held low
bcf |
PORTB,CLK |
; CLK low |
|
call |
delay_125 |
||
bcf |
PORTB,IO ; |
IO line low |
|
call |
delay_125 |
||
bsf |
PORTB,CE ; |
and CE line high |
|
; Data is read from RTC as follows: |
|||
; |
year |
8 |
bits (0 to 99) |
; |
month |
8 |
bits (1 to 12) |
; |
day |
8 |
bits (1 to 31) |
; |
dayOfWeek |
4 |
bits (1 to 7) |
; |
hour |
8 |
bits (0 to 23) |
; |
minutes |
8 |
bits (0 to 59) |
; |
seconds |
8 |
bits (0 to 59) |
; |
====== |
||
; |
Total |
52 bits |
|
call |
readRTC |
||
movwf |
year |
||
call |
delay_125 |
||
call |
readRTC |
||
movwf |
month |
||
call |
delay_125 |
||
call |
readRTC |
||
movwf |
day |
||
call |
delay_125 |
||
; dayOfWeek of week is a 4-bit value call read4RTC
Analog to Digital and Realtime Clocks |
609 |
movwf dayOfWeek call delay_125
call readRTC movwf hour
call delay_125
call readRTC movwf minutes call delay_125
call readRTC movwf seconds
bcf |
PORTB,CE ; CE line low to end output |
return |
;============================
;read 4/8 bits from RTC ;============================
;Procedure to read 4/8 bits stored in 6355 registers
;Value returned in w register
read4RTC |
|||
movlw |
.4 |
; 4 |
bit read |
goto |
anyBits |
||
readRTC |
|||
movlw |
.8 |
; 8 |
bits read |
anyBits: |
|||
movwf |
counter |
;Read 6355 read operation requires the IO line be set low
;and the CE line high. Data is read in the following order:
;year, month, day, day-of-week, hour, minutes, seconds readBits:
bsf |
PORTB,CLK; Set CLK high to validate data |
|
bsf |
STATUS,C |
; Set the carry flag (bit = 1) |
;Operation:
;If data line is high, then bit read is a 1-bit
;otherwise bit read is a 0-bit
btfss |
PORTB,DAT |
; Is data line high? |
; Leave carry set (1 bit) if high |
||
bcf |
STATUS,C ; Clear the carry bit (make bit 0) |
|
; At this point the carry bit matches the data line |
||
bcf |
PORTB,CLK |
; Set CLK low to end read |
; The carry bit is now rotated into the temp1 register |
||
rrf |
temp1,1 |
|
decfsz |
counter,1 |
; Decrement the bit counter |
goto |
readBits ; Continue if not last bit |
|
; At this point all bits have been read (8 or 4)
610 |
Chapter 16 |
|
movf |
temp1,0 |
; Result to w |
return |
;============================
;write 4/8 bits to RTC ;============================
;Procedure to write 4 or 8 bits to the RTC registers
;ON ENTRY:
;temp1 register holds value to be written
;ON EXIT:
;nothing
write4RTC |
|||
movlw |
.4 |
; Init for |
4 bits |
goto |
allBits |
||
writeRTC |
|||
movlw |
.8 |
; Init for |
8 bits |
allBits: |
|||
movwf |
counter |
; Store in |
bit counter |
writeBits: |
|||
bcf |
PORTB,CLK |
; Clear the CLK line |
|
call |
delay_5 |
; Wait |
|
bsf |
PORTB,DAT |
; Set the data line to RTC |
|
btfss |
temp1,0 |
; Send LSB |
|
bcf |
PORTB,DAT |
; Clear data line |
|
call |
delay_5 |
; Wait for |
operation to |
; complete |
|||
bsf |
PORTB,CLK |
; Bring CLK line high to |
|
; validate |
|||
rrf |
temp1,f |
; Rotate bits in storage |
|
decfsz |
counter,1 |
; Decrement bit counter |
|
goto |
writeBits |
; Continue |
if not last bit |
return |
|||
;============================
;init time variables ;============================
;Procedure to initialize time variables for testing
;Constants used in initialization are located in
;#define statements.
initRTC:
movlw iYear
movwf year
movlw iMonth
movwf month
movlw iDay
movwf day
movlw iDoW
movwf dayOfWeek
Analog to Digital and Realtime Clocks |
611 |
movlw iHour
movwf hour
movlw iMin
movwf minutes
movlw iSec
movwf seconds
return
;============================
;binary to BCD conversion ;============================
;Convert a binary number into two packed BCD digits
;ON ENTRY:
;w register has binary value in range 0 to 99
;ON EXIT:
;output variables bcdLow and bcdHigh contain two
;packed unpacked BCD digits
;w contains two packed BCD digits
;Routine logic:
;The value 10 is subtracted from the source operand
;until the reminder is < 0 (carry cleared). The number
;of subtractions is the high-order BCD digit. 10 is
;then added back to the subtrahend to compensate
;for the last subtraction. The final reminder is the
;low-order BCD digit
;Variables:
; |
inNum |
storage for source operand |
|
; |
bcdHigh |
storage for high-order nibble |
|
; |
bcdLow |
storage for low-order nibble |
|
; |
thisDig |
Digit counter |
|
bin2bcd: |
|||
movwf |
inNum |
; Save copy of source value |
|
clrf |
bcdHigh ; Clear storage |
||
clrf |
bcdLow |
||
clrf |
thisDig |
||
min10: |
|||
movlw |
.10 |
||
subwf |
inNum,f |
; Subtract 10 |
|
btfsc |
STATUS,C |
; Did subtract overflow? |
|
goto |
sum10 |
; No. Count subtraction |
|
goto |
fin10 |
||
sum10: |
|||
incf |
thisDig,f |
;increment digit counter |
|
goto |
min10 |
||
; Store 10th digit |
|||
fin10: |
|||
movlw |
.10 |
||
addwf |
inNum,f |
; Adjust for last subtract |
|
movf |
thisDig,w |
; get digit counter contents |
|
612 |
Chapter 16 |
|
movwf |
bcdHigh |
; Store it |
; Calculate and store low-order BCD digit |
||
movf |
inNum,w |
; Store units value |
movwf |
bcdLow |
; Store digit |
; Combine both digits |
||
swapf |
bcdHigh,w |
; High nibble to HOBs |
iorwf |
bcdLow,w ; ORin low nibble |
|
return |
||
;============================================================
end ; END OF PROGRAM
;============================================================