Файл: Microcontroller Programming. Thi Micro Chip PIC (Julio Sanchez, 2007).pdf
ВУЗ: Не указан
Категория: Не указан
Дисциплина: Не указана
Добавлен: 14.06.2025
Просмотров: 8676
Скачиваний: 0
564 |
Chapter 16 |
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
movwf dayOfWeek call delay_125
call readRTC movwf hour
call delay_125
call readRTC movwf minutes
Analog to Digital and Realtime Clocks |
565 |
|
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) |
||
movf |
temp1,0 |
; Result to w |
return |
||
BCD Conversion Procedures
In addition to the RTC procedures to initialize the clock registers and to read clock data, the application requires auxiliary procedures to manipulate and display data in BCD format. BCD encodings, covered in Section 3.4, are a way of representing decimal digits in binary form. Two common BCD formats are used: packed and unpacked. In
566 |
Chapter 16 |
the unpacked format each byte encodes a single BCD value. In packed form two BCD digits are encoded per byte. The 6355 uses the packed BCD format.
Since program data is usually in binary form, it is useful to have a routine to convert binary data into BCD form. A simple algorithm for converting binary to BCD is as follows:
1.The value 10 is subtracted from the source operand until the remainder is less than 0 (carry cleared). The number of subtractions is the high-order BCD digit.
2.The value 10 is then added back to the subtrahend to compensate for the last subtraction.
3.The final remainder is the low-order BCD digit.
The binary to BCD conversion procedure is coded as follows:
;============================
;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
;unpacked BCD digits
;w contains two packed BCD digits
;Routine logic:
;The value 10 is subtracted from the source operand
;until the remainder 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 remainder 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: |
|||
Analog to Digital and Realtime Clocks |
567 |
|
incf |
thisDig,f |
; Increment digit counter |
goto |
min10 |
|
; Store 10th digit |
||
fin10: |
||
movlw |
.10 |
|
addwf |
inNum,f |
; Adjust |
movf |
thisDig,w |
; Get digit counter contents |
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 |
||
Since the program requires displaying values encoded in BCD format, a routine is necessary to convert two packed BCD digits into two ASCII decimal digits. The conversion logic is quite simple since the BCD digit is converted to ASCII by adding 0x30 to its value. All that is necessary is to shift bits in the packed BCD operand so as to isolate each digit and then add 0x30 to each one. The routine’s code is as follows:
;==============================
;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
;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 |
||